Ejemplo n.º 1
0
 public void Initialize()
 {
     using (connection.Lock())
     {
         connection.CreateTable <BackgroundTrackItem>();
     }
 }
Ejemplo n.º 2
0
 public Task RunInTransactionAsync(Action <SQLiteConnection> action)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             conn.BeginTransaction();
             try
             {
                 action(conn);
                 conn.Commit();
             }
             catch (Exception)
             {
                 conn.Rollback();
                 throw;
             }
         }
     }));
 }
Ejemplo n.º 3
0
        private static SQLiteConnectionWithLock ReadConnectionFactory()
        {
            if (readConn != null)
            {
                return(readConn);
            }
            //SQLite3.Config(ConfigOption.Serialized);

            var databaseFile = "readDatabase.db";

            var connectionString = new SQLiteConnectionString(databaseFile, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex, true, null);

            readConn = new SQLiteConnectionWithLock(connectionString);

            using (readConn.Lock())
            {
                readConn.CreateCommand(@"PRAGMA synchronous = NORMAL;
PRAGMA temp_store = MEMORY;
PRAGMA page_size = 4096;
PRAGMA cache_size = 10000;
PRAGMA journal_mode = WAL;", Array.Empty <object>()).ExecuteScalar <int>();
            }

            return(readConn);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString">The connection string to the mbtiles file</param>
        /// <param name="schema">The TileSchema of the mbtiles file. If this parameter is set the schema information
        /// within the mbtiles file will be ignored. </param>
        /// <param name="type">BaseLayer or Overlay</param>
        /// <param name="determineZoomLevelsFromTilesTable">When 'determineZoomLevelsFromTilesTable' is true the zoom levels
        /// will be determined from the available tiles in the 'tiles' table. This operation can take long if there are many tiles in
        /// the 'tiles' table. When 'determineZoomLevelsFromTilesTable' is false the zoom levels will be read from the metadata table
        ///(by reading 'zoommin' and 'zoommax'). If there are no zoom levels specificied in the metadata table the GlobalSphericalMercator
        ///default levels are assumed. This parameter will have no effect if the schema is passed in as argument. The default is false.</param>
        /// <param name="determineTileRangeFromTilesTable">In some cases not all tiles specified by the schema are present in each
        /// level. When 'determineTileRangeFromTilesTable' is 'true' the range of tiles available for each level is determined
        /// by the tiles present for each level in the 'tiles' table. The advantage is that requests can be faster because they do not have to
        /// go to the database if they are outside the TileRange. The downside is that for large files it can take long to read the TileRange
        /// from the tiles table. The default is false.</param>
        public MbTilesTileSource(SQLiteConnectionString connectionString, ITileSchema schema = null, MbTilesType type  = MbTilesType.None,
                                 bool determineZoomLevelsFromTilesTable = false, bool determineTileRangeFromTilesTable = false)
        {
            _connectionString = connectionString;

            using (var connection = new SQLiteConnectionWithLock(connectionString))
                using (connection.Lock())
                {
                    Schema      = schema ?? ReadSchemaFromDatabase(connection, determineZoomLevelsFromTilesTable);
                    Type        = type == MbTilesType.None ? ReadType(connection) : type;
                    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");

                    if (determineTileRangeFromTilesTable)
                    {
                        // the tile range should be based on the tiles actually present.
                        var zoomLevelsFromDatabase = Schema.Resolutions.Select(r => r.Key);
                        _tileRange = ReadTileRangeForEachLevelFromTilesTable(connection, zoomLevelsFromDatabase);
                    }
                }
        }
Ejemplo n.º 5
0
 public Task RunInTransactionAsync(Action <SQLiteConnection> action)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             conn.BeginTransaction();
             try
             {
                 action(conn);
                 conn.Commit();
             }
             catch (Exception)
             {
                 conn.Rollback();
                 throw;
             }
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 6
0
        public void UpdateToLatestVersion()
        {
            var context = Mvx.Resolve <UpdateContext>();

            context.Connection = _connection;
            var version = GetCurrentVersion();
            var updates = GetPendingUpdates(version);

            foreach (var update in updates)
            {
                var meta = update.Item2;
                var code = update.Item1;
                Trace($"Applying updated #{meta.Version} [{meta.Tag}]...");
                using (_connection.Lock())
                {
                    _connection.RunInTransaction(() =>
                    {
                        code.Apply(context);
                        var row = new VersionRow {
                            Tag = meta.Tag, Timestamp = new DateTime(), Version = meta.Version
                        };
                        _connection.Insert(row);
                    });
                }
                Trace($"Update #{meta.Version} [{meta.Tag}] was successfully applied.");
            }
        }
        public static Item GetItemSync(long id)
        {
            SQLiteConnectionWithLock con = _connection.GetConnection();

            using (con.Lock())
            {
                return(con.Query <Item>("SELECT * FROM items WHERE _id=?", id).FirstOrDefault <Item>());
            }
        }
 public Task <TableMapping> GetMappingAsync <T> ()
 {
     return(Task.Factory.StartNew(() => {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock()) {
             return conn.GetMapping(typeof(T));
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
 public Task <int> InsertOrIgnoreAsync(object item)
 {
     return(Task.Factory.StartNew(() => {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock()) {
             return conn.InsertOrIgnore(item);
         }
     }));
 }
Ejemplo n.º 10
0
        public void RemoveRecipe(int id)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(RecipePersistenceService));
            }
            using (_connection.Lock())
            {
                _connection.RunInTransaction(() =>
                {
                    //Remove all fields
                    var mapping = _connection.GetMapping <RecipeTextFieldRow>();
                    _connection.Execute($"DELETE FROM {mapping.TableName} WHERE RecipeId = ?", id);

                    //Remove recipe
                    _connection.Delete <RecipeRow>(id);
                });
            }
        }
Ejemplo n.º 11
0
 public Task <int> DeleteAllAsync <T>()
 {
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.DeleteAll <T>();
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 12
0
 public Task <int> DeleteAllAsync <T>()
 {
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.DeleteAll <T>();
         }
     }));
 }
 public Task <int> DropTableAsync <T>(CancellationToken cancellationToken = default(CancellationToken))
     where T : new()
 {
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.DropTable <T>();
         }
     }, cancellationToken, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 14
