/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.shardingsphere.infra.binder.statement; import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory; import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement; import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml.OracleInsertStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.dml.SQL92InsertStatement; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement; import org.junit.Test; import java.util.Collections; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; public final class SQLStatementContextFactoryTest { @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfSelectStatement() { ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setLimit(new LimitSegment(0, 10, null, null)); selectStatement.setProjections(projectionsSegment); SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(mock(ShardingSphereSchema.class), null, selectStatement); assertNotNull(sqlStatementContext); assertTrue(sqlStatementContext instanceof SelectStatementContext); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfMySQLInsertStatement() { MySQLInsertStatement insertStatement = new MySQLInsertStatement(); insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singleton(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("IdentifierValue")), null)))); assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(insertStatement); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfOracleInsertStatement() { assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(new OracleInsertStatement()); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfPostgreSQLInsertStatement() { assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(new PostgreSQLInsertStatement()); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfSQL92InsertStatement() { assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(new SQL92InsertStatement()); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfSQLServerInsertStatement() { assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(new SQLServerInsertStatement()); } private void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(final InsertStatement insertStatement) { insertStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("tbl"))); SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(mock(ShardingSphereSchema.class), null, insertStatement); assertNotNull(sqlStatementContext); assertTrue(sqlStatementContext instanceof InsertStatementContext); } @Test public void assertSQLStatementContextCreatedWhenSQLStatementNotInstanceOfSelectStatementAndInsertStatement() { SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(mock(ShardingSphereSchema.class), null, mock(SQLStatement.class)); assertNotNull(sqlStatementContext); assertTrue(sqlStatementContext instanceof CommonSQLStatementContext); } }