use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlDdlNodes method populate.
/**
* Populates the table called {@code name} by executing {@code query}.
*/
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
// Generate, prepare and execute an "INSERT INTO table query" statement.
// (It's a bit inefficient that we convert from SqlNode to SQL and back
// again.)
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
pw.print("INSERT INTO ");
name.unparse(w, 0, 0);
pw.print(" ");
query.unparse(w, 0, 0);
pw.flush();
final String sql = sw.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlNode method toSqlString.
/**
* Returns the SQL text of the tree of which this <code>SqlNode</code> is
* the root.
*
* @param dialect Dialect
* @param forceParens wraps all expressions in parentheses; good for parse
* test, but false by default.
*
* <p>Typical return values are:</p>
* <ul>
* <li>'It''s a bird!'</li>
* <li>NULL</li>
* <li>12.3</li>
* <li>DATE '1969-04-29'</li>
* </ul>
*/
public SqlString toSqlString(SqlDialect dialect, boolean forceParens) {
if (dialect == null) {
dialect = AnsiSqlDialect.DEFAULT;
}
SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
writer.setAlwaysUseParentheses(forceParens);
writer.setSelectListItemsOnSeparateLines(false);
writer.setIndentation(0);
unparse(writer, 0, 0);
final String sql = writer.toString();
return new SqlString(dialect, sql);
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class JdbcTable method generateSql.
SqlString generateSql() {
final SqlNodeList selectList = SqlNodeList.SINGLETON_STAR;
SqlSelect node = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList, tableName(), null, null, null, null, null, null, null, null);
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true).withDialect(jdbcSchema.dialect);
final SqlPrettyWriter writer = new SqlPrettyWriter(config);
node.unparse(writer, 0, 0);
return writer.toSqlString();
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class SqlNode method toSqlString.
/**
* Returns the SQL text of the tree of which this <code>SqlNode</code> is
* the root.
*
* <p>Typical return values are:
*
* <ul>
* <li>'It''s a bird!'
* <li>NULL
* <li>12.3
* <li>DATE '1969-04-29'
* </ul>
*
* @param transform Transform that sets desired writer configuration
*/
public SqlString toSqlString(UnaryOperator<SqlWriterConfig> transform) {
final SqlWriterConfig config = transform.apply(SqlPrettyWriter.config());
SqlPrettyWriter writer = new SqlPrettyWriter(config);
unparse(writer, 0, 0);
return writer.toSqlString();
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class IntervalSqlType method generateTypeString.
// ~ Methods ----------------------------------------------------------------
@Override
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
sb.append("INTERVAL ");
final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(false).withSelectListItemsOnSeparateLines(false).withIndentation(0).withDialect(dialect);
final SqlPrettyWriter writer = new SqlPrettyWriter(config);
intervalQualifier.unparse(writer, 0, 0);
final String sql = writer.toString();
sb.append(new SqlString(dialect, sql).getSql());
}
Aggregations