Skip to content

Commit

Permalink
1.format code
Browse files Browse the repository at this point in the history
  • Loading branch information
krihy committed Sep 4, 2023
1 parent 496b8e1 commit 8ac1ef8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package com.oceanbase.odc.service.onlineschemachange;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.util.Assert;

import com.oceanbase.odc.ServiceTestEnv;
import com.oceanbase.odc.TestConnectionUtil;
Expand All @@ -31,7 +31,6 @@
import com.oceanbase.odc.core.shared.constant.ConnectType;
import com.oceanbase.odc.core.shared.constant.ErrorCodes;
import com.oceanbase.odc.core.shared.exception.BadArgumentException;
import com.oceanbase.odc.core.shared.exception.HttpException;
import com.oceanbase.odc.service.connection.ConnectionService;
import com.oceanbase.odc.service.connection.model.ConnectionConfig;
import com.oceanbase.odc.service.flow.model.CreateFlowInstanceReq;
Expand Down Expand Up @@ -110,9 +109,8 @@ public void test_Validate_Invalid_Sql() {
validService.validate(getCreateRequest(
sql,
OnlineSchemaChangeSqlType.CREATE));
} catch (Exception ex) {
Assert.isTrue(ex instanceof BadArgumentException);
Assert.isTrue(((HttpException) ex).getErrorCode() == ErrorCodes.ObPreCheckDdlFailed);
} catch (BadArgumentException ex) {
Assert.assertSame(ex.getErrorCode(), ErrorCodes.ObPreCheckDdlFailed);
throw ex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
*/
package com.oceanbase.odc.service.onlineschemachange.ddl;

import java.io.StringReader;
import java.util.List;
import java.util.Optional;

import org.junit.Assert;
import org.junit.Test;

import com.oceanbase.tools.sqlparser.OBMySQLParser;
import com.oceanbase.tools.sqlparser.OBOracleSQLParser;
import com.oceanbase.tools.sqlparser.statement.Statement;
import com.oceanbase.tools.sqlparser.statement.createtable.CreateTable;
import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineConstraint;
import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineForeignConstraint;

public class TableNameReplacerTest {
private static final String CREATE_STMT = "create table t1 (id int);";
private static final String CREATE_QUOTE_STMT = "create table \"t1\" (id int);";
Expand All @@ -44,6 +55,68 @@ public void test_RewriteCreateStmt_Oracle() {
Assert.assertEquals("create table t1_osc_new_ (id int);", newSql);
}

@Test
public void test_RewriteCreateStmtWittConstraint_Oracle() {
String createSql = "CREATE TABLE CHILD_TABLE1 (\n"
+ "COL NUMBER NOT NULL,\n"
+ "COL1 NUMBER NOT NULL,\n"
+ "CONSTRAINT P1 PRIMARY KEY (COL),\n"
+ "CONSTRAINT U1 UNIQUE (COL1),\n"
+ "CONSTRAINT F1 FOREIGN KEY (COL) REFERENCES PARENT_TABLE1 (COL) ON DELETE CASCADE \n"
+ ")";
String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(createSql, "CHILD_TABLE_NEW");
Statement statement = new OBOracleSQLParser().parse(new StringReader(newSql));
Assert.assertTrue(statement instanceof CreateTable);
CreateTable createTable = (CreateTable) statement;
List<OutOfLineConstraint> constraints = createTable.getConstraints();
Optional<OutOfLineConstraint> pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst();
Assert.assertTrue(pk.isPresent());
Assert.assertNotEquals("P1", pk.get().getConstraintName());

Optional<OutOfLineConstraint> uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst();
Assert.assertTrue(uk.isPresent());
Assert.assertNotEquals("U1", uk.get().getConstraintName());

Optional<OutOfLineForeignConstraint> fk =
constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint))
.map(c -> (OutOfLineForeignConstraint) c).findFirst();
Assert.assertTrue(fk.isPresent());
Assert.assertNotEquals("F1", pk.get().getConstraintName());
}



@Test
public void test_RewriteCreateStmtWittConstraint_MySql() {
String createSql = "CREATE TABLE `child_table1` (\n"
+ "`col` int NOT NULL,\n"
+ "`col1` int NOT NULL,\n"
+ "CONSTRAINT `p1` PRIMARY KEY (`col`),\n"
+ "CONSTRAINT `u1` UNIQUE (`col`),\n"
+ "UNIQUE (`col`),\n"
+ "CONSTRAINT `f1` FOREIGN KEY (`col`) REFERENCES `parent_table1` (`col`) ON DELETE CASCADE ON "
+ "UPDATE NO ACTION\n"
+ ")\n";
String newSql = new OBMysqlTableNameReplacer().replaceCreateStmt(createSql, "`child_table1_new`");
Statement statement = new OBMySQLParser().parse(new StringReader(newSql));
Assert.assertTrue(statement instanceof CreateTable);
CreateTable createTable = (CreateTable) statement;
List<OutOfLineConstraint> constraints = createTable.getConstraints();
Optional<OutOfLineConstraint> pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst();
Assert.assertTrue(pk.isPresent());
Assert.assertEquals("`p1`", pk.get().getConstraintName());

Optional<OutOfLineConstraint> uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst();
Assert.assertTrue(uk.isPresent());
Assert.assertEquals("`u1`", uk.get().getConstraintName());

Optional<OutOfLineForeignConstraint> fk =
constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint))
.map(c -> (OutOfLineForeignConstraint) c).findFirst();
Assert.assertTrue(fk.isPresent());
Assert.assertNotEquals("`f1`", pk.get().getConstraintName());
}

@Test
public void test_RewriteCreateStmtWithQuote_Oracle() {
String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(CREATE_QUOTE_STMT,
Expand Down

0 comments on commit 8ac1ef8

Please sign in to comment.