Merge pull request #1087 from querydsl/i921

Improve null handling
This commit is contained in:
Ruben Dijkstra 2014-12-19 18:43:43 +01:00
commit 58332a1582
7 changed files with 88 additions and 36 deletions

View File

@ -13,7 +13,6 @@
*/
package com.mysema.query.sql;
import javax.annotation.Nullable;
import java.lang.reflect.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -23,6 +22,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.common.primitives.Primitives;
@ -30,8 +34,6 @@ import com.mysema.query.sql.types.ArrayType;
import com.mysema.query.sql.types.Null;
import com.mysema.query.sql.types.Type;
import com.mysema.query.types.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Configuration for SQLQuery instances
@ -289,7 +291,7 @@ public final class Configuration {
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> void set(PreparedStatement stmt, Path<?> path, int i, T value) throws SQLException {
if (Null.class.isInstance(value)) {
Integer sqlType = path != null ? jdbcTypeMapping.get(path.getType()) : null;
Integer sqlType = path != null ? ColumnMetadata.getColumnMetadata(path).getJdbcType() : null;
if (sqlType != null) {
stmt.setNull(i, sqlType);
} else {

View File

@ -17,11 +17,12 @@ import java.sql.*;
import java.util.Map;
import java.util.Properties;
import org.hsqldb.types.Types;
import com.google.common.collect.Maps;
import com.mysema.query.ddl.CreateTableClause;
import com.mysema.query.ddl.DropTableClause;
import com.mysema.query.sql.*;
import org.hsqldb.types.Types;
/**
* @author tiwe
@ -287,6 +288,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
stmt.execute("drop table if exists NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 int)");
// xml
stmt.execute("drop table if exists XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -347,6 +352,10 @@ public final class Connections {
dropTable(templates, "DATE_TEST");
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 boolean)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -461,6 +470,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 char(1))");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -526,13 +539,16 @@ public final class Connections {
stmt.execute("alter table EMPLOYEE alter column id int auto_increment");
addEmployees(INSERT_INTO_EMPLOYEE);
// date_test and time_test
stmt.execute("drop table TIME_TEST if exists");
stmt.execute("drop table DATE_TEST if exists");
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 boolean)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -599,6 +615,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 boolean)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -751,6 +771,10 @@ public final class Connections {
dropTable(templates, "DATE_TEST");
stmt.execute("create table date_test(date_test date)");
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 number(1,0))");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL XMLTYPE)");
@ -843,6 +867,10 @@ public final class Connections {
stmt.execute(quote(CREATE_TABLE_TIMETEST, "TIME_TEST"));
stmt.execute(quote(CREATE_TABLE_DATETEST, "DATE_TEST"));
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table \"NUMBER_TEST\"(\"COL1\" boolean)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table \"XML_TEST\"(\"COL\" XML)");
@ -910,6 +938,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
stmt.execute("drop table if exists NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 integer)");
// xml
stmt.execute("drop table if exists XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");
@ -966,6 +998,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 bit)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL XML)");
@ -1037,6 +1073,10 @@ public final class Connections {
stmt.execute(CREATE_TABLE_TIMETEST);
stmt.execute(CREATE_TABLE_DATETEST);
// numbers
dropTable(templates, "NUMBER_TEST");
stmt.execute("create table NUMBER_TEST(col1 int)");
// xml
dropTable(templates, "XML_TEST");
stmt.execute("create table XML_TEST(COL varchar(128))");

View File

@ -28,6 +28,7 @@ import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@ -51,8 +52,6 @@ import com.mysema.query.types.template.NumberTemplate;
import com.mysema.testutil.ExcludeIn;
import com.mysema.testutil.IncludeIn;
import org.junit.Assert;
public class SelectBase extends AbstractBaseTest {
private static final Expression<?>[] NO_EXPRESSIONS = new Expression[0];
@ -996,6 +995,27 @@ public class SelectBase extends AbstractBaseTest {
query().uniqueResult(num.castToNum(Double.class));
}
@Test
@ExcludeIn({CUBRID, DERBY, FIREBIRD, POSTGRES})
public void Number_As_Boolean() {
QNumberTest numberTest = QNumberTest.numberTest;
delete(numberTest).execute();
insert(numberTest).set(numberTest.col1Boolean, true).execute();
insert(numberTest).set(numberTest.col1Number, (byte)1).execute();
assertEquals(2, query().from(numberTest).list(numberTest.col1Boolean).size());
assertEquals(2, query().from(numberTest).list(numberTest.col1Number).size());
}
@Test
public void Number_As_Boolean_Null() {
QNumberTest numberTest = QNumberTest.numberTest;
delete(numberTest).execute();
insert(numberTest).setNull(numberTest.col1Boolean).execute();
insert(numberTest).setNull(numberTest.col1Number).execute();
assertEquals(2, query().from(numberTest).list(numberTest.col1Boolean).size());
assertEquals(2, query().from(numberTest).list(numberTest.col1Number).size());
}
@Test
public void Offset_Only() {
query().from(employee)

View File

@ -2,11 +2,9 @@ package com.mysema.query;
import static com.mysema.query.Constants.survey;
import static com.mysema.query.Target.MYSQL;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.mysema.query.sql.domain.QNumberTest;
import com.mysema.query.sql.mysql.MySQLQuery;
import com.mysema.testutil.IncludeIn;
@ -32,15 +30,4 @@ public class SelectMySQLBase extends AbstractBaseTest {
mysqlQuery().from(survey).straightJoin().list(survey.id);
}
@Test
@IncludeIn(MYSQL)
public void Tinyint() {
QNumberTest numberTest = QNumberTest.numberTest;
delete(numberTest).execute();
insert(numberTest).set(numberTest.col1Boolean, true).execute();
insert(numberTest).set(numberTest.col1Number, (byte)1).execute();
assertEquals(2, query().from(numberTest).list(numberTest.col1Boolean).size());
assertEquals(2, query().from(numberTest).list(numberTest.col1Number).size());
}
}

View File

@ -14,6 +14,7 @@
package com.mysema.query.sql.domain;
import java.math.BigDecimal;
import java.sql.Types;
import com.mysema.query.sql.ColumnMetadata;
import com.mysema.query.sql.ForeignKey;
@ -65,13 +66,13 @@ public class QEmployee extends RelationalPathBase<Employee> {
}
protected void addMetadata() {
addMetadata(id, ColumnMetadata.named("ID"));
addMetadata(firstname, ColumnMetadata.named("FIRSTNAME"));
addMetadata(lastname, ColumnMetadata.named("LASTNAME"));
addMetadata(salary, ColumnMetadata.named("SALARY"));
addMetadata(datefield, ColumnMetadata.named("DATEFIELD"));
addMetadata(timefield, ColumnMetadata.named("TIMEFIELD"));
addMetadata(superiorId, ColumnMetadata.named("SUPERIOR_ID"));
addMetadata(id, ColumnMetadata.named("ID").ofType(Types.INTEGER));
addMetadata(firstname, ColumnMetadata.named("FIRSTNAME").ofType(Types.VARCHAR));
addMetadata(lastname, ColumnMetadata.named("LASTNAME").ofType(Types.VARCHAR));
addMetadata(salary, ColumnMetadata.named("SALARY").ofType(Types.DECIMAL));
addMetadata(datefield, ColumnMetadata.named("DATEFIELD").ofType(Types.DATE));
addMetadata(timefield, ColumnMetadata.named("TIMEFIELD").ofType(Types.TIME));
addMetadata(superiorId, ColumnMetadata.named("SUPERIOR_ID").ofType(Types.INTEGER));
}
}

View File

@ -30,7 +30,7 @@ public class QNumberTest extends RelationalPathSpatial<QNumberTest> {
public final NumberPath<Byte> col1Number = createNumber("col2", Byte.class);
public QNumberTest(String variable) {
super(QNumberTest.class, forVariable(variable), "null", "NUMBER_TEST");
super(QNumberTest.class, forVariable(variable), "PUBLIC", "NUMBER_TEST");
addMetadata();
}
@ -40,18 +40,18 @@ public class QNumberTest extends RelationalPathSpatial<QNumberTest> {
}
public QNumberTest(Path<? extends QNumberTest> path) {
super(path.getType(), path.getMetadata(), "null", "NUMBER_TEST");
super(path.getType(), path.getMetadata(), "PUBLIC", "NUMBER_TEST");
addMetadata();
}
public QNumberTest(PathMetadata<?> metadata) {
super(QNumberTest.class, metadata, "null", "NUMBER_TEST");
super(QNumberTest.class, metadata, "PUBLIC", "NUMBER_TEST");
addMetadata();
}
public void addMetadata() {
addMetadata(col1Boolean, ColumnMetadata.named("col1").withIndex(1).ofType(Types.BIT));
addMetadata(col1Number, ColumnMetadata.named("col1").withIndex(1).ofType(Types.BIT));
addMetadata(col1Boolean, ColumnMetadata.named("COL1").withIndex(1).ofType(Types.BIT));
addMetadata(col1Number, ColumnMetadata.named("COL1").withIndex(1).ofType(Types.BIT));
}
}

View File

@ -13,6 +13,8 @@
*/
package com.mysema.query.sql.domain;
import java.sql.Types;
import com.mysema.query.sql.ColumnMetadata;
import com.mysema.query.sql.PrimaryKey;
import com.mysema.query.sql.RelationalPathBase;
@ -48,9 +50,9 @@ public class QSurvey extends RelationalPathBase<QSurvey>{
}
protected void addMetadata() {
addMetadata(name, ColumnMetadata.named("NAME"));
addMetadata(name2, ColumnMetadata.named("NAME2"));
addMetadata(id, ColumnMetadata.named("ID"));
addMetadata(name, ColumnMetadata.named("NAME").ofType(Types.VARCHAR));
addMetadata(name2, ColumnMetadata.named("NAME2").ofType(Types.VARCHAR));
addMetadata(id, ColumnMetadata.named("ID").ofType(Types.INTEGER));
}
}