2021-08-25 13:34:33 +00:00
|
|
|
package pgdialect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-04-24 10:26:22 +00:00
|
|
|
"fmt"
|
2021-08-25 13:34:33 +00:00
|
|
|
"strconv"
|
2021-11-13 11:29:08 +00:00
|
|
|
"strings"
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
"github.com/uptrace/bun"
|
2021-08-25 13:34:33 +00:00
|
|
|
"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 11:14:37 +00:00
|
|
|
var pgDialect = New()
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func init() {
|
|
|
|
if Version() != bun.Version() {
|
|
|
|
panic(fmt.Errorf("pgdialect and Bun must have the same version: v%s != v%s",
|
|
|
|
Version(), bun.Version()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
type Dialect struct {
|
2021-10-24 11:14:37 +00:00
|
|
|
schema.BaseDialect
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
tables *schema.Tables
|
|
|
|
features feature.Feature
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Dialect {
|
|
|
|
d := new(Dialect)
|
|
|
|
d.tables = schema.NewTables(d)
|
2021-09-10 12:42:14 +00:00
|
|
|
d.features = feature.CTE |
|
2022-04-24 10:26:22 +00:00
|
|
|
feature.WithValues |
|
2021-09-10 12:42:14 +00:00
|
|
|
feature.Returning |
|
2022-04-24 10:26:22 +00:00
|
|
|
feature.InsertReturning |
|
2021-08-25 13:34:33 +00:00
|
|
|
feature.DefaultPlaceholder |
|
|
|
|
feature.DoubleColonCast |
|
|
|
|
feature.InsertTableAlias |
|
2022-04-24 10:26:22 +00:00
|
|
|
feature.UpdateTableAlias |
|
2021-08-25 13:34:33 +00:00
|
|
|
feature.DeleteTableAlias |
|
|
|
|
feature.TableCascade |
|
|
|
|
feature.TableIdentity |
|
2021-11-27 14:26:58 +00:00
|
|
|
feature.TableTruncate |
|
2022-04-24 10:26:22 +00:00
|
|
|
feature.TableNotExists |
|
|
|
|
feature.InsertOnConflict |
|
2022-08-15 10:35:05 +00:00
|
|
|
feature.SelectExists |
|
2022-09-28 17:30:40 +00:00
|
|
|
feature.GeneratedIdentity |
|
|
|
|
feature.CompositeIn
|
2021-08-25 13:34:33 +00:00
|
|
|
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)
|
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
if field.AutoIncrement && !field.Identity {
|
2021-08-25 13:34:33 +00:00
|
|
|
switch field.DiscoveredSQLType {
|
|
|
|
case sqltype.SmallInt:
|
|
|
|
field.CreateTableSQLType = pgTypeSmallSerial
|
|
|
|
case sqltype.Integer:
|
|
|
|
field.CreateTableSQLType = pgTypeSerial
|
|
|
|
case sqltype.BigInt:
|
|
|
|
field.CreateTableSQLType = pgTypeBigSerial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
if field.Tag.HasOption("array") || strings.HasSuffix(field.UserSQLType, "[]") {
|
2021-10-24 11:14:37 +00:00
|
|
|
field.Append = d.arrayAppender(field.StructField.Type)
|
2021-09-10 12:42:14 +00:00
|
|
|
field.Scan = arrayScanner(field.StructField.Type)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
|
|
|
|
if field.DiscoveredSQLType == sqltype.HSTORE {
|
|
|
|
field.Append = d.hstoreAppender(field.StructField.Type)
|
|
|
|
field.Scan = hstoreScanner(field.StructField.Type)
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) IdentQuote() byte {
|
|
|
|
return '"'
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
func (d *Dialect) AppendUint32(b []byte, n uint32) []byte {
|
|
|
|
return strconv.AppendInt(b, int64(int32(n)), 10)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
func (d *Dialect) AppendUint64(b []byte, n uint64) []byte {
|
|
|
|
return strconv.AppendInt(b, int64(n), 10)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|