Java Code Examples for org.apache.calcite.sql.pretty.SqlPrettyWriter#setAlwaysUseParentheses()

The following examples show how to use org.apache.calcite.sql.pretty.SqlPrettyWriter#setAlwaysUseParentheses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlCreateTable.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the projection format of the DDL columns(including computed columns).
 * e.g. If we got a DDL:
 * <pre>
 *   create table tbl1(
 *     col1 int,
 *     col2 varchar,
 *     col3 as to_timestamp(col2)
 *   ) with (
 *     'connector' = 'csv'
 *   )
 * </pre>
 * we would return a query like:
 *
 * <p>"col1, col2, to_timestamp(col2) as col3", caution that the "computed column" operands
 * have been reversed.
 */
public String getColumnSqlString() {
	SqlPrettyWriter writer = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
	writer.setAlwaysUseParentheses(true);
	writer.setSelectListItemsOnSeparateLines(false);
	writer.setIndentation(0);
	writer.startList("", "");
	for (SqlNode column : columnList) {
		writer.sep(",");
		if (column instanceof SqlTableColumn) {
			SqlTableColumn tableColumn = (SqlTableColumn) column;
			tableColumn.getName().unparse(writer, 0, 0);
		} else {
			column.unparse(writer, 0, 0);
		}
	}

	return writer.toString();
}
 
Example 2
Source File: IntervalSqlType.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
  sb.append("INTERVAL ");
  final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
  final SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
  writer.setAlwaysUseParentheses(false);
  writer.setSelectListItemsOnSeparateLines(false);
  writer.setIndentation(0);
  intervalQualifier.unparse(writer, 0, 0);
  final String sql = writer.toString();
  sb.append(new SqlString(dialect, sql).getSql());
}
 
Example 3
Source File: QuarkDDLExecutor.java    From quark with Apache License 2.0 5 votes vote down vote up
public int executeCreateView(SqlCreateQuarkView sqlNode) throws SQLException {
  DBI dbi = getDbi();
  List<String> tableNameList = sqlNode.getTableName().names;
  String dataSourceName = tableNameList.get(0);

  ViewDAO viewDAO = dbi.onDemand(ViewDAO.class);

  JdbcSourceDAO jdbcDAO = dbi.onDemand(JdbcSourceDAO.class);
  QuboleDbSourceDAO quboleDAO = dbi.onDemand(QuboleDbSourceDAO.class);
  DataSource dataSource = jdbcDAO.findByName(dataSourceName,
      connection.getDSSet().getId());
  if (dataSource == null) {
    dataSource = quboleDAO.findByName(dataSourceName, connection.getDSSet().getId());
  }

  if (dataSource == null) {
    throw new SQLException("DataSource with name '" + dataSourceName + "' not found");
  }

  SqlPrettyWriter writer = new SqlPrettyWriter(SqlDialect.CALCITE);
  writer.setAlwaysUseParentheses(false);
  writer.setSelectListItemsOnSeparateLines(false);
  writer.setIndentation(0);
  writer.setQuoteAllIdentifiers(true);
  sqlNode.getQuery().unparse(writer, 0, 0);
  final String sql = writer.toString();

  LOG.debug(sql);
  return viewDAO.insert(sqlNode.getName(),
      "No Description", sql,
      0L, dataSource.getId(),
      tableNameList.get(1), tableNameList.get(2),
      connection.getDSSet().getId());
}
 
Example 4
Source File: SqlNode.java    From Bats with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
  return writer.toSqlString();
}