Skip to content

Commit

Permalink
Support session control statements (closes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Dec 6, 2017
1 parent 67afd17 commit c8d749b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
'project:alexandria-plsql-utils/setup/grants.sql':[
5,
],
'project:pljson/install.sql':[
8,
],
'project:utPLSQL/examples/filepath1.pkg':[
104,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.sonar.plugins.plsqlopen.api.DdlGrammar.*;
import static org.sonar.plugins.plsqlopen.api.DmlGrammar.*;
import static org.sonar.plugins.plsqlopen.api.TclGrammar.*;
import static org.sonar.plugins.plsqlopen.api.SessionControlGrammar.SESSION_CONTROL_COMMAND;
import static org.sonar.plugins.plsqlopen.api.PlSqlKeyword.*;
import static org.sonar.plugins.plsqlopen.api.PlSqlPunctuator.*;
import static org.sonar.plugins.plsqlopen.api.PlSqlTokenType.*;
Expand Down Expand Up @@ -211,6 +212,7 @@ public static LexerfulGrammarBuilder create() {
DML_COMMAND,
TCL_COMMAND,
SQLPLUS_COMMAND,
SESSION_CONTROL_COMMAND,
EXECUTE_PLSQL_BUFFER)), EOF);

createLiterals(b);
Expand All @@ -225,6 +227,7 @@ public static LexerfulGrammarBuilder create() {
DclGrammar.buildOn(b);
TclGrammar.buildOn(b);
SqlPlusGrammar.buildOn(b);
SessionControlGrammar.buildOn(b);
SingleRowSqlFunctionsGrammar.buildOn(b);
AggregateSqlFunctionsGrammar.buildOn(b);
ConditionsGrammar.buildOn(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ public enum PlSqlKeyword implements TokenType {
MAP("map"),
CONSTRUCTOR("constructor"),
SELF("self"),
RESULT("result");
RESULT("result"),
SESSION("session"),
ROLE("role"),
NONE("none");

private final String value;
private final boolean reserved;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.sonar.plugins.plsqlopen.api;

import static com.sonar.sslr.api.GenericTokenType.EOF;
import static org.sonar.plugins.plsqlopen.api.PlSqlGrammar.IDENTIFIER_NAME;
import static org.sonar.plugins.plsqlopen.api.PlSqlKeyword.*;
import static org.sonar.plugins.plsqlopen.api.PlSqlPunctuator.*;

import org.sonar.sslr.grammar.GrammarRuleKey;
import org.sonar.sslr.grammar.LexerfulGrammarBuilder;

public enum SessionControlGrammar implements GrammarRuleKey {

ALTER_SESSION,
SET_ROLE,
SESSION_CONTROL_COMMAND;

public static void buildOn(LexerfulGrammarBuilder b) {

b.rule(ALTER_SESSION).is(ALTER, SESSION, b.oneOrMore(b.anyTokenButNot(b.firstOf(SEMICOLON, DIVISION, EOF))));

b.rule(SET_ROLE).is(SET, ROLE,
b.firstOf(
NONE,
b.sequence(ALL, b.optional(EXCEPT, b.oneOrMore(IDENTIFIER_NAME, b.optional(COMMA)))),
b.oneOrMore(IDENTIFIER_NAME, b.optional(IDENTIFIED, BY, b.anyToken()), b.optional(COMMA))
));

b.rule(SESSION_CONTROL_COMMAND).is(b.firstOf(ALTER_SESSION, SET_ROLE), b.optional(SEMICOLON));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.sonar.plugins.plsqlopen.api.session;

import static org.sonar.sslr.tests.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.sonar.plugins.plsqlopen.api.RuleTest;
import org.sonar.plugins.plsqlopen.api.SessionControlGrammar;

public class SessionControlGrammarTest extends RuleTest {

@Before
public void init() {
setRootRule(SessionControlGrammar.SESSION_CONTROL_COMMAND);
}

@Test
public void matchesAlterSession() {
assertThat(p).matches("alter session set nls_date_format = 'dd/mm/yyyy';");
}

@Test
public void matchesSetRole() {
assertThat(p).matches("set role foo;");
assertThat(p).matches("set role foo, bar, baz;");
}

@Test
public void matchesSetRoleNone() {
assertThat(p).matches("set role none;");
}

@Test
public void matchesSetRoleAll() {
assertThat(p).matches("set role all;");
}

@Test
public void matchesSetRoleAllExcept() {
assertThat(p).matches("set role all except foo;");
assertThat(p).matches("set role all except foo, bar, baz;");
}

@Test
public void matchesSetRoleWithPassword() {
assertThat(p).matches("set role foo identified by pass;");
assertThat(p).matches("set role foo identified by pass, bar, baz;");
}

}

0 comments on commit c8d749b

Please sign in to comment.