Permalink
Browse files

use binary trick for tolower, optimize switch, added tests

  • Loading branch information...
davecramer committed Apr 12, 2016
1 parent 4ddb693 commit 2200a4d5877aa805c89910ef62b47c076c8a2825
@@ -145,13 +145,15 @@
}
break;

case 'd':
case 'D':
if (Parser.parseDeleteKeyword(aChars, i)) {
current = DMLCommandType.DELETE;
i += 5;
}
break;

case 'i':
case 'I':
if (Parser.parseInsertKeyword(aChars, i)) {
if ( !isInsertPresent && (nativeQueries == null ? true : nativeQueries.size() == 0)) {
@@ -165,13 +167,15 @@
}
break;

case 'm':
case 'M':
if (Parser.parseMoveKeyword(aChars, i)) {
current = DMLCommandType.MOVE;
i += 3;
}
break;

case 'r':
case 'R':
// exclude re-write of insert statements with RETURNING keyword
isReturningPresent = Parser.parseReturningKeyword(aChars, i);
@@ -180,13 +184,15 @@
}
break;

case 'u':
case 'U':
if (Parser.parseUpdateKeyword(aChars, i)) {
current = DMLCommandType.UPDATE;
i += 5;
}
break;

case 'v':
case 'V':
if (Parser.parseValuesKeyword(aChars, i)) {
afterValuesParens = 0 ;
@@ -421,12 +427,12 @@ public static boolean parseDeleteKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset + 1]) == 'D'
&& Character.toUpperCase(query[offset + 1]) == 'E'
&& Character.toUpperCase(query[offset + 2]) == 'L'
&& Character.toUpperCase(query[offset + 3]) == 'E'
&& Character.toUpperCase(query[offset + 4]) == 'T'
&& Character.toUpperCase(query[offset + 5]) == 'E';
return Character.toUpperCase(query[offset + 1]) == 'd'
&& Character.toUpperCase(query[offset + 1]) == 'e'
&& Character.toUpperCase(query[offset + 2]) == 'l'
&& Character.toUpperCase(query[offset + 3]) == 'e'
&& Character.toUpperCase(query[offset + 4]) == 't'
&& Character.toUpperCase(query[offset + 5]) == 'e';
}

