Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-223] prepared statement getParameterCount return good value
  • Loading branch information
rusher committed Nov 22, 2015
1 parent 5432632 commit 76cfdfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
38 changes: 18 additions & 20 deletions src/main/java/org/mariadb/jdbc/MariaDbParameterMetaData.java
Expand Up @@ -50,7 +50,6 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

package org.mariadb.jdbc;

import org.mariadb.jdbc.internal.util.constant.ColumnFlags;
import org.mariadb.jdbc.internal.packet.dao.ColumnInformation;
import org.mariadb.jdbc.internal.MariaDbType;

Expand All @@ -61,67 +60,66 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
* Very basic info about the parameterized query, only reliable method is getParameterCount().
*/
public class MariaDbParameterMetaData implements ParameterMetaData {
private final ColumnInformation[] columnInformations;
private final ColumnInformation[] parametersInformation;

public MariaDbParameterMetaData(ColumnInformation[] columnInformations) {
this.columnInformations = columnInformations;
public MariaDbParameterMetaData(ColumnInformation[] parametersInformation) {
this.parametersInformation = parametersInformation;
}

@Override
public int getParameterCount() throws SQLException {
return columnInformations.length;
return parametersInformation.length;
}

private ColumnInformation getColumnInformation(int column) throws SQLException {
if (column >= 1 && column <= columnInformations.length) {
return columnInformations[column - 1];
private ColumnInformation getParameterInformation(int param) throws SQLException {
if (param >= 1 && param <= parametersInformation.length) {
return parametersInformation[param - 1];
}
throw new SQLException("Parameter metadata out of range : param was " + column + " and must be 1 <= param <=" + columnInformations.length,
throw new SQLException("Parameter metadata out of range : param was " + param + " and must be 1 <= param <=" + parametersInformation.length,
"22003");
}

@Override
public int isNullable(final int param) throws SQLException {
if ((getColumnInformation(param).getFlags() & ColumnFlags.NOT_NULL) == 0) {
return ParameterMetaData.parameterNullable;
} else {
if (getParameterInformation(param).isNotNull()) {
return ParameterMetaData.parameterNoNulls;
} else {
return ParameterMetaData.parameterNullable;
}
}

@Override
public boolean isSigned(int param) throws SQLException {
return getColumnInformation(param).isSigned();
return getParameterInformation(param).isSigned();
}

@Override
public int getPrecision(int param) throws SQLException {
//TODO check real length (with numeric)
long length = getColumnInformation(param).getLength();
long length = getParameterInformation(param).getLength();
return (length > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) length;
}

@Override
public int getScale(int param) throws SQLException {
if (MariaDbType.isNumeric(getColumnInformation(param).getType())) {
return getColumnInformation(param).getDecimals();
if (MariaDbType.isNumeric(getParameterInformation(param).getType())) {
return getParameterInformation(param).getDecimals();
}
return 0;
}

@Override
public int getParameterType(int param) throws SQLException {
return getColumnInformation(param).getType().getSqlType();
return getParameterInformation(param).getType().getSqlType();
}

@Override
public String getParameterTypeName(int param) throws SQLException {
return getColumnInformation(param).getType().getTypeName();
return getParameterInformation(param).getType().getTypeName();
}

@Override
public String getParameterClassName(int param) throws SQLException {
return getColumnInformation(param).getType().getClassName();
return getParameterInformation(param).getType().getClassName();
}

@Override
Expand Down
Expand Up @@ -107,7 +107,7 @@ private void prepare(String sql) throws SQLException {
returnTableAlias = protocol.getOptions().useOldAliasMetadataBehavior;
metadata = new MariaDbResultSetMetaData(prepareResult.columns,
protocol.getDataTypeMappingFlags(), returnTableAlias);
parameterMetaData = new MariaDbParameterMetaData(prepareResult.columns);
parameterMetaData = new MariaDbParameterMetaData(prepareResult.parameters);
} catch (QueryException e) {
try {
this.close();
Expand Down
12 changes: 9 additions & 3 deletions src/test/java/org/mariadb/jdbc/ServerPrepareStatementTest.java
Expand Up @@ -35,17 +35,22 @@ public static void initClass() throws SQLException {
createTable("ServerPrepareStatementCacheSize3", "id int not null primary key auto_increment, test boolean");
createTable("ServerPrepareStatementCacheSize", "id int not null primary key auto_increment, test int");
createTable("preparetestFactionnal", "time0 TIME(6) default '22:11:00'");

createTable("ServerPrepareStatementCacheSize2", "id int not null primary key auto_increment, test boolean");
createTable("ServerPrepareStatementCacheSize3", "id int not null primary key auto_increment, test blob");
createTable("ServerPrepareStatementParameters", "id int, id2 int");
createTable("ServerPrepareStatementCacheSize4", "id int not null primary key auto_increment, test LONGBLOB",
"ROW_FORMAT=COMPRESSED ENGINE=INNODB");

createTable("streamtest2", "id int primary key not null, strm text");

createTable("testServerPrepareMeta", "id int not null primary key auto_increment, id2 int not null, id3 DEC(4,2), id4 BIGINT UNSIGNED ");
}

@Test
public void testServerPrepareMeta() throws Throwable {
PreparedStatement ps = sharedConnection.prepareStatement(
"INSERT INTO testServerPrepareMeta(id2, id3, id4) VALUES (?, ?, ?)");
ParameterMetaData meta = ps.getParameterMetaData();
assertEquals(3, meta.getParameterCount());
}

@Test
public void serverExecutionTest() throws SQLException {
Expand Down Expand Up @@ -532,6 +537,7 @@ public void directExecuteNumber() throws Throwable {
assertEquals(rs.getInt(1), 1);
}


@Test
public void dataConformityTest2() throws SQLException {
prepareTestTable();
Expand Down

0 comments on commit 76cfdfe

Please sign in to comment.