Search in sources :

Example 41 with Column

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);
    }
}
Also used : Column(net.sf.jsqlparser.schema.Column) SQLException(java.sql.SQLException)

Example 42 with Column

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();
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) ReadContext(com.google.cloud.spanner.ReadContext) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DatabaseClient(com.google.cloud.spanner.DatabaseClient) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Column(net.sf.jsqlparser.schema.Column) Update(net.sf.jsqlparser.statement.update.Update) SQLWarning(java.sql.SQLWarning) Code(com.google.rpc.Code) PartitionOptions(com.google.cloud.spanner.PartitionOptions) TransactionContext(com.google.cloud.spanner.TransactionContext) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Table(net.sf.jsqlparser.schema.Table) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) Collectors(java.util.stream.Collectors) AbstractCloudSpannerFetcher(nl.topicus.jdbc.AbstractCloudSpannerFetcher) Partition(com.google.cloud.spanner.Partition) SpannerException(com.google.cloud.spanner.SpannerException) List(java.util.List) Select(net.sf.jsqlparser.statement.select.Select) Statement(java.sql.Statement) CloudSpannerDriver(nl.topicus.jdbc.CloudSpannerDriver) SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) FromItemVisitorAdapter(net.sf.jsqlparser.statement.select.FromItemVisitorAdapter) Column(net.sf.jsqlparser.schema.Column) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 43 with Column

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;
}
Also used : Table(net.sf.jsqlparser.schema.Table) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue)

Aggregations

Column (net.sf.jsqlparser.schema.Column)43 Expression (net.sf.jsqlparser.expression.Expression)19 Test (org.junit.Test)19 Table (net.sf.jsqlparser.schema.Table)18 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)18 Select (net.sf.jsqlparser.statement.select.Select)18 ArrayList (java.util.ArrayList)9 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)8 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)8 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)6 LongValue (net.sf.jsqlparser.expression.LongValue)5 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)5 Statement (net.sf.jsqlparser.statement.Statement)5 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)5 SimpleNode (net.sf.jsqlparser.parser.SimpleNode)4 SelectVisitorAdapter (net.sf.jsqlparser.statement.select.SelectVisitorAdapter)4 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)3 ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)3 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)3 SQLException (java.sql.SQLException)2