Skip to content

Commit de391ab

Browse files
committed
Update README.md
1 parent 554cb4a commit de391ab

1 file changed

Lines changed: 127 additions & 2 deletions

File tree

README.md

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
A simple wrapper around the SQLite3 API.
77

8-
Usage examples can be found in [iOS-MVVM-Clean-Architecture](https://github.com/denissimon/iOS-MVVM-Clean-Architecture).
9-
108
Installation
119
------------
1210

@@ -39,6 +37,133 @@ github "denissimon/SQLiteAdapter"
3937

4038
Copy folder `SQLiteAdapter` into your project.
4139

40+
Usage
41+
-----
42+
43+
**Opening the database:**
44+
45+
```swift
46+
import SQLiteAdapter
47+
48+
// The sqlite file will be created in the specified path if it has not yet been created
49+
let dbPath = try! (FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("db.sqlite")).path
50+
let sqlite = try? SQLite(path: dbPath) // with 'recreate: true', the sqlite file will be deleted and recreated
51+
52+
print(sqlite.dbPath) // -> the path of the sqlite file
53+
```
54+
55+
**Modeling and creating an SQL table:**
56+
57+
```swift
58+
let sqlTable = SQLTable(
59+
name: "ExampleTable",
60+
columns: [
61+
("id", .INT),
62+
("json", .TEXT),
63+
("isDeleted", .BOOL),
64+
("updated", .DATE)
65+
],
66+
primaryKey: "id" // optionally ("id" by default)
67+
)
68+
69+
let statementCreateTable = """
70+
CREATE TABLE IF NOT EXISTS "\(sqlTable.name)"(
71+
"\(sqlTable.primaryKey)" INTEGER NOT NULL,
72+
"json" TEXT NULL,
73+
"isDeleted" BOOLEAN DEFAULT 0 NOT NULL CHECK (isDeleted IN (0, 1)),
74+
"updated" DATETIME NOT NULL,
75+
PRIMARY KEY("\(sqlTable.primaryKey)" AUTOINCREMENT)
76+
);
77+
"""
78+
79+
try? sqlite?.createTable(sql: statementCreateTable)
80+
```
81+
82+
**SQL operations:**
83+
84+
```swift
85+
do {
86+
var sql = "INSERT INTO \(sqlTable.name) (json, updated) VALUES (?, ?);"
87+
let _ = try sqlite.insertRow(sql: sql, params: ["someJson", Date()])
88+
89+
sql = "INSERT INTO \(sqlTable.name) (json, updated) VALUES (?, ?), (?, ?);"
90+
let lastInsertID = try sqlite.insertRow(sql: sql, params: [nil, Date(), nil, Date()])
91+
assert(lastInsertID == 3)
92+
93+
sql = "UPDATE \(sqlTable.name) SET isDeleted = ?, updated = ? WHERE \(sqlTable.primaryKey) IN (2, 3)"
94+
try sqlite.updateRow(sql: sql, params: [true, Date()])
95+
assert(sqlite.changes == 2)
96+
97+
sql = "SELECT * FROM \(sqlTable.name) WHERE isDeleted = ?"
98+
let rows = try sqlite.getRow(from: sqlTable, sql: sql, params: [true])
99+
assert(rows.count == 2)
100+
101+
try sqlite.deleteByID(in: sqlTable, id: 2)
102+
try sqlite.deleteByID(in: sqlTable, id: 3)
103+
104+
let rowCount = try sqlite.getRowCount(in: sqlTable)
105+
assert(rowCount == 1)
106+
107+
var row = try sqlite.getFirstRow(from: sqlTable)
108+
assert(row[0].value as! Int == 1) // "id" INTEGER NOT NULL
109+
assert(row[1].value as? String == "someJson") // "json" TEXT NULL
110+
assert(row[2].value as! Bool == false) // "isDeleted" BOOLEAN DEFAULT 0 NOT NULL
111+
assert((row[3].value as! Date) <= Date()) // "updated" DATETIME NOT NULL
112+
} catch {
113+
print("SQLite:", error.localizedDescription)
114+
}
115+
```
116+
117+
**Optional settings:**
118+
119+
```swift
120+
sqlite.dateFormatter.locale = Locale(identifier: "en_US_POSIX")
121+
sqlite.dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
122+
sqlite.dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
123+
```
124+
125+
More usage examples can be found in [SQLiteAdapterTests.swift](https://github.com/denissimon/SQLiteAdapter/blob/main/Tests/SQLiteAdapterTests/SQLiteAdapterTests.swift) and [iOS-MVVM-Clean-Architecture](https://github.com/denissimon/iOS-MVVM-Clean-Architecture) where this adapter was used.
126+
127+
### Supported SQLite types
128+
129+
```swift
130+
case INT // Includes INT, INTEGER, INT2, INT8, BIGINT, MEDIUMINT, SMALLINT, TINYINT
131+
case BOOL // Includes BOOL, BOOLEAN, BIT
132+
case TEXT // Includes TEXT, CHAR, CHARACTER, VARCHAR, CLOB, VARIANT, VARYING_CHARACTER, NATIONAL_VARYING_CHARACTER, NATIVE_CHARACTER, NCHAR, NVARCHAR
133+
case REAL // Includes REAL, DOUBLE, FLOAT, NUMERIC, DECIMAL, DOUBLE_PRECISION
134+
case BLOB // Includes BLOB, BINARY, VARBINARY
135+
case DATE // Includes DATE, DATETIME, TIME, TIMESTAMP
136+
```
137+
138+
### Public methods
139+
140+
Can be extended and customized by inheriting the [SQLite](https://github.com/denissimon/SQLiteAdapter/blob/main/Sources/SQLiteAdapter/SQLiteAdapter.swift) class.
141+
142+
```swift
143+
func createTable(sql: String) throws
144+
func checkIfTableExists(_ table: SQLTable) throws -> Bool
145+
func dropTable(_ table: SQLTable, vacuum: Bool) throws
146+
func addIndex(to table: SQLTable, forColumn columnName: String, unique: Bool, order: SQLOrder) throws
147+
func checkIfIndexExists(in table: SQLTable, indexName: String) throws -> Bool
148+
func dropIndex(in table: SQLTable, forColumn columnName: String) throws
149+
func beginTransaction() throws
150+
func endTransaction() throws
151+
func insertRow(sql: String, params: [Any]?) throws -> Int
152+
func updateRow(sql: String, params: [Any]?) throws
153+
func deleteRow(sql: String, params: [Any]?) throws
154+
func deleteByID(in table: SQLTable, id: Int) throws
155+
func deleteAllRows(in table: SQLTable, vacuum: Bool, resetAutoincrement: Bool) throws
156+
func getRowCount(in table: SQLTable) throws -> Int
157+
func getRowCountWithCondition(sql: String, params: [Any]?) throws -> Int
158+
func getRow(from table: SQLTable, sql: String, params: [Any]?) throws -> [SQLValues]
159+
func getAllRows(from table: SQLTable) throws -> [SQLValues]
160+
func getByID(from table: SQLTable, id: Int) throws -> SQLValues
161+
func getFirstRow(from table: SQLTable) throws -> SQLValues
162+
func getLastRow(from table: SQLTable) throws -> SQLValues
163+
func vacuum() throws
164+
func query(sql: String, params: [Any]?) throws
165+
```
166+
42167
License
43168
-------
44169

0 commit comments

Comments
 (0)