コード例 #1
0
ファイル: MbTilesTileSource.cs プロジェクト: yjsh824/BruTile
        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);
        }
コード例 #2
0
 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));
 }
コード例 #3
0
        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));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
 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);
         }
     }));
 }
コード例 #6
0
 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>();
         }
     }));
 }
コード例 #7
0
        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);
                }
            }));
        }
コード例 #8
0
 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));
 }
コード例 #9
0
        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);
        }
コード例 #10
0
 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));
 }
コード例 #11
0
        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;
        }
コード例 #12
0
 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;
     }));
 }
コード例 #13
0
 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}");
                 }
             }
         }
     }
 }
コード例 #14
0
 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));
     }
 }
コード例 #15
0
        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;
            }
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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;
        }
コード例 #18
0
        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;
            }
        }
コード例 #19
0
            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;
                });
            }
コード例 #20
0
 public StorageResetter(SQLiteConnectionWithLock connection)
 {
     this.connection = connection;
 }
コード例 #21
0
 public TransactionDisposer(SQLiteConnectionWithLock connection, IDisposable @lock)
 {
     this.connection = connection;
     this.@lock      = @lock;
 }
コード例 #22
0
        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));
        }
コード例 #23
0
        public SqLiteConnectionFactory(IAppConfig appConfig)
        {
            _appDBConnectionWithLock = new SQLiteConnectionWithLock(appConfig.SqLitePlatform, new SQLiteConnectionString(appConfig.AppDatabaseConnectionString, true));

            _logsDBConnectionWithLock = new SQLiteConnectionWithLock(appConfig.SqLitePlatform, new SQLiteConnectionString(appConfig.LogDatabaseConnectionString, true));
        }
コード例 #24
0
 public void Dispose()
 {
     connection?.Dispose();
     connection = null;
 }
コード例 #25
0
        static public IDisposable Lock(this SQLiteConnectionWithLock connection)
        {
            var lockMethod = connection.GetType().GetTypeInfo().GetDeclaredMethod("Lock");

            return((IDisposable)lockMethod.Invoke(connection, null));
        }
コード例 #26
0
 public void OnApplicationSuspended()
 {
     Connection.Dispose();
     Connection = null;
 }
コード例 #27
0
 public Entry(SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags)
 {
     ConnectionString = connectionString;
     Connection       = new SQLiteConnectionWithLock(connectionString, openFlags);
 }
コード例 #28
0
        public SQLiteAsyncConnection GetAsyncConnection()
        {
            var connectionFactory = new Func <SQLiteConnectionWithLock>(() => _conn ?? (_conn = new SQLiteConnectionWithLock(new SQLitePlatformWinRT(),
                                                                                                                             new SQLiteConnectionString(GetDatabasePath(), true))));

            return(new SQLiteAsyncConnection(connectionFactory));
        }
コード例 #29
0
 public AppInternalDatabase(SQLiteConnectionWithLock connection)
 {
     Connection = connection;
 }
コード例 #30
0
ファイル: DataManager.cs プロジェクト: ericmackrodt/Costscape
        public DataManager()
        {
            var connectionWithLock = new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString("database.sqlite", true));

            _connection = new SQLiteAsyncConnection(() => connectionWithLock);
        }