2023-07-18 20:54:01 +00:00
|
|
|
/*\
|
|
|
|
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;
|
2023-07-18 20:54:01 +00:00
|
|
|
// Input box
|
2023-07-19 17:08:35 +00:00
|
|
|
this.consoleInput = dm("textarea",{
|
|
|
|
"class": "sql-console-input",
|
|
|
|
attributes: {
|
|
|
|
"rows": "10"
|
2023-07-19 08:04:38 +00:00
|
|
|
}
|
|
|
|
});
|
2023-07-18 20:54:01 +00:00
|
|
|
// Run button
|
2023-07-19 17:08:35 +00:00
|
|
|
this.consoleRunButton = dm("button",{
|
|
|
|
text: "run sql"
|
|
|
|
});
|
2023-07-18 20:54:01 +00:00
|
|
|
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));
|
2023-07-18 20:54:01 +00:00
|
|
|
// 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
|
|
|
|
]
|
|
|
|
});
|
2023-07-18 20:54:01 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-18 20:54:01 +00:00
|
|
|
$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 = [],
|
2023-07-18 20:54:01 +00:00
|
|
|
exception;
|
2023-07-19 17:08:35 +00:00
|
|
|
// Execute the query
|
2023-07-18 20:54:01 +00:00
|
|
|
try {
|
|
|
|
$tw.wiki.sqlFunctions.db.exec({
|
2023-07-19 17:08:35 +00:00
|
|
|
sql: sql,
|
2023-07-18 20:54:01 +00:00
|
|
|
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
|
|
|
|
}),
|
2023-10-30 10:03:14 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
2023-07-18 20:54:01 +00:00
|
|
|
this.consoleOutput.insertBefore(output,this.consoleOutput.firstChild);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
//# sourceURL=$:/plugins/tiddlywiki/sqlite3store/sql-console.js
|