com.datastax.data.dataset.provider.sql.SQLCommand Maven / Gradle / Ivy
The newest version!
package com.datastax.data.dataset.provider.sql;
import com.datastax.data.dataset.DataColumn;
import com.datastax.data.dataset.DataRow;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLCommand extends AbstractSqlCommand {
private static final Logger LOG = Logger.getLogger(SQLCommand.class.getName());
private String deleteSql;
private String insertSql;
private String selectSql;
private String updateSql;
public SQLCommand() {
}
public void setSelectSQL(String sql) {
if (this.selectSql != sql) {
String oldValue = this.selectSql;
this.selectSql = sql;
firePropertyChange("selectSql", oldValue, sql);
}
}
public String getSelectSQL() {
return selectSql;
}
public void setUpdateSQL(String sql) {
if (this.updateSql != sql) {
String oldValue = this.updateSql;
this.updateSql = sql;
firePropertyChange("updateSql", oldValue, sql);
}
}
public String getUpdateSQL() {
return updateSql;
}
public void setInsertSQL(String sql) {
if (this.insertSql != sql) {
String oldValue = this.insertSql;
this.insertSql = sql;
firePropertyChange("insertSql", oldValue, sql);
}
}
public String getInsertSQL() {
return insertSql;
}
public void setDeleteSQL(String sql) {
if (this.deleteSql != sql) {
String oldValue = this.deleteSql;
this.deleteSql = sql;
firePropertyChange("deleteSql", oldValue, sql);
}
}
public String getDeleteSQL() {
return deleteSql;
}
public String[] getParameterNames() {
return super.getParameterNames(new String[]{selectSql, updateSql, insertSql, deleteSql});
}
protected PreparedStatement createPreparedStatement(String parameterizedSql, JDBCDataConnection conn) throws Exception {
Map> indexes = new HashMap>();
String sql = constructSql(parameterizedSql, indexes);
PreparedStatement ps = conn.prepareStatement(sql);
for (String paramName : getParameterNames()) {
List list = indexes.get(paramName);
if (list != null) {
for (int index : list) {
ps.setObject(index + 1, getParameter(paramName));
}
}
}
return ps;
}
protected PreparedStatement getSelectStatement(JDBCDataConnection conn) throws Exception {
if (selectSql == null) {
throw new Exception("SQLCommand not configured with a select sql statement");
}
try {
return createPreparedStatement(selectSql, conn);
} catch (Exception e) {
LOG.log(Level.WARNING, "Problem with select SQL statement {0}", deleteSql);
LOG.log(Level.WARNING, e.getMessage(), e);
return null;
}
}
protected PreparedStatement getUpdateStatement(JDBCDataConnection conn, DataRow row) throws Exception {
if (updateSql == null) {
throw new Exception("SQLCommand not configured with an update sql statement");
}
try {
Map values = new HashMap();
List columns = row.getTable().getColumns();
for (int i=0; i values = new HashMap();
for (DataColumn col : row.getTable().getColumns()) {
values.put(col.getName(), row.getValue(col));
}
return super.prepareStatement(insertSql, values, conn);
} catch (Exception e) {
LOG.log(Level.WARNING, "Problem with insert SQL statement {0}", deleteSql);
LOG.log(Level.WARNING, e.getMessage(), e);
return null;
}
}
protected PreparedStatement getDeleteStatement(JDBCDataConnection conn, DataRow row) throws Exception {
if (deleteSql == null) {
throw new Exception("SQLCommand not configured with a delete sql statement");
}
try {
Map values = new HashMap();
List columns = row.getTable().getColumns();
for (int i=0; i