Example #1
0
        // allows only one statement in the sql string
        public static sqlite3_stmt prepare(this sqlite3 db, string sql, params object[] a)
        {
            sqlite3_stmt s = db.prepare(sql);

            s.bind(a);
            return(s);
        }
        private sqlite3_stmt BuildCommand(sqlite3 db, string sql, object[] paramArgs)
        {
            sqlite3_stmt command = null;

            try
            {
                if (!IsOpen)
                {
                    if (Open(Path) == false)
                    {
                        throw new CouchbaseLiteException("Failed to Open " + Path, StatusCode.DbError);
                    }
                }

                int err = raw.sqlite3_prepare_v2(db, sql, out command);

                if (err != raw.SQLITE_OK)
                {
                    Log.E(Tag, "sqlite3_prepare_v2: " + err);
                }

                if (paramArgs.Length > 0 && command != null && err != raw.SQLITE_ERROR)
                {
                    command.bind(paramArgs);
                }
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error when build a sql " + sql + " with params " + paramArgs, e);
                throw;
            }
            return(command);
        }
        private sqlite3_stmt BuildCommand(sqlite3 db, string sql, object[] paramArgs)
        {
            if (db == null)
            {
                Log.To.Database.E(TAG, "db cannot be null in BuildCommand, throwing...");
                throw new ArgumentNullException("db");
            }

            if (!IsOpen)
            {
                throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadRequest, TAG,
                                                 "BuildCommand called on closed database");
            }

            sqlite3_stmt command = null;

            try {
                lock (Cursor.StmtDisposeLock) {
                    LastErrorCode = raw.sqlite3_prepare_v2(db, sql, out command);
                }

                if (LastErrorCode != raw.SQLITE_OK || command == null)
                {
                    Log.To.Database.E(TAG, "sqlite3_prepare_v2: {0}", LastErrorCode);
                }

                if (paramArgs != null && paramArgs.Length > 0 && command != null && LastErrorCode != raw.SQLITE_ERROR)
                {
                    command.bind(paramArgs);
                }
            } catch (CouchbaseLiteException) {
                Log.To.Database.E(TAG, "Error when building sql '{0}' with params {1}, rethrowing...", sql,
                                  new SecureLogJsonString(paramArgs, LogMessageSensitivity.PotentiallyInsecure));
                throw;
            } catch (Exception e) {
                throw Misc.CreateExceptionAndLog(Log.To.Database, e, TAG,
                                                 "Error when building sql '{0}' with params {1}", sql,
                                                 new SecureLogJsonString(paramArgs, LogMessageSensitivity.PotentiallyInsecure));
            }

            return(command);
        }
Example #4
0
 public void test_explicit_prepare()
 {
     using (sqlite3 db = ugly.open(":memory:"))
     {
         db.exec("CREATE TABLE foo (x int);");
         const int num = 7;
         using (sqlite3_stmt stmt = db.prepare("INSERT INTO foo (x) VALUES (?)"))
         {
             for (int i = 0; i < num; i++)
             {
                 stmt.reset();
                 stmt.clear_bindings();
                 stmt.bind(1, i);
                 stmt.step();
             }
         }
         int c = db.query_scalar <int>("SELECT COUNT(*) FROM foo");
         Assert.AreEqual(c, num);
     }
 }
        private sqlite3_stmt BuildCommand(sqlite3 db, string sql, object[] paramArgs)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }

            sqlite3_stmt command = null;

            try
            {
                if (!IsOpen)
                {
                    Open(Path);
                }

                lock (Cursor.StmtDisposeLock)
                {
                    LastErrorCode = raw.sqlite3_prepare_v2(db, sql, out command);
                }

                if (LastErrorCode != raw.SQLITE_OK || command == null)
                {
                    Log.E(TAG, "sqlite3_prepare_v2: " + LastErrorCode);
                }

                if (paramArgs != null && paramArgs.Length > 0 && command != null && LastErrorCode != raw.SQLITE_ERROR)
                {
                    command.bind(paramArgs);
                }
            }
            catch (Exception e)
            {
                Log.E(TAG, "Error when build a sql " + sql + " with params " + paramArgs, e);
                throw;
            }

            return(command);
        }
Example #6
0
        private sqlite3_stmt BuildCommand(sqlite3 db, string sql, object[] paramArgs)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }

            if (!IsOpen)
            {
                throw new CouchbaseLiteException("BuildCommand called on closed database", StatusCode.BadRequest);
            }

            sqlite3_stmt command = null;

            try {
                lock (Cursor.StmtDisposeLock) {
                    LastErrorCode = raw.sqlite3_prepare_v2(db, sql, out command);
                }

                if (LastErrorCode != raw.SQLITE_OK || command == null)
                {
                    Log.E(TAG, "sqlite3_prepare_v2: " + LastErrorCode);
                }

                if (paramArgs != null && paramArgs.Length > 0 && command != null && LastErrorCode != raw.SQLITE_ERROR)
                {
                    command.bind(paramArgs);
                }
            } catch (CouchbaseLiteException) {
                Log.E(TAG, "Error when building sql '{0}' with params {1}", sql, Manager.GetObjectMapper().WriteValueAsString(paramArgs));
                throw;
            } catch (Exception e) {
                throw new CouchbaseLiteException(String.Format("Error when building sql '{0}' with params {1}", sql,
                                                               Manager.GetObjectMapper().WriteValueAsString(paramArgs)), e);
            }

            return(command);
        }
        /// <summary>
        /// Avoids the additional database trip that using SqliteCommandBuilder requires.
        /// </summary>
        /// <returns>The insert command.</returns>
        /// <param name="table">Table.</param>
        /// <param name="values">Values.</param>
        /// <param name="conflictResolutionStrategy">Conflict resolution strategy.</param>
        sqlite3_stmt GetInsertCommand(String table, ContentValues values, ConflictResolutionStrategy conflictResolutionStrategy)
        {
            if (!IsOpen)
            {
                Open(Path);
            }
            var builder = new StringBuilder("INSERT");

            if (conflictResolutionStrategy != ConflictResolutionStrategy.None)
            {
                builder.Append(" OR ");
                builder.Append(conflictResolutionStrategy);
            }

            builder.Append(" INTO ");
            builder.Append(table);
            builder.Append(" (");

            // Append our content column names and create our SQL parameters.
            var valueSet     = values.ValueSet();
            var valueBuilder = new StringBuilder();
            var index        = 0;

            var args = new object[valueSet.Count];

            foreach (var column in valueSet)
            {
                if (index > 0)
                {
                    builder.Append(",");
                    valueBuilder.Append(",");
                }

                builder.AppendFormat("{0}", column.Key);
                valueBuilder.Append("?");

                args[index] = column.Value;

                index++;
            }

            builder.Append(") VALUES (");
            builder.Append(valueBuilder);
            builder.Append(")");

            var          sql     = builder.ToString();
            sqlite3_stmt command = null;

            if (args != null)
            {
                Log.D(Tag, "Preparing statement: '{0}' with values: {1}", sql, String.Join(", ", args.Select(o => o == null ? "null" : o.ToString()).ToArray()));
            }
            else
            {
                Log.D(Tag, "Preparing statement: '{0}'", sql);
            }
            command = _writeConnection.prepare(sql);
            command.bind(args);

            return(command);
        }