Skip to content

Initial --params implementation - #12249

Merged
fdncred merged 12 commits into
nushell:mainfrom
Dorumin:query-db-params-wring-wahoo-wahey
Mar 24, 2024
Merged

fdncred merged 12 commits into
nushell:mainfrom
Dorumin:query-db-params-wring-wahoo-wahey

Conversation

@Dorumin

@Dorumin Dorumin commented Mar 20, 2024

Copy link
Copy Markdown
Contributor

Description

This PR adds a --params param to query db. This closes #11643.

You can't combine both named and positional parameters, I think this might be a limitation with rusqlite itself. I tried using named parameters with indices like { ':named': 123, '1': "positional" } but that always failed with a rusqlite error. On the flip side, the other way around works: for something like VALUES (:named, ?), you can treat both as positional: -p [hello 123].

This PR introduces some very gnarly code repetition in prepared_statement_to_nu_list. I tried, I swear; the compiler wasn't having any of it, it kept telling me to box my closures and then it said that the reference lifetimes were incompatible in the match arms. I gave up and put the mapping code in the match itself, but I'm still not happy.

Another thing I'm unhappy about: I don't like how you have to put the :colon in named parameters. I think nushell should insert it if it's missing. But this is the way rusqlite works, so for now, I'll let it be consistent. Just know that it's not really a blocker, and it isn't a compatibility change to later make { colon: 123 } work, without the quotes and :. This would require allocating and turning our pretty little &str into a String, though

User-Facing Changes

Less incentive to leave yourself open to SQL injection with statements like query db $"INSERT INTO x VALUES \($unsafe_user_input)". Additionally, the $"" syntax being annoying with parentheses plays in our favor, making users even more likely to use ? with --params.

Tests + Formatting

Hehe

@Dorumin
Dorumin marked this pull request as draft March 20, 2024 12:44
@Dorumin
Dorumin marked this pull request as ready for review March 23, 2024 03:31
@Dorumin

Dorumin commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor Author

Added tests (feat. stor, and lots of fiddling with the quirks of nu!, including multiline expressions requiring nu_repl_code, it filtering out newlines from Outcome.out, and print throwing that a PRN device is missing).

I also added the bare words params, so -p { named: 123 } works. This allocates a new string, but the default -p { ':named': 123 } doesn't allocate because of internal use of Cow::Borrowed. This does pessimize the more convenient syntax, but that's fine by me.

There's also DATETIME support, to an extent - since nushell enables the chrono feature in rusqlite, DateTime<FixedOffset> values can be sent as parameters. Internally, they're stored as strings, which is upsetting as I don't think you can inspect and adapt them directly, but at least they're convenient for storing into the database. When retrieving, you'll need things like into datetime and into bool.

@fdncred

fdncred commented Mar 23, 2024

Copy link
Copy Markdown
Contributor

Do you think we should update the examples in query db so that the ones with stor work? It's kind of a nitpick but things like stor open | query db "INSERT INTO my_table VALUES (:first, :second)" -p { first: "hello", second: 123 } don't work until you create a table. Thoughts?

@Dorumin

Dorumin commented Mar 23, 2024

Copy link
Copy Markdown
Contributor Author

I agree that it's unfortunate that they aren't immediately copy-pastable. In my defense, the original one that opened a .db file isn't reproducible without the file itself (which I assume never existed).

It would require the examples having the store creation first, which would be two commands. It'd add more noise to the examples, IMO, but it's not unreasonable. It would also let us test the examples in the file itself. How would you do it?

I'll take the chance to ask about stor create: Why does it insert an id INTEGER PRIMARY KEY column by default? This seems inconsistent since stor insert pretends like this column doesn't exist, and I believe it only gets away with it because it's called with conn.execute which executes a raw SQL string, while query db first prepares the statement because it needs to do some introspection.

@fdncred

fdncred commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor

How would you do it?

You're on the right track with stor create. If you get help on each of the stor commands, they kind of walk you through creating and accessing the in-mem db.

Why does it insert an id INTEGER PRIMARY KEY column by default?

Just to have a primary key. I don't think I allowed primary keys to be created otherwise.

@Dorumin

Dorumin commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor Author

You're on the right track with stor create. If you get help on each of the stor commands, they kind of walk you through creating and accessing the in-mem db.

I know how to create a table, I'm more talking about the examples themselves. How would you format/write an example that involves two commands? Simply put the multiline example and it works? Didn't know of any other commands that did that, not even stor insert.

Also, even if I did it, it wouldn't work. Because stor create inserts its own id column, I would necessarily have to list them in my query db insertion statements. It would only be compatible with stor inserts. Which leads onto my next point:

