Skip to content

Commit

Permalink
patched create or replace script pg SQL parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Jul 3, 2019
1 parent e13ad6f commit 211b398
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
28 changes: 24 additions & 4 deletions script/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
createKeyword
beginKeyword
functionKeyword
orKeyword
replaceKeyword
commandEnd
pgDelimiter
plSQLBlock
Expand All @@ -30,9 +32,12 @@ var matchers = map[int]toolbox.Matcher{
plSQLBlock: toolbox.NewBodyMatcher("BEGIN", "END;"),
beginKeyword: toolbox.NewTerminatorMatcher("BEGIN"),
createKeyword: toolbox.NewKeywordsMatcher(false, "create"),
functionKeyword: toolbox.NewKeywordsMatcher(false, "function"),
whitespaces: toolbox.CharactersMatcher{" \n\t"},
lineBreak: toolbox.CharactersMatcher{"\n"},
orKeyword: toolbox.NewKeywordsMatcher(false, "or"),
replaceKeyword: toolbox.NewKeywordsMatcher(false, "replace"),

functionKeyword: toolbox.NewKeywordsMatcher(false, "function"),
whitespaces: toolbox.CharactersMatcher{" \n\t"},
lineBreak: toolbox.CharactersMatcher{"\n"},
}

//ParseWithReader splits SQL blob into separate commands
Expand Down Expand Up @@ -81,9 +86,24 @@ outer:
case createKeyword:
pending += match.Matched
if match := tokenizer.Nexts(whitespaces, eofToken); match.Token == whitespaces {

pending += match.Matched
match := tokenizer.Nexts(functionKeyword, beginKeyword, eofToken)

candidates := []int{orKeyword, whitespaces, replaceKeyword, whitespaces, functionKeyword, beginKeyword, orKeyword}

match := tokenizer.Nexts(candidates...)

for ; len(candidates) > 3; {
if match.Token != candidates[0] {
break
}
pending += match.Matched
candidates = candidates[1:]
match = tokenizer.Nexts(candidates...)
}

switch match.Token {

case functionKeyword:
pending += match.Matched
if match = tokenizer.Nexts(pgDelimiter, eofToken); match.Token == pgDelimiter {
Expand Down
28 changes: 27 additions & 1 deletion script/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ CREATE TRIGGER insert_language BEFORE INSERT
ON languages
FOR EACH ROW
EXECUTE PROCEDURE insert_language_trigger();
CREATE OR REPLACE FUNCTION insert_language_trigger()
RETURNS TRIGGER
LANGUAGE plpgsql AS
$$
BEGIN
NEW.code := '11';
RETURN NEW;
END
$$;
INSERT INTO DUMMY(ID, NAME) VALUES(2, 'xyz');
`,
SQLs: []string{
`CREATE TABLE words(
Expand All @@ -145,7 +162,16 @@ CREATE TRIGGER insert_language BEFORE INSERT
`CREATE TRIGGER insert_language BEFORE INSERT
ON languages
FOR EACH ROW
EXECUTE PROCEDURE insert_language_trigger()`,
EXECUTE PROCEDURE insert_language_trigger()`,`CREATE OR REPLACE FUNCTION insert_language_trigger()
RETURNS TRIGGER
LANGUAGE plpgsql AS
$$
BEGIN
NEW.code := '11';
RETURN NEW;
END
$$`,
`INSERT INTO DUMMY(ID, NAME) VALUES(2, 'xyz')`,
},
},
}
Expand Down

0 comments on commit 211b398

Please sign in to comment.