예제 #1
0
        private void BindParameter(CodeContext context, int index, object arg)
        {
            int rc;

            if (arg == null)
            {
                rc = Sqlite3.sqlite3_bind_null(st, index);
            }
            else if (arg is int)
            {
                rc = Sqlite3.sqlite3_bind_int(st, index, (int)arg);
            }
            else if (arg is bool)
            {
                rc = Sqlite3.sqlite3_bind_int(st, index, (bool)arg ? 1 : 0);
            }
            else if (arg is long)
            {
                rc = Sqlite3.sqlite3_bind_int64(st, index, (long)arg);
            }
            else if (arg is System.Numerics.BigInteger)
            {
                rc = Sqlite3.sqlite3_bind_int64(st, index, (long)((System.Numerics.BigInteger)arg));
            }
            else if (arg is float)
            {
                rc = Sqlite3.sqlite3_bind_double(st, index, (float)arg);
            }
            else if (arg is double)
            {
                rc = Sqlite3.sqlite3_bind_double(st, index, (double)arg);
            }
            else if (arg is string)
            {
                rc = Sqlite3.sqlite3_bind_text(st, index, (string)arg, -1, Sqlite3.SQLITE_TRANSIENT);
            }
            else if (arg is byte[])
            {
                rc = Sqlite3.sqlite3_bind_blob(this.st, index, (byte[])arg, -1, Sqlite3.SQLITE_TRANSIENT);
            }
            else if (arg is PythonBuffer)
            {
                //TODO: see if there is a better way to do this
                PythonBuffer buffer = (PythonBuffer)arg;
                string       s      = buffer[new Slice(0, null)].ToString();
                byte[]       bytes  = PythonSQLite.Latin1.GetBytes(s);

                rc = Sqlite3.sqlite3_bind_blob(this.st, index, bytes, -1, Sqlite3.SQLITE_TRANSIENT);
            }
            else
            {
                throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0} - unsupported type {1}".Format(index, arg.GetType()));
            }

            if (rc != Sqlite3.SQLITE_OK)
            {
                throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0}: {1}".Format(index, Sqlite3.sqlite3_errmsg(db)));
            }
        }
예제 #2
0
        private void BindParameter(CodeContext context, int index, object arg)
        {
            int rc;

            if (arg == null)
            {
                rc = Sqlite3.sqlite3_bind_null(st, index);
            }
            else if (arg is int)
            {
                rc = Sqlite3.sqlite3_bind_int(st, index, (int)arg);
            }
            else if (arg is bool)
            {
                rc = Sqlite3.sqlite3_bind_int(st, index, (bool)arg ? 1 : 0);
            }
            else if (arg is long)
            {
                rc = Sqlite3.sqlite3_bind_int64(st, index, (long)arg);
            }
            else if (arg is System.Numerics.BigInteger)
            {
                rc = Sqlite3.sqlite3_bind_int64(st, index, (long)((System.Numerics.BigInteger)arg));
            }
            else if (arg is float)
            {
                rc = Sqlite3.sqlite3_bind_double(st, index, (float)arg);
            }
            else if (arg is double)
            {
                rc = Sqlite3.sqlite3_bind_double(st, index, (double)arg);
            }
            else if (arg is string)
            {
                rc = Sqlite3.sqlite3_bind_text(st, index, (string)arg, -1, Sqlite3.SQLITE_TRANSIENT);
            }
            else if (arg is byte[])
            {
                rc = Sqlite3.sqlite3_bind_blob(this.st, index, (byte[])arg, -1, Sqlite3.SQLITE_TRANSIENT);
            }
            else
            {
                throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0} - unsupported type {1}".Format(index, arg.GetType()));
            }

            if (rc != Sqlite3.SQLITE_OK)
            {
                throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0}: {1}".Format(index, Sqlite3.sqlite3_errmsg(db)));
            }
        }