Skip to content

Commit

Permalink
refactor: moves sql_parser, state_machine, string_parser, string_scan…
Browse files Browse the repository at this point in the history
…ner into the parser folder
  • Loading branch information
ziccardi committed Jun 19, 2024
1 parent 5ce66d8 commit 177492f
Show file tree
Hide file tree
Showing 22 changed files with 30 additions and 21 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package sql_parser

import (
"fmt"
"github.com/openshift-online/ocm-common/pkg/utils/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/string_parser"
"github.com/openshift-online/ocm-common/pkg/utils/parser/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_parser"
"regexp"
"strings"
)
Expand Down Expand Up @@ -50,8 +50,7 @@ func BasicSQLGrammar() string_parser.Grammar {
{Name: jsonbArrow, StateData: jsonbFamily, Acceptor: makeStringAcceptor(`->`)},
{Name: jsonbField, StateData: jsonbFamily, Acceptor: makeRegexpAcceptor(`'([^']|\\')*'`)},
{Name: jsonbToString, StateData: jsonbFamily, Acceptor: makeStringAcceptor(`->>`)},
{Name: jsonbAt, StateData: jsonbFamily, Acceptor: makeStringAcceptor(`@`)},
{Name: jsonbGT, StateData: jsonbFamily, Acceptor: makeStringAcceptor(`>`)},
{Name: jsonbContains, StateData: jsonbFamily, Acceptor: makeStringAcceptor(`@>`)},
{Name: jsonbFieldToStringify, StateData: jsonbFamily, Acceptor: makeRegexpAcceptor(`'([^']|\\')*'`)},
},
Transitions: []string_parser.TokenTransitions{
Expand All @@ -74,11 +73,10 @@ func BasicSQLGrammar() string_parser.Grammar {
{TokenName: valueInList, ValidTransitions: []string{comma, closedBrace}},
{TokenName: comma, ValidTransitions: []string{quotedValueInList, valueInList}},
{TokenName: jsonbArrow, ValidTransitions: []string{jsonbField}},
{TokenName: jsonbField, ValidTransitions: []string{jsonbArrow, jsonbToString, jsonbAt}},
{TokenName: jsonbAt, ValidTransitions: []string{jsonbGT}},
{TokenName: jsonbField, ValidTransitions: []string{jsonbArrow, jsonbToString, jsonbContains}},
{TokenName: jsonbToString, ValidTransitions: []string{jsonbFieldToStringify}},
{TokenName: jsonbFieldToStringify, ValidTransitions: []string{eq, notEq, like, ilike, in, not}},
{TokenName: jsonbGT, ValidTransitions: []string{quotedValue}},
{TokenName: jsonbContains, ValidTransitions: []string{quotedValue}},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package sql_parser

import (
"fmt"
"github.com/openshift-online/ocm-common/pkg/utils/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/string_parser"
"github.com/openshift-online/ocm-common/pkg/utils/parser/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_parser"
"strings"
)

Expand Down Expand Up @@ -40,8 +40,7 @@ const (
jsonbField = "JSON_FIELD" // Each JSONB field
jsonbArrow = "JSONB_ARROW" // The JSONB arrow token (->)
jsonbToString = "JSONB_TOSTRING" // The JSONB to-string token (->>)
jsonbAt = "JSON_AT" // The JSONB @ token
jsonbGT = "JSON_GT" // The JSONB > token
jsonbContains = "@>" // The JSONB @> token
jsonbFieldToStringify = "JSONB_FIELD_TO_STRINGIFY" // The field that will contain the `string` value, ie: ->> FIELD
)
const MaximumComplexity = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ var _ = Describe("SQLParser", func() {
}),
Entry("JSONB Query @>", testData{
qry: `resources.payload -> 'data' -> 'manifests' @> '[{"metadata":{"labels":{"foo":"bar"}}}]'`,
outQry: "resources.payload -> 'data' -> 'manifests' @ > ?",
outQry: "resources.payload -> 'data' -> 'manifests' @> ?",
outValues: []interface{}{`[{"metadata":{"labels":{"foo":"bar"}}}]`},
wantErr: false,
}),
Expand All @@ -215,7 +215,7 @@ var _ = Describe("SQLParser", func() {
outQry: "manifest -> 'data' -> 'manifest' -> 'metadata' -> 'labels' ->> 'foo' = ? " +
"and (manifest -> 'data' -> 'manifest' ->> 'foo' in( ? , ?) " +
"or manifest -> 'data' -> 'manifest' ->> 'labels' <> ?) " +
"AND resources.payload -> 'data' -> 'manifests' @ > ? " +
"AND resources.payload -> 'data' -> 'manifests' @> ? " +
"OR my_column in( ? , ? , ?) and my_column2 = ?",
outValues: []interface{}{
"bar", "value1", "value2", "foo1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sql_parser

import (
"fmt"
"github.com/openshift-online/ocm-common/pkg/utils/string_scanner"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_scanner"
)

const (
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *scanner) Init(txt string) {
}
currentTokenType = LITERAL
tokens = append(tokens, string_scanner.Token{TokenType: LITERAL, Value: `\`, Position: i})
case currentChar == '-', currentChar == '=', currentChar == '<', currentChar == '>':
case currentChar == '@', currentChar == '-', currentChar == '=', currentChar == '<', currentChar == '>':
// found op Token
if currentTokenType != NO_TOKEN && currentTokenType != OP {
sendCurrentTokens()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sql_parser
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/openshift-online/ocm-common/pkg/utils/string_scanner"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_scanner"
)

var _ = Describe("SQL String Scanner", func() {
Expand Down Expand Up @@ -151,6 +151,18 @@ var _ = Describe("SQL String Scanner", func() {
makeToken(QUOTED_LITERAL, "'bar'", 87),
},
),
Entry("SQL with JSONB contains token",
`resources.payload -> 'data' -> 'manifests' @> '[{"metadata":{"labels":{"foo":"bar"}}}]'`,
[]string_scanner.Token{
makeToken(LITERAL, "resources.payload", 0),
makeToken(OP, "->", 18),
makeToken(QUOTED_LITERAL, "'data'", 21),
makeToken(OP, "->", 28),
makeToken(QUOTED_LITERAL, "'manifests'", 31),
makeToken(OP, "@>", 43),
makeToken(QUOTED_LITERAL, `'[{"metadata":{"labels":{"foo":"bar"}}}]'`, 46),
},
),
)

DescribeTable("peeking", func(scanner string_scanner.Scanner, wantedBool bool, wantedResult *string_scanner.Token) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package string_parser

import "github.com/openshift-online/ocm-common/pkg/utils/state_machine"
import "github.com/openshift-online/ocm-common/pkg/utils/parser/state_machine"

type TokenDefinition = state_machine.StateDefinition[string, string]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package string_parser

import (
"fmt"
"github.com/openshift-online/ocm-common/pkg/utils/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/string_scanner"
"github.com/openshift-online/ocm-common/pkg/utils/parser/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_scanner"
)

type StringParser struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package string_parser

import (
"github.com/openshift-online/ocm-common/pkg/utils/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/string_scanner"
"github.com/openshift-online/ocm-common/pkg/utils/parser/state_machine"
"github.com/openshift-online/ocm-common/pkg/utils/parser/string_scanner"
)

type StringParserBuilder struct {
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 177492f

Please sign in to comment.