11import Foundation
22
33extension 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-
504 // https://sqlite.org/pragma.html#pragma_table_info
515 //
526 // 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 {
5812 try createTableSQL ( name: table) . flatMap { . init( sql: $0) }
5913 }
6014
61- let foreignKeys : [ String : [ ForeignKeyDefinition ] ] =
15+ let foreignKeys : [ String : [ ColumnDefinition . ForeignKey ] ] =
6216 Dictionary ( grouping: try foreignKeyInfo ( table: table) , by: { $0. column } )
6317
6418 return try run ( " PRAGMA table_info( \( table. quote ( ) ) ) " ) . compactMap { row -> ColumnDefinition ? in
@@ -71,7 +25,7 @@ extension Connection {
7125 primaryKey: primaryKey == 1 ? try parsePrimaryKey ( column: name) : nil ,
7226 type: ColumnDefinition . Affinity. from ( type) ,
7327 null: notNull == 0 ,
74- defaultValue: LiteralValue . from ( defaultValue) ,
28+ defaultValue: . from( defaultValue) ,
7529 references: foreignKeys [ name] ? . first)
7630 }
7731 }
@@ -119,23 +73,42 @@ extension Connection {
11973 }
12074 }
12175
122- func foreignKeyInfo( table: String ) throws -> [ ForeignKeyDefinition ] {
76+ func foreignKeyInfo( table: String ) throws -> [ ColumnDefinition . ForeignKey ] {
12377 try run ( " PRAGMA foreign_key_list( \( table. quote ( ) ) ) " ) . compactMap { row in
12478 if let table = row [ 2 ] as? String , // table
12579 let column = row [ 3 ] as? String , // from
12680 let primaryKey = row [ 4 ] as? String , // to
12781 let onUpdate = row [ 5 ] as? String ,
12882 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
13286 )
13387 } else {
13488 return nil
13589 }
13690 }
13791 }
13892
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+
139112 private func createTableSQL( name: String ) throws -> String ? {
140113 try run ( """
141114 SELECT sql FROM sqlite_master WHERE name=? AND type='table'
@@ -145,14 +118,4 @@ extension Connection {
145118 . compactMap { row in row [ 0 ] as? String }
146119 . first
147120 }
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- }
158121}
0 commit comments