Prepare many statements from a single string.#59
Open
cyisfor wants to merge 7 commits intodlang-community:v1.x.xfrom
Open
Prepare many statements from a single string.#59cyisfor wants to merge 7 commits intodlang-community:v1.x.xfrom
cyisfor wants to merge 7 commits intodlang-community:v1.x.xfrom
Conversation
added 7 commits
July 1, 2020 07:36
This will offer a prepare on demand InputRange instead of returning an array of statements.
Also added some documentation about what the heck I'm doing subtracting tail from head and all.
Right, can't mutate the stringz. Also explaining why the restrictive assert in there.
Gotta make sure it works.
I don't recall how you return an interface to a struct...
Oh right. You don't return an interface. You just return the struct and it magically has the right methods! Also a little more finagling to get the iterator to return empty only after preparation has failed on the tail.
Just paranoid about whether PrepareMany works as an input range or not.
Geod24
requested changes
Jul 29, 2020
| The statement becomes invalid if the Database goes out of scope and is destroyed. | ||
| +/ | ||
| Statement prepare(string sql) | ||
| Statement prepare(const string sql) |
Member
There was a problem hiding this comment.
Not needed, a string implicitly converts to a const string and vice versa.
Comment on lines
+59
to
+79
| struct PrepareMany { | ||
| Database db; | ||
| Statement current; | ||
| string sql_left; | ||
| bool empty; | ||
| this(Database db, const string sql) { | ||
| this.db = db; | ||
| this.sql_left = sql; | ||
| popFront(); | ||
| } | ||
| void popFront() { | ||
| const size_t old = sql_left.length; | ||
| current = Statement(this.db, sql_left); | ||
| empty = old == sql_left.length; | ||
| sql_left = sql_left.chomp(); | ||
| } | ||
| Statement front() { | ||
| return current; | ||
| } | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
This is lacking visibility attributes (private / package) and attributes (@safe / nothrow / ...)
Comment on lines
+317
to
+321
| unittest { | ||
| auto db = Database(":memory:"); | ||
| db.execute("CREATE TABLE test (val INTEGER)"); | ||
| auto statements = | ||
| db.prepare_many(q"[ |
Member
There was a problem hiding this comment.
I didn't see this was a unittest at first, please fix the alignment
Comment on lines
+88
to
+99
| this(Database db, const string sql) | ||
| { | ||
| string temp = sql; | ||
| this(db, temp); | ||
| // this constructor won't be able to give any indication that | ||
| // the sql wasn't fully parsed, so make sure it is. | ||
| assert(temp.length == 0); | ||
| // the second constructor moves the sql parameter to the | ||
| // unparsed tail, so use that if you might be preparing 2 statements | ||
| } | ||
|
|
||
| this(Database db, ref string sql) |
Member
There was a problem hiding this comment.
I don't really understand the point of this change. Why complicate the API this much ?
Member
|
It looks like a lot could be simplified and built purely on top of the existing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sqlite3_prepare_v2 provides a tail parameter, and I figured out the trick with it a while ago. It sets tail to the point that it stops being able to parse, which is helpful for showing where your error is in your SQL statement, but it's also helpful for parsing multiple SQL statements. sqlite3_prepare only prepares the first, then "fails" at the beginning of the second statement right after the semicolon, which lets one prepare many statements from a single string. So that's what I did. Hope you like it!