mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for mapping
This commit is contained in:
@@ -2,6 +2,7 @@ package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
@@ -13,7 +14,7 @@ public class MSSQLServerMappingProvider implements MappingProvider {
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine) {
|
||||
return new MySqlMappingVisitor(engine.getEntities());
|
||||
return new MSSQLServerMappingVisitor(engine.getEntities());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +34,7 @@ public class MSSQLServerMappingProvider implements MappingProvider {
|
||||
top.setExpr(new SQLNumberExpr(maxLimit));
|
||||
query.setTop(top);
|
||||
} else {
|
||||
SQLNumberExpr rowCountExpr = (SQLNumberExpr) query.getTop().getExpr();
|
||||
SQLNumericLiteralExpr rowCountExpr = (SQLNumericLiteralExpr) query.getTop().getExpr();
|
||||
int rowCount = rowCountExpr.getNumber().intValue();
|
||||
if (rowCount > maxLimit.intValue()) {
|
||||
rowCountExpr.setNumber(maxLimit);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
@@ -30,10 +31,10 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
if (maxLimit != null) {
|
||||
if (query.getLimit() == null) {
|
||||
Limit limit = new Limit();
|
||||
limit.setRowCount(new SQLNumberExpr(maxLimit));
|
||||
limit.setRowCount(new SQLIntegerExpr(maxLimit));
|
||||
query.setLimit(limit);
|
||||
} else {
|
||||
SQLNumberExpr rowCountExpr = (SQLNumberExpr) query.getLimit().getRowCount();
|
||||
SQLNumericLiteralExpr rowCountExpr = (SQLNumericLiteralExpr) query.getLimit().getRowCount();
|
||||
int rowCount = rowCountExpr.getNumber().intValue();
|
||||
if (rowCount > maxLimit.intValue()) {
|
||||
rowCountExpr.setNumber(maxLimit);
|
||||
|
||||
@@ -24,4 +24,6 @@ public abstract class SQLNumericLiteralExpr extends SQLLiteralExpr {
|
||||
}
|
||||
|
||||
public abstract Number getNumber();
|
||||
|
||||
public abstract void setNumber(Number number);
|
||||
}
|
||||
|
||||
+10
@@ -72,4 +72,14 @@ public class OracleBinaryDoubleExpr extends SQLNumericLiteralExpr implements Ora
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumber(Number number) {
|
||||
if (number == null) {
|
||||
this.setValue(null);
|
||||
return;
|
||||
}
|
||||
|
||||
this.setValue(number.doubleValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
@@ -72,4 +72,14 @@ public class OracleBinaryFloatExpr extends SQLNumericLiteralExpr implements Orac
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumber(Number number) {
|
||||
if (number == null) {
|
||||
this.setValue(null);
|
||||
return;
|
||||
}
|
||||
|
||||
this.setValue(number.floatValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
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 {
|
||||
|
||||
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *"));
|
||||
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'"));
|
||||
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
engine.setMaxLimit(10);
|
||||
|
||||
Assert.assertEquals("SELECT TOP 10 serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device", engine.explainToSelectSQL("select *"));
|
||||
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'"));
|
||||
|
||||
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'"));
|
||||
|
||||
engine.setMaxLimit(null);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,8 @@ public class MappingVisitorTest extends TestCase {
|
||||
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"IP地址\"\nFROM device\nLIMIT 10", engine.explainToSelectSQL("select *"));
|
||||
Assert.assertEquals("SELECT serviceTag AS \"编号\", ip AS \"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 AS \"编号\", ip AS \"IP地址\"\nFROM device\nWHERE ip = '127.0.0.1'\nLIMIT 3",
|
||||
engine.explainToSelectSQL("select 编号, IP地址 WHERE IP地址 = '127.0.0.1' limit 3"));
|
||||
|
||||
engine.setMaxLimit(null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user