private static SqliteConnection GetConnection(string source, SqliteOpenMode mode = SqliteOpenMode.ReadWrite) { return(new SqliteConnection(new SqliteConnectionStringBuilder { DataSource = source, Mode = mode }.ToString())); }
public static SqliteConnection NewDbConnection(DbConnectionMode mode = DbConnectionMode.ReadOnly) { string dataSource = ConfigurationManager.AppSettings["ADO_Datasource"]; string baseConnectionString = $"Data Source={dataSource}"; { // Sqllite SqliteOpenMode sqliteOpenMode = SqliteOpenMode.ReadOnly; switch (mode) { case DbConnectionMode.ReadOnly: sqliteOpenMode = SqliteOpenMode.ReadOnly; break; case DbConnectionMode.ReadWrite: sqliteOpenMode = SqliteOpenMode.ReadWrite; break; case DbConnectionMode.ReadWriteCreate: sqliteOpenMode = SqliteOpenMode.ReadWriteCreate; break; default: break; } var connectionString = new SqliteConnectionStringBuilder(baseConnectionString) { Mode = sqliteOpenMode }.ToString(); var connection = new SqliteConnection(connectionString); return(connection); } }
private SqliteFileSystem(SqliteConnectionStringBuilder builder) { this.ConnectionString = builder.ToString(); this.mode = builder.Mode; this.Connection = new SqliteConnection(this.ConnectionString); this.IsReadOnly = this.mode == SqliteOpenMode.ReadOnly; this.Connection.Open(); // ensure schema exists in this file var schemaValid = Adapter.ExecuteScalarBool(this.Connection, Adapter.VerifySchema); if (!schemaValid) { try { Adapter.ExecuteNonQuery(this.Connection, Adapter.CreateSchema); } catch (SqliteException se) { throw new SqliteFileSystemException( "Schema not found in provided Sqlite filesystem and could not be created", se); } } // check schema version matches this.SchemaVersion = Adapter.ExecuteScalarString(this.Connection, Adapter.GetSchemaVersion); if (this.SchemaVersion != Adapter.SchemaVersion) { throw new SqliteFileSystemException($"Schema version {this.SchemaVersion} does not match library required version {Adapter.SchemaVersion}"); } }
public SqliteConnection Make(SqliteOpenMode mode, bool isolated = false) { var connectionString = new SqliteConnectionStringBuilder { Mode = mode, DataSource = _databaseFile, RecursiveTriggers = true, Cache = !isolated ? SqliteCacheMode.Shared : SqliteCacheMode.Private }.ToString(); var connection = new SqliteConnection(connectionString); using var initializeCommand = connection.CreateCommand(); initializeCommand.CommandText = @" PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA cache_size=4000; PRAGMA case_sensitive_like=true; "; try { connection.Open(); initializeCommand.ExecuteNonQuery(); connection.Close(); return(connection); } catch (SqliteException sqliteException) { connection.Dispose(); throw new AggregateException($"Can't create sqlite connection for database file '{_databaseFile}'", sqliteException); } }
/// <summary> /// Initializes a new instance of the <see cref="SqliteConnectionFactory"/> class. /// </summary> /// <param name="dataSource">The database file.</param> /// <param name="sqliteOpenMode">The connection mode.</param> public SqliteConnectionFactory(string dataSource, SqliteOpenMode sqliteOpenMode = SqliteOpenMode.ReadWriteCreate) { var builder = new SqliteConnectionStringBuilder() { DataSource = dataSource, Mode = sqliteOpenMode, }; var connectionString = builder.ToString(); this.connectionFactory = () => new SqliteConnection(connectionString); }
private void Reset(Keywords index) { switch (index) { case Keywords.DataSource: _dataSource = string.Empty; return; case Keywords.Mode: _mode = SqliteOpenMode.ReadWriteCreate; return; case Keywords.Cache: _cache = SqliteCacheMode.Default; return; default: Debug.Fail("Unexpected keyword: " + index); return; } }
public SqliteConnection Make(SqliteOpenMode mode, bool isolated) { var connection = new SqliteConnection(ConnectionString); using var initializeCommand = connection.CreateCommand(); initializeCommand.CommandText = @" PRAGMA case_sensitive_like=true; "; try { connection.Open(); initializeCommand.ExecuteNonQuery(); connection.Close(); return(connection); } catch (SqliteException sqliteException) { connection.Dispose(); throw new AggregateException("Can't create sqlite connection in memory.", sqliteException); } }
public SQLite(string name, SqliteOpenMode mode = SqliteOpenMode.ReadWriteCreate) { _name = name; _mode = mode; }
private static SqliteConnectionStringBuilder BuildConnection(string databasePath, SqliteOpenMode mode) { if (!Path.IsPathRooted(databasePath)) { throw new ArgumentException("Path for file must be rooted", nameof(databasePath)); } var builder = new SqliteConnectionStringBuilder { Cache = SqliteCacheMode.Shared, DataSource = databasePath, Mode = mode, }; return(builder); }
protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess access, FileShare share) { // original implmentation lifted from: https://raw.githubusercontent.com/xoofx/zio/06e59868adaacd3fc9d174c992009a6a2520f659/src/Zio/FileSystems/MemoryFileSystem.cs // all notion of locking has been stripped but should probably be readded if (mode == FileMode.Append && (access & FileAccess.Read) != 0) { throw new ArgumentException( "Combining FileMode: Append with FileAccess: Read is invalid.", nameof(access)); } if (mode != FileMode.Open) { this.SetCheck(false); } // no support for sharing or locking files var isReading = (access & FileAccess.Read) != 0; var isWriting = (access & FileAccess.Write) != 0; var isExclusive = share == FileShare.None; try { // Append: Opens the file if it exists and seeks to the end of the file, or creates a new file. // This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in // conjunction with FileAccess.Write. Trying to seek to a position before the end of the file // throws an IOException exception, and any attempt to read fails and throws a // NotSupportedException exception. // // // CreateNew: Specifies that the operating system should create a new file.This requires // FileIOPermissionAccess.Write permission. If the file already exists, an IOException // exception is thrown. // // Open: Specifies that the operating system should open an existing file. The ability to open // the file is dependent on the value specified by the FileAccess enumeration. // A System.IO.FileNotFoundException exception is thrown if the file does not exist. // // OpenOrCreate: Specifies that the operating system should open a file if it exists; // otherwise, a new file should be created. If the file is opened with // FileAccess.Read, FileIOPermissionAccess.Read permission is required. // If the file access is FileAccess.Write, FileIOPermissionAccess.Write permission // is required. If the file is opened with FileAccess.ReadWrite, both // FileIOPermissionAccess.Read and FileIOPermissionAccess.Write permissions // are required. // // Truncate: Specifies that the operating system should open an existing file. // When the file is opened, it should be truncated so that its size is zero bytes. // This requires FileIOPermissionAccess.Write permission. Attempts to read from a file // opened with FileMode.Truncate cause an ArgumentException exception. // Create: Specifies that the operating system should create a new file.If the file already exists, // it will be overwritten.This requires FileIOPermissionAccess.Write permission. // FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; // otherwise, use Truncate. If the file already exists but is a hidden file, // an UnauthorizedAccessException exception is thrown. // does the file exist? var node = this.SafeExists(path); if (node == Adapter.Node.Directory) { throw NewDirectoryNotFileException(path); } bool exists = node == Adapter.Node.File; bool shouldTruncate = false; bool shouldAppend = false; if (mode == FileMode.Create) { if (exists) { mode = FileMode.Open; shouldTruncate = true; } else { mode = FileMode.CreateNew; } } if (mode == FileMode.OpenOrCreate) { mode = exists ? FileMode.Open : FileMode.CreateNew; } if (mode == FileMode.Append) { if (exists) { mode = FileMode.Open; shouldAppend = true; } else { mode = FileMode.CreateNew; } } if (mode == FileMode.Truncate) { if (exists) { mode = FileMode.Open; shouldTruncate = true; } else { throw NewFileNotFoundException(path); } } // Here we should only have Open or CreateNew Debug.Assert(mode == FileMode.Open || mode == FileMode.CreateNew); if (mode == FileMode.CreateNew) { // This is not completely accurate to throw an exception (as we have been called with an option to OpenOrCreate) // But we assume that between the beginning of the method and here, the filesystem is not changing, and // if it is, it is an unfortunate conrurrency if (exists) { throw NewDestinationFileExistException(path); } Adapter.CreateFile(this.Connection, path); } else { if (!exists) { throw NewFileNotFoundException(path); } } // TODO: Add checks between mode and access // todo: optimize for sending streams // Create a memory file stream var stream = new DatabaseBackedMemoryStream(this.Connection, path, isReading, isWriting); if (shouldAppend) { stream.Position = stream.Length; } else if (shouldTruncate) { stream.SetLength(0); } return(stream); } finally { } }
/// <summary> /// Apply the SQLite connection information to the migration. /// </summary> /// <param name="migrationBuilder">The <see cref="MigrationBuilder"/> instance.</param> /// <param name="dataSource">The database file.</param> /// <param name="sqliteOpenMode">The connection mode.</param> /// <param name="auditTable">The name of the audit table used for tracking applied migrations.</param> /// <returns>The <see cref="MigrationBuilder"/> instance with the applied connection.</returns> /// <exception cref="ArgumentNullException">Thrown when the <see cref="MigrationBuilder"/> parameter is null.</exception> public static MigrationBuilder UseSqlite(this MigrationBuilder migrationBuilder, string dataSource, SqliteOpenMode sqliteOpenMode = SqliteOpenMode.ReadWriteCreate, string auditTable = "changesets") { if (migrationBuilder == null) { throw new ArgumentNullException(nameof(migrationBuilder)); } var connectionFactory = new SqliteConnectionFactory(dataSource, sqliteOpenMode); var auditor = new SqliteAuditor(auditTable); migrationBuilder.SetConnectionFactory(connectionFactory); migrationBuilder.SetAuditor(auditor); return(migrationBuilder); }