summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/schema/dialect.go
blob: c50de715a8e3b4ac1cab946cc3021fbe81c57f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package schema

import (
	"database/sql"
	"reflect"
	"sync"

	"github.com/uptrace/bun/dialect"
	"github.com/uptrace/bun/dialect/feature"
)

type Dialect interface {
	Init(db *sql.DB)

	Name() dialect.Name
	Features() feature.Feature

	Tables() *Tables
	OnTable(table *Table)

	IdentQuote() byte
	Append(fmter Formatter, b []byte, v interface{}) []byte
	Appender(typ reflect.Type) AppenderFunc
	FieldAppender(field *Field) AppenderFunc
	Scanner(typ reflect.Type) ScannerFunc
}

//------------------------------------------------------------------------------

type nopDialect struct {
	tables   *Tables
	features feature.Feature

	appenderMap sync.Map
	scannerMap  sync.Map
}

func newNopDialect() *nopDialect {
	d := new(nopDialect)
	d.tables = NewTables(d)
	d.features = feature.Returning
	return d
}

func (d *nopDialect) Init(*sql.DB) {}

func (d *nopDialect) Name() dialect.Name {
	return dialect.Invalid
}

func (d *nopDialect) Features() feature.Feature {
	return d.features
}

func (d *nopDialect) Tables() *Tables {
	return d.tables
}

func (d *nopDialect) OnField(field *Field) {}

func (d *nopDialect) OnTable(table *Table) {}

func (d *nopDialect) IdentQuote() byte {
	return '"'
}

func (d *nopDialect) Append(fmter Formatter, b []byte, v interface{}) []byte {
	return Append(fmter, b, v, nil)
}

func (d *nopDialect) Appender(typ reflect.Type) AppenderFunc {
	if v, ok := d.appenderMap.Load(typ); ok {
		return v.(AppenderFunc)
	}

	fn := Appender(typ, nil)

	if v, ok := d.appenderMap.LoadOrStore(typ, fn); ok {
		return v.(AppenderFunc)
	}
	return fn
}

func (d *nopDialect) FieldAppender(field *Field) AppenderFunc {
	return FieldAppender(d, field)
}

func (d *nopDialect) Scanner(typ reflect.Type) ScannerFunc {
	if v, ok := d.scannerMap.Load(typ); ok {
		return v.(ScannerFunc)
	}

	fn := Scanner(typ)

	if v, ok := d.scannerMap.LoadOrStore(typ, fn); ok {
		return v.(ScannerFunc)
	}
	return fn
}