@@ -65,12 +65,19 @@ open class SQLite3ModelFetch: AdaptorModelFetch {
6565 // TBD: iterate on all returned describeDatabaseNames
6666 // (via dbname.sqlite_master)
6767 // ATTACH DATABASE 'DatabaseName' As 'Alias-Name';
68- var sql = " SELECT name FROM sqlite_master WHERE type IN ('table', 'view') "
69- if let like = like {
70- sql += " AND name LIKE ' " + like + " ' " ; // TODO: escape!
71- }
7268 var names = [ String ] ( )
73- try channel. select ( sql) { ( name : String ) in names. append ( name) }
69+ let expression = channel. expressionFactory. createExpression ( nil )
70+ expression. statement =
71+ " SELECT name FROM sqlite_master WHERE type IN ('table', 'view') "
72+ if let like {
73+ expression. statement += " AND name LIKE ? "
74+ expression. bindVariables = [
75+ SQLExpression . BindVariable ( attribute: nil , value: like)
76+ ]
77+ }
78+ try channel. evaluateQueryExpression ( expression, nil ) { record in
79+ if let name = record [ 0 ] as? String { names. append ( name) }
80+ }
7481 return names
7582 }
7683
@@ -89,17 +96,22 @@ open class SQLite3ModelFetch: AdaptorModelFetch {
8996
9097 func _fetchColumnsOfTable( _ table: String ) throws -> [ AdaptorRecord ] {
9198 // keys: cid, name, type, notnull, dflt_value, pk
92- let records : [ AdaptorRecord ] =
93- try channel. querySQL ( " PRAGMA table_info( \( table) ) " )
99+ let table = quotedIdentifier ( table )
100+ let records = try channel. querySQL ( " PRAGMA table_info( \( table) ) " )
94101 return records
95102 }
96103
97104 func _fetchForeignKeysOfTable( _ table: String ) throws -> [ AdaptorRecord ] {
98105 // keys: id, seq, table, from, to, on_update, on_delete, match
99- let records : [ AdaptorRecord ] =
100- try channel. querySQL ( " PRAGMA foreign_key_list( \( table) ) " )
106+ let table = quotedIdentifier ( table )
107+ let records = try channel. querySQL ( " PRAGMA foreign_key_list( \( table) ) " )
101108 return records
102109 }
110+
111+ private func quotedIdentifier( _ identifier: String ) -> String {
112+ return channel. expressionFactory. createExpression ( nil )
113+ . sqlStringFor ( schemaObjectName: identifier)
114+ }
103115
104116 func primaryKeyNamesFromColumnInfos( _ columnInfos : [ AdaptorRecord ] ,
105117 _ attributes : [ Attribute ] )
0 commit comments