2021-08-25 16:34:33 +03:00
|
|
|
package pgdialect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/uptrace/bun/dialect"
|
|
|
|
"github.com/uptrace/bun/dialect/feature"
|
|
|
|
"github.com/uptrace/bun/dialect/sqltype"
|
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
2021-10-24 14:14:37 +03:00
|
|
|
var pgDialect = New()
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
type Dialect struct {
|
2021-10-24 14:14:37 +03:00
|
|
|
schema.BaseDialect
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
tables *schema.Tables
|
|
|
|
features feature.Feature
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Dialect {
|
|
|
|
d := new(Dialect)
|
|
|
|
d.tables = schema.NewTables(d)
|
2021-09-10 15:42:14 +03:00
|
|
|
d.features = feature.CTE |
|
|
|
|
feature.Returning |
|
2021-08-25 16:34:33 +03:00
|
|
|
feature.DefaultPlaceholder |
|
|
|
|
feature.DoubleColonCast |
|
|
|
|
feature.InsertTableAlias |
|
|
|
|
feature.DeleteTableAlias |
|
|
|
|
feature.TableCascade |
|
|
|
|
feature.TableIdentity |
|
|
|
|
feature.TableTruncate
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) Init(*sql.DB) {}
|
|
|
|
|
|
|
|
func (d *Dialect) Name() dialect.Name {
|
|
|
|
return dialect.PG
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) Features() feature.Feature {
|
|
|
|
return d.features
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) Tables() *schema.Tables {
|
|
|
|
return d.tables
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) OnTable(table *schema.Table) {
|
|
|
|
for _, field := range table.FieldMap {
|
|
|
|
d.onField(field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) onField(field *schema.Field) {
|
|
|
|
field.DiscoveredSQLType = fieldSQLType(field)
|
|
|
|
|
|
|
|
if field.AutoIncrement {
|
|
|
|
switch field.DiscoveredSQLType {
|
|
|
|
case sqltype.SmallInt:
|
|
|
|
field.CreateTableSQLType = pgTypeSmallSerial
|
|
|
|
case sqltype.Integer:
|
|
|
|
field.CreateTableSQLType = pgTypeSerial
|
|
|
|
case sqltype.BigInt:
|
|
|
|
field.CreateTableSQLType = pgTypeBigSerial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Tag.HasOption("array") {
|
2021-10-24 14:14:37 +03:00
|
|
|
field.Append = d.arrayAppender(field.StructField.Type)
|
2021-09-10 15:42:14 +03:00
|
|
|
field.Scan = arrayScanner(field.StructField.Type)
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) IdentQuote() byte {
|
|
|
|
return '"'
|
|
|
|
}
|
|
|
|
|
2021-10-24 14:14:37 +03:00
|
|
|
func (d *Dialect) AppendUint32(b []byte, n uint32) []byte {
|
|
|
|
return strconv.AppendInt(b, int64(int32(n)), 10)
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-24 14:14:37 +03:00
|
|
|
func (d *Dialect) AppendUint64(b []byte, n uint64) []byte {
|
|
|
|
return strconv.AppendInt(b, int64(n), 10)
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|