Exemplo n.º 1
0
  public void test_0() throws Exception {
    String sql = "GRANT CREATE TEMPORARY TABLE ON mydb.* TO 'someuser'@'somehost';";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    //        print(statementList);

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

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

    Assert.assertEquals(
        "GRANT CREATE TEMPORARY TABLE ON mydb.* TO 'someuser'@'somehost'", //
        SQLUtils.toMySqlString(stmt));
    Assert.assertEquals(
        "grant CREATE TEMPORARY TABLE on mydb.* to 'someuser'@'somehost'", //
        SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));

    //        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(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    //        Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
  }
Exemplo n.º 2
0
  public void test_0() throws Exception {
    String sql = "UPDATE `z_code` SET `key`='INTRANT_NOTALLOWED_CATEGORY_C'";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.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(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

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

    Assert.assertTrue(visitor.getColumns().contains(new Column("z_code", "key")));
  }
Exemplo n.º 3
0
  public void test_0() throws Exception {
    String sql = "DELETE FROM a1, a2 USING t1 AS a1 INNER JOIN t2 AS a2 WHERE a1.id=a2.id;";

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

    Assert.assertEquals(
        "DELETE FROM a1, a2 USING (t1) AS a1" + "\n\tINNER JOIN t2 a2" + "\nWHERE a1.id = a2.id",
        SQLUtils.toMySqlString(stmt));
    Assert.assertEquals(
        "delete from a1, a2 using (t1) as a1" + "\n\tinner join t2 a2" + "\nwhere a1.id = a2.id",
        SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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(3, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("a1")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("a2")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    Assert.assertTrue(visitor.getColumns().contains(new Column("a1", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("a2", "id")));
  }
Exemplo n.º 4
0
  public void test_0() throws Exception {
    String sql = "SELECT * FROM t_department  WHERE name IN ('0000','4444') ORDER BY name ASC";

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

    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;

    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNotNull(queryBlock.getOrderBy());

    //        print(statementList);

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

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

    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(1, visitor.getOrderByColumns().size());

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_department")));
  }
Exemplo n.º 5
0
  void validate(int id, String sql) throws Exception {
    sql = sql.trim();
    boolean sqlFlag = false;
    String lowerSql = sql.toLowerCase();
    if (lowerSql.startsWith("insert")
        || lowerSql.startsWith("select")
        || lowerSql.startsWith("upate")
        || lowerSql.startsWith("delete")
        || lowerSql.startsWith("create")
        || lowerSql.startsWith("drop")) {
      sqlFlag = true;
    }

    if (!sqlFlag) {
      update(id, sql, 2);
      return;
    }

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

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

    StringBuilder out = new StringBuilder();
    MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
    statemen.accept(visitor);

    update(id, out.toString(), 1);
    System.out.println(sql);
    System.out.println(out.toString());
  }
Exemplo n.º 6
0
  public void test_0() throws Exception {
    String sql = "SELECT `group`.* FROM `group` WHERE (group.group_id=159754)";

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

    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;

    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());

    print(statementList);

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

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

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

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("group")));
  }
Exemplo n.º 7
0
  public void test_0() throws Exception {
    String sql = "GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    //        print(statementList);

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

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90", //
        output);

    //        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(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    //        Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
  }
Exemplo n.º 8
0
  public void test_alter_first() throws Exception {
    String sql = "alter table test   add  dspcode  char(200)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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());

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "ALTER TABLE test"
            + //
            "\n\tADD COLUMN dspcode char(200)",
        output);

    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "dspcode")));
  }
Exemplo n.º 9
0
  public void test_0() throws Exception {
    String sql = "GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'someuser'@'somehost';";

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

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

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'someuser'@'somehost'", //
        output);

    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(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    //        Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
  }
Exemplo n.º 10
0
  public void test_0() throws Exception {
    String sql = "SELECT 1--1 from t where id = 1";

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

    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;

    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());

    //        print(statementList);

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

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

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

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "SELECT 1 - -1" //
            + "\nFROM t" //
            + "\nWHERE id = 1", //
        output);
  }
Exemplo n.º 11
0
  public void test_0() throws Exception {
    String sql = "CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';";

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

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

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'", //
        output);

    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(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    //        Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
  }
Exemplo n.º 12
0
 public void testReplace_1() throws Exception {
   String sql = "ReplaCe   test.t1 seT t1.id1:=? ";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals("REPLACE INTO test.t1 (t1.id1)\nVALUES (?)", output);
 }
Exemplo n.º 13
0
 public void testReplace_7() throws Exception {
   String sql = "ReplaCe LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 (t1.col1)\nVALUES (123), ('12''34')", output);
 }
Exemplo n.º 14
0
 public void testReplace_6() throws Exception {
   String sql = "ReplaCe LOW_PRIORITY t1 (select id from t1) ";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1\n\tSELECT id\n\tFROM t1", output);
 }
Exemplo n.º 15
0
 public void testReplace_2() throws Exception {
   String sql = "ReplaCe t1 value (123,?) ";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals("REPLACE INTO t1\nVALUES (123, ?)", output);
 }
