Exemplo n.º 1
0
        public void SchemaDrop(string schemaName, bool force, bool throwIfNotExists)
        {
            if (String.IsNullOrWhiteSpace(schemaName))
            {
                throw new ArgumentNullException(nameof(schemaName));
            }

            try
            {
                var sqlCommand = new PgSqlCommand();
                sqlCommand.AppendCommand(SQLStatements.SchemaDrop(schemaName, force, throwIfNotExists));
                var resp = new DbExecuter(ConnectionString).ExecuteNonQuery(sqlCommand);
            }
            catch (PostgresException pex)
            {
                if (pex.SqlState == "2BP01")
                {
                    throw new DependentObjectsException(pex.Detail);
                }

                if (pex.SqlState == "3F000")
                {
                    throw new SchemaDoesntExistsException(schemaName);
                }

                throw;
            }
        }
Exemplo n.º 2
0
        public Schema SchemaRename(string schemaName, string newSchemaName)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(schemaName))
                {
                    throw new ArgumentNullException(nameof(schemaName));
                }

                if (String.IsNullOrWhiteSpace(newSchemaName))
                {
                    throw new ArgumentNullException(nameof(newSchemaName));
                }

                var sqlCommand = new PgSqlCommand();
                sqlCommand.AppendCommand(SQLStatements.SchemaRename(schemaName, newSchemaName));
                var resp = new DbExecuter(ConnectionString).ExecuteNonQuery(sqlCommand);

                return(GetSchema(newSchemaName));
            }
            catch (PostgresException pex)
            {
                if (pex.SqlState == "3F000")
                {
                    throw new SchemaDoesntExistsException(schemaName);
                }

                if (pex.SqlState == "42P06")
                {
                    throw new SchemaAlreadyExistsException(newSchemaName);
                }

                throw;
            }
        }
Exemplo n.º 3
0
        public Schema CreateSchema(string schemaName, bool throwIfAlreadyExists)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(schemaName))
                {
                    throw new ArgumentNullException(nameof(schemaName));
                }

                //schemaName = schemaName.ToLower();

                var sqlCommand = new PgSqlCommand();
                sqlCommand.AppendCommand(SQLStatements.SchemaCreate(schemaName, throwIfAlreadyExists));
                var resp = new DbExecuter(ConnectionString).ExecuteNonQuery(sqlCommand);

                return(GetSchema(schemaName));
            }
            catch (PostgresException pex)
            {
                if (pex.SqlState == "42P06")
                {
                    throw new SchemaAlreadyExistsException(schemaName);
                }

                throw;
            }
        }
Exemplo n.º 4
0
        public bool DatabaseExists(string database, bool throwIfNotExists, bool throwIfExists)
        {
            if (String.IsNullOrWhiteSpace(database))
            {
                throw new ArgumentNullException(nameof(database));
            }

            var exists = new DbExecuter(_configuration).ExecuteScalar <bool>(SQLStatements.DatabaseExists(database));

            if (!exists && throwIfNotExists)
            {
                throw new DatabaseDoesntExistsException(database);
            }

            if (exists && throwIfExists)
            {
                throw new DatabaseAlreadyExistsException(database);
            }

            return(exists);
        }
Exemplo n.º 5
0
        public bool SchemaExists(string schemaName, bool throwIfNotExists, bool throwIfAlreadyExists)
        {
            if (String.IsNullOrWhiteSpace(schemaName))
            {
                throw new ArgumentNullException(nameof(schemaName));
            }


            var exists = new DbExecuter(ConnectionString).ExecuteScalar <bool>(SQLStatements.SchemaExists(schemaName));

            if (!exists && throwIfNotExists)
            {
                throw new SchemaDoesntExistsException(schemaName);
            }

            if (exists && throwIfAlreadyExists)
            {
                throw new SchemaAlreadyExistsException(schemaName);
            }

            return(exists);
        }
Exemplo n.º 6
0
        internal static TableDefinition<T> FromTable<T>(ITable table)
        {
            var td = TableDefinition.FromType<T>();
            try
            {
                var columns = new DbExecuter(table.GetConnectionString()).ExecuteReader(SQLStatements.GetColumns(table.GetConnectionString().TableName, table.GetConnectionString().SchemaName)).ToArray();
                foreach (var column in columns)
                {

                    Column dbCol = column.ToObject<Column>();

                    var col = td.GetColumnByDbName(dbCol.DbName) ?? td.GetColumnByClrName(dbCol.DbName);

                    if (col == null)
                    {
                        dbCol.DotNetType = PgSqlTypeManager.GetDotNetType(dbCol.PgType);
                        td.AddColumn(dbCol);

                    }
                    else
                    {
                        col.DbName = dbCol.DbName;
                        col.CanBeNull = dbCol.CanBeNull;
                        col.DefaultValue = dbCol.DefaultValue;
                        col.Position = dbCol.Position;
                        col.IsPrimaryKey = dbCol.IsPrimaryKey;
                        col.MustBeUnique = dbCol.MustBeUnique;
                    }

                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "GetTableSchema");
                throw;
            }

            return td;
        }