static DenverDBFactory() { #if (SQLITE_STANDARD || USE_INTEROP_DLL || PLATFORM_COMPACTFRAMEWORK) && PRELOAD_NATIVE_LIBRARY UnsafeNativeMethods.Initialize(); #endif #if INTEROP_LOG if (UnsafeNativeMethods.sqlite3_config_log_interop() == SQLiteErrorCode.Ok) { UnsafeNativeMethods.sqlite3_log( SQLiteErrorCode.Ok, SQLiteConvert.ToUTF8("logging initialized.")); } #endif SQLiteLog.Initialize(); string version = #if NET_40 || NET_45 || NET_451 "4.0.0.0"; #else "3.5.0.0"; #endif _dbProviderServicesType = Type.GetType(String.Format(CultureInfo.InvariantCulture, "System.Data.Common.DbProviderServices, System.Data.Entity, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089", version), false); }
internal static bool ResetConnection(SQLiteConnectionHandle hdl, IntPtr db, bool canThrow) { SQLiteErrorCode sQLiteErrorCode; if (hdl == null || db == IntPtr.Zero) { return(false); } bool flag = false; try { } finally { lock (hdl) { if (canThrow && hdl.IsInvalid) { throw new InvalidOperationException("The connection handle is invalid."); } if (canThrow && hdl.IsClosed) { throw new InvalidOperationException("The connection handle is closed."); } if (!hdl.IsInvalid && !hdl.IsClosed) { IntPtr zero = IntPtr.Zero; do { zero = UnsafeNativeMethods.sqlite3_next_stmt(db, zero); if (zero == IntPtr.Zero) { continue; } sQLiteErrorCode = UnsafeNativeMethods.sqlite3_reset_interop(zero); }while (zero != IntPtr.Zero); if (!SQLiteBase.IsAutocommit(hdl, db)) { sQLiteErrorCode = UnsafeNativeMethods.sqlite3_exec(db, SQLiteConvert.ToUTF8("ROLLBACK"), IntPtr.Zero, IntPtr.Zero, ref zero); if (sQLiteErrorCode == SQLiteErrorCode.Ok) { flag = true; } else if (canThrow) { throw new SQLiteException(sQLiteErrorCode, SQLiteBase.GetLastError(hdl, db)); } } else { flag = true; } } } } GC.KeepAlive(hdl); return(flag); }
public static void Backup(SQLiteConnection source, SQLiteConnection destination) { IntPtr sourceHandle = GetConnectionHandle(source); IntPtr destinationHandle = GetConnectionHandle(destination); IntPtr backupHandle = sqlite3_backup_init(destinationHandle, SQLiteConvert.ToUTF8("main"), sourceHandle, SQLiteConvert.ToUTF8("main")); sqlite3_backup_step(backupHandle, -1); sqlite3_backup_finish(backupHandle); }
public static bool Backup(SQLiteConnection source, SQLiteConnection destination) { lock (Injection.Kernel.Get <IDatabase>().DbBackupLock) { try { IntPtr sourceHandle = GetConnectionHandle(source); IntPtr destinationHandle = GetConnectionHandle(destination); IntPtr backupHandle = sqlite3_backup_init(destinationHandle, SQLiteConvert.ToUTF8("main"), sourceHandle, SQLiteConvert.ToUTF8("main")); if (backupHandle != IntPtr.Zero) { sqlite3_backup_step(backupHandle, -1); sqlite3_backup_finish(backupHandle); string[] tablesToDelete = { "User", "Session", "Server" }; foreach (string tableName in tablesToDelete) { try { IDbCommand q = GetDbCommand("DROP TABLE IF EXISTS " + tableName, destination); q.Prepare(); q.ExecuteNonQuery(); } catch (Exception e) { logger.Error("Error deleting user table in backup: " + e); } } return(true); } return(false); } catch (Exception e) { logger.Error("Error backup up database: " + e); } finally { Close(source, null); Close(destination, null); } return(false); } }
private int xNext(ref sqlite3_tokenizer_cursor pCursor, ref IntPtr ppToken, ref int pnBytes, ref int piStartOffset, ref int piEndOffset, ref int piPosition) { if (this.lastStringPtr != IntPtr.Zero) { Marshal.FreeHGlobal(this.lastStringPtr); this.lastStringPtr = IntPtr.Zero; } if (owner.MoveNext()) { byte[] bytes = SQLiteConvert.ToUTF8(owner.Token); pnBytes = bytes.Length; this.lastStringPtr = Marshal.AllocHGlobal(pnBytes); Marshal.Copy(bytes, 0, this.lastStringPtr, pnBytes); ppToken = this.lastStringPtr; pnBytes--; string prevString = owner.InputString.Substring(0, owner.TokenIndexOfString); piStartOffset = Encoding.UTF8.GetByteCount(prevString); if (owner.NextIndexOfString < 0) // default { piEndOffset = piStartOffset + pnBytes; } else { prevString = owner.InputString.Substring(0, owner.NextIndexOfString); piEndOffset = Encoding.UTF8.GetByteCount(prevString); } owner.tokenNumber++; piPosition = owner.tokenNumber; return(SQLITE_OK); } else { return(SQLITE_DONE); } }
public static bool SaveToDatabase(string nummer, int snelheid, int zone) { // Bouw de insert-query op met de gegeven informatie DateTimeOffset dto = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); int timestamp = Convert.ToInt32(dto); var naam = SQLiteConvert.ToUTF8(nummer); Database.Query = "INSERT INTO RFIDS (Timestamp, nummer,snelheid) values (" + timestamp + ", " + naam + ", " + snelheid + "," + zone + ")"; Database.OpenConnection(); bool success = false; try { // ExecuteNonQuery wordt gebruikt als we geen gegevens verwachten van de query Database.Command.ExecuteNonQuery(); success = true; } catch (SQLiteException e) { // Code 19 geeft aan dat een veld wat uniek moet zijn in de database, dit door // deze insert niet meer zou zijn. Het is dus niet toegevoegd. Aangezien in deze // applicatie deze constraint alleen op het Kunstnummer staat, kunnen we de // foutmelding heel specifiek weergeven. if (e.ErrorCode == 19) { return(success); } } Database.CloseConnection(); return(success); }
internal override void Open(string strFilename, string vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool) { SQLiteErrorCode sQLiteErrorCode; if (this._sql != null) { this.Close(true); } if (this._sql != null) { throw new SQLiteException("connection handle is still active"); } this._usePool = usePool; this._fileName = strFilename; this._flags = connectionFlags; if (usePool) { this._sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out this._poolVersion); SQLiteConnectionHandle sQLiteConnectionHandle = this._sql; object[] objArray = new object[] { typeof(SQLite3_UTF16), strFilename, vfsName, connectionFlags, openFlags, maxPoolSize, usePool, this._poolVersion }; SQLiteConnection.OnChanged(null, new ConnectionEventArgs(SQLiteConnectionEventType.OpenedFromPool, null, null, null, null, sQLiteConnectionHandle, strFilename, objArray)); } if (this._sql == null) { try { } finally { IntPtr zero = IntPtr.Zero; int num = ((connectionFlags & SQLiteConnectionFlags.NoExtensionFunctions) != SQLiteConnectionFlags.NoExtensionFunctions ? 1 : 0); if (vfsName != null || num != 0) { sQLiteErrorCode = UnsafeNativeMethods.sqlite3_open16_interop(SQLiteConvert.ToUTF8(strFilename), SQLiteConvert.ToUTF8(vfsName), openFlags, num, ref zero); } else { if ((openFlags & SQLiteOpenFlagsEnum.Create) != SQLiteOpenFlagsEnum.Create && !File.Exists(strFilename)) { throw new SQLiteException(SQLiteErrorCode.CantOpen, strFilename); } if (vfsName != null) { CultureInfo currentCulture = CultureInfo.CurrentCulture; object[] objArray1 = new object[] { vfsName }; throw new SQLiteException(SQLiteErrorCode.CantOpen, HelperMethods.StringFormat(currentCulture, "cannot open using UTF-16 and VFS \"{0}\": need interop assembly", objArray1)); } sQLiteErrorCode = UnsafeNativeMethods.sqlite3_open16(strFilename, ref zero); } if (sQLiteErrorCode != SQLiteErrorCode.Ok) { throw new SQLiteException(sQLiteErrorCode, null); } this._sql = new SQLiteConnectionHandle(zero, true); } lock (this._sql) { } SQLiteConnectionHandle sQLiteConnectionHandle1 = this._sql; object[] objArray2 = new object[] { typeof(SQLite3_UTF16), strFilename, vfsName, connectionFlags, openFlags, maxPoolSize, usePool }; SQLiteConnection.OnChanged(null, new ConnectionEventArgs(SQLiteConnectionEventType.NewCriticalHandle, null, null, null, null, sQLiteConnectionHandle1, strFilename, objArray2)); } if ((connectionFlags & SQLiteConnectionFlags.NoBindFunctions) != SQLiteConnectionFlags.NoBindFunctions) { if (this._functions == null) { this._functions = new Dictionary <SQLiteFunctionAttribute, SQLiteFunction>(); } foreach (KeyValuePair <SQLiteFunctionAttribute, SQLiteFunction> value in SQLiteFunction.BindFunctions(this, connectionFlags)) { this._functions[value.Key] = value.Value; } } this.SetTimeout(0); GC.KeepAlive(this._sql); }
public static SQLiteBlob Create(SQLiteDataReader dataReader, int i, bool readOnly) { SQLiteConnection connection = SQLiteDataReader.GetConnection(dataReader); if (connection == null) { throw new InvalidOperationException("Connection not available"); } SQLite3 sQLite3 = connection._sql as SQLite3; if (sQLite3 == null) { throw new InvalidOperationException("Connection has no wrapper"); } SQLiteConnectionHandle sQLiteConnectionHandle = sQLite3._sql; if (sQLiteConnectionHandle == null) { throw new InvalidOperationException("Connection has an invalid handle."); } long?rowId = dataReader.GetRowId(i); if (!rowId.HasValue) { throw new InvalidOperationException("No RowId is available"); } SQLiteBlobHandle sQLiteBlobHandle = null; try { } finally { IntPtr zero = IntPtr.Zero; SQLiteErrorCode sQLiteErrorCode = UnsafeNativeMethods.sqlite3_blob_open(sQLiteConnectionHandle, SQLiteConvert.ToUTF8(dataReader.GetDatabaseName(i)), SQLiteConvert.ToUTF8(dataReader.GetTableName(i)), SQLiteConvert.ToUTF8(dataReader.GetName(i)), rowId.Value, (readOnly ? 0 : 1), ref zero); if (sQLiteErrorCode != SQLiteErrorCode.Ok) { throw new SQLiteException(sQLiteErrorCode, null); } sQLiteBlobHandle = new SQLiteBlobHandle(sQLiteConnectionHandle, zero); } object[] objArray = new object[] { typeof(SQLiteBlob), dataReader, i, readOnly }; SQLiteConnection.OnChanged(null, new ConnectionEventArgs(SQLiteConnectionEventType.NewCriticalHandle, null, null, null, dataReader, sQLiteBlobHandle, null, objArray)); return(new SQLiteBlob(sQLite3, sQLiteBlobHandle)); }