Skip to content

Commit

Permalink
[CONJ-438] insert query that contain table / column that contain SELE…
Browse files Browse the repository at this point in the history
…CT keyword can be rewritten - part 2
  • Loading branch information
rusher committed Mar 8, 2017
1 parent ec288bd commit 615e432
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Expand Up @@ -54,6 +54,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.util.List;

public class ClientPrepareResult implements PrepareResult {

private String sql;
private List<byte[]> queryParts;
private boolean isQueryMultiValuesRewritable = true;
Expand Down Expand Up @@ -497,9 +498,9 @@ public static ClientPrepareResult rewritableParts(String queryString, boolean no
&& (query[i + 4] == 'c' || query[i + 4] == 'C')
&& (query[i + 5] == 't' || query[i + 5] == 'T')) {

// field/table name might contain 'select'
if (i > 0 && (query[i - 1] > ' ' && query[i - 1] != '(' && query[i - 1] != ')')) break;
if ((query[i + 6] > ' ' && query[i + 6] != '(' && query[i + 6] != ')')) break;
//field/table name might contain 'select'
if (i > 0 && (query[i - 1] > ' ' && "();><=-+,".indexOf(query[i - 1]) == -1)) break;
if (query[i + 6] > ' ' && "();><=-+,".indexOf(query[i + 6]) == -1) break;

//SELECT queries, INSERT FROM SELECT not rewritable
reWritablePrepare = false;
Expand Down
Expand Up @@ -14,6 +14,8 @@ public void selectQuery() {
//SELECT query cannot be rewritable
assertFalse(ClientPrepareResult.rewritableParts("SELECT * FROM MyTable", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("SELECT\n * FROM MyTable", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("SELECT(1)", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("INSERT MyTable (a) VALUES (1);SELECT(1)", true).isQueryMultiValuesRewritable());
}

/**
Expand All @@ -22,6 +24,7 @@ public void selectQuery() {
@Test
public void insertSelectQuery() {
assertFalse(ClientPrepareResult.rewritableParts("INSERT INTO MyTable (a) SELECT * FROM seq_1_to_1000", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("INSERT INTO MyTable (a);SELECT * FROM seq_1_to_1000", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("INSERT INTO MyTable (a)SELECT * FROM seq_1_to_1000", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("INSERT INTO MyTable (a) (SELECT * FROM seq_1_to_1000)", true).isQueryMultiValuesRewritable());
assertFalse(ClientPrepareResult.rewritableParts("INSERT INTO MyTable (a) SELECT\n * FROM seq_1_to_1000", true).isQueryMultiValuesRewritable());
Expand Down

0 comments on commit 615e432

Please sign in to comment.