1
1
import Foundation
2
2
3
3
extension Connection {
4
- var sqliteVersion : String ? {
5
- ( try ? scalar ( " SELECT sqlite_version() " ) ) as? String
6
- }
7
-
8
- var sqliteVersionTriple : ( Int , Int , Int ) {
9
- guard let version = sqliteVersion,
10
- let splits = . some( version. split ( separator: " . " , maxSplits: 3 ) ) , splits. count == 3 ,
11
- let major = Int ( splits [ 0 ] ) , let minor = Int ( splits [ 1 ] ) , let point = Int ( splits [ 2 ] ) else {
12
- return ( 0 , 0 , 0 )
13
- }
14
- return ( major, minor, point)
15
- }
16
-
17
- // Changing the foreign_keys setting affects the execution of all statements prepared using the database
18
- // connection, including those prepared before the setting was changed.
19
- //
20
- // https://sqlite.org/pragma.html#pragma_foreign_keys
21
- var foreignKeys : Bool {
22
- get { getBoolPragma ( " foreign_keys " ) }
23
- set { setBoolPragma ( " foreign_keys " , newValue) }
24
- }
25
-
26
- var deferForeignKeys : Bool {
27
- get { getBoolPragma ( " defer_foreign_keys " ) }
28
- set { setBoolPragma ( " defer_foreign_keys " , newValue) }
29
- }
30
-
31
- // https://sqlite.org/pragma.html#pragma_foreign_key_check
32
-
33
- // There are four columns in each result row.
34
- // The first column is the name of the table that
35
- // contains the REFERENCES clause.
36
- // The second column is the rowid of the row that contains the
37
- // invalid REFERENCES clause, or NULL if the child table is a WITHOUT ROWID table.
38
- // The third column is the name of the table that is referred to.
39
- // The fourth column is the index of the specific foreign key constraint that failed.
40
- func foreignKeyCheck( ) throws -> [ ForeignKeyError ] {
41
- try run ( " PRAGMA foreign_key_check " ) . compactMap { row -> ForeignKeyError ? in
42
- guard let table = row [ 0 ] as? String ,
43
- let rowId = row [ 1 ] as? Int64 ,
44
- let target = row [ 2 ] as? String else { return nil }
45
-
46
- return ForeignKeyError ( from: table, rowId: rowId, to: target)
47
- }
48
- }
49
-
50
4
// https://sqlite.org/pragma.html#pragma_table_info
51
5
//
52
6
// This pragma returns one row for each column in the named table. Columns in the result set include the
@@ -58,7 +12,7 @@ extension Connection {
58
12
try createTableSQL ( name: table) . flatMap { . init( sql: $0) }
59
13
}
60
14
61
- let foreignKeys : [ String : [ ForeignKeyDefinition ] ] =
15
+ let foreignKeys : [ String : [ ColumnDefinition . ForeignKey ] ] =
62
16
Dictionary ( grouping: try foreignKeyInfo ( table: table) , by: { $0. column } )
63
17
64
18
return try run ( " PRAGMA table_info( \( table. quote ( ) ) ) " ) . compactMap { row -> ColumnDefinition ? in
@@ -71,7 +25,7 @@ extension Connection {
71
25
primaryKey: primaryKey == 1 ? try parsePrimaryKey ( column: name) : nil ,
72
26
type: ColumnDefinition . Affinity. from ( type) ,
73
27
null: notNull == 0 ,
74
- defaultValue: LiteralValue . from ( defaultValue) ,
28
+ defaultValue: . from( defaultValue) ,
75
29
references: foreignKeys [ name] ? . first)
76
30
}
77
31
}
@@ -119,23 +73,42 @@ extension Connection {
119
73
}
120
74
}
121
75
122
- func foreignKeyInfo( table: String ) throws -> [ ForeignKeyDefinition ] {
76
+ func foreignKeyInfo( table: String ) throws -> [ ColumnDefinition . ForeignKey ] {
123
77
try run ( " PRAGMA foreign_key_list( \( table. quote ( ) ) ) " ) . compactMap { row in
124
78
if let table = row [ 2 ] as? String , // table
125
79
let column = row [ 3 ] as? String , // from
126
80
let primaryKey = row [ 4 ] as? String , // to
127
81
let onUpdate = row [ 5 ] as? String ,
128
82
let onDelete = row [ 6 ] as? String {
129
- return ForeignKeyDefinition ( table: table, column: column, primaryKey: primaryKey,
130
- onUpdate: onUpdate == TableBuilder . Dependency. noAction. rawValue ? nil : onUpdate,
131
- onDelete: onDelete == TableBuilder . Dependency. noAction. rawValue ? nil : onDelete
83
+ return . init ( table: table, column: column, primaryKey: primaryKey,
84
+ onUpdate: onUpdate == TableBuilder . Dependency. noAction. rawValue ? nil : onUpdate,
85
+ onDelete: onDelete == TableBuilder . Dependency. noAction. rawValue ? nil : onDelete
132
86
)
133
87
} else {
134
88
return nil
135
89
}
136
90
}
137
91
}
138
92
93
+ // https://sqlite.org/pragma.html#pragma_foreign_key_check
94
+
95
+ // There are four columns in each result row.
96
+ // The first column is the name of the table that
97
+ // contains the REFERENCES clause.
98
+ // The second column is the rowid of the row that contains the
99
+ // invalid REFERENCES clause, or NULL if the child table is a WITHOUT ROWID table.
100
+ // The third column is the name of the table that is referred to.
101
+ // The fourth column is the index of the specific foreign key constraint that failed.
102
+ func foreignKeyCheck( ) throws -> [ ForeignKeyError ] {
103
+ try run ( " PRAGMA foreign_key_check " ) . compactMap { row -> ForeignKeyError ? in
104
+ guard let table = row [ 0 ] as? String ,
105
+ let rowId = row [ 1 ] as? Int64 ,
106
+ let target = row [ 2 ] as? String else { return nil }
107
+
108
+ return ForeignKeyError ( from: table, rowId: rowId, to: target)
109
+ }
110
+ }
111
+
139
112
private func createTableSQL( name: String ) throws -> String ? {
140
113
try run ( """
141
114
SELECT sql FROM sqlite_master WHERE name=? AND type='table'
@@ -145,14 +118,4 @@ extension Connection {
145
118
. compactMap { row in row [ 0 ] as? String }
146
119
. first
147
120
}
148
-
149
- private func getBoolPragma( _ key: String ) -> Bool {
150
- guard let binding = try ? scalar ( " PRAGMA \( key) " ) ,
151
- let intBinding = binding as? Int64 else { return false }
152
- return intBinding == 1
153
- }
154
-
155
- private func setBoolPragma( _ key: String , _ newValue: Bool ) {
156
- _ = try ? run ( " PRAGMA \( key) = \( newValue ? " 1 " : " 0 " ) " )
157
- }
158
121
}
0 commit comments