|
| 1 | +package gormpinot |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "gorm.io/gorm" |
| 8 | + "gorm.io/gorm/callbacks" |
| 9 | + "gorm.io/gorm/clause" |
| 10 | + "gorm.io/gorm/logger" |
| 11 | + "gorm.io/gorm/schema" |
| 12 | + |
| 13 | + "github.com/startreedata/pinot-client-go/pinot" |
| 14 | +) |
| 15 | + |
| 16 | +// Config configures the Pinot GORM dialector. |
| 17 | +type Config struct { |
| 18 | + Conn *pinot.Connection |
| 19 | + DefaultTable string |
| 20 | +} |
| 21 | + |
| 22 | +// Dialector is the GORM dialector for Pinot. |
| 23 | +type Dialector struct { |
| 24 | + config Config |
| 25 | +} |
| 26 | + |
| 27 | +// Open returns a GORM dialector configured for Pinot. |
| 28 | +func Open(config Config) gorm.Dialector { |
| 29 | + return Dialector{config: config} |
| 30 | +} |
| 31 | + |
| 32 | +// Name returns the dialector name. |
| 33 | +func (Dialector) Name() string { |
| 34 | + return "pinot" |
| 35 | +} |
| 36 | + |
| 37 | +// Initialize wires the dialector into the GORM DB instance. |
| 38 | +func (d Dialector) Initialize(db *gorm.DB) error { |
| 39 | + if d.config.Conn == nil { |
| 40 | + return errors.New("pinot connection is required") |
| 41 | + } |
| 42 | + db.DisableAutomaticPing = true |
| 43 | + |
| 44 | + connector := newConnector(d.config.Conn, d.config.DefaultTable) |
| 45 | + db.ConnPool = sql.OpenDB(connector) |
| 46 | + |
| 47 | + callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{ |
| 48 | + CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}, |
| 49 | + UpdateClauses: []string{"UPDATE", "SET", "WHERE", "RETURNING"}, |
| 50 | + DeleteClauses: []string{"DELETE", "FROM", "WHERE", "RETURNING"}, |
| 51 | + LastInsertIDReversed: false, |
| 52 | + }) |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// Migrator returns a migrator that rejects schema operations. |
| 57 | +func (Dialector) Migrator(db *gorm.DB) gorm.Migrator { |
| 58 | + return unsupportedMigrator{db: db} |
| 59 | +} |
| 60 | + |
| 61 | +// DataTypeOf returns an empty datatype since migrations are unsupported. |
| 62 | +func (Dialector) DataTypeOf(*schema.Field) string { |
| 63 | + return "" |
| 64 | +} |
| 65 | + |
| 66 | +// DefaultValueOf returns DEFAULT for compatibility. |
| 67 | +func (Dialector) DefaultValueOf(*schema.Field) clause.Expression { |
| 68 | + return clause.Expr{SQL: "DEFAULT"} |
| 69 | +} |
| 70 | + |
| 71 | +// BindVarTo writes a placeholder. |
| 72 | +func (Dialector) BindVarTo(writer clause.Writer, _ *gorm.Statement, _ interface{}) { |
| 73 | + writeByte(writer, '?') |
| 74 | +} |
| 75 | + |
| 76 | +// QuoteTo quotes identifiers with double quotes. |
| 77 | +func (Dialector) QuoteTo(writer clause.Writer, str string) { |
| 78 | + var ( |
| 79 | + underQuoted, selfQuoted bool |
| 80 | + continuousQuote int8 |
| 81 | + shiftDelimiter int8 |
| 82 | + ) |
| 83 | + |
| 84 | + for _, v := range []byte(str) { |
| 85 | + switch v { |
| 86 | + case '"': |
| 87 | + continuousQuote++ |
| 88 | + if continuousQuote == 2 { |
| 89 | + writeString(writer, `""`) |
| 90 | + continuousQuote = 0 |
| 91 | + } |
| 92 | + case '.': |
| 93 | + if continuousQuote > 0 || !selfQuoted { |
| 94 | + shiftDelimiter = 0 |
| 95 | + underQuoted = false |
| 96 | + continuousQuote = 0 |
| 97 | + writeByte(writer, '"') |
| 98 | + } |
| 99 | + writeByte(writer, v) |
| 100 | + continue |
| 101 | + default: |
| 102 | + if shiftDelimiter-continuousQuote <= 0 && !underQuoted { |
| 103 | + writeByte(writer, '"') |
| 104 | + underQuoted = true |
| 105 | + if selfQuoted = continuousQuote > 0; selfQuoted { |
| 106 | + continuousQuote-- |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for ; continuousQuote > 0; continuousQuote-- { |
| 111 | + writeString(writer, `""`) |
| 112 | + } |
| 113 | + |
| 114 | + writeByte(writer, v) |
| 115 | + } |
| 116 | + shiftDelimiter++ |
| 117 | + } |
| 118 | + |
| 119 | + if continuousQuote > 0 && !selfQuoted { |
| 120 | + writeString(writer, `""`) |
| 121 | + } |
| 122 | + writeByte(writer, '"') |
| 123 | +} |
| 124 | + |
| 125 | +func writeByte(writer clause.Writer, value byte) { |
| 126 | + //nolint:errcheck |
| 127 | + _ = writer.WriteByte(value) |
| 128 | +} |
| 129 | + |
| 130 | +func writeString(writer clause.Writer, value string) { |
| 131 | + //nolint:errcheck |
| 132 | + _, _ = writer.WriteString(value) |
| 133 | +} |
| 134 | + |
| 135 | +// Explain returns SQL with rendered parameters for logging. |
| 136 | +func (Dialector) Explain(sql string, vars ...interface{}) string { |
| 137 | + return logger.ExplainSQL(sql, nil, "'", vars...) |
| 138 | +} |
0 commit comments