diff --git a/src/main/java/com/alibaba/druid/mapping/DruidMappingException.java b/src/main/java/com/alibaba/druid/mapping/DruidMappingException.java deleted file mode 100644 index ee9d23463f..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/DruidMappingException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -public class DruidMappingException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public DruidMappingException(){ - super(); - } - - public DruidMappingException(String message, Throwable cause){ - super(message, cause); - } - - public DruidMappingException(String message){ - super(message); - } - - public DruidMappingException(Throwable cause){ - super(cause); - } - -} diff --git a/src/main/java/com/alibaba/druid/mapping/Entity.java b/src/main/java/com/alibaba/druid/mapping/Entity.java deleted file mode 100644 index 83985b6457..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/Entity.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -import java.util.LinkedHashMap; -import java.util.Map; - -public class Entity extends MappingObject { - - private String name; - private String description; - private String tableName; - - private Map properties = new LinkedHashMap(); - - public Entity(){ - } - - public Entity(String name, String description, String tableName){ - this.name = name; - this.description = description; - this.tableName = tableName; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getTableName() { - return tableName; - } - - public void setTableName(String tableName) { - this.tableName = tableName; - } - - public void addProperty(Property property) { - this.properties.put(property.getName(), property); - } - - public Property getProperty(String name) { - Property prop = this.properties.get(name); - - if (prop == null) { - for (Map.Entry entry : properties.entrySet()) { - if (entry.getKey().equalsIgnoreCase(name)) { - prop = entry.getValue(); - break; - } - } - } - - return prop; - } - - public Map getProperties() { - return properties; - } - - public void setProperties(Map properties) { - this.properties = properties; - } - - public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("{name="); - buf.append(name); - - buf.append(", tableName="); - buf.append(tableName); - - buf.append("}"); - - return buf.toString(); - } - -} diff --git a/src/main/java/com/alibaba/druid/mapping/MappingContext.java b/src/main/java/com/alibaba/druid/mapping/MappingContext.java deleted file mode 100644 index b86ce5e8b0..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/MappingContext.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -import java.util.ArrayList; -import java.util.List; - -public class MappingContext { - - private List parameters; - - private boolean generateAlias = false; - private boolean explainAllColumnToList = false; - - private Entity defaultEntity; - - public MappingContext(){ - this(new ArrayList()); - } - - public MappingContext(List parameters){ - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public boolean isGenerateAlias() { - return generateAlias; - } - - public void setGenerateAlias(boolean generateAlias) { - this.generateAlias = generateAlias; - } - - public boolean isExplainAllColumnToList() { - return explainAllColumnToList; - } - - public void setExplainAllColumnToList(boolean explainAllColumnToList) { - this.explainAllColumnToList = explainAllColumnToList; - } - - public Entity getDefaultEntity() { - return defaultEntity; - } - - public void setDefaultEntity(Entity defaultEntity) { - this.defaultEntity = defaultEntity; - } - -} diff --git a/src/main/java/com/alibaba/druid/mapping/MappingEngine.java b/src/main/java/com/alibaba/druid/mapping/MappingEngine.java deleted file mode 100644 index 204a0879db..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/MappingEngine.java +++ /dev/null @@ -1,395 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import com.alibaba.druid.mapping.spi.MappingProvider; -import com.alibaba.druid.mapping.spi.MappingVisitor; -import com.alibaba.druid.mapping.spi.MySqlMappingProvider; -import com.alibaba.druid.sql.ast.SQLObject; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.visitor.ExportParameterVisitor; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; -import com.alibaba.druid.util.JdbcUtils; - -public class MappingEngine { - - private LinkedHashMap entities = new LinkedHashMap(); - private Integer maxLimit; - private final MappingProvider provider; - private DataSource dataSource; - - public MappingEngine(){ - this(new MySqlMappingProvider()); - } - - public MappingEngine(MappingProvider provider){ - this.provider = provider; - } - - public Entity getFirstEntity(MappingContext context) { - Entity entity = context.getDefaultEntity(); - - if (entity != null) { - return entity; - } - - for (Map.Entry entry : entities.entrySet()) { - return entry.getValue(); - } - - return null; - } - - public DataSource getDataSource() { - return dataSource; - } - - public void setDataSource(DataSource dataSource) { - this.dataSource = dataSource; - } - - public MappingProvider getMappingProvider() { - return provider; - } - - public Integer getMaxLimit() { - return maxLimit; - } - - public void setMaxLimit(Integer maxLimit) { - this.maxLimit = maxLimit; - } - - public LinkedHashMap getEntities() { - return entities; - } - - public void addEntity(Entity entity) { - this.entities.put(entity.getName(), entity); - } - - public MappingVisitor createMappingVisitor() { - return provider.createMappingVisitor(this); - } - - public MappingVisitor createMappingVisitor(List parameters) { - return createMappingVisitor(new MappingContext(parameters)); - } - - public MappingVisitor createMappingVisitor(MappingContext context) { - return provider.createMappingVisitor(this, context); - } - - public SQLASTOutputVisitor createOutputVisitor(Appendable out) { - return provider.createOutputVisitor(this, out); - } - - public String explain(String sql) { - return explain(sql); - } - - public String explain(String sql, MappingContext context) { - List stmtList = provider.explain(this, sql); - - if (stmtList.size() > 0) { - throw new IllegalArgumentException(sql); - } - - SQLStatement stmt = stmtList.get(0); - - MappingVisitor visitor = this.createMappingVisitor(context); - stmt.accept(visitor); - visitor.afterResolve(); - afterResole(visitor); - - return toSQL(stmt); - } - - public SQLSelectQueryBlock explainToSelectSQLObject(String sql) { - return explainToSelectSQLObject(sql, new MappingContext()); - } - - public SQLSelectQueryBlock explainToSelectSQLObject(String sql, MappingContext context) { - return provider.explainToSelectSQLObject(this, sql, context); - } - - public String explainToSelectSQL(String sql) { - return explainToSelectSQL(sql, Collections.emptyList()); - } - - public String explainToSelectSQL(String sql, List parameters) { - return explainToSelectSQL(sql, new MappingContext(parameters)); - } - - public String explainToSelectSQL(String sql, MappingContext context) { - SQLSelectQueryBlock query = explainToSelectSQLObject(sql, context); - - MappingVisitor visitor = this.createMappingVisitor(context); - query.accept(visitor); - visitor.afterResolve(); - afterResole(visitor); - - return toSQL(query); - } - - public void afterResole(MappingVisitor visitor) { - - } - - public SQLDeleteStatement explainToDeleteSQLObject(String sql, MappingContext context) { - return provider.explainToDeleteSQLObject(this, sql, context); - } - - public String explainToDeleteSQL(String sql) { - return explainToDeleteSQL(sql, new MappingContext()); - } - - public String explainToDeleteSQL(String sql, MappingContext context) { - SQLDeleteStatement stmt = explainToDeleteSQLObject(sql, context); - - MappingVisitor visitor = this.createMappingVisitor(context); - stmt.accept(visitor); - visitor.afterResolve(); - afterResole(visitor); - - return toSQL(stmt); - } - - public String resolveTableName(Entity entity, MappingContext context) { - return entity.getTableName(); - } - - public String resovleColumnName(Entity entity, Property property, MappingContext context) { - return property.getDbColumnName(); - } - - public SQLUpdateStatement explainToUpdateSQLObject(String sql, MappingContext context) { - return provider.explainToUpdateSQLObject(this, sql, context); - } - - public String explainToUpdateSQL(String sql) { - return explainToUpdateSQL(sql, new MappingContext()); - } - - public String explainToUpdateSQL(String sql, MappingContext context) { - SQLUpdateStatement stmt = explainToUpdateSQLObject(sql, context); - - MappingVisitor visitor = this.createMappingVisitor(context); - stmt.accept(visitor); - visitor.afterResolve(); - afterResole(visitor); - - return toSQL(stmt); - } - - public SQLInsertStatement explainToInsertSQLObject(String sql, MappingContext context) { - return provider.explainToInsertSQLObject(this, sql, context); - } - - public String explainToInsertSQL(String sql) { - return explainToInsertSQL(sql, new MappingContext()); - } - - public String explainToInsertSQL(String sql, MappingContext context) { - SQLInsertStatement stmt = explainToInsertSQLObject(sql, context); - - MappingVisitor visitor = this.createMappingVisitor(context); - stmt.accept(visitor); - visitor.afterResolve(); - afterResole(visitor); - - return toSQL(stmt); - } - - public List exportParameters(SQLObject sqlObject) { - List parameters = new ArrayList(); - exportParameters(sqlObject, parameters); - return parameters; - } - - public void exportParameters(SQLObject sqlObject, List parameters) { - ExportParameterVisitor exporter = this.provider.createExportParameterVisitor(parameters); - sqlObject.accept(exporter); - } - - public String toSQL(SQLObject sqlObject) { - return toSQL(sqlObject, false); - } - - public String toSQL(SQLObject sqlObject, boolean exportParameter) { - if (exportParameter) { - exportParameters(sqlObject); - } - - StringBuilder out = new StringBuilder(); - SQLASTOutputVisitor outputVisitor = createOutputVisitor(out); - sqlObject.accept(outputVisitor); - - return out.toString(); - } - - public List> select(Connection conn, String sql, List parameters) throws SQLException { - return select(conn, sql, new MappingContext(parameters)); - } - - public List> select(Connection conn, String sql, MappingContext context) throws SQLException { - SQLSelectQueryBlock sqlObject = this.explainToSelectSQLObject(sql, context); - exportParameters(sqlObject, context.getParameters()); - String rawSql = this.toSQL(sqlObject); - return JdbcUtils.executeQuery(conn, rawSql, context.getParameters()); - } - - public int delete(Connection conn, String sql, List parameters) throws SQLException { - return delete(conn, sql, new MappingContext(parameters)); - } - - public int delete(Connection conn, String sql, MappingContext context) throws SQLException { - SQLDeleteStatement sqlObject = this.explainToDeleteSQLObject(sql, context); - exportParameters(sqlObject, context.getParameters()); - String rawSql = this.toSQL(sqlObject); - int updateCount = JdbcUtils.executeUpdate(conn, rawSql, context.getParameters()); - return updateCount; - } - - public int update(Connection conn, String sql, List parameters) throws SQLException { - return update(conn, sql, new MappingContext(parameters)); - } - - public int update(Connection conn, String sql, MappingContext context) throws SQLException { - SQLUpdateStatement sqlObject = this.explainToUpdateSQLObject(sql, context); - exportParameters(sqlObject, context.getParameters()); - String rawSql = this.toSQL(sqlObject); - int updateCount = JdbcUtils.executeUpdate(conn, rawSql, context.getParameters()); - return updateCount; - } - - public void insert(Connection conn, String sql, Object... parameters) throws SQLException { - insert(conn, sql, Arrays.asList(parameters)); - } - - public void insert(Connection conn, String sql, List parameters) throws SQLException { - insert(conn, sql, new MappingContext(parameters)); - } - - public void insert(Connection conn, String sql, MappingContext context) throws SQLException { - SQLInsertStatement sqlObject = this.explainToInsertSQLObject(sql, context); - exportParameters(sqlObject, context.getParameters()); - String rawSql = this.toSQL(sqlObject); - JdbcUtils.execute(conn, rawSql, context.getParameters()); - } - - public Connection getConnection() throws SQLException { - if (dataSource == null) { - throw new SQLException("datasource not init."); - } - return this.dataSource.getConnection(); - } - - public List> select(String sql, Object... parameters) throws SQLException { - return select(sql, Arrays.asList(parameters)); - } - - public List> select(String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = getConnection(); - return select(conn, sql, parameters); - } finally { - JdbcUtils.close(conn); - } - } - - public int delete(String sql, Object... parameters) throws SQLException { - return delete(sql, Arrays.asList(parameters)); - } - - public int delete(String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = getConnection(); - return delete(conn, sql, parameters); - } finally { - JdbcUtils.close(conn); - } - } - - public int update(String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = getConnection(); - return update(conn, sql, parameters); - } finally { - JdbcUtils.close(conn); - } - } - - public void insertToTable(String tableName, Map map) throws SQLException { - StringBuilder sql = new StringBuilder() // - .append("insert into ") // - .append(tableName) // - .append("("); // - - int nameCount = 0; - for (String name : map.keySet()) { - if (nameCount > 0) { - sql.append(","); - } - sql.append(name); - nameCount++; - } - sql.append(") values ("); - for (int i = 0; i < nameCount; ++i) { - if (i != 0) { - sql.append(","); - } - sql.append("?"); - } - sql.append(")"); - - List parameters = new ArrayList(map.values()); - - insert(sql.toString(), parameters); - } - - public void insert(String sql, Object... parameters) throws SQLException { - insert(sql, Arrays.asList(parameters)); - } - - public void insert(String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = getConnection(); - insert(conn, sql, parameters); - } finally { - JdbcUtils.close(conn); - } - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/MappingObject.java b/src/main/java/com/alibaba/druid/mapping/MappingObject.java deleted file mode 100644 index 0ddd4edd71..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/MappingObject.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -import java.util.HashMap; -import java.util.Map; - - -public class MappingObject { - private Map attributes; - - public Map getAttributes() { - if (attributes == null) { - attributes = new HashMap(1); - } - - return attributes; - } - - public Object getAttribute(String name) { - if (attributes == null) { - return null; - } - - return attributes.get(name); - } - - public void putAttribute(String name, Object value) { - if (attributes == null) { - attributes = new HashMap(1); - } - - attributes.put(name, value); - } - - public Map getAttributesDirect() { - return attributes; - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/Property.java b/src/main/java/com/alibaba/druid/mapping/Property.java deleted file mode 100644 index b84cc8673c..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/Property.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping; - -public class Property extends MappingObject { - private String name; - private String desciption; - private String dbColumnName; - - public Property(){ - } - - public Property(String name, String desciption, String dbColumnName){ - this.name = name; - this.desciption = desciption; - this.dbColumnName = dbColumnName; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDbColumnName() { - return dbColumnName; - } - - public void setDbColumnName(String dbColumnName) { - this.dbColumnName = dbColumnName; - } - - public String getDesciption() { - return desciption; - } - - public void setDesciption(String desciption) { - this.desciption = desciption; - } - - public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("{name="); - buf.append(name); - - buf.append(", dbColumnName="); - buf.append(dbColumnName); - - buf.append("}"); - - return buf.toString(); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingProvider.java b/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingProvider.java deleted file mode 100644 index e96b9e829d..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingProvider.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.List; - -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.expr.SQLNumberExpr; -import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock; -import com.alibaba.druid.sql.dialect.sqlserver.ast.Top; -import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerSelectParser; -import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; -import com.alibaba.druid.sql.dialect.sqlserver.visitor.MSSQLServerExportParameterVisitor; -import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor; -import com.alibaba.druid.sql.visitor.ExportParameterVisitor; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; - -public class MSSQLServerMappingProvider implements MappingProvider { - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine) { - return new MSSQLServerMappingVisitor(engine); - } - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine, MappingContext context) { - return new MSSQLServerMappingVisitor(engine, context); - } - - @Override - public SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out) { - return new SQLServerOutputVisitor(out); - } - - public SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql, MappingContext context) { - SQLServerSelectParser selectParser = new SQLServerSelectParser(sql); - SQLServerSelectQueryBlock query = (SQLServerSelectQueryBlock) selectParser.query(); - - Integer maxLimit = engine.getMaxLimit(); - - if (maxLimit != null) { - if (query.getTop() == null) { - Top top = new Top(); - top.setExpr(new SQLNumberExpr(maxLimit)); - query.setTop(top); - } else { - SQLNumericLiteralExpr rowCountExpr = (SQLNumericLiteralExpr) query.getTop().getExpr(); - int rowCount = rowCountExpr.getNumber().intValue(); - if (rowCount > maxLimit.intValue()) { - rowCountExpr.setNumber(maxLimit); - } - } - } - - return query; - } - - public SQLDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql, MappingContext context) { - SQLServerStatementParser parser = new SQLServerStatementParser(sql); - SQLDeleteStatement stmt = parser.parseDeleteStatement(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql, MappingContext context) { - SQLServerStatementParser parser = new SQLServerStatementParser(sql); - SQLUpdateStatement stmt = parser.parseUpdateStatement(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql, MappingContext context) { - SQLServerStatementParser parser = new SQLServerStatementParser(sql); - SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - @Override - public ExportParameterVisitor createExportParameterVisitor(List parameters) { - return new MSSQLServerExportParameterVisitor(parameters); - } - - @Override - public List explain(MappingEngine engine, String sql) { - SQLServerStatementParser parser = new SQLServerStatementParser(sql); - return parser.parseStatementList(); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingVisitor.java b/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingVisitor.java deleted file mode 100644 index 8003f4de28..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MSSQLServerMappingVisitor.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock; -import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitorAdapter; - -public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implements MappingVisitor { - - private final MappingEngine engine; - private final Map tableSources = new LinkedHashMap(); - - private final MappingContext context; - private final List propertyValues = new ArrayList(); - private int variantIndex = 0; - private final List unresolveList = new ArrayList(); - - public MSSQLServerMappingVisitor(MappingEngine engine){ - this(engine, new MappingContext()); - } - - public MSSQLServerMappingVisitor(MappingEngine engine, MappingContext context){ - this.engine = engine; - this.context = context; - } - - public MappingEngine getEngine() { - return engine; - } - - public List getParameters() { - return context.getParameters(); - } - - public MappingContext getContext() { - return context; - } - - public List getPropertyValues() { - return propertyValues; - } - - public LinkedHashMap getEntities() { - return engine.getEntities(); - } - - public Map getTableSources() { - return tableSources; - } - - @Override - public String resolveTableName(Entity entity) { - return engine.resolveTableName(entity, context); - } - - @Override - public String resovleColumnName(Entity entity, Property property) { - return engine.resovleColumnName(entity, property, context); - } - - public Entity getEntity(String name) { - return MappingVisitorUtils.getEntity(this, name); - } - - @Override - public boolean visit(SQLSelectItem x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLServerSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLIdentifierExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLPropertyExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLBinaryOpExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLAllColumnExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLJoinTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLExprTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public int getAndIncrementVariantIndex() { - return variantIndex++; - } - - public int getVariantIndex() { - return variantIndex; - } - - public List getUnresolveList() { - return unresolveList; - } - - @Override - public void afterResolve() { - MappingVisitorUtils.afterResolve(this); - } - -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MappingProvider.java b/src/main/java/com/alibaba/druid/mapping/spi/MappingProvider.java deleted file mode 100644 index 11e4f69b80..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MappingProvider.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.List; - -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.visitor.ExportParameterVisitor; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; - -public interface MappingProvider { - - MappingVisitor createMappingVisitor(MappingEngine engine); - - MappingVisitor createMappingVisitor(MappingEngine engine, MappingContext context); - - ExportParameterVisitor createExportParameterVisitor(List parameters); - - SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out); - - List explain(MappingEngine engine, String sql); - - SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql, MappingContext context); - - SQLDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql, MappingContext context); - - SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql, MappingContext context); - - SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql, MappingContext context); -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitor.java b/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitor.java deleted file mode 100644 index dd95b70a9f..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitor.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.visitor.SQLASTVisitor; - -public interface MappingVisitor extends SQLASTVisitor { - MappingEngine getEngine(); - - List getParameters(); - - MappingContext getContext(); - - Entity getEntity(String name); - - Map getEntities(); - - Map getTableSources(); - - String resolveTableName(Entity entity); - - String resovleColumnName(Entity entity, Property property); - - List getPropertyValues(); - - int getAndIncrementVariantIndex(); - - List getUnresolveList(); - - void afterResolve(); -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitorUtils.java b/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitorUtils.java deleted file mode 100644 index aef8fb3189..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MappingVisitorUtils.java +++ /dev/null @@ -1,493 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.Iterator; -import java.util.Map; - -import com.alibaba.druid.mapping.DruidMappingException; -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator; -import com.alibaba.druid.sql.ast.expr.SQLCharExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; -import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; -import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; - -public class MappingVisitorUtils { - - private static final String MAPPING_VALUE = "mapping.value"; - private static final String MAPPING_PROPERTY = "mapping.property"; - private static final String MAPPING_ENTITY = "mapping.entity"; - - public static boolean visit(MappingVisitor visitor, SQLExprTableSource x) { - SQLExpr expr = x.getExpr(); - - if (expr instanceof SQLIdentifierExpr) { - SQLIdentifierExpr tableExpr = (SQLIdentifierExpr) expr; - String entityName = tableExpr.getName(); - - Entity entity = (Entity) x.getAttribute(MAPPING_ENTITY); - - if (entity == null) { - entity = visitor.getEntity(entityName); - } - - if (entity == null) { - throw new DruidMappingException("entity not found : " + entityName); - } - - if (x.getParent() instanceof SQLSelectQueryBlock) { - SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getParent(); - if (queryBlock.getAttribute(MAPPING_ENTITY) == null) { - queryBlock.putAttribute(MAPPING_ENTITY, entity); - } - } - - x.putAttribute(MAPPING_ENTITY, entity); - String tableName = visitor.resolveTableName(entity); - tableExpr.setName(tableName); - - visitor.getTableSources().put(entityName, x); - } - - if (x.getAlias() != null) { - visitor.getTableSources().put(x.getAlias(), x); - } - - return false; - } - - public static boolean visit(MappingVisitor visitor, SQLTableSource x) { - if (x.getAlias() != null) { - visitor.getTableSources().put(x.getAlias(), x); - } - - return true; - } - - public static boolean fillSelectList(MappingVisitor visitor, SQLSelectQueryBlock x) { - Entity entity = (Entity) x.getAttribute(MAPPING_ENTITY); - - if (entity == null && x.getFrom() != null) { - entity = (Entity) x.getFrom().getAttribute(MAPPING_ENTITY); - } - - if (entity == null) { - return false; - } - - x.getSelectList().clear(); - - for (Property property : entity.getProperties().values()) { - String columnName = visitor.resovleColumnName(entity, property); - String alias = null; - if (visitor.getContext().isGenerateAlias()) { - alias = '"' + property.getName() + '"'; - } - - SQLExpr expr = new SQLIdentifierExpr(columnName); - - expr.putAttribute(MAPPING_ENTITY, entity); - expr.putAttribute(MAPPING_PROPERTY, property); - - SQLSelectItem selelctItem = new SQLSelectItem(expr, alias); - - x.getSelectList().add(selelctItem); - } - - return true; - } - - public static boolean visit(MappingVisitor visitor, SQLPropertyExpr x) { - SQLIdentifierExpr ownerExpr = (SQLIdentifierExpr) x.getOwner(); - String ownerName = ownerExpr.getName(); - - String propertyName = x.getName(); - - Property property = null; - Entity entity = visitor.getEntity(ownerName); - - if (entity == null) { - visitor.getUnresolveList().add(x); - return false; - } - property = entity.getProperty(propertyName); - - if (property == null) { - throw new DruidMappingException("property not found : " + propertyName); - } - - String dbColumName = visitor.resovleColumnName(entity, property); - x.setName(dbColumName); - x.putAttribute(MAPPING_PROPERTY, property); - x.putAttribute(MAPPING_ENTITY, entity); - - if (x.getParent() instanceof SQLSelectItem) { - SQLSelectItem selectItem = (SQLSelectItem) x.getParent(); - if (visitor.getContext().isGenerateAlias() && selectItem.getAlias() == null) { - selectItem.setAlias('"' + property.getName() + '"'); - } - } - - return false; - } - - public static boolean visit(MappingVisitor visitor, SQLAllColumnExpr x) { - if (visitor.getContext().isExplainAllColumnToList()) { - visitor.getUnresolveList().add(x); - } - - return true; - } - - public static boolean visit(MappingVisitor visitor, SQLIdentifierExpr x) { - String propertyName = x.getName(); - - Property property = null; - Entity propertyEntity = null; - - for (SQLTableSource tableSource : visitor.getTableSources().values()) { - Entity entity = (Entity) tableSource.getAttribute(MAPPING_ENTITY); - if (entity != null) { - property = entity.getProperty(propertyName); - if (property != null) { - propertyEntity = entity; - break; - } - } - } - - if (property == null) { - visitor.getUnresolveList().add(x); - return false; - } - - String dbColumName = visitor.resovleColumnName(propertyEntity, property); - x.setName(dbColumName); - - x.putAttribute(MAPPING_PROPERTY, property); - x.putAttribute(MAPPING_ENTITY, propertyEntity); - - if (visitor.getContext().isGenerateAlias() && x.getParent() instanceof SQLSelectItem) { - SQLSelectItem selectItem = (SQLSelectItem) x.getParent(); - if (visitor.getContext().isGenerateAlias() && selectItem.getAlias() == null) { - selectItem.setAlias('"' + property.getName() + '"'); - } - } - - return false; - } - - public static boolean visit(MappingVisitor visitor, SQLSelectItem x) { - x.getExpr().setParent(x); - return true; - } - - public static boolean visit(MappingVisitor visitor, SQLBinaryOpExpr x) { - x.getLeft().setParent(x); - x.getRight().setParent(x); - - if (x.getOperator() == SQLBinaryOperator.Equality) { - if (x.getLeft() instanceof SQLIdentifierExpr && isSimpleValue(visitor, x.getRight())) { - visit(visitor, (SQLIdentifierExpr) x.getLeft()); - x.getRight().accept(visitor); - - Entity entity = (Entity) x.getLeft().getAttribute(MAPPING_ENTITY); - Property property = (Property) x.getLeft().getAttribute(MAPPING_PROPERTY); - Object value = x.getRight().getAttribute(MAPPING_VALUE); - - PropertyValue propertyValue = new PropertyValue(entity, property, value); - propertyValue.putAttribute("mapping.expr", x.getRight()); - - visitor.getPropertyValues().add(propertyValue); - - return false; - } - - if (x.getLeft() instanceof SQLPropertyExpr && isSimpleValue(visitor, x.getRight())) { - visit(visitor, (SQLPropertyExpr) x.getLeft()); - x.getRight().accept(visitor); - - Entity entity = (Entity) x.getLeft().getAttribute(MAPPING_ENTITY); - Property property = (Property) x.getLeft().getAttribute(MAPPING_PROPERTY); - Object value = x.getRight().getAttribute(MAPPING_VALUE); - - PropertyValue propertyValue = new PropertyValue(entity, property, value); - propertyValue.putAttribute("mapping.expr", x.getRight()); - - visitor.getPropertyValues().add(propertyValue); - - return false; - } - } - - return true; - } - - private static boolean isSimpleValue(MappingVisitor visitor, SQLExpr expr) { - if (expr instanceof SQLNumericLiteralExpr) { - expr.putAttribute(MAPPING_VALUE, ((SQLNumericLiteralExpr) expr).getNumber()); - return true; - } - - if (expr instanceof SQLCharExpr) { - expr.putAttribute(MAPPING_VALUE, ((SQLCharExpr) expr).getText()); - return true; - } - - if (expr instanceof SQLVariantRefExpr) { - int varIndex = ((SQLVariantRefExpr) expr).getIndex(); - - if (varIndex != -1 && visitor.getParameters().size() > varIndex) { - Object parameter = visitor.getParameters().get(varIndex); - expr.putAttribute(MAPPING_VALUE, parameter); - } - - return true; - } - - return false; - } - - public static void afterResolve(MappingVisitor visitor) { - for (Iterator iter = visitor.getUnresolveList().iterator(); iter.hasNext();) { - SQLExpr expr = iter.next(); - if (expr instanceof SQLIdentifierExpr) { - if (resolve(visitor, (SQLIdentifierExpr) expr)) { - iter.remove(); - } - } else if (expr instanceof SQLPropertyExpr) { - if (resolve(visitor, (SQLPropertyExpr) expr)) { - iter.remove(); - } - } else if (expr instanceof SQLAllColumnExpr) { - if (resolve(visitor, (SQLAllColumnExpr) expr)) { - iter.remove(); - } - } - } - } - - public static boolean resolve(MappingVisitor visitor, SQLAllColumnExpr x) { - if (!(x.getParent() instanceof SQLSelectItem)) { - return true; - } - SQLSelectItem selectItem = (SQLSelectItem) x.getParent(); - - if (!(selectItem.getParent() instanceof SQLSelectQueryBlock)) { - return true; - } - - SQLSelectQueryBlock select = (SQLSelectQueryBlock) selectItem.getParent(); - - if (select.getSelectList().size() == 1) { - if (select.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) { - boolean result = fillSelectList(visitor, select); - if (!result && visitor.getContext().isExplainAllColumnToList()) { - visitor.getUnresolveList().add(x); - } - } - } - - return false; - } - - public static boolean resolve(MappingVisitor visitor, SQLIdentifierExpr x) { - String propertyName = x.getName(); - - for (SQLTableSource tableSource : visitor.getTableSources().values()) { - Entity entity = (Entity) tableSource.getAttribute(MAPPING_ENTITY); - if (entity != null) { - Property property = entity.getProperty(propertyName); - if (property != null) { - String columnName = visitor.resovleColumnName(entity, property); - x.setName(columnName); - - x.putAttribute(MAPPING_ENTITY, entity); - x.putAttribute(MAPPING_PROPERTY, property); - - if (visitor.getContext().isGenerateAlias() && x.getParent() instanceof SQLSelectItem) { - SQLSelectItem selectItem = (SQLSelectItem) x.getParent(); - if (selectItem.getAlias() == null) { - selectItem.setAlias('"' + property.getName() + '"'); - } - } - - return true; - } - } - } - - return false; - } - - public static boolean resolve(MappingVisitor visitor, SQLPropertyExpr x) { - if (x.getOwner() instanceof SQLIdentifierExpr) { - String ownerName = ((SQLIdentifierExpr) x.getOwner()).getName(); - SQLTableSource tableSource = visitor.getTableSources().get(ownerName); - Entity entity = (Entity) tableSource.getAttribute(MAPPING_ENTITY); - - if (entity != null) { - Property property = entity.getProperty(x.getName()); - if (property != null) { - String columnName = visitor.resovleColumnName(entity, property); - x.setName(columnName); - x.putAttribute(MAPPING_ENTITY, entity); - x.putAttribute(MAPPING_PROPERTY, property); - - if (visitor.getContext().isGenerateAlias() && x.getParent() instanceof SQLSelectItem) { - SQLSelectItem selectItem = (SQLSelectItem) x.getParent(); - if (selectItem.getAlias() == null) { - selectItem.setAlias('"' + property.getName() + '"'); - } - } - - return true; - } - } - } - - return false; - } - - public static boolean visit(MappingVisitor visitor, MySqlSelectQueryBlock x) { - Integer maxLimit = visitor.getEngine().getMaxLimit(); - - if (maxLimit != null) { - if (x.getLimit() == null) { - Limit limit = new Limit(); - limit.setRowCount(new SQLIntegerExpr(maxLimit)); - x.setLimit(limit); - } else { - SQLNumericLiteralExpr rowCountExpr = (SQLNumericLiteralExpr) x.getLimit().getRowCount(); - int rowCount = rowCountExpr.getNumber().intValue(); - if (rowCount > maxLimit.intValue()) { - rowCountExpr.setNumber(maxLimit); - } - } - } - - return visit(visitor, (SQLSelectQueryBlock) x); - } - - public static boolean visit(MappingVisitor visitor, SQLSelectQueryBlock x) { - if (x.getSelectList().size() == 0) { - SQLAllColumnExpr expr = new SQLAllColumnExpr(); - SQLSelectItem selectItem = new SQLSelectItem(expr); - x.getSelectList().add(selectItem); - - expr.setParent(selectItem); - } - - if (x.getFrom() == null) { - Entity firstEntity = visitor.getEngine().getFirstEntity(visitor.getContext()); - SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName())); - from.putAttribute(MAPPING_ENTITY, firstEntity); - x.setFrom(from); - x.putAttribute(MAPPING_ENTITY, firstEntity); - } - - for (SQLSelectItem item : x.getSelectList()) { - item.setParent(x); - item.getExpr().setParent(item); - } - - x.getFrom().setParent(x); - if (x.getWhere() != null) { - x.getWhere().setParent(x); - } - - if (x.getInto() != null) { - x.getInto().setParent(x); - } - - return true; - } - - public static Entity getEntity(MappingVisitor visitor, String name) { - SQLTableSource tableSource = visitor.getTableSources().get(name); - - if (tableSource != null) { - Entity entity = (Entity) tableSource.getAttribute(MAPPING_ENTITY); - if (entity != null) { - return entity; - } - - if (tableSource instanceof SQLExprTableSource) { - SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr(); - - if (expr instanceof SQLIdentifierExpr) { - name = ((SQLIdentifierExpr) expr).getName(); - } else { - return null; - } - } else { - return null; - } - } - - Entity entity = visitor.getEntities().get(name); - - if (entity == null) { - for (Map.Entry entry : visitor.getEntities().entrySet()) { - if (entry.getKey().equalsIgnoreCase(name)) { - entity = entry.getValue(); - break; - } - } - } - - return entity; - } - - public static void setTableSource(MappingEngine engine, SQLDeleteStatement stmt, MappingContext context) { - if (stmt.getExprTableSource() == null) { - Entity entity = engine.getFirstEntity(context); - stmt.setTableSource(new SQLIdentifierExpr(entity.getName())); - } - } - - public static void setTableSource(MappingEngine engine, SQLUpdateStatement stmt, MappingContext context) { - if (stmt.getTableSource() == null) { - Entity entity = engine.getFirstEntity(context); - stmt.setTableSource(new SQLIdentifierExpr(entity.getName())); - } - } - - public static void setTableSource(MappingEngine engine, SQLInsertStatement stmt, MappingContext context) { - if (stmt.getTableSource() == null) { - Entity entity = engine.getFirstEntity(context); - stmt.setTableSource(new SQLIdentifierExpr(entity.getName())); - } - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingProvider.java b/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingProvider.java deleted file mode 100644 index b6c9d7fd11..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingProvider.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.List; - -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; -import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; -import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor; -import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor; -import com.alibaba.druid.sql.visitor.ExportParameterVisitor; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; - -public class MySqlMappingProvider implements MappingProvider { - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine) { - return new MySqlMappingVisitor(engine); - } - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine, MappingContext context) { - return new MySqlMappingVisitor(engine, context); - } - - @Override - public SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out) { - return new MySqlOutputVisitor(out); - } - - public SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql, MappingContext context) { - MySqlSelectParser selectParser = new MySqlSelectParser(sql); - MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) selectParser.query(); - - Integer maxLimit = engine.getMaxLimit(); - - if (maxLimit != null) { - if (query.getLimit() == null) { - Limit limit = new Limit(); - limit.setRowCount(new SQLIntegerExpr(maxLimit)); - query.setLimit(limit); - } else { - SQLNumericLiteralExpr rowCountExpr = (SQLNumericLiteralExpr) query.getLimit().getRowCount(); - int rowCount = rowCountExpr.getNumber().intValue(); - if (rowCount > maxLimit.intValue()) { - rowCountExpr.setNumber(maxLimit); - } - } - } - - return query; - } - - public MySqlDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql, MappingContext context) { - MySqlStatementParser parser = new MySqlStatementParser(sql); - - MySqlDeleteStatement stmt = parser.parseDeleteStatement(); - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql, MappingContext context) { - MySqlStatementParser parser = new MySqlStatementParser(sql); - SQLUpdateStatement stmt = parser.parseUpdateStatement(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql, MappingContext context) { - MySqlStatementParser parser = new MySqlStatementParser(sql); - SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - @Override - public ExportParameterVisitor createExportParameterVisitor(List parameters) { - return new MySqlExportParameterVisitor(parameters); - } - - @Override - public List explain(MappingEngine engine, String sql) { - MySqlStatementParser parser = new MySqlStatementParser(sql); - return parser.parseStatementList(); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingVisitor.java b/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingVisitor.java deleted file mode 100644 index 80e8ed1381..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/MySqlMappingVisitor.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter; - -public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements MappingVisitor { - - private final MappingEngine engine; - private final Map tableSources = new LinkedHashMap(); - - private final MappingContext context; - private final List propertyValues = new ArrayList(); - private int variantIndex = 0; - private final List unresolveList = new ArrayList(); - - public MySqlMappingVisitor(MappingEngine engine){ - this(engine, new MappingContext()); - } - - public MySqlMappingVisitor(MappingEngine engine, MappingContext context){ - this.engine = engine; - this.context = context; - } - - public MappingEngine getEngine() { - return engine; - } - - public List getParameters() { - return context.getParameters(); - } - - public MappingContext getContext() { - return context; - } - - public List getPropertyValues() { - return propertyValues; - } - - public Map getTableSources() { - return tableSources; - } - - public Map getEntities() { - return engine.getEntities(); - } - - @Override - public String resolveTableName(Entity entity) { - return engine.resolveTableName(entity, context); - } - - @Override - public String resovleColumnName(Entity entity, Property property) { - return engine.resovleColumnName(entity, property, context); - } - - public Entity getEntity(String name) { - return MappingVisitorUtils.getEntity(this, name); - } - - @Override - public boolean visit(SQLSelectItem x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(MySqlSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLIdentifierExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLPropertyExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLBinaryOpExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLAllColumnExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLJoinTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLExprTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public int getAndIncrementVariantIndex() { - return variantIndex++; - } - - public int getVariantIndex() { - return variantIndex; - } - - public List getUnresolveList() { - return unresolveList; - } - - @Override - public void afterResolve() { - MappingVisitorUtils.afterResolve(this); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingProvider.java b/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingProvider.java deleted file mode 100644 index 757943d246..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingProvider.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.List; - -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.sql.ast.SQLStatement; -import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; -import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement; -import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock; -import com.alibaba.druid.sql.dialect.oracle.parser.OracleSelectParser; -import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser; -import com.alibaba.druid.sql.dialect.oracle.visitor.OracleExportParameterVisitor; -import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor; -import com.alibaba.druid.sql.visitor.ExportParameterVisitor; -import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; - -public class OracleMappingProvider implements MappingProvider { - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine) { - return new OracleMappingVisitor(engine); - } - - @Override - public MappingVisitor createMappingVisitor(MappingEngine engine, MappingContext context) { - return new OracleMappingVisitor(engine, context); - } - - @Override - public SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out) { - return new OracleOutputVisitor(out, false); - } - - public SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql, MappingContext context) { - OracleSelectParser selectParser = new OracleSelectParser(sql); - OracleSelectQueryBlock query = (OracleSelectQueryBlock) selectParser.query(); - - return query; - } - - public OracleDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql, MappingContext context) { - OracleStatementParser parser = new OracleStatementParser(sql); - OracleDeleteStatement stmt = parser.parseDeleteStatement(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql, MappingContext context) { - OracleStatementParser parser = new OracleStatementParser(sql); - SQLUpdateStatement stmt = parser.parseUpdateStatement(); - - MappingVisitorUtils.setTableSource(engine, stmt, context); - - return stmt; - } - - public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql, MappingContext context) { - OracleStatementParser parser = new OracleStatementParser(sql); - SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert(); - MappingVisitorUtils.setTableSource(engine, stmt, context); - return stmt; - - } - - @Override - public ExportParameterVisitor createExportParameterVisitor(List parameters) { - return new OracleExportParameterVisitor(parameters); - } - - @Override - public List explain(MappingEngine engine, String sql) { - OracleStatementParser parser = new OracleStatementParser(sql); - return parser.parseStatementList(); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingVisitor.java b/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingVisitor.java deleted file mode 100644 index deae7684b0..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/OracleMappingVisitor.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr; -import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectItem; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; -import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement; -import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock; -import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference; -import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter; - -public class OracleMappingVisitor extends OracleASTVisitorAdapter implements MappingVisitor { - - private final MappingEngine engine; - private final Map tableSources = new LinkedHashMap(); - - private final MappingContext context; - private final List propertyValues = new ArrayList(); - private int variantIndex = 0; - private final List unresolveList = new ArrayList(); - - public OracleMappingVisitor(MappingEngine engine){ - this(engine, new MappingContext()); - } - - public OracleMappingVisitor(MappingEngine engine, MappingContext context){ - this.engine = engine; - this.context = context; - } - - public MappingEngine getEngine() { - return engine; - } - - public List getParameters() { - return context.getParameters(); - } - - public MappingContext getContext() { - return context; - } - - public List getPropertyValues() { - return propertyValues; - } - - public LinkedHashMap getEntities() { - return engine.getEntities(); - } - - public Map getTableSources() { - return tableSources; - } - - @Override - public String resolveTableName(Entity entity) { - return engine.resolveTableName(entity, context); - } - - @Override - public String resovleColumnName(Entity entity, Property property) { - return engine.resovleColumnName(entity, property, context); - } - - public Entity getEntity(String name) { - return MappingVisitorUtils.getEntity(this, name); - } - - @Override - public boolean visit(SQLSelectItem x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(OracleSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSelectQueryBlock x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLIdentifierExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLPropertyExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLBinaryOpExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLAllColumnExpr x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLJoinTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLExprTableSource x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(OracleSelectTableReference x) { - return MappingVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(OracleDeleteStatement x) { - - return true; - } - - @Override - public int getAndIncrementVariantIndex() { - return variantIndex++; - } - - public int getVariantIndex() { - return variantIndex; - } - - public List getUnresolveList() { - return unresolveList; - } - - @Override - public void afterResolve() { - MappingVisitorUtils.afterResolve(this); - } -} diff --git a/src/main/java/com/alibaba/druid/mapping/spi/PropertyValue.java b/src/main/java/com/alibaba/druid/mapping/spi/PropertyValue.java deleted file mode 100644 index b273ef2c09..0000000000 --- a/src/main/java/com/alibaba/druid/mapping/spi/PropertyValue.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.mapping.spi; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingObject; -import com.alibaba.druid.mapping.Property; - -public class PropertyValue extends MappingObject { - - private Property property; - private Entity entity; - private Object value; - - public PropertyValue(Entity entity, Property property, Object value){ - this.property = property; - this.entity = entity; - this.value = value; - } - - public PropertyValue(){ - } - - public Property getProperty() { - return property; - } - - public void setProperty(Property property) { - this.property = property; - } - - public Entity getEntity() { - return entity; - } - - public void setEntity(Entity entity) { - this.entity = entity; - } - - public Object getValue() { - return value; - } - - public void setValue(Object value) { - this.value = value; - } - -} diff --git a/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java b/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java index 0efeec00d9..7933cf643a 100644 --- a/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java +++ b/src/main/java/com/alibaba/druid/sharding/ShardingDataSource.java @@ -15,6 +15,10 @@ */ package com.alibaba.druid.sharding; -public class ShardingDataSource { +import javax.sql.DataSource; + +import com.alibaba.druid.pool.DataSourceAdapter; + +public class ShardingDataSource extends DataSourceAdapter implements DataSource { } diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorOracleTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorOracleTest.java deleted file mode 100644 index 75b91b6d09..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorOracleTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import junit.framework.TestCase; - -import org.junit.Assert; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.mapping.spi.OracleMappingProvider; - -public class MappingVisitorOracleTest extends TestCase { - - MappingEngine engine = new MappingEngine(new OracleMappingProvider()); - - protected void setUp() throws Exception { - - Entity entity = new Entity(); - entity.setName("设备"); - entity.setDescription("Device"); - entity.setTableName("device"); - - entity.addProperty(new Property("编号", "", "serviceTag")); - entity.addProperty(new Property("IP地址", "", "ip")); - - engine.addEntity(entity); - } - - public void test_0() throws Exception { - MappingContext context = new MappingContext(); - context.setGenerateAlias(true); - context.setExplainAllColumnToList(true); - - Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", - engine.explainToSelectSQL("select *", context)); - Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context)); - Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'", context)); - - } - - public void test_delete() throws Exception { - Assert.assertEquals("DELETE FROM device\nWHERE ip = '127.0.0.1'", - engine.explainToDeleteSQL("WHERE IP地址 = '127.0.0.1'")); - } - - public void test_update() throws Exception { - Assert.assertEquals("UPDATE device\nSET serviceTag = ?\nWHERE ip = '127.0.0.1'", - engine.explainToUpdateSQL("SET 编号 = ? WHERE IP地址 = '127.0.0.1'")); - } - - public void test_insert() throws Exception { - Assert.assertEquals("INSERT INTO device\n\t(serviceTag, ip)\nVALUES\n(?, ?)", - engine.explainToInsertSQL("(编号, IP地址) VALUES (?, ?)")); - } - -} diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorSQLServerTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorSQLServerTest.java deleted file mode 100644 index d16739a053..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorSQLServerTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import junit.framework.TestCase; - -import org.junit.Assert; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.mapping.spi.MSSQLServerMappingProvider; - -public class MappingVisitorSQLServerTest extends TestCase { - - MappingEngine engine = new MappingEngine(new MSSQLServerMappingProvider()); - - protected void setUp() throws Exception { - Entity entity = new Entity(); - entity.setName("设备"); - entity.setDescription("Device"); - entity.setTableName("device"); - - entity.addProperty(new Property("编号", "", "serviceTag")); - entity.addProperty(new Property("IP地址", "", "ip")); - - engine.addEntity(entity); - } - - public void test_0() throws Exception { - MappingContext context = new MappingContext(); - context.setGenerateAlias(true); - context.setExplainAllColumnToList(true); - - Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *", context)); - Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context)); - - } - - public void test_1() throws Exception { - engine.setMaxLimit(10); - - MappingContext context = new MappingContext(); - context.setGenerateAlias(true); - context.setExplainAllColumnToList(true); - - Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *", context)); - Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context)); - - Assert.assertEquals("SELECT TOP 3 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("select top 3 编号, IP地址 WHERE IP地址 = '127.0.0.1'", context)); - - engine.setMaxLimit(null); - - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorTest.java deleted file mode 100644 index 75b23132af..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MappingVisitorTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import junit.framework.TestCase; - -import org.junit.Assert; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; - -public class MappingVisitorTest extends TestCase { - - MappingEngine engine = new MappingEngine(); - - protected void setUp() throws Exception { - Entity entity = new Entity(); - entity.setName("设备"); - entity.setDescription("Device"); - entity.setTableName("device"); - - entity.addProperty(new Property("编号", "", "serviceTag")); - entity.addProperty(new Property("IP地址", "", "ip")); - - engine.addEntity(entity); - } - - public void test_0() throws Exception { - - Assert.assertEquals("SELECT *\nFROM device", - engine.explainToSelectSQL("select *")); - Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'")); - Assert.assertEquals("SELECT *\nFROM device\nWHERE ip = '127.0.0.1'", - engine.explainToSelectSQL("WHERE IP地址 = '127.0.0.1'")); - - } - - public void test_delete() throws Exception { - Assert.assertEquals("DELETE FROM device\nWHERE ip = '127.0.0.1'", - engine.explainToDeleteSQL("WHERE IP地址 = '127.0.0.1'")); - } - - public void test_update() throws Exception { - Assert.assertEquals("UPDATE device\nSET serviceTag = ?\nWHERE ip = '127.0.0.1'", - engine.explainToUpdateSQL("SET 编号 = ? WHERE IP地址 = '127.0.0.1'")); - } - - public void test_insert() throws Exception { - Assert.assertEquals("INSERT INTO device (serviceTag, ip)\nVALUES (?, ?)", - engine.explainToInsertSQL("(编号, IP地址) VALUES (?, ?)")); - } - - public void test_1() throws Exception { - engine.setMaxLimit(10); - - Assert.assertEquals("SELECT *\nFROM device\nLIMIT 10", - engine.explainToSelectSQL("select *")); - Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 10", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1'")); - Assert.assertEquals("SELECT serviceTag, ip\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 3", - engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1' limit 3")); - Assert.assertEquals(0, engine.exportParameters(engine.explainToSelectSQLObject("select 1")).size()); - Assert.assertEquals(0, - engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0")).size()); - Assert.assertEquals(1, - engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0 and IP地址 = '127.0.0.1'")).size()); - Assert.assertEquals("127.0.0.1", - engine.exportParameters(engine.explainToSelectSQLObject("select 编号, IP地址 where 1 = 0 and IP地址 = '127.0.0.1'")).get(0)); - - engine.setMaxLimit(null); - - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingEngineTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingEngineTest.java deleted file mode 100644 index de3a0ad1da..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingEngineTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import java.util.List; -import java.util.Map; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.util.JdbcUtils; - -public class MySqlMappingEngineTest extends TestCase { - - private DruidDataSource dataSource; - private MappingEngine mapping = new MappingEngine(); - - protected void setUp() throws Exception { - dataSource = new DruidDataSource(); - dataSource.setMinIdle(1); - dataSource.setUrl("jdbc:h2:mem:test;"); - dataSource.setTestOnBorrow(false); - - JdbcUtils.execute(dataSource, "CREATE TABLE user (id INT, name VARCHAR(40))"); - - mapping.setDataSource(dataSource); - } - - protected void tearDown() throws Exception { - JdbcUtils.execute(dataSource, "DROP TABLE user"); - JdbcUtils.close(dataSource); - } - - public void test_curd() throws Exception { - { - List> list = mapping.select("select * from user"); - Assert.assertEquals(0, list.size()); - } - - mapping.insert("insert into user (id, name) values (?, ?)", 123, "wenshao"); - - { - List> list = mapping.select("select * from user"); - Assert.assertEquals(1, list.size()); - - Map data = list.get(0); - Assert.assertEquals(123, data.get("ID")); - Assert.assertEquals("wenshao", data.get("NAME")); - } - - { - List> list = mapping.select("select id \"id\", name \"name\" from user"); - Assert.assertEquals(1, list.size()); - - Map data = list.get(0); - Assert.assertEquals(123, data.get("id")); - Assert.assertEquals("wenshao", data.get("name")); - } - - mapping.delete("delete from user"); - - { - List> list = mapping.select("select * from user"); - Assert.assertEquals(0, list.size()); - } - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java deleted file mode 100644 index bdf8669d2a..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingShardingTest.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import java.util.Collections; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingContext; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; -import com.alibaba.druid.mapping.spi.MappingVisitor; -import com.alibaba.druid.mapping.spi.PropertyValue; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; - -public class MySqlMappingShardingTest extends TestCase { - - MappingEngine engine = new ShardingMappingEngine(); - - protected void setUp() throws Exception { - { - Entity entity = new Entity(); - entity.setName("用户"); - entity.setTableName("user"); - - entity.addProperty(new Property("名称", "", "uid")); - entity.addProperty(new Property("昵称", "", "name")); - - engine.addEntity(entity); - } - { - Entity entity = new Entity(); - entity.setName("英雄"); - entity.setTableName("hero"); - - entity.addProperty(new Property("用户名", "", "uid")); - entity.addProperty(new Property("称号", "", "name")); - - engine.addEntity(entity); - } - } - - public void test_0() throws Exception { - String oql = "select * from 用户 u where u.名称 = 'a'"; - - MappingContext context = new MappingContext(); - context.setGenerateAlias(true); - context.setExplainAllColumnToList(true); - String sql = engine.explainToSelectSQL(oql, context); - - String expected = "SELECT uid AS \"名称\", name AS \"昵称\"\n" + // - "FROM user_a u\n" + // - "WHERE u.uid = 'a'"; - - Assert.assertEquals(expected, sql); - } - - public void test_1() throws Exception { - String oql = "select * from 用户 u where u.名称 = ?"; - - MappingContext context = new MappingContext(Collections. singletonList("a")); - context.setGenerateAlias(true); - String sql = engine.explainToSelectSQL(oql, context); - - String expected = "SELECT *\n" + // - "FROM user_a u\n" + // - "WHERE u.uid = ?"; - - Assert.assertEquals(expected, sql); - } - - public void test_2() throws Exception { - String oql = "select * from 用户 u where u.名称 = ?"; - MappingContext context = new MappingContext(Collections. singletonList("b")); - context.setExplainAllColumnToList(true); - - String sql = engine.explainToSelectSQL(oql, context); - - String expected = "SELECT uid, name\n" + // - "FROM user_x u\n" + // - "WHERE u.uid = ?"; - - Assert.assertEquals(expected, sql); - } - - public void test_3() throws Exception { - String oql = "select h.用户名, u.昵称 from 用户 u left join 英雄 h on h.用户名 = u.名称 where u.名称 = 2"; - - String sql = engine.explainToSelectSQL(oql, Collections. singletonList("b")); - - String expected = "SELECT h.uid, u.name\n" + // - "FROM user_x u LEFT JOIN hero h ON h.uid = u.uid\n" + // - "WHERE u.uid = 2"; - - System.out.println(sql); - Assert.assertEquals(expected, sql); - } - - public void test_4() throws Exception { - String oql = "select * from 用户 u where 名称 = ?"; - - String sql = engine.explainToSelectSQL(oql, Collections. singletonList("b")); - - String expected = "SELECT *\n" + // - "FROM user_x u\n" + // - "WHERE uid = ?"; - - Assert.assertEquals(expected, sql); - } - - public void test_5() throws Exception { - String oql = "select h.用户名, u.昵称 from 用户 u left join 英雄 h on h.用户名 = u.名称 where 名称 = 2"; - - String sql = engine.explainToSelectSQL(oql, Collections. singletonList("b")); - - String expected = "SELECT h.uid, u.name\n" + // - "FROM user_x u LEFT JOIN hero h ON h.uid = u.uid\n" + // - "WHERE uid = 2"; - - System.out.println(sql); - Assert.assertEquals(expected, sql); - } - - public static class ShardingMappingEngine extends MappingEngine { - - public void afterResole(MappingVisitor visitor) { - String shardingTableName = null; - for (PropertyValue entry : visitor.getPropertyValues()) { - Entity entity = entry.getEntity(); - Property property = entry.getProperty(); - Object value = entry.getValue(); - - if ("用户".equals(entity.getName()) && "名称".equals(property.getName())) { - if ("a".equals(value)) { - shardingTableName = "user_a"; - } else { - shardingTableName = "user_x"; - } - } - } - - for (SQLTableSource tableSource : visitor.getTableSources().values()) { - Entity entity = (Entity) tableSource.getAttribute("mapping.entity"); - if (entity != null && "用户".equals(entity.getName())) { - SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource; - exprTableSource.setExpr(new SQLIdentifierExpr(shardingTableName)); - } - } - } - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingTest.java b/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingTest.java deleted file mode 100644 index 083f27dcd6..0000000000 --- a/src/test/java/com/alibaba/druid/bvt/mapping/MySqlMappingTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.druid.bvt.mapping; - -import junit.framework.TestCase; - -import com.alibaba.druid.mapping.Entity; -import com.alibaba.druid.mapping.MappingEngine; -import com.alibaba.druid.mapping.Property; - - -public class MySqlMappingTest extends TestCase { - MappingEngine engine = new MappingEngine(); - - protected void setUp() throws Exception { - Entity entity = new Entity(); - entity.setName("用户"); - entity.setTableName("user"); - - entity.addProperty(new Property("名称", "", "uid")); - entity.addProperty(new Property("昵称", "", "name")); - - engine.addEntity(entity); - } - - public void test_0 () throws Exception { - String oql = "select * from 用户 u where u.名称 = 'a'"; - - String sql = engine.explainToSelectSQL(oql); - - System.out.println(sql); - } -}