From 29e01e9c57480aba2494ddd421af6916f1e61e9e Mon Sep 17 00:00:00 2001 From: Alexandros Filios Date: Mon, 23 Sep 2024 11:00:24 +0200 Subject: [PATCH] Fixed condition interpreter Signed-off-by: Alexandros Filios --- .../db/driver/sql/postgres/conditions.go | 44 ++++++++++++++++- .../db/driver/sql/sqlite/conditions.go | 48 ++----------------- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/platform/view/services/db/driver/sql/postgres/conditions.go b/platform/view/services/db/driver/sql/postgres/conditions.go index 77750f3ed..7b5c67c40 100644 --- a/platform/view/services/db/driver/sql/postgres/conditions.go +++ b/platform/view/services/db/driver/sql/postgres/conditions.go @@ -10,6 +10,46 @@ import ( "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" ) -func NewInterpreter() common.Interpreter { - return common.NewInterpreter() +type interpreter struct { + common.Interpreter +} + +func NewInterpreter() *interpreter { + return &interpreter{Interpreter: common.NewInterpreter()} +} + +func (i *interpreter) In(field common.FieldName, vals []any) common.Condition { + tuples := make([]common.Tuple, len(vals)) + for i, val := range vals { + tuples[i] = common.Tuple{val} + } + return i.InTuple([]common.FieldName{field}, tuples) +} +func (i *interpreter) InStrings(field common.FieldName, vals []string) common.Condition { + tuples := make([]common.Tuple, len(vals)) + for i, val := range vals { + tuples[i] = common.Tuple{val} + } + return i.InTuple([]common.FieldName{field}, tuples) +} +func (i *interpreter) InInts(field common.FieldName, vals []int) common.Condition { + tuples := make([]common.Tuple, len(vals)) + for i, val := range vals { + tuples[i] = common.Tuple{val} + } + return i.InTuple([]common.FieldName{field}, tuples) +} +func (i *interpreter) InTuple(fields []common.FieldName, vals []common.Tuple) common.Condition { + if len(vals) == 0 || len(fields) == 0 { + return common.EmptyCondition + } + ors := make([]common.Condition, len(vals)) + for j, tuple := range vals { + ands := make([]common.Condition, len(tuple)) + for k, val := range tuple { + ands[k] = i.Cmp(fields[k], "=", val) + } + ors[j] = i.And(ands...) + } + return i.Or(ors...) } diff --git a/platform/view/services/db/driver/sql/sqlite/conditions.go b/platform/view/services/db/driver/sql/sqlite/conditions.go index 4bdd16e19..df3f8e9bb 100644 --- a/platform/view/services/db/driver/sql/sqlite/conditions.go +++ b/platform/view/services/db/driver/sql/sqlite/conditions.go @@ -6,50 +6,8 @@ SPDX-License-Identifier: Apache-2.0 package sqlite -import ( - "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" -) +import "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" -type interpreter struct { - common.Interpreter -} - -func NewInterpreter() *interpreter { - return &interpreter{Interpreter: common.NewInterpreter()} -} - -func (i *interpreter) In(field common.FieldName, vals []any) common.Condition { - tuples := make([]common.Tuple, len(vals)) - for i, val := range vals { - tuples[i] = common.Tuple{val} - } - return i.InTuple([]common.FieldName{field}, tuples) -} -func (i *interpreter) InStrings(field common.FieldName, vals []string) common.Condition { - tuples := make([]common.Tuple, len(vals)) - for i, val := range vals { - tuples[i] = common.Tuple{val} - } - return i.InTuple([]common.FieldName{field}, tuples) -} -func (i *interpreter) InInts(field common.FieldName, vals []int) common.Condition { - tuples := make([]common.Tuple, len(vals)) - for i, val := range vals { - tuples[i] = common.Tuple{val} - } - return i.InTuple([]common.FieldName{field}, tuples) -} -func (i *interpreter) InTuple(fields []common.FieldName, vals []common.Tuple) common.Condition { - if len(vals) == 0 || len(fields) == 0 { - return common.EmptyCondition - } - ors := make([]common.Condition, len(vals)) - for j, tuple := range vals { - ands := make([]common.Condition, len(tuple)) - for k, val := range tuple { - ands[k] = i.Cmp(fields[k], "=", val) - } - ors[j] = i.And(ands...) - } - return i.Or(ors...) +func NewInterpreter() common.Interpreter { + return common.NewInterpreter() }