generated from DbUp/dbup-provider-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #680 PostgreSQL statements split implemented (#19)
* fix: #680 PostgreSQL statements split implemented Co-authored-by: shokurov <egor.shokurov@gmail.com> * Updated approval file --------- Co-authored-by: shokurov <egor.shokurov@gmail.com> Co-authored-by: Robert Wagner <robert@wagner.id.au>
- Loading branch information
1 parent
b33151c
commit 80c9a50
Showing
4 changed files
with
448 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DbUp.Postgresql.Tests; | ||
|
||
public class PostgresqlQueryParserTests | ||
{ | ||
[Theory] | ||
[InlineData("SELECT 1\n;\nSELECT 2", 2, "SELECT 1", "SELECT 2")] | ||
[InlineData(";;SELECT 1", 1, "SELECT 1")] | ||
[InlineData("SELECT 1;", 1, "SELECT 1")] | ||
[InlineData("", 0)] | ||
[InlineData("CREATE OR REPLACE RULE test AS ON UPDATE TO test DO (SELECT 1; SELECT 1)", | ||
1, | ||
"CREATE OR REPLACE RULE test AS ON UPDATE TO test DO (SELECT 1; SELECT 1)")] | ||
[InlineData("CREATE OR REPLACE RULE test AS ON UPDATE TO test DO (SELECT 1); SELECT 2", | ||
2, | ||
"CREATE OR REPLACE RULE test AS ON UPDATE TO test DO (SELECT 1)", "SELECT 2")] | ||
[InlineData("SELECT 1 /* block comment; */", 1, "SELECT 1 /* block comment; */")] | ||
[InlineData( | ||
""" | ||
SELECT 1; | ||
-- Line comment; with semicolon | ||
SELECT 2; | ||
""", 2, | ||
"SELECT 1", | ||
""" | ||
-- Line comment; with semicolon | ||
SELECT 2 | ||
""")] | ||
[InlineData("SELECT 'string with; semicolon'", 1, "SELECT 'string with; semicolon'")] | ||
[InlineData("SELECT 'string with'' quote and; semicolon'", 1, "SELECT 'string with'' quote and; semicolon'")] | ||
[InlineData(""" | ||
CREATE FUNCTION TXT() | ||
LANGUAGE PLPGSQL AS | ||
$BODY$ | ||
BEGIN | ||
SELECT 'string with'' quote and; semicolon'; | ||
END | ||
$BODY$ | ||
""", 1)] | ||
[InlineData("SELECT 1 as \"QUOTED;IDENT\"", 1)] | ||
[InlineData("SELECT E'\\041'; SELECT '1'", 2, "SELECT E'\\041'", "SELECT '1'")] | ||
[InlineData(""" | ||
SELECT 'some' | ||
'text'; | ||
SELECT '1' | ||
""", 2)] | ||
public void split_into_statements(string sql, int statementCount, params string[] expected) | ||
{ | ||
var results = ParseCommand(sql); | ||
Assert.Equal(statementCount, results.Count); | ||
if (expected.Length > 0) | ||
Assert.Equal(expected, results); | ||
} | ||
|
||
[Fact] | ||
public void split_into_statements_non_sql_standard() | ||
{ | ||
const string sql = "SELECT 'string with\\' quote and; semicolon'"; | ||
var results = ParseCommand(sql, false); | ||
Assert.Single(results); | ||
Assert.Equal(sql, results[0]); | ||
} | ||
|
||
private List<string> ParseCommand(string sql) | ||
=> ParseCommand(sql, true); | ||
|
||
private static List<string> ParseCommand(string sql, bool standardConformingStrings) | ||
{ | ||
var manager = new PostgresqlConnectionManager("") { StandardConformingStrings = standardConformingStrings }; | ||
var commands = manager.SplitScriptIntoCommands(sql); | ||
return commands.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.