Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PreparedStatement replacing the parameter placeholders #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,41 @@ public String getSqlWithValues() {
// iterate over the characters in the query replacing the parameter placeholders
// with the actual values
int currentParameter = 0;

// Skip replacing the parameter placeholders
// when the parameter placeholder is singleQuoted or commented.
boolean isSingleQuoted = false;
boolean isSingleCommented = false;
boolean isMultiLineCommented = false;

for( int pos = 0; pos < statementQuery.length(); pos ++) {
char character = statementQuery.charAt(pos);
if( statementQuery.charAt(pos) == '?' && currentParameter <= parameterValues.size()) {

if (character == '\'') {
isSingleQuoted = !isSingleQuoted;
}

if (!isSingleCommented) {
if (character == '-' && (pos + 1 < statementQuery.length()) && statementQuery.charAt(pos + 1) == '-') {
isSingleCommented = true;
}
} else {
if (character == '\n') {
isSingleCommented = false;
}
}

if (!isMultiLineCommented) {
if (character == '/' && (pos + 1 < statementQuery.length()) && statementQuery.charAt(pos + 1) == '*') {
isMultiLineCommented = true;
}
} else {
if (character == '*' && (pos + 1 < statementQuery.length()) && statementQuery.charAt(pos + 1) == '/') {
isMultiLineCommented = false;
}
}

if( statementQuery.charAt(pos) == '?' && currentParameter <= parameterValues.size() && !isSingleQuoted && !isSingleCommented && !isMultiLineCommented) {
// replace with parameter value
Value value = parameterValues.get(currentParameter);
sb.append(value != null ? value.toString() : new Value().toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.p6spy.engine.common;

import com.p6spy.engine.spy.P6SpyDriver;
import org.junit.Test;

import java.sql.Driver;

import static org.junit.Assert.assertEquals;

public class PreparedStatementInformationTest {

@Test
public void getSqlWithValues() {
Driver driver = new P6SpyDriver();
StringBuilder sql = new StringBuilder();
sql.append("select \n");
sql.append("1 AS COL1 -- test? \n");
sql.append(", 2 AS COL2 /* test? */ \n");
sql.append(" /* test multiline comment \n");
sql.append(" ?? \n");
sql.append(" */ \n");
sql.append(" , '?' AS QUESTION \n");
sql.append("WHERE testParam1 = ? \n");
sql.append("AND testParam2 = ? \n");
PreparedStatementInformation preparedStatementInformation = new PreparedStatementInformation(ConnectionInformation.fromDriver(driver), sql.toString());
preparedStatementInformation.setParameterValue(1, "testParam1");
preparedStatementInformation.setParameterValue(2, "testParam2");

StringBuilder expectedValue = new StringBuilder();
expectedValue.append("select \n");
expectedValue.append("1 AS COL1 -- test? \n");
expectedValue.append(", 2 AS COL2 /* test? */ \n");
expectedValue.append(" /* test multiline comment \n");
expectedValue.append(" ?? \n");
expectedValue.append(" */ \n");
expectedValue.append(" , '?' AS QUESTION \n");
expectedValue.append("WHERE testParam1 = 'testParam1' \n");
expectedValue.append("AND testParam2 = 'testParam2' \n");

assertEquals(expectedValue.toString(), preparedStatementInformation.getSqlWithValues());
}
}