public byte[] GetTile(TileInfo tileInfo) { var index = tileInfo.Index; if (IsTileIndexValid(index)) { byte[] result; var cn = new SQLiteConnectionWithLock(_connectionString, SQLiteOpenFlags.ReadOnly); using (cn.Lock()) { const string sql = "SELECT tile_data FROM \"tiles\" WHERE zoom_level=? AND tile_row=? AND tile_column=?;"; result = cn.ExecuteScalar <byte[]>(sql, int.Parse(index.Level), index.Row, index.Col); } return(result == null || result.Length == 0 ? null : result); } return(null); }
public Task <T> ExecuteScalarAsync <T>(string sql, params object[] args) { if (sql == null) { throw new ArgumentNullException("sql"); } if (args == null) { throw new ArgumentNullException("args"); } return(Task.Factory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { SQLiteCommand command = conn.CreateCommand(sql, args); return command.ExecuteScalar <T>(); } }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default)); }
public SQLiteAsyncConnection GetAsyncConnection() { var dbPath = GetDatabasePath(); var platForm = new SQLitePlatformAndroid(); var connectionFactory = new Func <SQLiteConnectionWithLock>( () => { if (_conn == null) { _conn = new SQLiteConnectionWithLock(platForm, new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: true)); } return(_conn); }); return(new SQLiteAsyncConnection(connectionFactory)); }
public SearchService(IMvxSqliteConnectionFactory connectionFactory, ICultureProvider cultureProvider, DatabaseSettings settings) { if (connectionFactory == null) { throw new ArgumentNullException(nameof(connectionFactory)); } if (cultureProvider == null) { throw new ArgumentNullException(nameof(cultureProvider)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _cultureProvider = cultureProvider; _parser = new QueryParser(cultureProvider); var config = new SqLiteConfig(settings.DatabaseName, serializer: new JsonBlobSerializer()); _connection = connectionFactory.GetConnectionWithLock(config); }
public Task <List <T> > QueryAsync <T>(string sql, params object[] args) where T : new() { if (sql == null) { throw new ArgumentNullException("sql"); } if (args == null) { throw new ArgumentNullException("args"); } return(_taskFactory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { return conn.Query <T>(sql, args); } })); }
public Task <T> ExecuteScalarAsync <T>(string sql, params object[] args) { if (sql == null) { throw new ArgumentNullException("sql"); } if (args == null) { throw new ArgumentNullException("args"); } return(_taskFactory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { SQLiteCommand command = conn.CreateCommand(sql, args); return command.ExecuteScalar <T>(); } })); }
public Task <int> ExecuteAsync(string query, params object[] args) { if (query == null) { throw new ArgumentNullException("query"); } if (args == null) { throw new ArgumentNullException("args"); } return(_taskFactory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { return conn.Execute(query, args); } })); }
public Task <List <T> > QueryAsync <T>(string sql, params object[] args) where T : new() { if (sql == null) { throw new ArgumentNullException("sql"); } if (args == null) { throw new ArgumentNullException("args"); } return(Task.Factory.StartNew(() => { SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { return conn.Query <T>(sql, args); } }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default)); }
public SQLiteAsyncConnection GetConnection() { var sqliteFilename = "WorkTimer.db3"; string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder var path = Path.Combine(libraryPath, sqliteFilename); // This is where we copy in the prepopulated database if (!File.Exists(path)) { File.Create(path); } var plat = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS(); var connectionString = new SQLiteConnectionString(path, false); var synchronousConn = new SQLiteConnectionWithLock(plat, connectionString); var conn = new SQLiteAsyncConnection(() => synchronousConn); // Return the database connection return(conn); }
public Task <CreateTablesResult> CreateTablesAsync(params Type[] types) { if (types == null) { throw new ArgumentNullException("types"); } return(Task.Factory.StartNew(() => { var result = new CreateTablesResult(); SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { foreach (Type type in types) { int aResult = conn.CreateTable(type); result.Results[type] = aResult; } } return result; }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default)); }
public void DeleteDatabase() { var path = GetDatabasePath(); try { if (_conn != null) { _conn.Close(); } } catch { //no hay que preocuparse si tira una exception } if (File.Exists(path)) { File.Delete(path); } _conn = null; }
public Task <CreateTablesResult> CreateTablesAsync(params Type[] types) { if (types == null) { throw new ArgumentNullException("types"); } return(_taskFactory.StartNew(() => { var result = new CreateTablesResult(); SQLiteConnectionWithLock conn = GetConnection(); using (conn.Lock()) { foreach (Type type in types) { int aResult = conn.CreateTable(type); result.Results[type] = aResult; } } return result; })); }
public void UpdateAllWithChildren(System.Collections.IEnumerable collection) { foreach (T item in collection) { //using (SQLiteConnection _dbManager = new SQLiteConnection(_baseUrl.GetDatabasePath(), WriteOnlyFlags)) using (SQLiteConnectionWithLock _dbManager = new SQLiteConnectionWithLock(new SQLiteConnectionString(_baseUrl.GetDatabasePath(), false, null), SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.SharedCache)) { using (_dbManager.Lock()) { try { _dbManager.UpdateWithChildren(item); } catch (Exception ex) { Debug.WriteLine($"SQLiteError: {ex.Message}"); } } } } }
public SQLiteAsyncConnection GetAsyncConnection() { lock (_connectionLock) { var sqliteFilename = DatabaseName; string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename); var platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(); var connectionString = new SQLiteConnectionString(path, storeDateTimeAsTicks: false); var connectionFactory = new Func <SQLiteConnectionWithLock>( () => { if (_conn == null) { _conn = new SQLiteConnectionWithLock(platform, connectionString); } return(_conn); }); return(new SQLiteAsyncConnection(connectionFactory)); } }
public MbTilesTileSource(SQLiteConnectionString connectionString, ITileSchema schema = null, MbTilesType type = MbTilesType.None, bool tileRangeOptimization = true) { _connectionString = connectionString; if (tileRangeOptimization || schema == null) { using (var connection = new SQLiteConnectionWithLock(connectionString, SQLiteOpenFlags.ReadOnly)) using (connection.Lock()) { Type = type == MbTilesType.None ? ReadType(connection) : type; var schemaFromDatabase = ReadSchemaFromDatabase(connection); Schema = schema ?? schemaFromDatabase; // the tile range should be based on the tiles actually present. var zoomLevelsFromDatabase = schemaFromDatabase.Resolutions.Select(r => r.Key); _tileRange = ReadZoomLevelsFromTilesTable(connection, zoomLevelsFromDatabase); } } else { Schema = schema; _tileRange = null; } }
private static SQLiteConnectionWithLock ConnectionFactory() { if (writeConn != null) { return(writeConn); } //SQLite3.Config(ConfigOption.Serialized); var databaseFile = "writeDatabase.db"; var connectionString = new SQLiteConnectionString(databaseFile, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex, true, null); writeConn = new SQLiteConnectionWithLock(connectionString); using (writeConn.Lock()) { writeConn.CreateCommand(@"PRAGMA synchronous = NORMAL; PRAGMA journal_mode = WAL;", Array.Empty <object>()).ExecuteScalar <int>(); } return(writeConn); }
public void DeleteDatabase() { var path = GetDatabasePath(); try { if (_conn != null) { _conn.Close(); } } catch { // Best effort close. No need to worry if throws an exception } if (File.Exists(path)) { File.Delete(path); } _conn = null; }
public MbTilesTileSource(SQLiteConnectionString connectionString, ITileSchema schema = null, MbTilesType type = MbTilesType.None, bool tileRangeOptimization = true) { _connectionString = connectionString; if (tileRangeOptimization || schema == null) { using (var connection = new SQLiteConnectionWithLock(connectionString)) using (connection.Lock()) { Type = type == MbTilesType.None ? ReadType(connection) : type; var schemaFromDatabase = ReadSchemaFromDatabase(connection); Schema = schema ?? schemaFromDatabase; // Read other stuff Version = ReadString(connection, "version"); Attribution = new Attribution(ReadString(connection, "attribution")); Description = ReadString(connection, "description"); Name = ReadString(connection, "name"); Json = ReadString(connection, "json"); Compression = ReadString(connection, "compression"); var zoomMin = ReadInt(connection, "minzoom"); ZoomMin = zoomMin < 0 ? 0 : zoomMin; var zoomMax = ReadInt(connection, "maxzoom"); ZoomMax = zoomMax < 0 ? int.MaxValue : zoomMax; // the tile range should be based on the tiles actually present. var zoomLevelsFromDatabase = schemaFromDatabase.Resolutions.Select(r => r.Key); _tileRange = ReadZoomLevelsFromTilesTable(connection, zoomLevelsFromDatabase); } } else { Schema = schema; _tileRange = null; } }
public Func <SQLiteConnectionWithLock> GetReadConnectionFactory() { return(() => { if (readConnection != null) { return readConnection; } var databaseFile = "readDatabase.db"; var connectionString = new SQLiteConnectionString(databaseFile, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex, true, null); readConnection = new SQLiteConnectionWithLock(connectionString); using (readConnection.Lock()) { readConnection.CreateCommand(@"PRAGMA synchronous = NORMAL; PRAGMA journal_mode = WAL;", Array.Empty <object>()).ExecuteScalar <int>(); } return readConnection; }); }
public StorageResetter(SQLiteConnectionWithLock connection) { this.connection = connection; }
public TransactionDisposer(SQLiteConnectionWithLock connection, IDisposable @lock) { this.connection = connection; this.@lock = @lock; }
public SQLiteAsyncConnection DbConnect(string dbName) { var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); var librarypPath = Path.Combine(documentsPath, "..", "Library"); var path = Path.Combine(librarypPath, dbName); var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS(); var connectionFactory = new Func <SQLiteConnectionWithLock>(() => _connection ?? (_connection = new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, storeDateTimeAsTicks: false)))); return(new SQLiteAsyncConnection(connectionFactory)); }
public SqLiteConnectionFactory(IAppConfig appConfig) { _appDBConnectionWithLock = new SQLiteConnectionWithLock(appConfig.SqLitePlatform, new SQLiteConnectionString(appConfig.AppDatabaseConnectionString, true)); _logsDBConnectionWithLock = new SQLiteConnectionWithLock(appConfig.SqLitePlatform, new SQLiteConnectionString(appConfig.LogDatabaseConnectionString, true)); }
public void Dispose() { connection?.Dispose(); connection = null; }
static public IDisposable Lock(this SQLiteConnectionWithLock connection) { var lockMethod = connection.GetType().GetTypeInfo().GetDeclaredMethod("Lock"); return((IDisposable)lockMethod.Invoke(connection, null)); }
public void OnApplicationSuspended() { Connection.Dispose(); Connection = null; }
public Entry(SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags) { ConnectionString = connectionString; Connection = new SQLiteConnectionWithLock(connectionString, openFlags); }
public SQLiteAsyncConnection GetAsyncConnection() { var connectionFactory = new Func <SQLiteConnectionWithLock>(() => _conn ?? (_conn = new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(GetDatabasePath(), true)))); return(new SQLiteAsyncConnection(connectionFactory)); }
public AppInternalDatabase(SQLiteConnectionWithLock connection) { Connection = connection; }
public DataManager() { var connectionWithLock = new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString("database.sqlite", true)); _connection = new SQLiteAsyncConnection(() => connectionWithLock); }