Skip to content

Commit

Permalink
dbcsql v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ghpv committed May 1, 2023
1 parent 29900dc commit 7441bb9
Show file tree
Hide file tree
Showing 97 changed files with 375 additions and 188 deletions.
5 changes: 2 additions & 3 deletions core/src/main/antlr4/CommonLex.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ fragment DIGIT: [0-9];
fragment INT: DIGIT+;
fragment EST_CHAR: (CHAR | EST_ONLY_CHAR);

NUMBER: ('-'|'+') NUMBER
| INT
NUMBER: INT
| INT '.' INT?
| '.' INT
;
SINGLE_QUOTE: '\'';
SINGLE_DQUOTE: '"';
STRING: SINGLE_QUOTE (~['])* SINGLE_QUOTE;
STRING: SINGLE_QUOTE ('\\\'' | ~['])* SINGLE_QUOTE;
fragment ID_START: (EST_CHAR | '_');
fragment ID_END: (EST_CHAR | DIGIT | '_');
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/antlr4/RestrictionExpr.g4
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ singleTargetOperators: '='
|'<'
;

multiTargetOperators:'not'? 'in'
multiTargetOperators :'not'? 'in'
;

literalOrVarList: literalOrVar
| '(' literalOrVar (',' literalOrVar)* ')'
;

literalOrVar: symbol=ID // p_car_code
| NUMBER // 5
| ('-'|'+')? NUMBER // 5
| STRING // 'active'
| functionLiteral
| '(' literalOrVar ')'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CommonLex, RestrictionExpr;
// RULES
init: contract;

contract: headerDecl preconditionDecl postconditionDecl ';'?
contract: headerDecl preconditionDecl postconditionDecl commentDecl? ';'?
;

headerDecl: 'operation' func_name=ID argList
Expand All @@ -16,6 +16,9 @@ preconditionDecl: 'preconditions' preconditionList
postconditionDecl: 'postconditions' postconditionList
;

commentDecl: 'comment' STRING
;

argList: '{' (arg ';')* '}'
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import ee.taltech.dbcsql.core.model.dsl.argument.ArgumentDef;
import ee.taltech.dbcsql.core.model.dsl.argument.resolution.ArgumentPostconditionVisitor;
Expand All @@ -19,6 +20,7 @@ public class ContractDef
private List<ArgumentDef> arguments = new LinkedList<>();
private List<PreconditionDef> preconditions = new LinkedList<>();
private List<PostconditionDef> postconditions = new LinkedList<>();
private Optional<String> comment = Optional.empty();

public ContractDef()
{
Expand Down Expand Up @@ -114,6 +116,21 @@ public void resolveArgumentTypes()
}
}

public Optional<String> getComment()
{
return comment;
}

public void setComment(Optional<String> comment)
{
this.comment = comment;
}

public void setComment(String comment)
{
this.setComment(Optional.ofNullable(comment));
}

@Override
public boolean equals(Object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ public BuilderT build()
}
}

@SuppressWarnings("unchecked")
public BuilderT withComment(String comm)
{
this.data.setComment(comm);
return (BuilderT) this;
}

@SuppressWarnings("unchecked")
public UpdatedPostconditionSubBuilder makeUpdatedPostcondition()
{
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/ee/taltech/dbcsql/core/model/sql/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Function
private String returnType = "VOID";
private UniqueValues searchSpace = new UniqueValues();
private boolean securityInvoker;
private String comment;

public Function()
{
Expand Down Expand Up @@ -104,6 +105,16 @@ public boolean getSecurityInvoker()
return this.securityInvoker;
}

public String getComment()
{
return comment;
}

public void setComment(String comment)
{
this.comment = comment;
}

@Override
public boolean equals(Object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public FunctionBuilder withSecurityInvoker(boolean b)
return this;
}

public FunctionBuilder withComment(String comment)
{
this.data.setComment(comment);
return this;
}

public Function build()
{
return this.data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ee.taltech.dbcsql.core.phase.input;

import java.io.InputStream;
import java.util.Locale;

import ee.taltech.dbcsql.core.phase.GenerationContext;
import ee.taltech.dbcsql.core.phase.GenerationContextBaseBuilder;
Expand All @@ -10,6 +11,11 @@

public class InputPhase
{
static
{
Locale.setDefault(new Locale("en", "US"));
System.setProperty("file.encoding", "UTF-8");
}
private GenerationRequestBuilder builder = new GenerationRequestBuilder();

public InputPhase withGenerationContext(GenerationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ee.taltech.dbcsql.core.phase.TranslatorInputException;
import ee.taltech.dbcsql.core.phase.input.ContractBaseListener;
import ee.taltech.dbcsql.core.phase.input.ContractParser.ArgContext;
import ee.taltech.dbcsql.core.phase.input.ContractParser.CommentDeclContext;
import ee.taltech.dbcsql.core.phase.input.ContractParser.ConnectionPreconditionContext;
import ee.taltech.dbcsql.core.phase.input.ContractParser.ContractContext;
import ee.taltech.dbcsql.core.phase.input.ContractParser.DeletedPostconditionContext;
Expand Down Expand Up @@ -470,6 +471,12 @@ public void exitUpdatedPostcondition(UpdatedPostconditionContext ctx)
this.updated = null;
}

@Override
public void enterCommentDecl(CommentDeclContext ctx)
{
this.contract.withComment(ctx.STRING().getText());
}

private VariableDef findVariable(String alias)
{
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,28 @@ private void writeToDB()
.getMemory()
.toString()
;
this.driver.execute(qry);
int start = 0;
int idx = -1;
while (true)
{
idx = qry.indexOf("END;", start);
if (idx != -1)
{
idx += 4;
}
else
{
break;
}
String subqry = qry.substring(start, idx);
start = idx;
this.driver.execute(subqry);
}
String subqry = qry.substring(start);
if (!subqry.strip().equals(""))
{
this.driver.execute(subqry);
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions package/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Combining these 2 definitions the utility then translates a contract into a vali
Only a single contract should be supplied at a time.

Currently, only recent postgres syntax is supported - each operation outputted assumes it will be used in postgres database, in some version that supports having plain SQL operations within the functions.
Additionally, please note that only `UTF-8` encoding is supported, if you are getting weird characters please check whether you are using `UTF-8`.

## Context

Expand Down
2 changes: 1 addition & 1 deletion package/files/run.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@ECHO OFF

.\\windows_jdk\\jdk-17.0.6+10\\bin\\java -jar gui-${revision}-jar-with-dependencies.jar
.\\windows_jdk\\jdk-17.0.6+10\\bin\\java -jar gui-${revision}-jar-with-dependencies.jar -Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8
2 changes: 1 addition & 1 deletion package/files/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

./linux_jdk/jdk-17.0.6+10/bin/java -jar ./gui*.jar
LC_ALL=en_US.UTF-8 ./linux_jdk/jdk-17.0.6+10/bin/java -jar ./gui*.jar
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<packaging>pom</packaging>

<properties>
<revision>1.0.0</revision>
<revision>1.0.1</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static String translateVariableForDeclaration(VariableDef var)
StringBuilder sb = new StringBuilder();
return sb
.append(translateTableName(var.getTable()))
.append(" as ")
.append(" AS ")
.append(var.getAlias())
.toString()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public Function translateContract(ContractDef contract)
.translatePostconditionsToStatementsAndResolveReturnType(contract, func)
.resolveSearchPath(contract, func)
;
if (contract.getComment().isPresent())
{
String comm = contract
.getComment()
.get()
.replaceAll("\\\\'", "''")
;
func.withComment(comm);
}

return func.build();
}
Expand Down
10 changes: 9 additions & 1 deletion process/postgres/src/main/resources/postgres.stg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ LANGUAGE SQL SECURITY $if(d.securityInvoker)$INVOKER$else$DEFINER$endif$
SET SEARCH_PATH TO $d.searchSpace.values:{x |'$x$'};separator=", "$
BEGIN ATOMIC
$d.statements;separator="\n"$
END;>>
END;
$if(d.comment)$
COMMENT ON FUNCTION $d.name$
(
$d.args;separator=",\n"$
)
IS $d.comment$;
$endif$
>>

deleted(d) ::= <<
DELETE FROM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testOutput()
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
DELETE FROM
public.test as a
public.test AS a
WHERE
(
a.id = p_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void testDeleteAllFromModel()
;

String actual = this.generateCondition(condition);
String expected = "DELETE FROM public.test as a ;";
String expected = "DELETE FROM public.test AS a ;";

SQLStatementAssertions.assertStatementsEqual(actual, expected);
}
Expand Down Expand Up @@ -54,7 +54,7 @@ public void testDeleteWithRestrictions()
this.reg.extendRestriction(restriction);

String actual = this.generateCondition(condition);
String expected = "DELETE FROM public.test as a WHERE ( a.id = 5 ) ;";
String expected = "DELETE FROM public.test AS a WHERE ( a.id = 5 ) ;";

SQLStatementAssertions.assertStatementsEqual(actual, expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class ActivateCarTests extends SampleDataTest
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
UPDATE
auto as a
auto AS a
SET
auto_seisundi_liik_kood = new_status.auto_seisundi_liik_kood
FROM
auto_seisundi_liik as old_status,
auto_seisundi_liik as new_status,
auto_kategooria_omamine as cco
auto_seisundi_liik AS old_status,
auto_seisundi_liik AS new_status,
auto_kategooria_omamine AS cco
WHERE
(
a.auto_kood = p_car_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AddCarToCategoryTests extends SampleDataTest
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
INSERT INTO
auto_kategooria_omamine as cco
auto_kategooria_omamine AS cco
(
auto_kood,
auto_kategooria_kood
Expand All @@ -27,9 +27,9 @@ public class AddCarToCategoryTests extends SampleDataTest
a.auto_kood,
ci.auto_kategooria_kood
FROM
auto as a,
auto_kategooria as ci,
auto_seisundi_liik as status
auto AS a,
auto_kategooria AS ci,
auto_seisundi_liik AS status
WHERE
(
a.auto_kood = p_car_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class DeleteFromCategoryTests extends SampleDataTest
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
DELETE FROM
auto_kategooria_omamine as cco
auto_kategooria_omamine AS cco
USING
auto_seisundi_liik as status,
auto as c,
auto_kategooria as category
auto_seisundi_liik AS status,
auto AS c,
auto_kategooria AS category
WHERE
(
status.nimetus in ('Aktiivne', 'Mitteaktiivne')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class FinishCarTests extends SampleDataTest
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
UPDATE
auto as a
auto AS a
SET
auto_seisundi_liik_kood = new_status.auto_seisundi_liik_kood
FROM
auto_seisundi_liik as old_status,
auto_seisundi_liik as new_status
auto_seisundi_liik AS old_status,
auto_seisundi_liik AS new_status
WHERE
(
old_status.nimetus in ('Aktiivne', 'Mitteaktiivne')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class ForgetCarTests extends SampleDataTest
SET SEARCH_PATH TO 'public', 'pg_temp'
BEGIN ATOMIC
DELETE FROM
auto as c
auto AS c
USING
auto_seisundi_liik as status
auto_seisundi_liik AS status
WHERE
(
c.auto_kood = p_car_id
Expand Down
Loading

0 comments on commit 7441bb9

Please sign in to comment.