예제 #1
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")));
  }
예제 #2
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")));
  }
예제 #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")));
  }
예제 #4
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")));
  }
예제 #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());
  }
예제 #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")));
  }
예제 #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")));
  }
예제 #8
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")));
  }
예제 #9
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")));
  }
예제 #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);
  }
예제 #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")));
  }
예제 #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);
 }
예제 #13
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);
 }
예제 #14
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);
 }
예제 #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);
 }
예제 #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);
    }
  }
예제 #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);
 }
예제 #20
0
  @Test
  public void test_one() throws Exception {
    String sql =
        "create table hp_db.g20_relationship_communication_daily(                   "
            + " a_iden_string    varchar,"
            + " b_iden_string    varchar,"
            + " counter          bigint,"
            + " durationtime     bigint"
            + ") "
            + "\nPARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
            + "\nSUBPARTITION BY LIST(bdt bigint)"
            + "\nSUBPARTITION OPTIONS(available_Partition_Num=90)"
            + "\nTABLEGROUP g20_test_group;";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();

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

    {
      String output = SQLUtils.toMySqlString(stmt);
      Assert.assertEquals(
          "CREATE TABLE hp_db.g20_relationship_communication_daily ("
              + "\n\ta_iden_string varchar, "
              + "\n\tb_iden_string varchar, "
              + "\n\tcounter bigint, "
              + "\n\tdurationtime bigint"
              + "\n)"
              + "\nPARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
              + "\nSUBPARTITION BY LIST (bdt bigint)"
              + "\nSUBPARTITION OPTIONS (available_Partition_Num = 90)"
              + "\nTABLEGROUP g20_test_group",
          output);
    }
    {
      String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
      Assert.assertEquals(
          "create table hp_db.g20_relationship_communication_daily ("
              + "\n\ta_iden_string varchar, "
              + "\n\tb_iden_string varchar, "
              + "\n\tcounter bigint, "
              + "\n\tdurationtime bigint"
              + "\n)"
              + "\npartition by hash key(a_iden_string) partition num 100"
              + "\nsubpartition by list (bdt bigint)"
              + "\nsubpartition options (available_Partition_Num = 90)"
              + "\ntablegroup g20_test_group",
          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());
  }
예제 #22
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);
  }
예제 #23
0
  @Test
  public void test_two() throws Exception {
    String sql =
        "CREATE TABLE `t_cpi_driskconfig_bak` ("
            + "`Sequence` bigint(20) NOT NULL AUTO_INCREMENT,"
            + "`comcode` binary(20) NOT NULL,"
            + "`riskcode` varchar(10) binary NOT NULL,"
            + "`configcodehead` varchar(30) binary NOT NULL,"
            + "`configcodebody` varchar(100) binary,"
            + "`configvalue` varchar(200) binary ,"
            + "`inputdate` datetime NOT NULL,"
            + "`validstatus` char(1) NOT NULL,"
            + "`remark` varchar(3000),"
            + "`flag` varchar(10) ,"
            + "PRIMARY KEY (`Sequence`)"
            + ") ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();

    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());

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "CREATE TABLE `t_cpi_driskconfig_bak` ("
            + "\n\t`Sequence` bigint(20) NOT NULL AUTO_INCREMENT, "
            + "\n\t`comcode` binary(20) NOT NULL, "
            + "\n\t`riskcode` varchar(10) BINARY  NOT NULL, "
            + "\n\t`configcodehead` varchar(30) BINARY  NOT NULL, "
            + "\n\t`configcodebody` varchar(100) BINARY , "
            + "\n\t`configvalue` varchar(200) BINARY , "
            + "\n\t`inputdate` datetime NOT NULL, "
            + "\n\t`validstatus` char(1) NOT NULL, "
            + "\n\t`remark` varchar(3000), "
            + "\n\t`flag` varchar(10), "
            + "\n\tPRIMARY KEY (`Sequence`)"
            + "\n) ENGINE = InnoDB AUTO_INCREMENT = 49 CHARSET = utf8",
        output);
  }
예제 #24
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());
  }
예제 #25
0
  public void route(
      SystemConfig sysConfig,
      SchemaConfig schema,
      int sqlType,
      String realSQL,
      String charset,
      ServerConnection sc,
      LayerCachePool cachePool) {
    int rs = ServerParse.parse(realSQL);
    this.sqltype = rs & 0xff;
    this.sysConfig = sysConfig;
    this.schema = schema;
    this.charset = charset;
    this.sc = sc;
    this.cachePool = cachePool;
    try {
      //  RouteStrategy routes=RouteStrategyFactory.getRouteStrategy();
      // rrs =RouteStrategyFactory.getRouteStrategy().route(sysConfig, schema, sqlType2,
      // realSQL,charset, sc, cachePool);
      MySqlStatementParser parser = new MySqlStatementParser(realSQL);
      SQLStatement statement = parser.parseStatement();
      if (statement instanceof SQLSelectStatement) {
        SQLSelectStatement st = (SQLSelectStatement) statement;
        SQLSelectQuery sqlSelectQuery = st.getSelect().getQuery();
        if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
          MySqlSelectQueryBlock mysqlSelectQuery =
              (MySqlSelectQueryBlock) st.getSelect().getQuery();
          joinParser = new JoinParser(mysqlSelectQuery, realSQL);
          joinParser.parser();
        }
      }
      /*
       if (routes instanceof DruidMysqlRouteStrategy) {
        SQLSelectStatement st=((DruidMysqlRouteStrategy) routes).getSQLStatement();
        SQLSelectQuery sqlSelectQuery =st.getSelect().getQuery();
      if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
      	MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)st.getSelect().getQuery();
      	joinParser=new JoinParser(mysqlSelectQuery,realSQL);
      	joinParser.parser();
      }
       }
       */
    } catch (Exception e) {

    }
  }
예제 #26
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")));
  }
예제 #28
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);
  }
예제 #29
0
  @Test
  public void test_one() throws Exception {
    String sql =
        "CREATE TABLE t (pk int auto_increment primary key, a TIMESTAMP, KEY ( a DESC ) );";

    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();

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

    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(
        "CREATE TABLE t ("
            + "\n\tpk int PRIMARY KEY AUTO_INCREMENT, "
            + "\n\ta TIMESTAMP, "
            + "\n\tKEY (a DESC)"
            + "\n)",
        output);
  }
예제 #30
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);
  }