@@ -51,7 +51,7 @@ import Darwin
5151/// - SeeAlso: `SQLiteCloudConfig` for configuring the SQLite Cloud connection.
5252/// - SeeAlso: `SQLiteCloudCommand` for representing SQL commands and queries.
5353/// - SeeAlso: `SQLiteCloudResult` for representing the result of database operations.
54- public final actor SQLiteCloud {
54+ public final actor SQLiteCloud : SQLiteCloudProvider {
5555 /// The sqlcloud connection opaque pointer.
5656 ///
5757 /// This property represents an opaque pointer to the SQLite Cloud connection.
@@ -1144,3 +1144,51 @@ public extension SQLiteCloud {
11441144 }
11451145 }
11461146}
1147+
1148+ // MARK: - VM API
1149+
1150+ public extension SQLiteCloud {
1151+ /// Compiles an SQL query into a byte-code virtual machine (VM). This method creates a
1152+ /// `SQLiteCloudVM` instance that you can use to execute the compiled SQL statement.
1153+ ///
1154+ /// - Parameters:
1155+ /// - query: The SQL query to compile.
1156+ ///
1157+ /// - Throws:
1158+ /// - `SQLiteCloudError.connectionFailure`: If the connection to the SQLite Cloud
1159+ /// backend has failed. More details can be found in the associated value
1160+ /// `SQLiteCloudError.ConnectionContext`.
1161+ ///
1162+ /// - `SQLiteCloudError.handleError`: If an error occurs while handling the SQLite
1163+ /// Cloud operation.
1164+ ///
1165+ /// - Returns:
1166+ /// A `SQLiteCloudVM` instance representing the compiled virtual machine for the SQL
1167+ /// query. You can use this VM to execute the SQL statement.
1168+ ///
1169+ /// Example usage:
1170+ ///
1171+ /// ```swift
1172+ /// let query = "SELECT * FROM your_table"
1173+ /// let vm = try await sqliteCloud.compile(query: query)
1174+ /// try await vm.step()
1175+ /// ```
1176+ func compile( query: String ) async throws -> SQLiteCloudVM {
1177+ // Checks if connection is open and valid.
1178+ let conn = try getConnection ( )
1179+
1180+ // Compile an SQL statement into a byte-code virtual machine.
1181+ // This function resembles the sqlite3_prepare SQLite API.
1182+ let vm = SQCloudVMCompile ( conn, query, - 1 , nil )
1183+
1184+ // Checks if compile is failed.
1185+ guard let vm = vm else {
1186+ let error = SQLiteCloudError . handleError ( connection: conn)
1187+ logError ( category: " VIRTUAL MACHINE " , message: " 🚨 VM compile failed: \( error) " )
1188+ throw error
1189+ }
1190+
1191+ logInfo ( category: " VIRTUAL MACHINE " , message: " 🚀 ' \( query) ' virtual machine created succesfully " )
1192+ return SQLiteCloudVM ( vm: vm)
1193+ }
1194+ }
0 commit comments