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.__getslice__(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))); } }
private static void setResult(Sqlite3.sqlite3_context ctx, object result) { if (result == null) { Sqlite3.sqlite3_result_null(ctx); } else if (result is bool) { Sqlite3.sqlite3_result_int64(ctx, ((bool)result) ? 1 : 0); } else if (result is int) { Sqlite3.sqlite3_result_int64(ctx, (int)result); } else if (result is long) { Sqlite3.sqlite3_result_int64(ctx, (long)result); } else if (result is System.Numerics.BigInteger) { Sqlite3.sqlite3_result_int64(ctx, (long)((System.Numerics.BigInteger)result)); } else if (result is float) { Sqlite3.sqlite3_result_double(ctx, (float)result); } else if (result is double) { Sqlite3.sqlite3_result_double(ctx, (double)result); } else if (result is string) { Sqlite3.sqlite3_result_text(ctx, (string)result, -1, Sqlite3.SQLITE_TRANSIENT); } else if (result is byte[]) { byte[] b = (byte[])result; string s = Latin1.GetString(b, 0, b.Length); Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT); } else if (result is PythonBuffer) { PythonBuffer buffer = (PythonBuffer)result; string s = buffer.__getslice__(0, null).ToString(); Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT); } else { // TODO raise error } }