Create comprehensive documentation for future Claude Code instances working in this repository, including: - Development commands for testing, building, and code quality - Core architecture overview of the SQL query builder system - Directory structure and component explanations - Testing patterns and conventions - Key dependencies and their purposes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
219 lines
6.1 KiB
Go
219 lines
6.1 KiB
Go
package postgres
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.fsdpf.net/go/db"
|
|
"git.fsdpf.net/go/db/dialect/postgres"
|
|
"git.fsdpf.net/go/db/internal/sb"
|
|
"git.fsdpf.net/go/db/schema"
|
|
"git.fsdpf.net/go/db/sqlgen"
|
|
)
|
|
|
|
type Postgres struct {
|
|
db *db.Database
|
|
esg sqlgen.ExpressionSQLGenerator
|
|
}
|
|
|
|
var pgDefaultModifiers = []string{
|
|
"Nullable", "Default", "Increment", "Comment",
|
|
}
|
|
var pgSerials = []string{
|
|
"bigInteger", "integer", "smallInteger",
|
|
}
|
|
|
|
func (this Postgres) GetColumnListing(table string) ([]string, error) {
|
|
cols := []string{}
|
|
err := this.db.From(db.S("information_schema").Table("columns")).Where(
|
|
db.C("table_name").Eq(table),
|
|
db.C("table_schema").Eq(db.L("current_database()")),
|
|
).Pluck(&cols, "column_name")
|
|
return cols, err
|
|
}
|
|
|
|
func (this Postgres) TableExists(table string) (bool, error) {
|
|
result, err := this.db.From(db.S("information_schema").Table("tables")).Where(db.Ex{
|
|
"table_name": table,
|
|
"table_schema": db.L("current_database()"),
|
|
}).Count()
|
|
return result > 0, err
|
|
}
|
|
|
|
func (this Postgres) CompileCreate(bp *schema.Blueprint) []string {
|
|
temporary := db.L("CREATE")
|
|
if bp.Temporary {
|
|
temporary = db.L("CREATE TEMPORARY")
|
|
}
|
|
columns := strings.Join(this.getAddedColumns(bp), ",\n")
|
|
sql := this.GenerateSQL("? TABLE ? (\n?\n)", temporary, db.T(bp.GetTable()), db.L(columns))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileAdd(bp *schema.Blueprint) []string {
|
|
columns := strings.Join(schema.PrefixArray("ADD COLUMN", this.getAddedColumns(bp)), ",\n")
|
|
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(columns))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileChange(bp *schema.Blueprint) []string {
|
|
columns := strings.Join(schema.PrefixArray("ALTER COLUMN", this.getChangedColumns(bp)), ",\n")
|
|
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(columns))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileDropColumn(bp *schema.Blueprint) []string {
|
|
columns := []string{}
|
|
for _, item := range bp.GetCommands() {
|
|
if item.Type != "dropColumn" {
|
|
continue
|
|
}
|
|
for _, col := range item.Columns {
|
|
columns = append(columns, this.GenerateSQL("DROP COLUMN ?", db.C(col)))
|
|
}
|
|
}
|
|
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(strings.Join(columns, ",\n")))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileDrop(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("DROP TABLE ?", db.T(bp.GetTable()))}
|
|
}
|
|
|
|
func (this Postgres) CompileDropIfExists(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("DROP TABLE IF EXISTS ?", db.T(bp.GetTable()))}
|
|
}
|
|
|
|
func (this Postgres) CompileRename(bp *schema.Blueprint) []string {
|
|
toName := ""
|
|
commands := bp.GetCommands()
|
|
if len(commands) == 0 {
|
|
panic("new table undefined")
|
|
}
|
|
toName = bp.GetCommands()[0].To
|
|
if toName == "" {
|
|
panic("new table undefined")
|
|
}
|
|
return []string{this.GenerateSQL("ALTER TABLE ? RENAME TO ?", db.T(bp.GetTable()), db.T(toName))}
|
|
}
|
|
|
|
// 修改表备注
|
|
func (this Postgres) CompileModifyComment(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("COMMENT ON TABLE ? IS ?", db.T(bp.GetTable()), db.V(bp.Comment))}
|
|
}
|
|
|
|
func (this Postgres) addModifiers(sql string, bp *schema.Blueprint, column *schema.ColumnDefinition) string {
|
|
for _, modifier := range pgDefaultModifiers {
|
|
sql += this.GetColumnModifier(modifier, bp, column)
|
|
}
|
|
return sql
|
|
}
|
|
|
|
func (this Postgres) GetColumnType(column *schema.ColumnDefinition) string {
|
|
switch column.Type {
|
|
case "char":
|
|
return this.GenerateSQL("char(?)", column.Length)
|
|
case "string":
|
|
return this.GenerateSQL("varchar(?)", column.Length)
|
|
case "text":
|
|
return "text"
|
|
case "integer":
|
|
return "integer"
|
|
case "bigInteger":
|
|
return "bigint"
|
|
case "smallInteger":
|
|
return "smallint"
|
|
case "decimal":
|
|
return this.GenerateSQL("numeric(?,?)", column.Total, column.Places)
|
|
case "boolean":
|
|
return "boolean"
|
|
case "enum":
|
|
return this.GenerateSQL("varchar(?)", column.Length) // PostgreSQL uses custom types or check constraints
|
|
case "json":
|
|
return "json"
|
|
case "binary":
|
|
return "bytea"
|
|
case "datetime":
|
|
return "timestamp without time zone"
|
|
case "timestamp":
|
|
return "timestamp with time zone"
|
|
case "date":
|
|
return "date"
|
|
case "time":
|
|
return "time"
|
|
case "year":
|
|
return "integer"
|
|
case "uuid":
|
|
return "uuid"
|
|
}
|
|
panic("Unsupported data type: " + column.Type)
|
|
}
|
|
|
|
func (this Postgres) GetColumnModifier(modifier string, bp *schema.Blueprint, column *schema.ColumnDefinition) string {
|
|
switch modifier {
|
|
case "Nullable":
|
|
if column.IsNullable() {
|
|
return " NULL"
|
|
}
|
|
return " NOT NULL"
|
|
case "Default":
|
|
v := column.GetDefault()
|
|
if v == nil {
|
|
if column.IsUseCurrent() {
|
|
return " DEFAULT CURRENT_TIMESTAMP"
|
|
}
|
|
return ""
|
|
}
|
|
if column.IsUseCurrent() {
|
|
return this.GenerateSQL(" DEFAULT CURRENT_TIMESTAMP ?", v)
|
|
}
|
|
return this.GenerateSQL(" DEFAULT ?", v)
|
|
case "Increment":
|
|
if column.IsAutoIncrement() {
|
|
switch column.Type {
|
|
case "integer":
|
|
return " SERIAL PRIMARY KEY"
|
|
case "bigInteger":
|
|
return " BIGSERIAL PRIMARY KEY"
|
|
case "smallInteger":
|
|
return " SMALLSERIAL PRIMARY KEY"
|
|
}
|
|
}
|
|
case "Comment":
|
|
// PostgreSQL handles comments separately via COMMENT ON
|
|
return ""
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (this Postgres) getAddedColumns(bp *schema.Blueprint) (columns []string) {
|
|
for _, column := range bp.GetAddedColumns() {
|
|
sql := this.GenerateSQL("? ?", db.C(column.Name), db.L(this.GetColumnType(column)))
|
|
columns = append(columns, this.addModifiers(sql, bp, column))
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this Postgres) getChangedColumns(bp *schema.Blueprint) (columns []string) {
|
|
for _, column := range bp.GetChangedColumns() {
|
|
sql := this.GenerateSQL("? TYPE ?", db.C(column.Name), db.L(this.GetColumnType(column)))
|
|
columns = append(columns, this.addModifiers(sql, bp, column))
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this Postgres) GenerateSQL(sql string, args ...any) string {
|
|
sb := sb.NewSQLBuilder(false)
|
|
this.esg.Generate(sb, db.L(sql, args...))
|
|
result, _, _ := sb.ToSQL()
|
|
return result
|
|
}
|
|
|
|
func init() {
|
|
schema.RegisterDialect("postgres", func(db *db.Database) schema.Schema {
|
|
return &Postgres{
|
|
db: db,
|
|
esg: sqlgen.NewExpressionSQLGenerator("postgres", postgres.DialectOptions()),
|
|
}
|
|
})
|
|
}
|