Skip to content

Commit 9ad860d

Browse files
authored
Merge pull request #44 from gcpug/build-tag-check
add json1 build tag check to use some features
2 parents ea1068a + f63e151 commit 9ad860d

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

server/database_json.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2020 Masahiro Sano
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build json1
16+
17+
package server
18+
19+
func useSqliteJSON() { return }

server/database_nonjson.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2020 Masahiro Sano
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build !json1
16+
17+
package server
18+
19+
func useSqliteJSON() { panic(`Require "json1" build tag to use some features`) }

server/database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ func TestInsertOrRepace_CommitTimestamp(t *testing.T) {
14291429
}
14301430

14311431
d := time.Since(timestamp)
1432-
if d >= 3*time.Millisecond {
1432+
if d >= 10*time.Millisecond {
14331433
t.Fatalf("unexpected time: %v", d)
14341434
}
14351435
}

server/query.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (b *QueryBuilder) buildSelectQuery(selectStmt *ast.Select) (string, []inter
156156
}
157157

158158
if selectStmt.AsStruct {
159+
useSqliteJSON()
159160
values := make([]string, len(resultItems))
160161
quotedNames := make([]string, len(resultItems))
161162
vts := make([]*ValueType, len(resultItems))
@@ -459,6 +460,7 @@ func (b *QueryBuilder) buildQueryTable(exp ast.TableExpr) (*TableView, string, [
459460
//
460461
// See the doc comment of buildUnnestExpr about the generated query.
461462
func (b *QueryBuilder) buildUnnestView(src *ast.Unnest) (*TableView, string, []interface{}, error) {
463+
useSqliteJSON()
462464
var offset bool
463465
var offsetAlias string
464466
if src.WithOffset != nil {
@@ -607,6 +609,7 @@ func (b *QueryBuilder) buildResultSet(selectItems []ast.SelectItem) ([]ResultIte
607609
if st.IsTable {
608610
exprs = append(exprs, fmt.Sprintf("%s.*", ex.Raw))
609611
} else {
612+
useSqliteJSON()
610613
n := len(st.FieldTypes)
611614
for i := 0; i < n; i++ {
612615
exprs = append(exprs, fmt.Sprintf("JSON_EXTRACT(%s, '$.values[%d]')", ex.Raw, i))
@@ -924,6 +927,7 @@ func (b *QueryBuilder) buildInCondition(cond ast.InCondition) (Expr, error) {
924927
// If offset for unnest is needed `UNNEST([a, b, c])` generates:
925928
// `SELECT value, key as offset JSON_EACH(JSON_ARRAY(a, b, c))`
926929
func (b *QueryBuilder) buildUnnestExpr(expr ast.Expr, offset bool, asview bool) (Expr, error) {
930+
useSqliteJSON()
927931
ex, err := b.buildExpr(expr)
928932
if err != nil {
929933
return NullExpr, wrapExprError(err, expr, "UNNEST")
@@ -962,6 +966,7 @@ func (b *QueryBuilder) expandParamByPlaceholders(v Value) (Expr, error) {
962966
Args: []interface{}{v.Data},
963967
}, nil
964968
case []bool, []int64, []float64, []string, [][]byte:
969+
useSqliteJSON()
965970
vv := reflect.ValueOf(v.Data)
966971
n := vv.Len()
967972

@@ -982,6 +987,7 @@ func (b *QueryBuilder) expandParamByPlaceholders(v Value) (Expr, error) {
982987
}, nil
983988

984989
case ArrayValue:
990+
useSqliteJSON()
985991
rv := reflect.ValueOf(v.Data.(ArrayValue).Elements())
986992
n := rv.Len()
987993

@@ -1042,6 +1048,7 @@ func (b *QueryBuilder) accessField(expr Expr, name string) (Expr, error) {
10421048
if st.IsTable {
10431049
raw = fmt.Sprintf("%s.%s", expr.Raw, name)
10441050
} else {
1051+
useSqliteJSON()
10451052
raw = fmt.Sprintf("JSON_EXTRACT(%s, '$.values[%d]')", expr.Raw, idx)
10461053
}
10471054

@@ -1192,6 +1199,7 @@ func (b *QueryBuilder) buildExpr(expr ast.Expr) (Expr, error) {
11921199
}, nil
11931200

11941201
case *ast.IndexExpr:
1202+
useSqliteJSON()
11951203
ex1, err := b.buildExpr(e.Expr)
11961204
if err != nil {
11971205
return NullExpr, wrapExprError(err, expr, "Expr")
@@ -1565,6 +1573,7 @@ func (b *QueryBuilder) buildExpr(expr ast.Expr) (Expr, error) {
15651573
}, nil
15661574

15671575
case *ast.ArraySubQuery:
1576+
useSqliteJSON()
15681577
query, args, items, err := BuildQuery(b.db, e.Query, b.params, true)
15691578
if err != nil {
15701579
return NullExpr, wrapExprError(err, expr, "Array")
@@ -1600,6 +1609,7 @@ func (b *QueryBuilder) buildExpr(expr ast.Expr) (Expr, error) {
16001609
}, nil
16011610

16021611
case *ast.ArrayLiteral:
1612+
useSqliteJSON()
16031613
var args []interface{}
16041614
var ss []string
16051615
var vts []ValueType
@@ -1650,6 +1660,7 @@ func (b *QueryBuilder) buildExpr(expr ast.Expr) (Expr, error) {
16501660
}, nil
16511661

16521662
case *ast.StructLiteral:
1663+
useSqliteJSON()
16531664
var names []string
16541665
var vt ValueType
16551666
var typedef bool

0 commit comments

Comments
 (0)