-
Notifications
You must be signed in to change notification settings - Fork 0
Fix SaveAutoIncrement to write back a database-assigned, non-reused id (#6) #8
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
Changes from all commits
383f75d
09b5eb2
e78b0cb
91864d5
62625ff
c96bd60
859b444
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,12 @@ void Database::Save(void* p, const Reflection& record) const { | |
| query.Execute(); | ||
| } | ||
|
|
||
| int64_t Database::SaveAutoIncrement(void* p, const Reflection& record) const { | ||
| InsertQuery query(db_, record, p, true); | ||
| query.Execute(); | ||
| return sqlite3_last_insert_rowid(db_); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When two threads share Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks — the analysis is correct, but this is out of scope for the current design. The library exposes a single shared connection via A truly atomic fix is available (the bundled SQLite is 3.53.0, so Generated by Claude Code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When two threads share Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same Generated by Claude Code |
||
| } | ||
|
|
||
| void Database::Update(void* p, const Reflection& record) const { | ||
| UpdateQuery query(db_, record, p); | ||
| query.Execute(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // MIT License | ||
| // | ||
| // Copyright (c) 2026 Ioannis Kaliakatsos | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
|
|
||
| #pragma once | ||
|
|
||
| // A record with no user-defined fields besides the implicit id, used to exercise | ||
| // the auto-increment insert path for id-only tables (INSERT INTO ... DEFAULT VALUES) | ||
| #define REFLECTABLE IdOnlyRecord | ||
| #define FIELDS | ||
| #include "reflection.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user opens a database file created by the previous release, this guarantee is not true:
CreateTableQuery::PrepareSql()still usesCREATE TABLE IF NOT EXISTS, so existing tables keep their oldid INTEGER PRIMARY KEYdefinition, and the newSaveAutoIncrementpath now relies on SQLite's table schema to avoid reusing a deleted highest rowid. Please either document that the never-reused guarantee only applies to newly created/migrated tables or add a migration so upgraded databases get theAUTOINCREMENTbehavior too.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Since the library has no migration layer yet, I've taken the documentation route (as suggested): the README now qualifies the never-reused guarantee, noting it applies to tables created by this version onwards and that a database file created by an earlier release keeps its original
id INTEGER PRIMARY KEYschema until recreated or migrated. Done in 91864d5.Generated by Claude Code