use of net.sf.jsqlparser.schema.Column in project spanner-jdbc by olavloite.
the class CloudSpannerResultSetMetaData method registerAllTableColumns.
private void registerAllTableColumns(Table table) {
String schema = Strings.isNullOrEmpty(table.getSchemaName()) ? "" : CloudSpannerDriver.unquoteIdentifier(table.getSchemaName());
String tableName = CloudSpannerDriver.unquoteIdentifier(table.getName());
try (java.sql.ResultSet rs = statement.getConnection().getMetaData().getColumns("", schema, tableName, null)) {
while (rs.next()) {
registerColumn(new Column(table, rs.getString("COLUMN_NAME")));
}
} catch (SQLException e) {
throw new ParseException(e);
}
}
use of net.sf.jsqlparser.schema.Column in project spanner-jdbc by olavloite.
the class AbstractCloudSpannerStatement method createInsertSelectOnDuplicateKeyUpdateStatement.
/**
* Transform the given UPDATE-statement into an "INSERT INTO TAB1 (...) SELECT ... FROM TAB1 WHERE
* ... ON DUPLICATE KEY UPDATE"
*
* @param update The UPDATE-statement
* @return An SQL-statement equal to the UPDATE-statement but in INSERT form
* @throws SQLException if a database exception occurs while getting the table meta data or if the
* statement tries to update the primary key value
*/
protected String createInsertSelectOnDuplicateKeyUpdateStatement(Update update) throws SQLException {
String tableName = unquoteIdentifier(update.getTables().get(0).getName());
TableKeyMetaData table = getConnection().getTable(tableName);
List<String> keyColumns = table.getKeyColumns();
List<String> updateColumns = update.getColumns().stream().map(Column::getColumnName).map(String::toUpperCase).collect(Collectors.toList());
List<String> quotedKeyColumns = keyColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
List<String> quotedAndQualifiedKeyColumns = keyColumns.stream().map(x -> quoteIdentifier(tableName) + "." + quoteIdentifier(x)).collect(Collectors.toList());
List<String> quotedUpdateColumns = updateColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
List<String> expressions = update.getExpressions().stream().map(Object::toString).collect(Collectors.toList());
if (updateColumns.stream().anyMatch(keyColumns::contains)) {
String invalidCols = updateColumns.stream().filter(keyColumns::contains).collect(Collectors.joining());
throw new CloudSpannerSQLException("UPDATE of a primary key value is not allowed, cannot UPDATE the column(s) " + invalidCols, Code.INVALID_ARGUMENT);
}
StringBuilder res = new StringBuilder();
res.append("INSERT INTO ").append(quoteIdentifier(tableName)).append("\n(");
res.append(String.join(", ", quotedKeyColumns)).append(", ");
res.append(String.join(", ", quotedUpdateColumns)).append(")");
res.append("\nSELECT ").append(String.join(", ", quotedAndQualifiedKeyColumns)).append(", ");
res.append(String.join(", ", expressions));
res.append("\nFROM ").append(quoteIdentifier(tableName));
if (update.getWhere() != null)
res.append("\n").append("WHERE ").append(update.getWhere().toString());
res.append("\nON DUPLICATE KEY UPDATE");
return res.toString();
}
use of net.sf.jsqlparser.schema.Column in project dbeaver by serge-rider.
the class SQLSemanticProcessor method getOrderConstraintExpression.
private static Expression getOrderConstraintExpression(DBRProgressMonitor monitor, DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co, boolean forceNumeric) throws JSQLParserException, DBException {
Expression orderExpr;
String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttributeName());
if (forceNumeric || attrName.isEmpty()) {
orderExpr = new LongValue(co.getOrderPosition());
} else if (CommonUtils.isJavaIdentifier(attrName)) {
// Use column table only if there are multiple source tables (joins)
Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
if (!isValidTableColumn(monitor, dataSource, orderTable, co)) {
orderTable = null;
}
orderExpr = new Column(orderTable, attrName);
} else {
// TODO: set tableAlias for all column references in expression
orderExpr = CCJSqlParserUtil.parseExpression(attrName);
// orderExpr = new CustomExpression(attrName);
// orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
}
return orderExpr;
}
Aggregations