1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-16 23:04:50 +00:00
TiddlyWiki5/plugins/tiddlywiki/sqlite3store/sql-console.js

94 lines
2.1 KiB
JavaScript
Raw Normal View History

/*\
title: $:/plugins/tiddlywiki/sqlite3store/sql-console.js
type: application/javascript
SQL console for debugging
\*/
(function() {
$tw.SqlConsole = function SqlConsole() {
2023-07-19 17:08:35 +00:00
var self = this,
dm = $tw.utils.domMaker;
// Input box
2023-07-19 17:08:35 +00:00
this.consoleInput = dm("textarea",{
"class": "sql-console-input",
attributes: {
"rows": "10"
}
});
// Run button
2023-07-19 17:08:35 +00:00
this.consoleRunButton = dm("button",{
text: "run sql"
});
this.consoleRunButton.addEventListener("click",this.runQuery.bind(this));
2023-07-19 17:08:35 +00:00
// Clear output button
this.consoleClearButton = dm("button",{
text: "clear output"
});
this.consoleClearButton.addEventListener("click",this.clearOutput.bind(this));
// Output
2023-07-19 17:08:35 +00:00
this.consoleOutput = dm("div",{
"class": "sql-console-output-container"
});
// Container
this.consoleContainer = dm("div",{
"class": "sql-console",
children: [
document.createTextNode("console for sqlite3"),
this.consoleInput,
this.consoleRunButton,
this.consoleClearButton,
this.consoleOutput
]
});
// Insert into DOM
document.body.insertBefore(this.consoleContainer,document.body.firstChild);
};
2023-07-19 17:08:35 +00:00
$tw.SqlConsole.prototype.clearOutput = function() {
while(this.consoleOutput.firstChild) {
this.consoleOutput.removeChild(this.consoleOutput.firstChild);
}
};
$tw.SqlConsole.prototype.runQuery = function() {
2023-07-19 17:08:35 +00:00
var self = this,
dm = $tw.utils.domMaker,
sql = this.consoleInput.value,
resultRows = [],
exception;
2023-07-19 17:08:35 +00:00
// Execute the query
try {
$tw.wiki.sqlFunctions.db.exec({
2023-07-19 17:08:35 +00:00
sql: sql,
rowMode: "object",
resultRows: resultRows
});
} catch(e) {
exception = e.toString();
}
2023-07-19 17:08:35 +00:00
// Display the result
var output = dm("div",{
"class": "sql-console-output",
children: [
dm("div",{
"class": "sql-console-output-input",
text: sql
}),
dm("div",{
"class": "sql-console-output-count",
text: "" + resultRows.length
}),
2023-07-19 17:08:35 +00:00
dm("div",{
"class": "sql-console-output-output",
text: exception || JSON.stringify(resultRows)
})
]
});
this.consoleOutput.insertBefore(output,this.consoleOutput.firstChild);
};
})();
//# sourceURL=$:/plugins/tiddlywiki/sqlite3store/sql-console.js