-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathselector.go
More file actions
48 lines (40 loc) · 1.74 KB
/
selector.go
File metadata and controls
48 lines (40 loc) · 1.74 KB
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
package compiler
import "strings"
// selector is an interface used by a compiler for generating expressions for
// output columns in a `SELECT ...` or `RETURNING ...` statement.
//
// This interface is exclusively needed at the moment for SQLite, which must
// wrap output `jsonb` columns with a `json(column_name)` invocation so that a
// publicly consumable format (i.e. not jsonb) is returned.
type selector interface {
// ColumnExpr generates output to be used in a `SELECT ...` or `RETURNING
// ...` statement based on input column name and metadata.
ColumnExpr(name string, column *Column) string
}
// defaultSelector is a selector implementation that does the simpliest possible
// pass through when generating column expressions. Its use is suitable for all
// database engines not requiring additional customization.
type defaultSelector struct{}
func newDefaultSelector() *defaultSelector {
return &defaultSelector{}
}
func (s *defaultSelector) ColumnExpr(name string, column *Column) string {
return name
}
type sqliteSelector struct{}
func newSQLiteSelector() *sqliteSelector {
return &sqliteSelector{}
}
func (s *sqliteSelector) ColumnExpr(name string, column *Column) string {
// Under SQLite, neither json nor jsonb are real data types, and rather just
// of type blob, so database drivers just return whatever raw binary is
// stored as values. This is a problem for jsonb, which is considered an
// internal format to SQLite and no attempt should be made to parse it
// outside of the database itself. For jsonb columns in SQLite, wrap values
// in `json(col)` to coerce the internal binary format to JSON parsable by
// the user-space application.
if strings.EqualFold(column.DataType, "jsonb") {
return "json(" + name + ")"
}
return name
}