mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Improve null handling
This commit is contained in:
parent
acd71883ed
commit
a506aed4e3
@ -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 {
|
||||
|
||||
@ -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
|
||||
@ -751,6 +752,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)");
|
||||
|
||||
@ -13,6 +13,10 @@
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import static com.mysema.query.Constants.*;
|
||||
import static com.mysema.query.Target.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Date;
|
||||
@ -20,6 +24,13 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
@ -39,16 +50,8 @@ import com.mysema.query.types.query.NumberSubQuery;
|
||||
import com.mysema.query.types.template.NumberTemplate;
|
||||
import com.mysema.testutil.ExcludeIn;
|
||||
import com.mysema.testutil.IncludeIn;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import static com.mysema.query.Constants.*;
|
||||
import static com.mysema.query.Target.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SelectBase extends AbstractBaseTest {
|
||||
|
||||
@ -993,6 +996,28 @@ public class SelectBase extends AbstractBaseTest {
|
||||
query().uniqueResult(num.castToNum(Double.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@IncludeIn({MYSQL, ORACLE})
|
||||
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
|
||||
@IncludeIn({MYSQL, ORACLE})
|
||||
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)
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user