/// <summary> /// Opens or creates a database file. /// </summary> /// <param name="file"> /// Database file. Can be: /// - Full path. Supports environment variables etc, see <see cref="APath.ExpandEnvVar"/> /// - ":memory:" - create a private, temporary in-memory database. /// - "" - create a private, temporary on-disk database. /// - Starts with "file:" - see sqlite3_open_v2 documentation in SQLite website. /// </param> /// <param name="flags">sqlite3_open_v2 flags, documanted in SQLite website. Default: read-write, create file if does not exist (and parent directory).</param> /// <param name="sql"> /// SQL to execute. For example, one or more ;-separated PRAGMA statements to configure the database connection. Or even "CREATE TABLE IF NOT EXISTS ...". /// This function also always executes "PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;". /// </param> /// <exception cref="ArgumentException">Not full path.</exception> /// <exception cref="SLException">Failed to open database or execute sql.</exception> /// <remarks> /// Calls sqlite3_open_v2. /// <note>If a variable of this class is used by multiple threads, use <c>lock(variable) { }</c> where need.</note> /// </remarks> public ASqlite(string file, SLFlags flags = SLFlags.ReadWriteCreate, string sql = null) { bool isSpec = file != null && (file.Length == 0 || file == ":memory:" || file.Starts("file:")); if (!isSpec) { file = APath.Normalize(file); if (flags.Has(SLFlags.SQLITE_OPEN_CREATE) && !AFile.ExistsAsFile(file, true)) { AFile.CreateDirectoryFor(file); } } var r = SLApi.sqlite3_open_v2(AConvert.ToUtf8(file), ref _db, flags, null); if (r != 0) { Dispose(); throw new SLException(r, "sqlite3_open " + file); } Execute("PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;" + sql); }
/// <summary> /// Opens or creates a database file. /// </summary> /// <param name="file"> /// Database file. Can be: /// - Full path. Supports environment variables etc, see <see cref="pathname.expand"/> /// - ":memory:" - create a private, temporary in-memory database. /// - "" - create a private, temporary on-disk database. /// - Starts with "file:" - see <google>sqlite3_open_v2</google>. /// </param> /// <param name="flags"><google>sqlite3_open_v2</google> flags. Default: read-write, create file if does not exist (and parent directory).</param> /// <param name="sql"> /// SQL to execute. For example, one or more ;-separated PRAGMA statements to configure the database connection. Or even "CREATE TABLE IF NOT EXISTS ...". /// This function also always executes "PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;". /// </param> /// <exception cref="ArgumentException">Not full path.</exception> /// <exception cref="SLException">Failed to open database or execute sql.</exception> /// <remarks> /// Calls <google>sqlite3_open_v2</google>. /// <note>If a variable of this class is used by multiple threads, use <c>lock(variable) { }</c> where need.</note> /// </remarks> public sqlite(string file, SLFlags flags = SLFlags.ReadWriteCreate, string sql = null) { bool isSpec = file != null && (file.Length == 0 || file == ":memory:" || file.Starts("file:")); if (!isSpec) { file = pathname.normalize(file); if (flags.Has(SLFlags.SQLITE_OPEN_CREATE) && !filesystem.exists(file, true).File) { filesystem.createDirectoryFor(file); } } var r = SLApi.sqlite3_open_v2(Convert2.Utf8Encode(file), ref _db, flags, null); if (r != 0) { Dispose(); throw new SLException(r, "sqlite3_open " + file); } Execute("PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;" + sql); }