示例#1
0
  public boolean visit(SQLCreateTableStatement x) {

    MySqlCreateTableStatement mysqlCreateTableStatement = null;
    if (x instanceof MySqlCreateTableStatement) {
      mysqlCreateTableStatement = (MySqlCreateTableStatement) x;
    }

    if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) {
      print("CREATE TEMPORARY TABLE ");
    } else {
      print("CREATE TABLE ");
    }

    if (mysqlCreateTableStatement != null && mysqlCreateTableStatement.isIfNotExiists()) {
      print("IF NOT EXISTS ");
    }

    x.getName().accept(this);
    print(" (");
    incrementIndent();
    println();
    for (int i = 0, size = x.getTableElementList().size(); i < size; ++i) {
      if (i != 0) {
        print(", ");
        println();
      }
      x.getTableElementList().get(i).accept(this);
    }
    decrementIndent();
    println();
    print(")");

    if (mysqlCreateTableStatement != null) {
      for (Map.Entry<String, String> option :
          mysqlCreateTableStatement.getTableOptions().entrySet()) {
        print(" ");
        print(option.getKey());
        print(" = ");
        print(option.getValue());
      }
    }

    if (mysqlCreateTableStatement != null && mysqlCreateTableStatement.getQuery() != null) {
      print(" ");
      incrementIndent();
      println();
      mysqlCreateTableStatement.getQuery().accept(this);
      decrementIndent();
    }

    return false;
  }
  public void test_0() throws Exception {
    String sql =
        "CREATE TABLE dbo.customfieldoption (" //
            + "ID NUMERIC NOT NULL, CUSTOMFIELD NUMERIC, CUSTOMFIELDCONFIG NUMERIC, " //
            + "PARENTOPTIONID NUMERIC, SEQUENCE NUMERIC, customvalue NVARCHAR(255), " //
            + "optiontype NVARCHAR(60), disabled NVARCHAR(60), " //
            + "CONSTRAINT PK_customfieldoption PRIMARY KEY (ID))";

    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);

    Assert.assertEquals(1, statementList.size());

    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals(
        "CREATE TABLE dbo.customfieldoption (" //
            + "\n\tID NUMERIC NOT NULL," //
            + "\n\tCUSTOMFIELD NUMERIC," //
            + "\n\tCUSTOMFIELDCONFIG NUMERIC," //
            + "\n\tPARENTOPTIONID NUMERIC," //
            + "\n\tSEQUENCE NUMERIC," //
            + "\n\tcustomvalue NVARCHAR(255)," //
            + "\n\toptiontype NVARCHAR(60)," //
            + "\n\tdisabled NVARCHAR(60)," //
            + "\n\tCONSTRAINT PK_customfieldoption PRIMARY KEY (ID)" //
            + "\n)",
        output);

    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);

    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("orderBy : " + visitor.getOrderByColumns());

    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(9, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.customfieldoption")));

    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "ID")));
    Assert.assertTrue(
        visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELD")));
    Assert.assertTrue(
        visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELDCONFIG")));
  }
示例#3
0
  public boolean visit(SQLCreateTableStatement x) {
    for (SQLTableElement e : x.getTableElementList()) {
      e.setParent(x);
    }

    String tableName = x.getName().toString();

    TableStat stat = getTableStat(tableName);
    stat.incrementCreateCount();
    setCurrentTable(x, tableName);

    accept(x.getTableElementList());

    restoreCurrentTable(x);

    return false;
  }
示例#4
0
  public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
      accept(Token.CREATE);
    }

    SQLCreateTableStatement createTable = newCreateStatement();

    if (identifierEquals("GLOBAL")) {
      lexer.nextToken();

      if (identifierEquals("TEMPORARY")) {
        lexer.nextToken();
        createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
      } else {
        throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
      }
    } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
      lexer.nextToken();
      if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
        lexer.nextToken();
        createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
      } else {
        throw new ParserException("syntax error");
      }
    }

    accept(Token.TABLE);

    createTable.setName(this.exprParser.name());

    if (lexer.token() == Token.LPAREN) {
      lexer.nextToken();

      for (; ; ) {
        if (lexer.token() == Token.IDENTIFIER //
            || lexer.token() == Token.LITERAL_ALIAS) {
          SQLColumnDefinition column = this.exprParser.parseColumn();
          createTable.getTableElementList().add(column);
        } else if (lexer.token == Token.PRIMARY //
            || lexer.token == Token.UNIQUE //
            || lexer.token == Token.CHECK //
            || lexer.token == Token.CONSTRAINT) {
          SQLConstraint constraint = this.exprParser.parseConstaint();
          constraint.setParent(createTable);
          createTable.getTableElementList().add((SQLTableElement) constraint);
        } else if (lexer.token() == Token.TABLESPACE) {
          throw new ParserException("TODO " + lexer.token());
        } else {
          SQLColumnDefinition column = this.exprParser.parseColumn();
          createTable.getTableElementList().add(column);
        }

        if (lexer.token() == Token.COMMA) {
          lexer.nextToken();

          if (lexer.token() == Token.RPAREN) { // compatible for sql server
            break;
          }
          continue;
        }

        break;
      }

      // while
      // (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
      // parseConstaint(table.getConstraints());
      //
      // if (this.tokenList.current().equals(OracleToken.CommaToken))
      // ;
      // lexer.nextToken();
      // }

      accept(Token.RPAREN);

      if (identifierEquals("INHERITS")) {
        lexer.nextToken();
        accept(Token.LPAREN);
        SQLName inherits = this.exprParser.name();
        createTable.setInherits(new SQLExprTableSource(inherits));
        accept(Token.RPAREN);
      }
    }

    return createTable;
  }