use of com.alibaba.cobar.parser.ast.stmt.mts.MTSRollbackStatement in project cobar by alibaba.
the class MySQLMTSParser method rollback.
/**
* first token <code>ROLLBACK</code> is scanned but not yet consumed
*
* <pre>
* ROLLBACK [WORK] TO [SAVEPOINT] identifier
* ROLLBACK [WORK] [AND [NO] CHAIN | [NO] RELEASE]
* </pre>
*/
public MTSRollbackStatement rollback() throws SQLSyntaxErrorException {
// matchIdentifier("ROLLBACK"); // for performance issue, change to
// follow:
lexer.nextToken();
SpecialIdentifier siTemp = specialIdentifiers.get(lexer.stringValueUppercase());
if (siTemp == SpecialIdentifier.WORK) {
lexer.nextToken();
}
switch(lexer.token()) {
case EOF:
return new MTSRollbackStatement(MTSRollbackStatement.CompleteType.UN_DEF);
case KW_TO:
lexer.nextToken();
String str = lexer.stringValueUppercase();
if (specialIdentifiers.get(str) == SpecialIdentifier.SAVEPOINT) {
lexer.nextToken();
}
Identifier savepoint = identifier();
match(EOF);
return new MTSRollbackStatement(savepoint);
case KW_AND:
lexer.nextToken();
siTemp = specialIdentifiers.get(lexer.stringValueUppercase());
if (siTemp == SpecialIdentifier.NO) {
lexer.nextToken();
matchIdentifier("CHAIN");
match(EOF);
return new MTSRollbackStatement(MTSRollbackStatement.CompleteType.NO_CHAIN);
}
matchIdentifier("CHAIN");
match(EOF);
return new MTSRollbackStatement(MTSRollbackStatement.CompleteType.CHAIN);
case KW_RELEASE:
lexer.nextToken();
match(EOF);
return new MTSRollbackStatement(MTSRollbackStatement.CompleteType.RELEASE);
case IDENTIFIER:
siTemp = specialIdentifiers.get(lexer.stringValueUppercase());
if (siTemp == SpecialIdentifier.NO) {
lexer.nextToken();
match(KW_RELEASE);
match(EOF);
return new MTSRollbackStatement(MTSRollbackStatement.CompleteType.NO_RELEASE);
}
default:
throw err("unrecognized complete type: " + lexer.token());
}
}
use of com.alibaba.cobar.parser.ast.stmt.mts.MTSRollbackStatement in project cobar by alibaba.
the class MySQLMTSParserTest method testRollback.
public void testRollback() throws SQLSyntaxErrorException {
// ROLLBACK [WORK] TO [SAVEPOINT] identifier
// ROLLBACK [WORK] [AND [NO] CHAIN | [NO] RELEASE]
String sql = "rollBack work ";
MySQLMTSParser parser = new MySQLMTSParser(new MySQLLexer(sql));
MTSRollbackStatement rollback = parser.rollback();
String output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.UN_DEF, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.UN_DEF, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack work TO savepoint 123e ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK TO SAVEPOINT 123e", output);
Assert.assertEquals("123e", rollback.getSavepoint().getIdText());
Assert.assertNull(rollback.getCompleteType());
sql = "rollBack to savePOINT savepoint ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK TO SAVEPOINT savepoint", output);
Assert.assertEquals("savepoint", rollback.getSavepoint().getIdText());
Assert.assertNull(rollback.getCompleteType());
sql = "rollBack to `select` ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK TO SAVEPOINT `select`", output);
Assert.assertEquals("`select`", rollback.getSavepoint().getIdText());
Assert.assertNull(rollback.getCompleteType());
sql = "rollBack work to `select` ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK TO SAVEPOINT `select`", output);
Assert.assertEquals("`select`", rollback.getSavepoint().getIdText());
Assert.assertNull(rollback.getCompleteType());
sql = "rollBack work and no chaiN ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK AND NO CHAIN", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_CHAIN, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack work and chaiN ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK AND CHAIN", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.CHAIN, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack work NO release ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK NO RELEASE", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_RELEASE, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack work release ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK RELEASE", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.RELEASE, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack and no chaiN ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK AND NO CHAIN", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_CHAIN, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack and chaiN ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK AND CHAIN", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.CHAIN, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack NO release ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK NO RELEASE", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_RELEASE, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
sql = "rollBack release ";
parser = new MySQLMTSParser(new MySQLLexer(sql));
rollback = parser.rollback();
output = output2MySQL(rollback, sql);
Assert.assertEquals("ROLLBACK RELEASE", output);
Assert.assertEquals(MTSRollbackStatement.CompleteType.RELEASE, rollback.getCompleteType());
Assert.assertNull(rollback.getSavepoint());
}
use of com.alibaba.cobar.parser.ast.stmt.mts.MTSRollbackStatement in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(MTSRollbackStatement node) {
appendable.append("ROLLBACK");
Identifier savepoint = node.getSavepoint();
if (savepoint == null) {
MTSRollbackStatement.CompleteType type = node.getCompleteType();
switch(type) {
case CHAIN:
appendable.append(" AND CHAIN");
break;
case NO_CHAIN:
appendable.append(" AND NO CHAIN");
break;
case NO_RELEASE:
appendable.append(" NO RELEASE");
break;
case RELEASE:
appendable.append(" RELEASE");
break;
case UN_DEF:
break;
default:
throw new IllegalArgumentException("unrecgnized complete type: " + type);
}
} else {
appendable.append(" TO SAVEPOINT ");
savepoint.accept(this);
}
}
Aggregations