Skip to content

Commit

Permalink
Fixed handling of whitespace in sql line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Oct 19, 2021
1 parent a5e62c9 commit 49f7208
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ compileStatement AddConstraint { tableName, constraintName, constraint } = "ALTE
compileStatement AddColumn { tableName, column } = "ALTER TABLE " <> compileIdentifier tableName <> " ADD COLUMN " <> (compileColumn (PrimaryKeyConstraint []) column) <> ";"
compileStatement DropColumn { tableName, columnName } = "ALTER TABLE " <> compileIdentifier tableName <> " DROP COLUMN " <> compileIdentifier columnName <> ";"
compileStatement DropTable { tableName } = "DROP TABLE " <> compileIdentifier tableName <> ";"
compileStatement Comment { content } = "-- " <> content
compileStatement Comment { content } = "--" <> content
compileStatement CreateIndex { indexName, unique, tableName, expressions, whereClause } = "CREATE" <> (if unique then " UNIQUE " else " ") <> "INDEX " <> indexName <> " ON " <> tableName <> " (" <> (intercalate ", " (map compileExpression expressions)) <> ")" <> (case whereClause of Just expression -> " WHERE " <> compileExpression expression; Nothing -> "") <> ";"
compileStatement CreateFunction { functionName, functionBody, orReplace, returns, language } = "CREATE " <> (if orReplace then "OR REPLACE " else "") <> "FUNCTION " <> functionName <> "() RETURNS " <> compilePostgresType returns <> " AS $$" <> functionBody <> "$$ language " <> language <> ";"
compileStatement EnableRowLevelSecurity { tableName } = "ALTER TABLE " <> tableName <> " ENABLE ROW LEVEL SECURITY;"
Expand Down
2 changes: 1 addition & 1 deletion IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ identifier = do
pure i

comment = do
lexeme "--" <?> "Line comment"
(char '-' >> char '-') <?> "Line comment"
content <- takeWhileP Nothing (/= '\n')
pure Comment { content }

Expand Down
5 changes: 4 additions & 1 deletion Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ tests = do
compileSql [CreateExtension { name = "uuid-ossp", ifNotExists = True }] `shouldBe` "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\n"

it "should compile a line comment" do
compileSql [Comment { content = "Comment value" }] `shouldBe` "-- Comment value\n"
compileSql [Comment { content = " Comment value" }] `shouldBe` "-- Comment value\n"

it "should compile a empty line comments" do
compileSql [Comment { content = "" }, Comment { content = "" }] `shouldBe` "--\n--\n"

it "should compile a CREATE TABLE with columns" do
let sql = cs [plain|CREATE TABLE users (
Expand Down
5 changes: 4 additions & 1 deletion Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ tests = do
parseSql "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" `shouldBe` CreateExtension { name = "uuid-ossp", ifNotExists = True }

it "should parse a line comment" do
parseSql "-- Comment value" `shouldBe` Comment { content = "Comment value" }
parseSql "-- Comment value" `shouldBe` Comment { content = " Comment value" }

it "should parse an empty comment" do
parseSqlStatements "--\n--" `shouldBe` [ Comment { content = "" }, Comment { content = "" } ]

it "should parse a CREATE TABLE with columns" do
let sql = cs [plain|CREATE TABLE users (
Expand Down

0 comments on commit 49f7208

Please sign in to comment.