Just to have a primary key. I don't think I allowed primary keys to be created otherwise.

(nearly) All SQLite tables have an INTEGER PRIMARY KEY. This implicit, autoincrementing unique id is usually the primary index, and other non-integer primary keys are simply UNIQUE INDEXes.

By adding an explicit id NOT NULL PRIMARY KEY, you're making it explicit, and thus it needs to be mentioned in any INSERT. Honestly I'm not sure why stor insert accepts ignoring it.

If the user wants to optimize access patterns on the in-memory database, they should create an index with query db:

stor open | query db "CREATE UNIQUE INDEX my_table_index ON my_table(first);"

I can use stor create in the examples, but then I'd need to leak the id column implementation detail. I'd rather not, in fact I'd rather the rowid be implicit, like in base sqlite, if you're exposing a stor create interface that by design doesn't expose things like indexes and primary keys and constraints

(extra: here's the commands that work/fail with stor create's id column)

stor create -t weird -c { value: str }

stor insert -t weird -d { value: "hello" } # Works, autoincrements `id`
stor open | query db "INSERT INTO weird VALUES ('hello')" # Fails, requires two columns
stor open | query db "INSERT INTO weird VALUES (?)" -p [hello] # Also fails

@Dorumin

Dorumin commented Mar 23, 2024

Copy link
Copy Markdown
Contributor Author

Also, I would like for query db to keep a map of prepared SQLite statements, like the LruCache for regexes. Might be impossible if it references the database itself, and I'm definitely not implementing it in this PR. Just throwing it out there so I don't forget

@fdncred

fdncred commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor

... example that involves two commands?

You could separate commands by semicolon.

primary key

I'm fine with removing the primary key as long as all the stor commands and sqlite db commands continue to work.

I'm not sure why stor insert accepts ignoring it.

stor insert is simplistic, it's just doing an insert into table values, nothing special.

@Dorumin

Dorumin commented Mar 24, 2024

Copy link
Copy Markdown
Contributor Author

I removed the default "id" column from stor create. Keep in mind that this is a breaking change, if anyone interacted with that table via query db or other table exporting methods and used id explicitly. But it shouldn't break anything else, and since stor isn't persisted anywhere it shouldn't invalidate any schemas. If anyone used id before, they can either remove it because it's no longer necessary, or change it to rowid because the integer primary column still exists, it's just hidden.

I tried making the examples testable, but I had a few issues. Since I wanted to test with stor, I had to add it to the working set, and I didn't want to add it to the default working set so I added a test_examples_with_commands function. That worked fine.

However, a bigger issue is that changes to the in-memory database didn't seem to be persisted between pipelines. I don't know why that is, eval_block seems to run all the pipelines. I had to #[ignore] the command examples test case, as I'm genuinely clueless why stor open always looks like an empty database in those tests.

If you want me to remove all test changes in the last commit, I can do that too. I technically added a bit of complexity to test_examples only to use them in an ignored test.

@fdncred fdncred added notes:breaking-changes Noted in Breaking Changes section deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way labels Mar 24, 2024
@fdncred

fdncred commented Mar 24, 2024

Copy link
Copy Markdown
Contributor

@Dorumin How are you feeling about this PR now? Do you think it's ready to land? Are you happy with the test coverage?

@Dorumin

Dorumin commented Mar 24, 2024

Copy link
Copy Markdown
Contributor Author

@fdncred I think I did what I wanted to for --params, it works for named and ordered values and all the ancillary work to make it function. I tested every case I could think of, but I don't know about exact code coverage since the workflow to check it was removed (when did that happen?)

I'm surprised by how relatively untested stor commands were, so I technically added some checks to those by using them with query db. I'm a little unsettled by the removal of the id column, but since I don't have many examples of people using it in practice, I've already explained my rationale that most people probably never touched it unless they had to.

I would've liked to test the examples too, but you can test them manually and see that the values check out. I'd say it's ready, yes. Do you agree?

@fdncred

fdncred commented Mar 24, 2024

Copy link
Copy Markdown
Contributor

I don't think it's perfect but perfect is not a thing we deal in here. I think it's definitely an incremental step or two better, which is mostly all that I ever look for. Will this breaking change break people's code? I'm not sure but I'm ready to see. Thanks for your help. 🤞🏻

@fdncred
fdncred merged commit d1a8992 into nushell:main Mar 24, 2024
@Dorumin
Dorumin deleted the query-db-params-wring-wahoo-wahey branch March 24, 2024 20:41
@hustcer hustcer added this to the v0.92.0 milestone Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add parameters to query db

3 participants