func (pgDbc PostgresDbConnection) CreateTable(table baseDbConnection.TableDefinition, dbConn *baseDbConnection.DbConnection) error { existingTable, err := dbConn.GetTableDefinition(table.Name) if err != nil { return err } if existingTable.Name != table.Name { sqlCreateTable := "CREATE TABLE IF NOT EXISTS " + dbConn.Schema + "." + table.Name + " (\n" fields := make([]string, 0) primaryKey := make([]string, 0) for _, tableColumn := range table.Columns { fieldDef := "\"" + tableColumn.Name + "\" " + tableColumn.Type if tableColumn.Default != "" { fieldDef += " DEFAULT " + tableColumn.Default } fields = append(fields, fieldDef) if tableColumn.PrimaryKey { primaryKey = append(primaryKey, tableColumn.Name) } } fields = append(fields, "CONSTRAINT pk_"+existingTable.Name+" PRIMARY KEY ("+strings.Join(primaryKey, ",")+")") sqlCreateTable = sqlCreateTable + strings.Join(fields, "\n,") sqlCreateTable = sqlCreateTable + ") WITH (OIDS = FALSE);\n" //TODO: Default Table Grants //TODO: Indexes //TODO: Partitioning _, err := dbConn.Exec(sqlCreateTable) return err } else { return errors.New("Table already exists.") } }
func (pgDbc PostgresDbConnection) CreateUser(userName string, password string, dbConn *baseDbConnection.DbConnection) error { _, err := dbConn.Exec("CREATE USER " + userName + " WITH PASSWORD '" + password + "'") if err == nil { _, err = dbConn.Exec("GRANT ALL PRIVILEGES ON DATABASE " + dbConn.DBName + " TO " + userName) } if err == nil { _, err = dbConn.Exec("GRANT ALL PRIVILEGES ON SCHEMA public TO " + userName) } return err }
func (pgDbc PostgresDbConnection) DropUser(userName string, dbConn *baseDbConnection.DbConnection) error { _, err := dbConn.Exec("REVOKE ALL PRIVILEGES ON SCHEMA public FROM " + userName) if err == nil { _, err = dbConn.Exec("REVOKE ALL PRIVILEGES ON DATABASE " + dbConn.DBName + " FROM " + userName) } if err == nil { //RISK ITEM: If the user owns any objects, this will fail. //HOW DO WE WANT TO HANDLE THIS? _, err = dbConn.Exec("DROP USER IF EXISTS " + userName) } return err }
func (pgDbc PostgresDbConnection) DropDatabase(databaseName string, dbConn *baseDbConnection.DbConnection) error { _, err := dbConn.Exec("DROP DATABASE " + databaseName) return err }
func (pgDbc PostgresDbConnection) CreateDatabase(databaseName string, dbConn *baseDbConnection.DbConnection) error { _, err := dbConn.Exec("CREATE DATABASE " + databaseName) return err }