Exemplo n.º 16
0
  public void test_0() throws Exception {
    String sql =
        "UPDATE ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 "
            + "`table_3966` AS `table_3966_11` SET `version` = `version` + 3, `gmt_modified` = NOW(), `optype` = ?, `feature` = ? "
            + "WHERE `sub_biz_order_id` = ? AND `biz_order_type` = ? AND `id` = ? AND `ti_id` = ? AND `optype` = ? AND `root_id` = ?";

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

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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(6, visitor.getConditions().size());

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

    Assert.assertTrue(visitor.getColumns().contains(new Column("table_3966", "sub_biz_order_id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("table_3966", "feature")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("table_3966", "id")));

    {
      String output = SQLUtils.toMySqlString(stmt);
      Assert.assertEquals(
          "UPDATE ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 `table_3966` `table_3966_11`"
              + "\nSET `version` = `version` + 3, `gmt_modified` = NOW(), `optype` = ?, `feature` = ?"
              + "\nWHERE `sub_biz_order_id` = ?"
              + "\n\tAND `biz_order_type` = ?"
              + "\n\tAND `id` = ?"
              + "\n\tAND `ti_id` = ?"
              + "\n\tAND `optype` = ?"
              + "\n\tAND `root_id` = ?", //
          output);
    }
    {
      String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
      Assert.assertEquals(
          "update rollback_on_fail target_affect_row 1 `table_3966` `table_3966_11`"
              + "\nset `version` = `version` + 3, `gmt_modified` = NOW(), `optype` = ?, `feature` = ?"
              + "\nwhere `sub_biz_order_id` = ?"
              + "\n\tand `biz_order_type` = ?"
              + "\n\tand `id` = ?"
              + "\n\tand `ti_id` = ?"
              + "\n\tand `optype` = ?"
              + "\n\tand `root_id` = ?", //
          output);
    }
  }
Exemplo n.º 17
0
 public void testReplace_0() throws Exception {
   String sql = "ReplaCe LOW_PRIORITY intO test.t1 seT t1.id1:=?, id2='123'";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals(
       "REPLACE LOW_PRIORITY INTO test.t1 (t1.id1, id2)\nVALUES (?, '123')", output);
 }
 public void test_alter_first() throws Exception {
   String sql = "ALTER TABLE `rules` DROP `enabled`";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals(
       "ALTER TABLE `rules`" //
           + "\n\tDROP COLUMN `enabled`",
       output);
 }
 public void test_alter_first() throws Exception {
   String sql = "alter table tabelname alter column operateVersion set default 0";
   MySqlStatementParser parser = new MySqlStatementParser(sql);
   SQLStatement stmt = parser.parseStatementList().get(0);
   parser.match(Token.EOF);
   String output = SQLUtils.toMySqlString(stmt);
   Assert.assertEquals(
       "ALTER TABLE tabelname"
           + //
           "\n\tALTER COLUMN operateVersion SET DEFAULT 0",
       output);
 }
  void validate(String sql, String expect) {

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

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

    StringBuilder out = new StringBuilder();
    MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
    statemen.accept(visitor);

    Assert.assertEquals(expect, out.toString());
  }
Exemplo n.º 21
0
  public void test_0() throws Exception {
    String sql = "SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);";

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

    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;

    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());

    //        print(statementList);

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

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

    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(3, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t1")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "SELECT *" //
            + "\nFROM t1" //
            + "\nWHERE column1 = (" //
            + "\n\tSELECT column1" //
            + "\n\tFROM t2" //
            + "\n\t)", //
        output);

    String output_lcase = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
    Assert.assertEquals(
        "select *" //
            + "\nfrom t1" //
            + "\nwhere column1 = (" //
            + "\n\tselect column1" //
            + "\n\tfrom t2" //
            + "\n\t)", //
        output_lcase);
  }
Exemplo n.º 22
0
  void mergValidate(String sql, String expect) {

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

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

    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out, true);
    statemen.accept(visitor);

    //        System.out.println(out.toString());

    Assert.assertEquals(expect, out.toString());
  }
Exemplo n.º 23
0
  public void test_0() throws Exception {
    String sql = "DROP USER 'jeffrey'@'localhost';";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.accept(visitor);

    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());

    Assert.assertTrue(visitor.getTables().size() == 0);
  }
  public void test_0() throws Exception {
    String sql = "SHOW FULL TABLES FROM sonar LIKE 'a'";

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

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

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

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

    // Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
  }
Exemplo n.º 25
0
  public void test_0() throws Exception {
    String sql =
        "CREATE TABLE t1 ("
            + //
            "c1 INT STORAGE DISK,"
            + //
            "c2 INT STORAGE MEMORY "
            + //
            ") TABLESPACE ts_1 ENGINE NDB;";

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

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "CREATE TABLE t1 ("
            + //
            "\n\tc1 INT STORAGE DISK, "
            + //
            "\n\tc2 INT STORAGE MEMORY"
            + //
            "\n) TABLESPACE ts_1 ENGINE = NDB",
        output);
  }
Exemplo n.º 26
0
  public void test_0() throws Exception {
    String sql =
        "CREATE TABLE lookup"
            + //
            "  (id INT, INDEX USING BTREE (id))"
            + //
            "  MAX_ROWS 1024;"; //

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    //        print(statementList);

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

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "CREATE TABLE lookup ("
            + //
            "\n\tid INT, "
            + //
            "\n\tINDEX USING BTREE(id)"
            + //
            "\n) MAX_ROWS = 1024",
        output);
  }
Exemplo n.º 27
0
  public void test_alter_first() throws Exception {
    String sql = "alter table st_jklsxxb enable constraint FK_ST_xxx_REFERENCE_ST_xxx";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);

    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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());

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "ALTER TABLE st_jklsxxb"
            + //
            "\n\tENABLE CONSTRAINT FK_ST_xxx_REFERENCE_ST_xxx",
        output);

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