Skip to content

Commit bfc8bf7

Browse files
authored
feat(storage): copy FSC query builder into token-sdk and redirect imports (#1655) (#1734)
Signed-off-by: Siddhi Khandelwal <siddhi.200727@gmail.com>
1 parent bfe7088 commit bfc8bf7

77 files changed

Lines changed: 3064 additions & 146 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ cmd/token_validation_service/example-*.txt
1212
cmd/token_validation_service/out/
1313
/.antigravitycli/
1414
/site/
15+
coverage.out

integration/token/fungible/views/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1414
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/assert"
1515
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections/iterators"
16-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/pagination"
1716
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1817
"github.com/hyperledger-labs/fabric-token-sdk/token"
18+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/pagination"
1919
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/ttxdb"
2020
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx"
2121
token2 "github.com/hyperledger-labs/fabric-token-sdk/token/token"

token/services/storage/db/sql/common/bigint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (b *BigInt) Scan(value interface{}) error {
3838

3939
return nil
4040
default:
41+
4142
return fmt.Errorf("cannot scan type %T into BigInt", value)
4243
}
4344

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package common
6+
7+
import (
8+
common2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
9+
)
10+
11+
func NewTestInterpreter() common2.CondInterpreter {
12+
return newTestInterpreter()
13+
}

token/services/storage/db/sql/common/identity.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ import (
1717
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver"
1818
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
1919
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/common"
20-
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query"
21-
common3 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
22-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
2320
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
2421
"github.com/hyperledger-labs/fabric-token-sdk/token"
2522
tdriver "github.com/hyperledger-labs/fabric-token-sdk/token/driver"
2623
idriver "github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/driver"
2724
"github.com/hyperledger-labs/fabric-token-sdk/token/services/logging"
2825
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage"
2926
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/driver"
27+
q "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query"
28+
common3 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
29+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
3030
"github.com/hyperledger-labs/fabric-token-sdk/token/services/utils"
3131
cache2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/utils/cache"
3232
)
@@ -442,6 +442,7 @@ func (db *IdentityStore) registerIdentityDescriptor(
442442
}
443443
if exists {
444444
// no need to continue
445+
445446
return nil
446447
}
447448

token/services/storage/db/sql/common/keystore.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver"
1818
dcommon "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
1919
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/common"
20-
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query"
21-
qcommon "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
22-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
2320
"github.com/hyperledger-labs/fabric-token-sdk/token/services/logging"
21+
q "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query"
22+
qcommon "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
23+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
2424
)
2525

