Skip to content

Commit

Permalink
extends support for PreparedStatements #5797
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrank committed Mar 14, 2016
1 parent b1c4202 commit 764e2b1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,24 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
}

public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
throw new SQLFeatureNotSupportedException();
return new OrientJdbcPreparedStatement(this, sql);
}

public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
throw new SQLFeatureNotSupportedException();
return new OrientJdbcPreparedStatement(this, sql);
}

public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
throw new SQLFeatureNotSupportedException();
return new OrientJdbcPreparedStatement(this, sql);
}

public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
throw new SQLFeatureNotSupportedException();
return new OrientJdbcPreparedStatement(this, resultSetType, resultSetConcurrency, sql);
}

public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new SQLFeatureNotSupportedException();
return new OrientJdbcPreparedStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability, sql);
}

public void releaseSavepoint(Savepoint savepoint) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.sql.ParameterMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -41,8 +42,8 @@ static class ParameterDefinition {

private final List<ParameterDefinition> definitions;

public OrientJdbcParameterMetadata(List<ParameterDefinition> definitions) {
this.definitions = definitions;
public OrientJdbcParameterMetadata() {
this.definitions = new ArrayList<ParameterDefinition>();
}

@Override
Expand Down Expand Up @@ -114,4 +115,7 @@ private void checkIndex(int index) throws SQLException {
}
}

public boolean add(ParameterDefinition parameterDefinition) {
return definitions.add(parameterDefinition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ORecordBytes;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.jdbc.OrientJdbcParameterMetadata.ParameterDefinition;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -40,11 +41,21 @@
*/
public class OrientJdbcPreparedStatement extends OrientJdbcStatement implements PreparedStatement {

private final String sql;
private final Map<Integer, Object> params;
protected final String sql;
protected final Map<Integer, Object> params;

public OrientJdbcPreparedStatement(OrientJdbcConnection iConnection, String sql) {
super(iConnection);
this(iConnection, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, sql);
}

public OrientJdbcPreparedStatement(OrientJdbcConnection iConnection, int resultSetType, int resultSetConcurrency, String sql)
throws SQLException {
this(iConnection, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT, sql);
}

public OrientJdbcPreparedStatement(OrientJdbcConnection iConnection, int resultSetType, int resultSetConcurrency,
int resultSetHoldability, String sql) {
super(iConnection, resultSetType, resultSetConcurrency, resultSetHoldability);
this.sql = sql;
params = new HashMap<Integer, Object>();
}
Expand Down Expand Up @@ -214,20 +225,22 @@ public void setURL(int parameterIndex, URL x) throws SQLException {
params.put(parameterIndex, null);
}

@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
final List<OrientJdbcParameterMetadata.ParameterDefinition> definitions = new ArrayList<OrientJdbcParameterMetadata.ParameterDefinition>();

OrientJdbcParameterMetadata parameterMetadata = new OrientJdbcParameterMetadata();
int start = 0;
int index = sql.indexOf('?', start);
while (index > 0) {
final OrientJdbcParameterMetadata.ParameterDefinition def = new OrientJdbcParameterMetadata.ParameterDefinition();
final ParameterDefinition def = new ParameterDefinition();
// TODO find a way to know a bit more on each parameter
definitions.add(def);

parameterMetadata.add(def);
start = index + 1;
index = sql.indexOf('?', start);
}

return new OrientJdbcParameterMetadata(definitions);
return parameterMetadata;
}

public void setRowId(int parameterIndex, RowId x) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.orientechnologies.orient.jdbc;

import org.hamcrest.Matchers;
import org.junit.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;


public class OrientJdbcPreparedStatementTest extends OrientJdbcBaseTest {

Expand Down Expand Up @@ -117,4 +122,21 @@ public void shouldExecutePreparedStatementWithExecuteMethod() throws Exception {
ResultSet resultSet = conn.createStatement().executeQuery("SELECT count(*) FROM insertable WHERE id = 'someRandomUid'");
assertThat(resultSet.getInt(1), equalTo(1));
}

@Test
public void shouldCreatePreparedStatementWithExtendConstructor() throws Exception {

PreparedStatement stmt = conn.prepareStatement("SELECT FROM Item WHERE intKey = ?", TYPE_FORWARD_ONLY, CONCUR_READ_ONLY);
stmt.setInt(1, 1);

ResultSet rs = stmt.executeQuery();

assertThat(rs.next(), is(true));

assertThat(rs.getString("@class"), Matchers.equalToIgnoringCase("Item"));

assertThat(rs.getString("stringKey"), Matchers.equalTo("1"));
assertThat(rs.getInt("intKey"), Matchers.is(1));
//
}
}

0 comments on commit 764e2b1

Please sign in to comment.