2021-08-29 16:41:41 +02:00
|
|
|
package sqlitedialect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2021-10-24 13:14:37 +02:00
|
|
|
"encoding/hex"
|
2022-04-24 12:26:22 +02:00
|
|
|
"fmt"
|
2021-08-29 16:41:41 +02:00
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
"github.com/uptrace/bun"
|
2021-08-29 16:41:41 +02:00
|
|
|
"github.com/uptrace/bun/dialect"
|
|
|
|
"github.com/uptrace/bun/dialect/feature"
|
|
|
|
"github.com/uptrace/bun/dialect/sqltype"
|
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
func init() {
|
|
|
|
if Version() != bun.Version() {
|
|
|
|
panic(fmt.Errorf("sqlitedialect and Bun must have the same version: v%s != v%s",
|
|
|
|
Version(), bun.Version()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:41:41 +02:00
|
|
|
type Dialect struct {
|
2021-10-24 13:14:37 +02:00
|
|
|
schema.BaseDialect
|
|
|
|
|
2021-08-29 16:41:41 +02:00
|
|
|
tables *schema.Tables
|
|
|
|
features feature.Feature
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Dialect {
|
|
|
|
d := new(Dialect)
|
|
|
|
d.tables = schema.NewTables(d)
|
2021-09-10 14:42:14 +02:00
|
|
|
d.features = feature.CTE |
|
2022-04-24 12:26:22 +02:00
|
|
|
feature.WithValues |
|
2021-09-10 14:42:14 +02:00
|
|
|
feature.Returning |
|
2022-04-24 12:26:22 +02:00
|
|
|
feature.InsertReturning |
|
2021-09-10 14:42:14 +02:00
|
|
|
feature.InsertTableAlias |
|
2022-04-24 12:26:22 +02:00
|
|
|
feature.UpdateTableAlias |
|
2021-11-27 15:26:58 +01:00
|
|
|
feature.DeleteTableAlias |
|
2022-04-24 12:26:22 +02:00
|
|
|
feature.InsertOnConflict |
|
|
|
|
feature.TableNotExists |
|
2022-09-28 19:30:40 +02:00
|
|
|
feature.SelectExists |
|
2024-01-15 14:08:07 +01:00
|
|
|
feature.AutoIncrement |
|
2024-11-25 16:42:37 +01:00
|
|
|
feature.CompositeIn |
|
|
|
|
feature.DeleteReturning
|
2021-08-29 16:41:41 +02:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) Init(*sql.DB) {}
|
|
|
|
|
|
|
|
func (d *Dialect) Name() dialect.Name {
|
|
|
|
return dialect.SQLite
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-10-24 13:14:37 +02:00
|
|
|
field.DiscoveredSQLType = fieldSQLType(field)
|
2021-08-29 16:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dialect) IdentQuote() byte {
|
|
|
|
return '"'
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func (d *Dialect) AppendBytes(b []byte, bs []byte) []byte {
|
|
|
|
if bs == nil {
|
|
|
|
return dialect.AppendNull(b)
|
2021-08-29 16:41:41 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
b = append(b, `X'`...)
|
2021-08-29 16:41:41 +02:00
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
s := len(b)
|
|
|
|
b = append(b, make([]byte, hex.EncodedLen(len(bs)))...)
|
|
|
|
hex.Encode(b[s:], bs)
|
2021-08-29 16:41:41 +02:00
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
b = append(b, '\'')
|
2021-08-29 16:41:41 +02:00
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
return b
|
|
|
|
}
|
2021-08-29 16:41:41 +02:00
|
|
|
|
2023-01-22 12:26:47 +01:00
|
|
|
func (d *Dialect) DefaultVarcharLen() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:08:07 +01:00
|
|
|
// AppendSequence adds AUTOINCREMENT keyword to the column definition. As per [documentation],
|
|
|
|
// AUTOINCREMENT is only valid for INTEGER PRIMARY KEY, and this method will be a noop for other columns.
|
|
|
|
//
|
|
|
|
// Because this is a valid construct:
|
2024-11-25 16:42:37 +01:00
|
|
|
//
|
2024-01-15 14:08:07 +01:00
|
|
|
// CREATE TABLE ("id" INTEGER PRIMARY KEY AUTOINCREMENT);
|
2024-11-25 16:42:37 +01:00
|
|
|
//
|
2024-01-15 14:08:07 +01:00
|
|
|
// and this is not:
|
2024-11-25 16:42:37 +01:00
|
|
|
//
|
2024-01-15 14:08:07 +01:00
|
|
|
// CREATE TABLE ("id" INTEGER AUTOINCREMENT, PRIMARY KEY ("id"));
|
2024-11-25 16:42:37 +01:00
|
|
|
//
|
2024-01-15 14:08:07 +01:00
|
|
|
// AppendSequence adds a primary key constraint as a *side-effect*. Callers should expect it to avoid building invalid SQL.
|
|
|
|
// SQLite also [does not support] AUTOINCREMENT column in composite primary keys.
|
|
|
|
//
|
|
|
|
// [documentation]: https://www.sqlite.org/autoinc.html
|
|
|
|
// [does not support]: https://stackoverflow.com/a/6793274/14726116
|
|
|
|
func (d *Dialect) AppendSequence(b []byte, table *schema.Table, field *schema.Field) []byte {
|
|
|
|
if field.IsPK && len(table.PKs) == 1 && field.CreateTableSQLType == sqltype.Integer {
|
|
|
|
b = append(b, " PRIMARY KEY AUTOINCREMENT"...)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2024-11-25 16:42:37 +01:00
|
|
|
// DefaultSchemaName is the "schema-name" of the main database.
|
|
|
|
// The details might differ from other dialects, but for all means and purposes
|
|
|
|
// "main" is the default schema in an SQLite database.
|
|
|
|
func (d *Dialect) DefaultSchema() string {
|
|
|
|
return "main"
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func fieldSQLType(field *schema.Field) string {
|
|
|
|
switch field.DiscoveredSQLType {
|
|
|
|
case sqltype.SmallInt, sqltype.BigInt:
|
|
|
|
// INTEGER PRIMARY KEY is an alias for the ROWID.
|
|
|
|
// It is safe to convert all ints to INTEGER, because SQLite types don't have size.
|
|
|
|
return sqltype.Integer
|
|
|
|
default:
|
|
|
|
return field.DiscoveredSQLType
|
2021-08-29 16:41:41 +02:00
|
|
|
}
|
|
|
|
}
|