2626
type keystoreTables struct {
@@ -91,6 +91,7 @@ func (db *KeystoreStore) Put(key string, state interface{}) error {
9191
}
9292
if bytes.Equal(rawFromDB, raw) {
9393
// It might be that this key was already inserted before. The node is restarting, for example.
94+
9495
return nil
9596
}
9697

token/services/storage/db/sql/common/query_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ package common_test
99
import (
1010
"testing"
1111

12-
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query"
13-
qcommon "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
14-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
15-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/sqlite"
12+
q "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query"
13+
qcommon "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
14+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
15+
16+
common "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/common"
1617
"github.com/stretchr/testify/assert"
1718
)
1819

@@ -22,23 +23,23 @@ func TestSelect_Compile(t *testing.T) {
2223
FieldsByName("id", "name").
2324
From(q.Table("users")).
2425
Where(cond.Eq("id", 1)).
25-
Format(sqlite.NewConditionInterpreter())
26+
Format(common.NewTestInterpreter())
2627
assert.Equal(t, "SELECT id, name FROM users WHERE id = $1", query)
2728
assert.Equal(t, 1, args[0])
2829

2930
// SELECT DISTINCT
3031
query, args = q.SelectDistinct().
3132
FieldsByName("email_id").
3233
From(q.Table("customers")).
33-
Format(sqlite.NewConditionInterpreter())
34+
Format(common.NewTestInterpreter())
3435
assert.Equal(t, "SELECT DISTINCT email_id FROM customers", query)
3536
assert.Empty(t, args)
3637

3738
// No columns selected
3839
query, args = q.Select().
3940
AllFields().
4041
From(q.Table("users")).
41-
Format(sqlite.NewConditionInterpreter())
42+
Format(common.NewTestInterpreter())
4243
assert.Equal(t, "SELECT * FROM users", query)
4344
assert.Empty(t, args)
4445
}
@@ -60,7 +61,7 @@ func TestUpdate_Compile(t *testing.T) {
6061
Set("name", "TheName").
6162
Set("age", 16).
6263
Where(cond.Eq("id", 1)).
63-
Format(sqlite.NewConditionInterpreter())
64+
Format(common.NewTestInterpreter())
6465
assert.Equal(t, "UPDATE users SET name = $1, age = $2 WHERE id = $3", query)
6566
assert.Equal(t, "TheName", args[0])
6667
assert.Equal(t, 16, args[1])
@@ -71,7 +72,7 @@ func TestDelete_Compile(t *testing.T) {
7172
// Simple DELETE
7273
query, args := q.DeleteFrom("users").
7374
Where(cond.Eq("id", 1)).
74-
Format(sqlite.NewConditionInterpreter())
75+
Format(common.NewTestInterpreter())
7576
assert.Equal(t, "DELETE FROM users WHERE id = $1", query)
7677
assert.Equal(t, 1, args[0])
7778
}
@@ -81,7 +82,7 @@ func TestDelete_Compile(t *testing.T) {
8182
// numbering continuing across branches and args concatenated in order.
8283
// This is the pattern used by UnspentTokensIteratorBy.
8384
func TestUnionAll_Compile(t *testing.T) {
84-
ci := sqlite.NewConditionInterpreter()
85+
ci := common.NewTestInterpreter()
8586

8687
branch1 := q.Select().
8788
FieldsByName("id", "name").

token/services/storage/db/sql/common/querybuilder_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"testing"
1111
"time"
1212

13-
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query"
14-
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
15-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
16-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/sqlite"
13+
q "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query"
14+
common2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
15+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
16+
1717
driver2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/driver"
1818
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
1919
"github.com/stretchr/testify/assert"
@@ -333,7 +333,7 @@ func TestTokenSql(t *testing.T) {
333333

334334
func evalCondition(condition cond.Condition) (string, []common2.Param) {
335335
sb := common2.NewBuilder()
336-
condition.WriteString(sqlite.NewConditionInterpreter(), sb)
336+
condition.WriteString(newTestInterpreter(), sb)
337337
actualSql, actualArgs := sb.Build()
338338

339339
return actualSql, actualArgs

token/services/storage/db/sql/common/tcondition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package common
99
import (
1010
"time"
1111

12-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
13-
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
1412
driver2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/driver"
13+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
14+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
1515
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
1616
)
1717

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package common
6+
7+
import (
8+
"math"
9+
"strconv"
10+
"time"
11+
12+
common2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/common"
13+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/sql/query/cond"
14+
)
15+
16+
var signs = map[bool]rune{true: '+', false: '-'}
17+
18+
type testInterpreter struct{}
19+
20+
func newTestInterpreter() common2.CondInterpreter {
21+
return &testInterpreter{}
22+
}
23+
24+
func (i *testInterpreter) TimeOffset(duration time.Duration, sb common2.Builder) {
25+
sb.WriteString("datetime('now'")
26+
if duration == 0 {
27+
sb.WriteRune(')')
28+
29+
return
30+
}
31+
sb.WriteString(", '").
32+
WriteRune(signs[duration > 0]).
33+
WriteString(strconv.Itoa(int(math.Abs(duration.Seconds())))).
34+
WriteString(" seconds')")
35+
}
36+
37+
func (i *testInterpreter) InTuple(fields []common2.Serializable, vals []common2.Tuple, sb common2.Builder) {
38+
if len(vals) == 0 || len(fields) == 0 {
39+
return
40+
}
41+
if len(vals) == 1 && len(fields) == 1 {
42+
sb.WriteConditionSerializable(cond.CmpVal(fields[0], "=", vals[0][0]), i)
43+
44+
return
45+
}
46+
sb.WriteString("(").
47+
WriteSerializables(common2.ToSerializables(fields)...).
48+
WriteString(") IN (").
49+
WriteTuples(vals).
50+
WriteString(")")
51+
}

0 commit comments

Comments
 (0)