SwiftKueryMySQL icon indicating copy to clipboard operation
SwiftKueryMySQL copied to clipboard

Execution of queries without prepare statement needed

Open janigro opened this issue 7 years ago • 1 comments

Hello, I found that some mysql statements cannot be "prepared". For example:

PREPARE mystmt FROM 'LOCK TABLE mytable READ';

returns error:

Error Code: 1295. This command is not supported in the prepared statement protocol yet

For such cases, we need a way of executing statements that bypass the PREPARE STATEMENT phase.

As a temporary workaround, I have created an extension and added the method below. It is based on the existing MySQLConnection.executionTransaction(). However, there is one problem, self.mysql is not public and cannot be accessed from an extension.

In my case, the problem can be either solved by adding an execute method that does not prepare the statement (as the one I wrote), or by making the mysql variable accessible from extensions, so I can use my own method.

Ideally, both changes would be welcomed!

Thank you! Javier.-

  public func executeSimple(command: String, errorMessage: String, onCompletion: @escaping ((QueryResult) -> ())) {
        
        guard let mysql = self.mysql else {
            return runCompletionHandler(.error(QueryError.connection("Not connected, call connect() first")), onCompletion: onCompletion)
        }
        
        DispatchQueue.global().async {
            mysql_thread_init()
            if mysql_query(mysql, command) == 0 {
                mysql_thread_end()
                return self.runCompletionHandler(.successNoData, onCompletion: onCompletion)
            } else {
                let error = self.getError(mysql)
                mysql_thread_end()
                return self.runCompletionHandler(.error(QueryError.databaseError("\(errorMessage): \(error)")), onCompletion: onCompletion)
            }
        }
    }

janigro avatar Apr 21 '19 07:04 janigro

This looks like a reasonable feature to add. Moving to backlog for future consideration.

kilnerm avatar Apr 24 '19 12:04 kilnerm