Exemple #1
0
        public static void CreateDatabase(string server, string databaseName, string username = null, string password = null, bool?useIntegratedSecurity = null, AlreadyExistsPolicy existsPolicy = AlreadyExistsPolicy.Error)
        {
            if (string.IsNullOrWhiteSpace(server))
            {
                throw new ArgumentNullException("server");
            }

            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentNullException("databaseName");
            }


            var connString = CreateConnectionString(server, "master", username, password, useIntegratedSecurity);
            var dac        = new MSSQLDAC(connString);

            using (dac.BeginScope()) {
                var isAzure = dac.IsAzure();

                var alreadyExists = dac.ExecuteScalar <bool>(CheckDatabaseExistsQuery.FormatWith(databaseName));

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

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

                    case AlreadyExistsPolicy.Error:
                    default:
                        throw new SoftwareException("Database '{0}' already exists on server '{1}'", databaseName, server);
                        break;
                    }
                }
                else
                {
                    shouldCreate = true;
                }

                var sqlBuilder = dac.CreateSQLBuilder();
                if (shouldDrop)
                {
                    if (!isAzure)
                    {
                        sqlBuilder.Emit("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", databaseName).EndOfStatement(SQLStatementType.DDL);
                    }
                    sqlBuilder.Emit("DROP DATABASE [{0}]", databaseName).EndOfStatement(SQLStatementType.DDL);
                }
                if (shouldCreate)
                {
                    sqlBuilder
                    .Emit("CREATE DATABASE [{0}]", databaseName).EndOfStatement(SQLStatementType.DDL);

                    if (!isAzure)
                    {
                        sqlBuilder.Emit("ALTER DATABASE [{0}] SET RECOVERY SIMPLE", databaseName).EndOfStatement(SQLStatementType.DDL);
                    }
                }
                if (shouldDrop || shouldCreate)
                {
                    sqlBuilder.Statements.ForEach(s => dac.ExecuteNonQuery(s.SQL));
                }
            }
        }
Exemple #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));
        }