public static void ThrowExceptionForRC(int rc, Sqlite3Handle db) { if (rc == Constants.SQLITE_OK || rc == Constants.SQLITE_ROW || rc == Constants.SQLITE_DONE) { return; } var message = db == null || db.IsInvalid ? NativeMethods.sqlite3_errstr(rc) : NativeMethods.sqlite3_errmsg(db); throw new SqliteException(Strings.FormatSqliteNativeError(rc, message), rc); }
public static int sqlite3_exec(Sqlite3Handle pDb, string sql) { var zSql = MarshalEx.StringToHGlobalUTF8(sql); try { IntPtr tmp; return(Sqlite3.exec(pDb, zSql, IntPtr.Zero, IntPtr.Zero, out tmp)); } finally { if (zSql != IntPtr.Zero) { Marshal.FreeHGlobal(zSql); } } }
public static string sqlite3_db_filename(Sqlite3Handle db, string zDbName) { var ptr = MarshalEx.StringToHGlobalUTF8(zDbName); try { return(MarshalEx.PtrToStringUTF8(Sqlite3.db_filename(db, ptr))); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } }
//TODO move to a utility class public static int sqlite3_step_blocking(Sqlite3Handle db, Sqlite3StmtHandle stmt, int milliseconds) { var timer = new Stopwatch(); int rc; timer.Start(); while (SQLITE_LOCKED == (rc = sqlite3_step(stmt))) { if (timer.ElapsedMilliseconds >= milliseconds) { return(rc); } sqlite3_reset(stmt); } return(rc); }
public static int sqlite3_prepare_v2(Sqlite3Handle db, string zSql, out Sqlite3StmtHandle ppStmt, out string pzTail) { int nByte; var zSqlPtr = MarshalEx.StringToHGlobalUTF8(zSql, out nByte); try { IntPtr pzTailPtr; var rc = Sqlite3.prepare_v2(db, zSqlPtr, nByte, out ppStmt, out pzTailPtr); pzTail = MarshalEx.PtrToStringUTF8(pzTailPtr); return(rc); } finally { Marshal.FreeHGlobal(zSqlPtr); } }
public static int sqlite3_open_v2(string filename, out Sqlite3Handle ppDb, int flags, string vfs) { var zFilename = MarshalEx.StringToHGlobalUTF8(filename); var zVfs = string.IsNullOrEmpty(vfs) ? IntPtr.Zero : MarshalEx.StringToHGlobalUTF8(vfs); try { return(Sqlite3.open_v2(zFilename, out ppDb, flags, zVfs)); } finally { if (zFilename != IntPtr.Zero) { Marshal.FreeHGlobal(zFilename); } if (zVfs != IntPtr.Zero) { Marshal.FreeHGlobal(zVfs); } } }
public static int sqlite3_blob_open(Sqlite3Handle pDb, string db, string table, string column, long iRow, int flags, out Sqlite3BlobHandle ppBlob) { var zDb = MarshalEx.StringToHGlobalUTF8(db); var zTable = MarshalEx.StringToHGlobalUTF8(table); var zColumn = MarshalEx.StringToHGlobalUTF8(column); try { return(Sqlite3.blob_open(pDb, zDb, zTable, zColumn, iRow, flags, out ppBlob)); } finally { if (zDb != IntPtr.Zero) { Marshal.FreeHGlobal(zDb); } if (zTable != IntPtr.Zero) { Marshal.FreeHGlobal(zTable); } if (zColumn != IntPtr.Zero) { Marshal.FreeHGlobal(zColumn); } } }
private static extern int sqlite3_open_v2(IntPtr filename, out Sqlite3Handle ppDb, int flags, IntPtr vfs);
private static extern IntPtr sqlite3_errmsg_raw(Sqlite3Handle db);
private static extern IntPtr sqlite3_db_filename(Sqlite3Handle db, IntPtr zDbName);
public static string sqlite3_errmsg(Sqlite3Handle db) => MarshalEx.PtrToStringUTF8(Sqlite3.errmsg(db));
public static int sqlite3_busy_timeout(Sqlite3Handle db, int ms) => Sqlite3.busy_timeout(db, ms);
public int blob_open(Sqlite3Handle ppDb, IntPtr zDb, IntPtr zTable, IntPtr zColumn, long iRow, int flags, out Sqlite3BlobHandle ppBlob) => sqlite3_blob_open(ppDb, zDb, zTable, zColumn, iRow, flags, out ppBlob);
public int exec(Sqlite3Handle pDb, IntPtr zSql, IntPtr callback, IntPtr callbackArg, out IntPtr errMsg) => sqlite3_exec(pDb, zSql, callback, callbackArg, out errMsg);
public int open_v2(IntPtr filename, out Sqlite3Handle ppDb, int flags, IntPtr vfs) => sqlite3_open_v2(filename, out ppDb, flags, vfs);
public IntPtr errmsg(Sqlite3Handle db) => sqlite3_errmsg(db);
public int enable_load_extension(Sqlite3Handle db, int onoff) => sqlite3_enable_load_extension(db, onoff);
private static extern int sqlite3_enable_load_extension(Sqlite3Handle db, int onoff);
public IntPtr db_filename(Sqlite3Handle db, IntPtr zDbName) => sqlite3_db_filename(db, zDbName);
public int busy_timeout(Sqlite3Handle db, int ms) => sqlite3_busy_timeout(db, ms);
private static extern int sqlite3_prepare_v2(Sqlite3Handle db, IntPtr zSql, int nByte, out Sqlite3StmtHandle ppStmt, out IntPtr pzTail);
//TODO move to a utility class public static int sqlite3_step_blocking(Sqlite3Handle db, Sqlite3StmtHandle stmt, int milliseconds) { var timer = new Stopwatch(); int rc; timer.Start(); while (SQLITE_LOCKED == (rc = sqlite3_step(stmt))) { if (timer.ElapsedMilliseconds >= milliseconds) { return rc; } sqlite3_reset(stmt); } return rc; }
public int prepare_v2(Sqlite3Handle db, IntPtr zSql, int nByte, out Sqlite3StmtHandle ppStmt, out IntPtr pzTail) => sqlite3_prepare_v2(db, zSql, nByte, out ppStmt, out pzTail);
public override string GetFilename(Sqlite3Handle db, string zDbName) => NativeMethods.sqlite3_db_filename(db, zDbName);
private static extern int sqlite3_changes(Sqlite3Handle db);
public virtual string GetFilename(Sqlite3Handle db, string zDbName) => null;
public int changes(Sqlite3Handle db) => sqlite3_changes(db);
public static string sqlite3_db_filename(Sqlite3Handle db, string zDbName) { var ptr = MarshalEx.StringToHGlobalUTF8(zDbName); try { return MarshalEx.PtrToStringUTF8(sqlite3_db_filename(db, ptr)); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } }
public override void Close() { if (_db == null || _db.IsInvalid) { return; } Transaction?.Dispose(); _db.Dispose(); _db = null; SetState(ConnectionState.Closed); }
public static string sqlite3_errmsg(Sqlite3Handle db) => MarshalEx.PtrToStringUTF8(sqlite3_errmsg_raw(db));
public static int sqlite3_enable_load_extension(Sqlite3Handle db, int onoff) => Sqlite3.enable_load_extension(db, onoff);
public static int sqlite3_open_v2(string filename, out Sqlite3Handle ppDb, int flags, string vfs) { var zFilename = MarshalEx.StringToHGlobalUTF8(filename); var zVfs = string.IsNullOrEmpty(vfs) ? IntPtr.Zero : MarshalEx.StringToHGlobalUTF8(vfs); try { return sqlite3_open_v2(zFilename, out ppDb, flags, zVfs); } finally { if (zFilename != IntPtr.Zero) { Marshal.FreeHGlobal(zFilename); } if (zVfs != IntPtr.Zero) { Marshal.FreeHGlobal(zVfs); } } }
public static int sqlite3_prepare_v2(Sqlite3Handle db, string zSql, out Sqlite3StmtHandle ppStmt, out string pzTail) { int nByte; var zSqlPtr = MarshalEx.StringToHGlobalUTF8(zSql, out nByte); try { // TODO: Something fancy with indexes? IntPtr pzTailPtr; var rc = sqlite3_prepare_v2(db, zSqlPtr, nByte, out ppStmt, out pzTailPtr); pzTail = MarshalEx.PtrToStringUTF8(pzTailPtr); return rc; } finally { Marshal.FreeHGlobal(zSqlPtr); } }
public static int sqlite3_changes(Sqlite3Handle db) => Sqlite3.changes(db);
private static extern int sqlite3_exec(Sqlite3Handle ppDb, IntPtr zSql, IntPtr callback, IntPtr callbackArg, out IntPtr errMsg);
private static extern int sqlite3_blob_open(Sqlite3Handle pDb, IntPtr zDb, IntPtr zTable, IntPtr zColumn, long iRow, int flags, out Sqlite3BlobHandle ppBlob);
public static string GetFilename(Sqlite3Handle db, string zDbName) => _strategy.GetFilename(db, zDbName);
public static extern int sqlite3_busy_timeout(Sqlite3Handle db, int ms);
public static extern int sqlite3_changes(Sqlite3Handle db);