@@ -20,26 +20,23 @@ import SwiftKuery
2020// This class is a dummy SwiftKuery plugin that allows the saving and retrieval of "Grade" objects in local storage.
2121// This allows the Database example to run prior to connecting to a real database.
2222class DummyConnection : Connection {
23- var grades = [ Grade] ( )
24- let queryBuilder : QueryBuilder
25-
26- init ( withDeleteRequiresUsing: Bool = false , withUpdateRequiresFrom: Bool = false , createAutoIncrement: ( ( String , Bool ) -> String ) ? = nil ) {
27- self . queryBuilder = QueryBuilder ( withDeleteRequiresUsing: withDeleteRequiresUsing, withUpdateRequiresFrom: withUpdateRequiresFrom, createAutoIncrement: createAutoIncrement)
23+ func execute( preparedStatement: PreparedStatement , parameters: [ String : Any ? ] , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) {
24+ onCompletion ( . successNoData)
2825 }
2926
30- func connect( onCompletion: ( QueryError ? ) -> ( ) ) { }
31-
32- public var isConnected : Bool { return true }
33-
34- func closeConnection( ) { }
27+ var grades = [ Grade] ( )
3528
3629 func execute( query: Query , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) {
3730 do {
3831 let queryComponents = ( try query. build ( queryBuilder: queryBuilder) ) . components ( separatedBy: " " )
3932 switch queryComponents [ 0 ] {
4033 case " SELECT " :
4134 if queryComponents [ 1 ] == " * " {
42- return onCompletion ( . resultSet( ResultSet ( DummyResultFetcher ( grades: grades) ) ) )
35+ if grades. isEmpty {
36+ return onCompletion ( . successNoData)
37+ } else {
38+ return onCompletion ( QueryResult . resultSet ( ResultSet ( DummyResultFetcher ( grades: grades) , connection: self ) ) )
39+ }
4340 }
4441 case " DELETE " :
4542 grades = [ ]
@@ -64,7 +61,7 @@ class DummyConnection: Connection {
6461 case " SELECT " :
6562 if let course = parameters [ 0 ] as? String {
6663 let matchingGrades = grades. filter { $0. course == course }
67- return onCompletion ( . resultSet( ResultSet ( DummyResultFetcher ( grades: matchingGrades) ) ) )
64+ return onCompletion ( . resultSet( ResultSet ( DummyResultFetcher ( grades: matchingGrades) , connection : self ) ) )
6865 }
6966 default :
7067 return onCompletion ( QueryResult . successNoData)
@@ -94,35 +91,45 @@ class DummyConnection: Connection {
9491 return ( try ? query. build ( queryBuilder: queryBuilder) ) ?? " "
9592 }
9693
97- private func returnResult ( _ onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) {
98- return onCompletion ( . successNoData)
94+ func execute ( preparedStatement : PreparedStatement , parameters : [ Any ? ] , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) {
95+ onCompletion ( . successNoData)
9996 }
10097
101- func startTransaction( onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
98+ func execute( preparedStatement: PreparedStatement , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) {
99+ onCompletion ( . successNoData)
100+ }
102101
103- func commit( onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
102+ init ( withDeleteRequiresUsing: Bool = false , withUpdateRequiresFrom: Bool = false ) {
103+ self . queryBuilder = QueryBuilder ( columnBuilder: DummySQLColumnBuilder ( ) )
104+ }
104105
105- func rollback ( onCompletion : @escaping ( ( QueryResult ) -> ( ) ) ) { }
106+ var queryBuilder : QueryBuilder
106107
107- func create ( savepoint : String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
108+ func connect ( onCompletion: @escaping ( QueryResult ) -> ( ) ) { }
108109
109- func rollback( to savepoint: String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
110+ func connectSync( ) -> QueryResult { return QueryResult . successNoData }
111+ func closeConnection( ) { }
110112
111- func release( savepoint: String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
113+ var isConnected : Bool = true
114+
115+ func prepareStatement( _ query: Query , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
116+
117+ func prepareStatement( _ raw: String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
112118
113- struct TestPreparedStatement : PreparedStatement { }
119+ func release( preparedStatement: PreparedStatement , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
120+
121+ func startTransaction( onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
114122
115- func prepareStatement ( _ query : Query ) throws -> PreparedStatement { return TestPreparedStatement ( ) }
123+ func commit ( onCompletion : @escaping ( ( QueryResult ) -> ( ) ) ) { }
116124
117- func prepareStatement ( _ raw : String ) throws -> PreparedStatement { return TestPreparedStatement ( ) }
125+ func rollback ( onCompletion : @escaping ( ( QueryResult ) -> ( ) ) ) { }
118126
119- func execute ( preparedStatement : PreparedStatement , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
127+ func create ( savepoint : String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
120128
121- func execute ( preparedStatement : PreparedStatement , parameters : [ Any ? ] , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
129+ func rollback ( to savepoint : String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
122130
123- func execute ( preparedStatement : PreparedStatement , parameters : [ String : Any ? ] , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
131+ func release ( savepoint : String , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
124132
125- func release( preparedStatement: PreparedStatement , onCompletion: @escaping ( ( QueryResult ) -> ( ) ) ) { }
126133}
127134
128135class DummyResultFetcher : ResultFetcher {
@@ -138,19 +145,55 @@ class DummyResultFetcher: ResultFetcher {
138145 }
139146 }
140147
141- func fetchNext( ) -> [ Any ? ] ? {
148+ func fetchNext( callback : @escaping ( ( [ Any ? ] ? , Error ? ) ) -> ( ) ) {
142149 if fetched < numberOfRows {
143150 fetched += 1
144- return rows [ fetched - 1 ]
151+ callback ( ( rows [ fetched - 1 ] , nil ) )
145152 }
146- return nil
153+ callback ( ( nil , nil ) )
147154 }
148155
149- func fetchNext ( callback: ( [ Any ? ] ? ) -> ( ) ) {
150- callback ( fetchNext ( ) )
156+ func fetchTitles ( callback: @escaping ( ( [ String ] ? , Error ? ) ) -> ( ) ) {
157+ callback ( ( titles , nil ) )
151158 }
152159
153- func fetchTitles( ) -> [ String ] {
154- return titles
160+ func done( ) { }
161+ }
162+
163+ class DummySQLColumnBuilder : ColumnCreator {
164+ func buildColumn( for column: Column , using queryBuilder: QueryBuilder ) -> String ? {
165+ guard let type = column. type else {
166+ return nil
167+ }
168+
169+ var result = column. name
170+ let identifierQuoteCharacter = queryBuilder. substitutions [ QueryBuilder . QuerySubstitutionNames. identifierQuoteCharacter. rawValue]
171+ if !result. hasPrefix ( identifierQuoteCharacter) {
172+ result = identifierQuoteCharacter + result + identifierQuoteCharacter + " "
173+ }
174+
175+ var typeString = type. create ( queryBuilder: queryBuilder)
176+ if let length = column. length {
177+ typeString += " ( \( length) ) "
178+ }
179+
180+ if column. isPrimaryKey {
181+ result += " PRIMARY KEY "
182+ }
183+ if column. isNotNullable {
184+ result += " NOT NULL "
185+ }
186+ if column. isUnique {
187+ result += " UNIQUE "
188+ }
189+ if let checkExpression = column. checkExpression {
190+ result += checkExpression. contains ( column. name) ? " CHECK ( " + checkExpression. replacingOccurrences ( of: column. name, with: " \" \( column. name) \" " ) + " ) " : " CHECK ( " + checkExpression + " ) "
191+ }
192+ if let collate = column. collate {
193+ result += " COLLATE \" " + collate + " \" "
194+ }
195+ return result
155196 }
197+
198+
156199}
0 commit comments