public void DropDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["Database"];
                sb["Database"]           = "postgres";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '{dbName}' AND pid <> pg_backend_pid(); ";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = $"drop database if exists {dbName}";
                        cmd.ExecuteNonQuery();
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
        public bool ExistsDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["Database"];
                sb["Database"]           = "postgres";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"SELECT count(datname) from pg_database WHERE datname='{dbName}'";
                        return(Convert.ToInt32(cmd.ExecuteScalar()) > 0);
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
        public void CreateDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["Database"];
                sb["Database"]           = "postgres";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"CREATE DATABASE {dbName} ENCODING = 'UTF8';";
                        cmd.ExecuteNonQuery();
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
예제 #4
0
        public bool ExistsDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["Database"];
                sb["Database"]           = "mysql";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"SHOW DATABASES LIKE '{dbName}';";
                        using (IDataReader reader = cmd.ExecuteReader())
                        {
                            return(reader.Read());
                        }
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
예제 #5
0
        public bool ExistsDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["InitialCatalog"];
                sb["InitialCatalog"]     = "master";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"select isnull(DB_ID(N'{dbName}'), -1)";
                        return(Convert.ToInt32(cmd.ExecuteScalar()) > -1);
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
예제 #6
0
        public void DropDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["InitialCatalog"];
                sb["InitialCatalog"]     = "master";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"IF DB_ID (N'{dbName}') IS NOT NULL DROP DATABASE {dbName}";
                        cmd.ExecuteNonQuery();
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }
예제 #7
0
        public void CreateDatabase(IAnyDbSetting setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            CheckDatabseProvider(setting);
            string original = setting.ConnectionString;

            try
            {
                AnyDbConnectionStringBuilder sb = new AnyDbConnectionStringBuilder(setting.ConnectionString);
                string dbName = (string)sb["InitialCatalog"];
                sb["InitialCatalog"]     = "master";
                setting.ConnectionString = sb.ToString();
                AnyDbFactory factory = new AnyDbFactory(setting);
                using (AnyDbConnection con = factory.OpenConnection())
                    using (AnyDbCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = $"CREATE DATABASE {dbName}";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = $"ALTER DATABASE {dbName} SET RECOVERY SIMPLE";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = $"alter database {dbName} set allow_snapshot_isolation on;";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = $"alter database {dbName} set read_committed_snapshot on;";
                        cmd.ExecuteNonQuery();
                    }
            }
            finally
            {
                setting.ConnectionString = original;
            }
        }