All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.datastax.data.dataset.provider.sql.TableCommand Maven / Gradle / Ivy
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TableCommand extends SQLCommand {
private static final Logger LOG = Logger.getLogger(TableCommand.class.getName());
private String tableName;
private String whereClause = "";
private String orderByClause = "";
private String havingClause = "";
public TableCommand() {
this(null, null);
}
public TableCommand(String tableName) {
this(tableName, null);
}
public TableCommand(String tableName, String whereClause) {
setTableName(tableName);
setWhereClause(whereClause);
}
public void setTableName(String tableName) {
if (this.tableName != tableName) {
String oldValue = this.tableName;
this.tableName = tableName;
firePropertyChange("tableName", oldValue, tableName);
}
}
public void setWhereClause(String clause) {
if (whereClause != clause) {
String oldValue = this.whereClause;
whereClause = clause == null ? "" : clause;
firePropertyChange("whereClause", oldValue, whereClause);
}
}
public void setOrderByClause(String clause) {
if (orderByClause != clause) {
String oldValue = this.orderByClause;
orderByClause = clause == null ? "" : clause;
firePropertyChange("orderByClause", oldValue, orderByClause);
}
}
public void setHavingClause(String clause) {
if (havingClause != clause) {
String oldValue = this.havingClause;
havingClause = clause == null ? "" : clause;
firePropertyChange("havingClause", oldValue, havingClause);
}
}
public String[] getParameterNames() {
List strings = new ArrayList();
if (getSelectSQL() != null) {
strings.add(getSelectSQL());
} else {
strings.add(whereClause);
strings.add(orderByClause);
strings.add(havingClause);
}
if (getUpdateSQL() != null) {
strings.add(getUpdateSQL());
}
if (getDeleteSQL() != null) {
strings.add(getDeleteSQL());
}
if (getInsertSQL() != null) {
strings.add(getInsertSQL());
}
return super.getParameterNames(strings.toArray(new String[strings.size()]));
}
protected PreparedStatement getSelectStatement(JDBCDataConnection conn) throws Exception {
if (tableName == null) {
throw new Exception("TableCommand not configured with a table name");
}
if (super.getSelectSQL() != null) {
return super.getSelectStatement(conn);
}
String sql = "";
try {
StringBuilder buffer = new StringBuilder();
buffer.append("select * from ");
buffer.append("\"").append(tableName).append("\"");
buffer.append(" ");
buffer.append(whereClause);
buffer.append(" ");
buffer.append(orderByClause);
buffer.append(" ");
buffer.append(havingClause);
sql = buffer.toString().trim();
LOG.log(Level.FINE, "Generated Select SQL: " + sql);
return createPreparedStatement(sql, conn);
} catch (Exception e) {
LOG.log(Level.WARNING, "Problem with creating select SQL statement {0}", sql);
LOG.log(Level.WARNING, e.getMessage(), e);
return null;
}
}
protected PreparedStatement getUpdateStatement(JDBCDataConnection conn, DataRow row) throws Exception {
if (tableName == null) {
throw new Exception("TableCommand not configured with a table name");
}
if (super.getUpdateSQL() != null) {
return super.getUpdateStatement(conn, row);
}
String sql = "";
try {
Map values = new HashMap();
StringBuilder buffer = new StringBuilder();
buffer.append("update ");
buffer.append("\"").append(tableName).append("\"");
buffer.append(" set ");
List columns = getUpdateableColumns(row.getTable().getColumns());
int modCount = 0;
for (int i=0; i values = new HashMap();
StringBuilder buffer = new StringBuilder();
buffer.append("insert into ");
buffer.append("\"").append(tableName).append("\"");
buffer.append("(");
List cols = getUpdateableColumns(row.getTable().getColumns());
for (DataColumn col : cols) {
buffer.append("\"").append(col.getName()).append("\"");
buffer.append(", ");
}
buffer.replace(buffer.length()-2, buffer.length(), ")");
buffer.append(" values(");
for (DataColumn col : cols) {
buffer.append(":" + col.getName() + ", ");
values.put(col.getName(), row.getValue(col));
}
buffer.replace(buffer.length()-2, buffer.length(), ")");
sql = buffer.toString().trim();
LOG.log(Level.FINE, "Generated Insert SQL: " + sql);
return super.prepareStatement(sql, values, conn);
} catch (Exception e) {
LOG.log(Level.WARNING, "Problem with creating insert SQL statement {0}", sql);
LOG.log(Level.WARNING, e.getMessage(), e);
return null;
}
}
protected PreparedStatement getDeleteStatement(JDBCDataConnection conn, DataRow row) throws Exception {
if (tableName == null) {
throw new Exception("TableCommand not configured with a table name");
}
if (super.getDeleteSQL() != null) {
return super.getDeleteStatement(conn, row);
}
String sql = "";
try {
Map values = new HashMap();
StringBuilder buffer = new StringBuilder();
buffer.append("delete from ");
buffer.append("\"").append(tableName).append("\"");
buffer.append(" where ");
int keyColCount = 0;
List columns = row.getTable().getColumns();
for (int i=0; i getUpdateableColumns(List cols) {
List results = new ArrayList();
for (DataColumn col : cols) {
if (col.getExpression() == null || col.getExpression().equals("")) {
results.add(col);
}
}
return results;
}
}