Exemplo n.º 1
0
Arquivo: Alias.java Projeto: kxa/jOOQ
  @Override
  public final void toSQL(RenderContext context) {
    if (context.declareFields() || context.declareTables()) {
      if (wrapInParentheses) {
        context.sql("(");
      }

      context.sql(wrapped);

      if (wrapInParentheses) {
        context.sql(")");
      }

      // [#291] some aliases cause trouble, if they are not explicitly marked using "as"
      if (Arrays.asList(DERBY, HSQLDB, MYSQL, POSTGRES).contains(context.getDialect())) {
        context.keyword(" as");
      }

      context.sql(" ");
      context.literal(alias);

      // [#756] If the aliased object is an anonymous table (usually an
      // unnested array), then field names must be part of the alias
      // declaration. For example:
      //
      // SELECT t.column_value FROM UNNEST(ARRAY[1, 2]) AS t(column_value)

      // TODO: Is this still needed?

      switch (context.getDialect()) {
        case HSQLDB:
        case POSTGRES:
          {
            // The javac compiler doesn't like casting of generics
            Object o = wrapped;

            if (context.declareTables() && o instanceof ArrayTable) {
              ArrayTable table = (ArrayTable) o;

              context.sql("(");
              Utils.fieldNames(context, table.getFields());
              context.sql(")");
            }

            break;
          }
      }
    } else {
      context.literal(alias);
    }
  }
Exemplo n.º 2
0
  @Override
  public final void toSQL(RenderContext context) {
    if (context.declareFields() || context.declareTables()) {
      SQLDialect dialect = context.configuration().dialect();
      boolean simulateDerivedColumnList = false;

      // [#1801] Some databases don't allow "derived column names" in
      // "simple class specifications". Hence, wrap the table reference in
      // a subselect
      if (fieldAliases != null
          && asList(CUBRID, FIREBIRD, SQLSERVER, SYBASE).contains(dialect.family())
          && wrapped instanceof TableImpl) {

        @SuppressWarnings("unchecked")
        Select<Record> select = select(list(field("*"))).from(((Table<?>) wrapped).as(alias));

        context
            .sql("(")
            .formatIndentStart()
            .formatNewLine()
            .visit(select)
            .formatIndentEnd()
            .formatNewLine()
            .sql(")");
      }

      // [#1801] Some databases do not support "derived column names".
      // They can be simulated by concatenating a dummy SELECT with no
      // results using UNION ALL
      else if (fieldAliases != null
          && asList(H2, MARIADB, MYSQL, ORACLE, SQLITE).contains(dialect.family())) {
        simulateDerivedColumnList = true;

        SelectFieldList fields = new SelectFieldList();
        for (String fieldAlias : fieldAliases) {
          fields.add(field("null").as(fieldAlias));
        }

        Select<Record> select =
            select(fields)
                .where(falseCondition())
                .unionAll(select(field("*")).from(((Table<?>) wrapped).as(alias)));

        context
            .sql("(")
            .formatIndentStart()
            .formatNewLine()
            .visit(select)
            .formatIndentEnd()
            .formatNewLine()
            .sql(")");
      }

      // The default behaviour
      else {
        toSQLWrapped(context);
      }

      // [#291] some aliases cause trouble, if they are not explicitly marked using "as"
      toSQLAs(context);

      context.sql(" ");
      context.literal(alias);

      // [#1801] Add field aliases to the table alias, if applicable
      if (fieldAliases != null && !simulateDerivedColumnList) {
        toSQLDerivedColumnList(context);
      } else {
        // [#756] If the aliased object is an anonymous table (usually an
        // unnested array), then field names must be part of the alias
        // declaration. For example:
        //
        // SELECT t.column_value FROM UNNEST(ARRAY[1, 2]) AS t(column_value)

        // TODO: Is this still needed?
        switch (dialect) {
          case HSQLDB:
          case POSTGRES:
            {
              // The javac compiler doesn't like casting of generics
              Object o = wrapped;

              if (context.declareTables() && o instanceof ArrayTable) {
                ArrayTable table = (ArrayTable) o;

                context.sql("(");
                Utils.fieldNames(context, table.fields());
                context.sql(")");
              }

              break;
            }
        }
      }
    } else {
      context.literal(alias);
    }
  }