Ejemplo n.º 1
0
        private void GetNextStatement(string pzStart, ref string pzTail, ref Sqlite3.Vdbe pStmt)
        {
            SQLiteError err = (SQLiteError)Sqlite3.sqlite3_prepare_v2(parent_conn.Handle2, pzStart, pzStart.Length, ref pStmt, ref pzTail);

            if (err != SQLiteError.OK)
            {
                throw new SQLiteSyntaxException(parent_conn.Handle2.errCode, GetError3());
            }
        }
 private void NotifyOnSQLiteException(SQLiteError error)
 {
     try
     {
         OnSQLiteError?.Invoke(error);
     }
     catch (Exception)
     {
         // no-op
     }
 }
        private Dictionary <string, object> ExecuteQuery(OpenDB dbInfo, DBQuery query)
        {
            System.Diagnostics.Debug.Assert(!_isExecutingQuery, "SQLitePluginModule: Only 1 query should be executing at a time.");

            _isExecutingQuery = true;
            try
            {
                if (query.SQL == null)
                {
                    throw new RNSQLiteException("You must specify a sql query to execute");
                }

                try
                {
                    var previousRowsAffected = _sqliteAPI.TotalChanges(dbInfo.Handle);

                    var statement = _sqliteAPI.Prepare2(dbInfo.Handle, query.SQL);
                    if (query.Params != null)
                    {
                        var argIndex = 0;
                        foreach (var arg in query.Params.Children())
                        {
                            // sqlite bind uses 1-based indexing for the arguments
                            BindStatement(statement, argIndex + 1, arg);
                            argIndex++;
                        }
                    }

                    var resultRows = new List <Dictionary <string, object> >();

                    long?       insertId     = null;
                    var         rowsAffected = 0;
                    SQLiteError error        = null;
                    var         keepGoing    = true;
                    while (keepGoing)
                    {
                        switch (_sqliteAPI.Step(statement))
                        {
                        case SQLite.Net.Interop.Result.Row:
                            resultRows.Add(ExtractRow(statement));
                            break;

                        case SQLite.Net.Interop.Result.Done:
                            var nowRowsAffected = _sqliteAPI.TotalChanges(dbInfo.Handle);
                            rowsAffected = nowRowsAffected - previousRowsAffected;
                            var nowInsertId = _sqliteAPI.LastInsertRowid(dbInfo.Handle);
                            if (rowsAffected > 0 && nowInsertId != 0)
                            {
                                insertId = nowInsertId;
                            }
                            keepGoing = false;
                            break;

                        default:
                            var webErrorCode = sqliteToWebSQLError(_sqliteAPI.ErrCode(dbInfo.Handle));
                            var message      = _sqliteAPI.Errmsg16(dbInfo.Handle);
                            error     = new SQLiteError(webErrorCode, message);
                            keepGoing = false;
                            break;
                        }
                    }

                    _sqliteAPI.Finalize(statement);

                    if (error != null)
                    {
                        NotifyOnSQLiteException(error);
                        throw new RNSQLiteException(error);
                    }

                    var resultSet = new Dictionary <string, object>
                    {
                        { "rows", resultRows },
                        { "rowsAffected", rowsAffected }
                    };
                    if (insertId != null)
                    {
                        resultSet["insertId"] = insertId;
                    }
                    return(resultSet);
                }
                catch (SQLite.Net.SQLiteException ex)
                {
                    var error = new SQLiteError(sqliteToWebSQLError(ex.Result), ex.Message);
                    NotifyOnSQLiteException(error);
                    throw new RNSQLiteException(error);
                }
            }
            finally
            {
                _isExecutingQuery = false;
            }
        }
Ejemplo n.º 4
0
 public virtual void OnSQLiteError(SQLiteErrorEventArgs e)
 {
     SQLiteError?.Invoke(this, e);
 }