0
 public Task <int> DropTableAsync <T>()
     where T : new()
 {
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.DropTable <T>();
         }
     }));
 }
        public Task <int> InsertOrIgnoreAllAsync(IEnumerable objects, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (objects == null)
            {
                throw new ArgumentNullException("objects");
            }

            return(Task.Factory.StartNew(() => {
                SQLiteConnectionWithLock conn = GetConnection();
                using (conn.Lock()) {
                    return conn.InsertOrIgnoreAll(objects);
                }
            }, cancellationToken, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
        }
Ejemplo n.º 16
0
 public Task <int> UpdateAsync(object item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Update(item);
         }
     }));
 }
Ejemplo n.º 17
0
 public Task <int> DeleteAsync <T>(object pk)
 {
     if (pk == null)
     {
         throw new ArgumentNullException("pk");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Delete <T>(pk);
         }
     }));
 }
Ejemplo n.º 18
0
 public Task <int> InsertAllAsync(IEnumerable items)
 {
     if (items == null)
     {
         throw new ArgumentNullException("items");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.InsertAll(items);
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 19
0
 public Task <int> DeleteAsync <T>(object pk)
 {
     if (pk == null)
     {
         throw new ArgumentNullException("pk");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Delete <T>(pk);
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 20
0
 public Task <int> InsertAllAsync(IEnumerable items)
 {
     if (items == null)
     {
         throw new ArgumentNullException("items");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.InsertAll(items);
         }
     }));
 }
Ejemplo n.º 21
0
 public Task <int> UpdateAsync(object item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Update(item);
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
 public Task <int> InsertOrReplaceAsync(object item, CancellationToken cancellationToken = default(CancellationToken))
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.InsertOrReplace(item);
         }
     }, cancellationToken, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 23
0
 public void InsertOrReplaceWithChildren(T entity)
 {
     //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.InsertOrReplaceWithChildren(entity, true);
             }
             catch (Exception ex)
             {
                 Debug.WriteLine($"SQLiteError: {ex.Message}");
             }
         }
 }
Ejemplo n.º 24
0
 public Task <T> FindAsync <T>(Expression <Func <T, bool> > predicate)
     where T : new()
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Find(predicate);
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Ejemplo n.º 25
0
 public Task <T> FindAsync <T>(Expression <Func <T, bool> > predicate)
     where T : new()
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Find(predicate);
         }
     }));
 }
Ejemplo n.º 26
0
        public MbTilesTileSource(SQLiteConnectionString connectionString, ITileSchema schema = null, MbTilesType type = MbTilesType.None)
        {
            _connectionString = connectionString;
            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);
            }
        }
Ejemplo n.º 27
0
        internal MbTilesCache(SQLiteConnectionString connectionString, ITileSchema schema = null, MbTilesType type = MbTilesType.None)
        {
            /*
             * if (_connectionPool == null)
             *  throw new InvalidOperationException("You must assign a platform prior to using MbTilesCache by calling MbTilesTileSource.SetPlatform()");
             */
            _connectionString = connectionString;
            var connection = new SQLiteConnectionWithLock(_platform, connectionString);

            using (connection.Lock())
            {
                _type = type == MbTilesType.None ? ReadType(connection) : type;

                if (schema == null)
                {
                    // Format (if defined)
                    _format = ReadFormat(connection);

                    // Extent
                    _extent = ReadExtent(connection);


                    if (HasMapTable(connection))
                    {
                        // it is possible to override the schema by definining it in a 'map' table.
                        // This method depends on reading tiles from an 'images' table, which
                        // is not part of the MBTiles spec

                        // Declared zoom levels
                        var declaredZoomLevels = ReadZoomLevels(connection, out _tileRange);

                        // Create schema
                        _schema = new GlobalMercator(_format.ToString(), declaredZoomLevels);
                    }
                    else
                    {
                        // this is actually the most regular case:
                        _schema = new GlobalSphericalMercator();
                    }
                }
                else
                {
                    _schema = schema;
                }
            }
        }
Ejemplo n.º 28
0
 public byte[] Find(TileIndex index)
 {
     if (IsTileIndexValid(index))
     {
         byte[] result;
         var    cn = new SQLiteConnectionWithLock(_platform, _connectionString);
         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> FindAsync <T>(object pk, CancellationToken cancellationToken = default(CancellationToken))
     where T : new()
 {
     if (pk == null)
     {
         throw new ArgumentNullException("pk");
     }
     return(Task.Factory.StartNew(() =>
     {
         cancellationToken.ThrowIfCancellationRequested();
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             cancellationToken.ThrowIfCancellationRequested();
             return conn.Find <T>(pk);
         }
     }, cancellationToken, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
 public Task <T> GetAsync <T>(Expression <Func <T, bool> > predicate, CancellationToken cancellationToken = default(CancellationToken))
     where T : new()
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(Task.Factory.StartNew(() =>
     {
         cancellationToken.ThrowIfCancellationRequested();
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             cancellationToken.ThrowIfCancellationRequested();
             return conn.Get(predicate);
         }
     }, cancellationToken, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }