コード例 #1
0
        public static void Attach(ManagedConnection db, string path, string alias)
        {
            var commandText = $"attach @path as {alias};";

            using (var statement = db.PrepareStatement(commandText))
            {
                statement.TryBind("@path", path);
                statement.MoveNext();
            }
        }
コード例 #2
0
        protected ManagedConnection CreateConnection(bool isReadOnly = false)
        {
            if (_connection != null)
            {
                return(_connection);
            }

            lock (WriteLock)
            {
                if (!_versionLogged)
                {
                    _versionLogged = true;
                    _logger.LogInformation("SQLite version: {Version}", SQLite3.Version);
                    _logger.LogInformation("SQLite compiler options: {Options}", string.Join(',', SQLite3.CompilerOptions));
                }

                ConnectionFlags connectionFlags;

                if (isReadOnly)
                {
                    // TODO: set connection flags correctly
                    // connectionFlags = ConnectionFlags.ReadOnly
                    _logger.LogDebug("Opening read connection to database");
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }
                else
                {
                    _logger.LogDebug("Opening write connection to database.");
                    connectionFlags  = ConnectionFlags.Create;
                    connectionFlags |= ConnectionFlags.ReadWrite;
                }

                if (EnableSingleConnection)
                {
                    connectionFlags |= ConnectionFlags.PrivateCache;
                }
                else
                {
                    connectionFlags |= ConnectionFlags.SharedCached;
                }

                connectionFlags |= ConnectionFlags.NoMutex;

                var db = SQLite3.Open(DbFilePath, connectionFlags, null);

                try
                {
                    if (string.IsNullOrWhiteSpace(_defaultWal))
                    {
                        _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First();

                        _logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
                    }

                    var queries = new List <string>
                    {
                        // "PRAGMA cache size=-10000"
                        // "PRAGMA read_uncommitted = true",
                        "PRAGMA synchronous=Normal"
                    };

                    if (CacheSize.HasValue)
                    {
                        queries.Add("PRAGMA cache_size=" + CacheSize.Value.ToString(CultureInfo.InvariantCulture));
                    }

                    if (EnableTempStoreMemory)
                    {
                        queries.Add("PRAGMA temp_store = memory");
                    }
                    else
                    {
                        queries.Add("PRAGMA temp_store = file");
                    }

                    db.ExecuteAll(string.Join(';', queries));
                }
                catch
                {
                    db.Dispose();
                    throw;
                }

                _dbConnection = db;
                _connection   = new ManagedConnection(db);
                return(_connection);
            }
        }
コード例 #3
0
 protected bool TableExists(ManagedConnection connection, string name)
 {
     return(connection.RunInTransaction(db => TableExists(db, name), ReadTransactionMode));
 }
コード例 #4
0
 public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
 {
     return(connection.PrepareStatement(sql));
 }