-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SQL language evaluation #60
Comments
Love the idea :) sql.js looks cool and lots of folks would benefit from being able to run sql with this lib |
This might give you a starting point: https://github.com/live-codes/livecodes/blob/d9be82ae11b282c225af3056b45920c58536565e/src/livecodes/languages/lang-sql.ts |
I can try to work on this. So the steps I can think of about it.
Part 2
|
That sounds great to me! I think you've definitely got the right idea :) I'd lean toward putting everything expensive into/called as part of the constructor as much as possible, mostly because it's much easier to make sure it only happens once there. I think the biggest decision to make in Part 1 is probably which |
So all of the things which we will need to make it work I feel is in the below-mentioned code block reference. So here's this is also a very useful example to achieve our goal. We can run A few points which I will have to go deeper into is.
Thoughts plz? const initSqlJs = require('sql.js');
// or if you are in a browser:
// const initSqlJs = window.initSqlJs;
const SQL = await initSqlJs({
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
// You can omit locateFile completely when running in node
locateFile: file => `https://sql.js.org/dist/${file}`
});
// Create a database
const db = new SQL.Database();
// NOTE: You can also use new SQL.Database(data) where
// data is an Uint8Array representing an SQLite database file
// Execute a single SQL string that contains multiple statements
let sqlstr = "CREATE TABLE hello (a int, b char); \
INSERT INTO hello VALUES (0, 'hello'); \
INSERT INTO hello VALUES (1, 'world');";
db.run(sqlstr); // Run the query without returning anything
// Prepare an sql statement
const stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
// Bind values to the parameters and fetch the results of the query
const result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
console.log(result); // Will print {a:1, b:'world'}
// Bind other values
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
// free the memory used by the statement
stmt.free();
// You can not use your statement anymore once it has been freed.
// But not freeing your statements causes memory leaks. You don't want that.
const res = db.exec("SELECT * FROM hello");
/*
[
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
]
*/ |
Hmm I think for our use case I'd skip the prepared statements. Generally they'd be a must to protect from malicious input variables, but in our case the database is only in the user's browser and the entire input is untrusted. I don't think we can gain anything from using them.
Based on that usage example I'd go with |
I will start working on it then. What do you all suggest about how do we load the wasm binary mentioned in the code block in this comment?
|
I'd go with a CDN for now. We could consider moving it into the package though when we move to multiple packages (when we merge #92), so that the bundle size would only impact sql users. @kennethcassel Does that make sense to you? |
Makes sense to me! I think we're pretty close to getting #92 merged in though |
Is your feature request related to a problem? Please describe.
Nope, but it'd be sweet! I'd definitely be able to make use of this on my running dashboard, but "I want to do more wacky hacks" probably doesn't count as a problem :)
Describe the solution you'd like
We should add sql as a supported language! You'd be able to run sql queries on an in-memory database in the browser and view their output
Describe alternatives you've considered
Use sql.js directly: https://github.com/sql-js/sql.js/
Or see if this still works: https://pypi.org/project/pandasql/ - since we already have Python + Pandas
Additional context
I came across this awesome project: https://github.com/nalgeon/sqlime / http://sqlime.org
It uses sql.js which turns out to be sqlite -> WASM!
We probably don't want to implement things from sqlime like connecting to a live DB in the core library, but the simple case of database in browser memory + run queries on it + display results would be a nice addition.
The text was updated successfully, but these errors were encountered: