-
Notifications
You must be signed in to change notification settings - Fork 350
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
Common Table Expressions #3
Comments
Those would work just fine with usql. In fact, if you watch the asciinema recording I made -- https://asciinema.org/a/73gxbg62ny2fx9ppxu0kd8c48 -- I purposefully did a 'use' statement on MySQL. But they work just fine with Microsoft SQL server as well. Try it out! If they don't work, then it's a bug/issue, and I'd be glad to fix. |
This is what I mean:
Real SQLite3:
Note the differences between |
Basically I believe you need a more sophisticated SQL parser to determine if the query should be sent to your |
Yeah, you're right there are some rough edges at the moment. However, fixing this specific issue would be trivial. BTW -- you can do this: |
Just wanted to update you that I've begun work on general parsing SQL, and while it won't be a full SQL parser, the intention is to be able to correctly pass the statement to either the 'query' handler or the 'executor' handler. This is a "non-trivial" amount of work, and will likely take be a bit of time to get working properly. BTW -- I appreciate the work you've done on rqlite. I haven't had a chance to use it in production yet, but I've played with it extensively. |
If you build a parser that can determine which handler to pass the statement to, I'd be very interested in it too. I need something very similar in rqlite: rqlite/rqlite#263 |
@otoolep So, originally, I had been thinking I was going to use a kind of cool "trick" for each of the supported databases, by "inspecting" the SQL query. It was a trick I had come up with a long time ago, but basically it takes a query and wraps it inside a view or a stored procedure, and then uses the database's introspection to see the fields and types that get returned by the database. It's used by xo in order to get the column info for a custom query. This ultimately isn't going to work for
As such, I'm likely going to write a semi/full partial SQL parser/lexer (and/or adapt one of the existing ones), and control is going to need to be passed off to individual database handlers to for performance issues, but also to use the respective database drivers directly to determine if there are rows affected, and to handle any returned result sets. However, I was thinking that the trick I use with CREATE TEMPORARY VIEW __some_random_generated_id AS <query> You can then use the For instance, some quick tests I did with ken@ken-mbair:~$ usql file:test.sqlite3
Type "help" for help.
sq:test.sqlite3=> create temporary view __blah__ as create table mytable (id integer, name text);
error: sqlite3: near "create": syntax error
sq:test.sqlite3=> create table mytable (id integer, name text);
CREATE
sq:test.sqlite3=> create temporary view __blah2__ as pragma index_list('mytable');
error: sqlite3: near "pragma": syntax error
sq:test.sqlite3=> pragma index_list('mytable');
seq | name | unique | origin | partial
+-----+------+--------+--------+---------+
(0 rows)
sq:test.sqlite3=> create temporary view __blah3__ as select * from pragma_index_list('mytable');
CREATE
sq:test.sqlite3=> select * from sql_master;
error: sqlite3: no such table: sql_master
sq:test.sqlite3=> select * from sqlite_master;
type | name | tbl_name | rootpage | sql
+-------+---------+----------+----------+--------------------------------+
table | mytable | mytable | 2 | CREATE TABLE mytable (id
| | | | integer, name text)
(1 rows)
sq:test.sqlite3=> select * from pragma_table_info('__blah__');
cid | name | type | notnull | dflt_value | pk
+-----+------+------+---------+------------+----+
(0 rows)
sq:test.sqlite3=> select * from pragma_table_info('__blah3__');
cid | name | type | notnull | dflt_value | pk
+-----+---------+------+---------+------------+----+
0 | seq | | 0 | <nil> | 0
1 | name | | 0 | <nil> | 0
2 | unique | | 0 | <nil> | 0
3 | origin | | 0 | <nil> | 0
4 | partial | | 0 | <nil> | 0
(5 rows)
sq:test.sqlite3=> Obviously this is not perfect. But I believe it would be almost 100% perfect for the |
For the record, this is how I'm going to address this problem with SQLite3, at least on
|
The other option, BTW, is to use |
So, I've decided on the final way forward for
UPDATE books SET author_id=2 WHERE book_id=1 RETURNING title \uquery
UPDATE books SET author_id=2 WHERE book_id=1 RETURNING title \uexec I don't know if these will be the final names I choose for these backslash commands, but it will be something like the above. |
Just an update: I've implemented a half-not-naive version of a "parser" that just checks for quoted strings/balanced parens. It works fairly well, so far. Check it out, and see if it works. Have not yet finished adding the other backslash |
I could be wrong, but I don't think usql will work with https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx since those statements do not start with an
INSERT
orSELECT
. Am I wrong?The text was updated successfully, but these errors were encountered: