Comments (6)
I spent a long time vetting out transaction convenience methods, and it boils down to how Swift works and what I wanted in the balance of complexity. I wanted to leave as little room possible in the convenience methods to make a mistake. Sometimes this requires more upfront design (and can be inconvenient), but I figured there are 2 paths to take:
- Distill statements into functions that can be invoked one at a time (and ensure that all of them run and are checked, which is currently more-or-less enforced)
- Use the raw
run
/prepare
API and invokedb.run("BEGIN TRANSACTION")
manually.
The example you give is less common from what I've used transactions for, so I'd like to better understand the use-case and how to support it (I've generally used transactions for 1., two+-step foreign key application, and 2., cascading CHECK validation failures). I could add convenience/helper functions, but it would generally be the same as this:
db.run("BEGIN DEFERRED TRANSACTION")
if /* condition */ {
/* run statement */
} else {
/* run other statement */
}
db.run("COMMIT TRANSACTION") // or ROLLBACK, depending on the logic
What kind of code would be ideal for you?
from sqlite.swift.
The use case is synchronising data from a remote API. The returned JSON is quite complex, with some values conditionally inserting extra rows, or checking if an object already exists locally, and so on.
Using db.run()
manually works though, and I can just write my own transaction wrapper function.
from sqlite.swift.
Everything is up for review and change. I just want to understand use cases before supporting them too early. I should write a post on how I designed the early API, but it boils down to this:
- Swift doesn't support exceptions, so you can't use them for flow control (which Ruby/Python and many others use to implicitly COMMIT/ROLLBACK transactions).
- The best you get with a block/closure method is enforcing that it returns
true
/false
or.Commit
/.Rollback
. This is probably a good secondary API to support, and you definitely have me thinking more about it.
For me, having implicit error handling, flow control, and simple boolean logic makes code a lot more readable and maintainable, so I tend to err on the side of naïve legibility.
from sqlite.swift.
Let me sleep on this, but so far I'm inclined to add a simple, alternative block-based API that requires you to return true
/false
or .Commit
/.Rollback
. Preference? true
/false
is simple but the enum would be explicit.
This isn't advertised, but statements are already Bool-savvy. If they are successful, they are true, otherwise false. (Another thing to add to the documentation bucket list.)
from sqlite.swift.
I personally prefer the explicit .Commit
/.Rollback
.
from sqlite.swift.
Closed by c5881a8
from sqlite.swift.
Related Issues (20)
- SqlCipher support for Swift PM, Here's a solution
- Feature: Request visionOS Support HOT 2
- Windows support HOT 3
- can not get column info with join
- Subscrip to changes
- Problem with CSQLite HOT 1
- bug in optional support for decoding HOT 1
- Version 0.15.0 Not Applied in CocoaPods HOT 10
- Swift Compiler Error when project depend SQLite.swift and sqlite3_flutter_libs HOT 1
- ncorrect checksum for freed object HOT 1
- Double quotes when using ULID HOT 1
- Unable to decode optional custom type HOT 2
- Sqllite
- Symbol not found: _$s6SQLite10ConnectionC15TransactionModeO8deferredyA2EmFWC
- Error: use of @import in framework header is discouraged, requires -fmodules
- SQLite.swift and SQLCipher HOT 1
- SQLiteCipher and visionOS support
- New SQL Coders HOT 1
- PrivacyInfo.xcprivacy HOT 2
- Will this lib consider `sendable`?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlite.swift.