Пример #1
0
        public static SynchronizationModes Convert(SqliteSyncMode syncMode)
        {
            switch (syncMode)
            {
            case SqliteSyncMode.Full:
                return(SynchronizationModes.Full);

            case SqliteSyncMode.Off:
                return(SynchronizationModes.Off);

            case SqliteSyncMode.Normal:
            default:
                return(SynchronizationModes.Normal);
            }
        }
Пример #2
0
        public static SqliteDAC Create(string path, string password = null, int?pageSize = null, SqliteJournalMode journalMode = SqliteJournalMode.Default, SqliteSyncMode syncMode = SqliteSyncMode.Normal, AlreadyExistsPolicy existsPolicy = AlreadyExistsPolicy.Error, ILogger logger = null)
        {
            var shouldDrop   = false;
            var shouldCreate = true;

            if (ExistsByFilePath(path))
            {
                switch (existsPolicy)
                {
                case AlreadyExistsPolicy.Skip:
                    shouldDrop   = false;
                    shouldCreate = false;
                    break;

                case AlreadyExistsPolicy.Overwrite:
                    shouldDrop   = true;
                    shouldCreate = true;
                    break;

                case AlreadyExistsPolicy.Error:
                    throw new SoftwareException("Unable to create Sqlite database '{0}' as a file by that path already exists", path);
                }
            }
            if (shouldDrop)
            {
                File.Delete(path);
            }

            if (shouldCreate)
            {
                var connString = CreateConnectionString(path, password, pageSize, false, journalMode, syncMode);

                // set password, page config since ado.net doesn't do it
                var dac = new SqliteDAC(connString);
                using (var scope = dac.BeginScope(false)) {
                    // Set password explicitly since ADO.NET doesnt
                    var builder = dac.CreateSQLBuilder();

                    if (pageSize.HasValue)
                    {
                        builder.Emit("PRAGMA PAGE_SIZE={0}", pageSize.Value).EndOfStatement(SQLStatementType.DDL);
                    }

                    // VACUUM is necessary on create to ensure file is created (ADO.NET leaves empty stub otherwise)
                    builder.Emit("VACUUM").EndOfStatement(SQLStatementType.DDL);
                    dac.ExecuteNonQuery(builder.ToString());
                }
            }
            return(Open(path, password, journalMode, syncMode, logger));
        }