Skip to content

Instantly share code, notes, and snippets.

@SamJakob
Created September 8, 2021 00:00
Show Gist options
  • Save SamJakob/61237dfb5d15dc1cb3e4f31be9f1fa55 to your computer and use it in GitHub Desktop.
Save SamJakob/61237dfb5d15dc1cb3e4f31be9f1fa55 to your computer and use it in GitHub Desktop.
<%@ language=JScript %>
<%
var SPDBInterface = {
read: function(file){
return new SPDB(file);
},
write: function(spdb, file){
FileSystem.writeFile(file, spdb.toXML());
}
}
function SPDBTable(tableData){
this.columns = [];
this.rows = [];
this.getRow = function(column, value){
for(row in this.rows){
var cols = this.rows[row];
if (cols[column] == value){
return row;
}
}
return null;
}
this.selectRow = function(column, value){
for(row in this.rows){
var cols = this.rows[row];
if (cols[column] == value){
return this.rows[row];
}
}
return null;
}
this.selectRowExtended = function(conditions){
for(row in this.rows){
var cols = this.rows[row];
var rowMeetsCondition = true;
for(var condition in conditions){
if(rowMeetsCondition && cols[condition] == conditions[condition]){
rowMeetsCondition = true;
}else{
rowMeetsCondition = false;
}
}
if(rowMeetsCondition){
return row;
}
}
return null;
}
this.toElement = function(xmlDocumentObject){
var tableElement = xmlDocumentObject.createElement("table");
var columnsElement = xmlDocumentObject.createElement("columns");
for(var column in this.columns){
var columnElement = xmlDocumentObject.createElement("column");
columnElement.appendChild(xmlDocumentObject.createTextNode(this.columns[column]));
columnsElement.appendChild(columnElement);
}
tableElement.appendChild(columnsElement);
for(var row in this.rows){
var rowElement = xmlDocumentObject.createElement("row");
for(var cell in this.rows[row]){
var cellElement = xmlDocumentObject.createElement("cell");
cellElement.setAttribute('type', typeof this.rows[row][cell]);
cellElement.appendChild(xmlDocumentObject.createTextNode(this.rows[row][cell].toString()));
rowElement.appendChild(cellElement);
}
tableElement.appendChild(rowElement);
}
return tableElement;
}
this.populate = function(tableData){
var columnsObject = tableData.selectSingleNode("columns");
for(var i = 0; i < columnsObject.selectNodes("column").length; i++){
this.columns.push(columnsObject.selectNodes("column")[i].text);
}
for(var i = 0; i < tableData.selectNodes("row").length; i++){
var rowObject = tableData.selectNodes("row")[i];
var obj = {};
for(var ix = 0; ix < rowObject.selectNodes("cell").length; ix++){
obj[this.columns[ix]] = rowObject.selectNodes("cell")[ix].text;
}
this.rows.push(obj);
}
return this;
}
}
function SPDB(file) {
/* Consider all of this private */
this.databaseMeta = {};
this.tables = {};
this.toXML = function(){
var xmlDocumentObject = Server.CreateObject("MSXML2.DOMDocument.6.0");
xmlDocumentObject.async = false;
xmlDocumentObject.loadXML('<?xml version="1.0" encoding="UTF-8"?><database name="' + this.databaseMeta.name + '" version="' + this.databaseMeta.spdbVersion + '"></database>');
var database = xmlDocumentObject.selectSingleNode("database");
for(table in this.tables){
var tableElement = this.tables[table].toElement(xmlDocumentObject);
tableElement.setAttribute('name', table);
database.appendChild(tableElement);
}
return xmlDocumentObject.xml;
}
// Read file into structure
var xmlDocumentObject = Server.CreateObject("MSXML2.DOMDocument.6.0");
xmlDocumentObject.async = false;
xmlDocumentObject.load(Server.MapPath(file));
this.databaseMeta.name =
xmlDocumentObject.selectSingleNode("database").getAttribute("name");
this.databaseMeta.spdbVersion =
xmlDocumentObject.selectSingleNode("database").getAttribute("version");
var databaseObject = xmlDocumentObject.selectSingleNode("database");
for(var i = 0; i < databaseObject.selectNodes("table").length; i++){
var tbl = databaseObject.selectNodes("table")[i];
this.tables[tbl.getAttribute("name")] = new SPDBTable().populate(tbl);
}
}
// Usage:
var myDatabase = SPDBInterface.read("/data/myDatabase.spdb");
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment