Search in sources :

Example 61 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerRollbackTest method test_2.

public void test_2() {
    String sql = "ROLLBACK TRANSACTION @tran_name_variable";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList();
    Assert.assertEquals(1, stmtList.size());
    String text = SQLUtils.toSQLString(stmtList, JdbcUtils.SQL_SERVER);
    Assert.assertEquals("ROLLBACK TRANSACTION @tran_name_variable", text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 62 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerRowNumberTest2 method test_isEmpty.

public void test_isEmpty() throws Exception {
    String sql = "SELECT * FROM (" + // 
    "   SELECT ROW_NUMBER() OVER (ORDER BY FAlertDate Desc, FAlertLevel, FAlertType)  AS RowNumber, *" + // 
    "        from monitor_business" + // 
    "   where FRemoveAlert = ?" + // 
    " ) AS temp_table" + // 
    "   WHERE RowNumber BETWEEN ? AND ?";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    assertEquals("SELECT *\n" + "FROM (\n" + "\tSELECT ROW_NUMBER() OVER (ORDER BY FAlertDate DESC, FAlertLevel, FAlertType) AS RowNumber, *\n" + "\tFROM monitor_business\n" + "\tWHERE FRemoveAlert = ?\n" + ") temp_table\n" + "WHERE RowNumber BETWEEN ? AND ?", SQLUtils.toSQLServerString(stmt));
    assertEquals("select *\n" + "from (\n" + "\tselect row_number() over (order by FAlertDate desc, FAlertLevel, FAlertType) as RowNumber, *\n" + "\tfrom monitor_business\n" + "\twhere FRemoveAlert = ?\n" + ") temp_table\n" + "where RowNumber between ? and ?", SQLUtils.toSQLServerString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 63 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerRowNumberTest2 method test_isEmpty_2.

public void test_isEmpty_2() throws Exception {
    String sql = "SELECT * FROM (" + // 
    "   SELECT ROW_NUMBER() OVER (ORDER BY FAlertDate Desc, FAlertLevel, FAlertType)  AS RowNumber, *" + // 
    "        from monitor_business" + // 
    "   where FRemoveAlert = ?" + // 
    " ) AS temp_table" + // 
    "   WHERE RowNumber NOT BETWEEN ? AND ?";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    Assert.assertEquals("SELECT *\n" + "FROM (\n" + "\tSELECT ROW_NUMBER() OVER (ORDER BY FAlertDate DESC, FAlertLevel, FAlertType) AS RowNumber, *\n" + "\tFROM monitor_business\n" + "\tWHERE FRemoveAlert = ?\n" + ") temp_table\n" + "WHERE RowNumber NOT BETWEEN ? AND ?", SQLUtils.toSQLServerString(stmt));
    assertEquals("select *\n" + "from (\n" + "\tselect row_number() over (order by FAlertDate desc, FAlertLevel, FAlertType) as RowNumber, *\n" + "\tfrom monitor_business\n" + "\twhere FRemoveAlert = ?\n" + ") temp_table\n" + "where RowNumber not between ? and ?", SQLUtils.toSQLServerString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 64 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerInsertTest10 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO DataDB..TBL_TEST_7 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLServerInsertStatement insertStmt = (SQLServerInsertStatement) stmt;
    assertEquals(1, insertStmt.getValuesList().size());
    assertEquals(33, insertStmt.getValues().getValues().size());
    assertEquals(0, insertStmt.getColumns().size());
    assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = "INSERT INTO DataDB..TBL_TEST_7\n" + "VALUES (?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?)";
    assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 65 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerSelectTest14 method test_simple.

public void test_simple() throws Exception {
    String sql = // 
    "SELECT " + // 
    "    a.* " + // 
    "FROM " + // 
    "    ( " + // 
    "            SELECT " + // 
    "                    row_number () over (ORDER BY a.time_add DESC) ROW, " + // 
    "                    a.detail_no AS detailNo, " + // 
    "                    a.ba_id AS baId, " + // 
    "                    a.ba_name AS baName, " + // 
    "                    a.tran_no AS tranNo, " + // 
    "                    a.tran_name AS tranName, " + // 
    "                    a.tran_type AS tranType, " + // 
    "                    a.balance_type AS balanceType, " + // 
    "                    a.detail_income AS detailIncome, " + // 
    "                    a.detail_payout AS detailPayout, " + // 
    "                    a.before_balance AS beforeBalance, " + // 
    "                    a.after_balance AS afterBalance, " + // 
    "                    a.time_add AS timeAdd, " + // 
    "                    a.user_add AS userAdd, " + // 
    "                    a.remark AS remark, " + // 
    "                    ( " + // 
    "                            SELECT " + // 
    "                                    top 1 t.param_name " + // 
    "                            FROM " + // 
    "                                    config.sys_params t " + // 
    "                            WHERE " + // 
    "                                    t.param_type = 2 " + // 
    "                            AND t.param_value = a.tran_type " + // 
    "                    ) AS tranTypeName " + // 
    "            FROM " + // 
    "                    bussiness.account_detail a " + // 
    "            WHERE " + // 
    "                    1 = 1 " + // 
    "            AND a.time_add >= 2 " + // 
    "            AND a.time_add <= 3 " + // 
    "    ) a " + // 
    "WHERE " + // 
    "    a.ROW BETWEEN (10+2) AND 20 ";
    String expect = "SELECT a.*\n" + "FROM (\n" + "\tSELECT row_number() OVER (ORDER BY a.time_add DESC) AS ROW, a.detail_no AS detailNo, a.ba_id AS baId, a.ba_name AS baName\n" + "\t\t, a.tran_no AS tranNo, a.tran_name AS tranName, a.tran_type AS tranType, a.balance_type AS balanceType, a.detail_income AS detailIncome\n" + "\t\t, a.detail_payout AS detailPayout, a.before_balance AS beforeBalance, a.after_balance AS afterBalance, a.time_add AS timeAdd, a.user_add AS userAdd\n" + "\t\t, a.remark AS remark\n" + "\t\t, (\n" + "\t\t\tSELECT TOP 1 t.param_name\n" + "\t\t\tFROM config.sys_params t\n" + "\t\t\tWHERE t.param_type = 2\n" + "\t\t\t\tAND t.param_value = a.tran_type\n" + "\t\t) AS tranTypeName\n" + "\tFROM bussiness.account_detail a\n" + "\tWHERE 1 = 1\n" + "\t\tAND a.time_add >= 2\n" + "\t\tAND a.time_add <= 3\n" + ") a\n" + "WHERE a.ROW BETWEEN (10 + 2) AND 20";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    String text = TestUtils.outputSqlServer(stmt);
    assertEquals(expect, text);
// System.out.println(text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)152 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)145 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)64 Column (com.alibaba.druid.stat.TableStat.Column)21 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)17 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)8 SQLServerInsertStatement (com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement)8 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)4 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)2 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)2 SQLMergeStatement (com.alibaba.druid.sql.ast.statement.SQLMergeStatement)1