/**
@@ -440,12 +446,12 @@ public static boolean parseInsertKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset]) == 'I'
&& Character.toUpperCase(query[offset + 1]) == 'N'
&& Character.toUpperCase(query[offset + 2]) == 'S'
&& Character.toUpperCase(query[offset + 3]) == 'E'
&& Character.toUpperCase(query[offset + 4]) == 'R'
&& Character.toUpperCase(query[offset + 5]) == 'T';
return (query[offset] | 32) == 'i'
&& (query[offset + 1] | 32) == 'n'
&& (query[offset + 2] | 32) == 's'
&& (query[offset + 3] | 32) == 'e'
&& (query[offset + 4] | 32) == 'r'
&& (query[offset + 5] | 32) == 't';
}

/**
@@ -459,10 +465,10 @@ public static boolean parseMoveKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset + 1]) == 'M'
&& Character.toUpperCase(query[offset + 1]) == 'O'
&& Character.toUpperCase(query[offset + 2]) == 'V'
&& Character.toUpperCase(query[offset + 3]) == 'E';
return Character.toUpperCase(query[offset + 1]) == 'm'
&& Character.toUpperCase(query[offset + 1]) == 'o'
&& Character.toUpperCase(query[offset + 2]) == 'v'
&& Character.toUpperCase(query[offset + 3]) == 'e';
}

/**
@@ -476,15 +482,15 @@ public static boolean parseReturningKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset]) == 'R'
&& Character.toUpperCase(query[offset + 1]) == 'E'
&& Character.toUpperCase(query[offset + 2]) == 'T'
&& Character.toUpperCase(query[offset + 3]) == 'U'
&& Character.toUpperCase(query[offset + 4]) == 'R'
&& Character.toUpperCase(query[offset + 5]) == 'N'
&& Character.toUpperCase(query[offset + 6]) == 'I'
&& Character.toUpperCase(query[offset + 7]) == 'N'
&& Character.toUpperCase(query[offset + 8]) == 'G';
return (query[offset] | 32) == 'r'
&& (query[offset + 1] | 32) == 'e'
&& (query[offset + 2] | 32) == 't'
&& (query[offset + 3] | 32) == 'u'
&& (query[offset + 4] | 32) == 'r'
&& (query[offset + 5] | 32) == 'n'
&& (query[offset + 6] | 32) == 'i'
&& (query[offset + 7] | 32) == 'n'
&& (query[offset + 8] | 32) == 'g';
}

/**
@@ -498,12 +504,12 @@ public static boolean parseUpdateKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset + 1]) == 'U'
&& Character.toUpperCase(query[offset + 1]) == 'P'
&& Character.toUpperCase(query[offset + 2]) == 'D'
&& Character.toUpperCase(query[offset + 3]) == 'A'
&& Character.toUpperCase(query[offset + 4]) == 'T'
&& Character.toUpperCase(query[offset + 5]) == 'E';
return Character.toUpperCase(query[offset + 1]) == 'u'
&& Character.toUpperCase(query[offset + 1]) == 'p'
&& Character.toUpperCase(query[offset + 2]) == 'd'
&& Character.toUpperCase(query[offset + 3]) == 'a'
&& Character.toUpperCase(query[offset + 4]) == 't'
&& Character.toUpperCase(query[offset + 5]) == 'e';
}

/**
@@ -517,12 +523,12 @@ public static boolean parseValuesKeyword(final char[] query, int offset) {
return false;
}

return Character.toUpperCase(query[offset]) == 'V'
&& Character.toUpperCase(query[offset + 1]) == 'A'
&& Character.toUpperCase(query[offset + 2]) == 'L'
&& Character.toUpperCase(query[offset + 3]) == 'U'
&& Character.toUpperCase(query[offset + 4]) == 'E'
&& Character.toUpperCase(query[offset + 5]) == 'S';
return (query[offset] | 32) == 'v'
&& (query[offset + 1] | 32) == 'a'
&& (query[offset + 2] | 32) == 'l'
&& (query[offset + 3] | 32) == 'u'
&& (query[offset + 4] | 32) == 'e'
&& (query[offset + 5] | 32) == 's';
}

/**
@@ -84,24 +84,26 @@ public void testHasReturning() {

queries = Parser.parseJdbcSql("select 1 as returning", true, true, false, true, true);
query = queries.get(0);
assertFalse("This is not an insert command", query.command.getType()== DMLCommandType.INSERT);
assertFalse("This is not an insert command", query.command.getType() == DMLCommandType.INSERT);
assertTrue("Returning is OK here as it is not an insert command ", query.command.isReturningKeywordPresent());

}

public void testIsInsert() {

List<NativeQuery> queries = Parser.parseJdbcSql("insert into foo (a,b,c) values (?,?,?) returning a", true, true, false, true, true);
NativeQuery query = queries.get(0);
assertTrue("This is an insert command", query.command.getType()== DMLCommandType.INSERT);
assertTrue("This is an insert command", query.command.getType() == DMLCommandType.INSERT);

queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true, true);
query = queries.get(0);
assertFalse("This is an insert command", query.command.getType()== DMLCommandType.INSERT);
assertFalse("This is an insert command", query.command.getType() == DMLCommandType.INSERT);

queries = Parser.parseJdbcSql("select 1 as insert", true, true, false, true, true);
query = queries.get(0);
assertFalse("This is not insert command", query.command.getType()== DMLCommandType.INSERT);
assertFalse("This is not insert command", query.command.getType() == DMLCommandType.INSERT);
}

public void testMultipleEmptyQueries() {
assertEquals("select 1;/*cut*/\n" + "select 2",
reparse("select 1; ;\t;select 2", true, false, true));

0 comments on commit 2200a4d

Please sign in to comment.