mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Update for pg - Internal changes for DB Migration / DDL generation #369
This commit is contained in:
1 parent
22407506ff
commit
689d201b04
92 files changed
+2976
-661
No files matched your search
@@ -100,6 +100,8 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
*/
|
||||
private final IdType idType;
|
||||
|
||||
private final boolean idTypePlatformDefault;
|
||||
|
||||
private final IdGenerator idGenerator;
|
||||
|
||||
/**
|
||||
@@ -339,6 +341,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
this.defaultSelectClauseSet = deploy.parseDefaultSelectClause(defaultSelectClause);
|
||||
|
||||
this.idType = deploy.getIdType();
|
||||
this.idTypePlatformDefault = deploy.isIdTypePlatformDefault();
|
||||
this.idGenerator = deploy.getIdGenerator();
|
||||
this.sequenceName = deploy.getSequenceName();
|
||||
this.sequenceInitialValue = deploy.getSequenceInitialValue();
|
||||
@@ -1677,6 +1680,13 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
return idType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the identity is the platform default (not explicitly set).
|
||||
*/
|
||||
public boolean isIdTypePlatformDefault() {
|
||||
return idTypePlatformDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sequence name.
|
||||
*/
|
||||
|
||||
@@ -1062,8 +1062,14 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
}
|
||||
|
||||
if (desc.getIdType() == null) {
|
||||
if (desc.isPrimaryKeyCompoundOrNonNumeric()) {
|
||||
// assuming that this is a user supplied key like ISO country code or ISO currency code or lookup table code
|
||||
logger.debug("Expecting user defined identity on " + desc.getFullName() + " - not using db sequence or autoincrement");
|
||||
return;
|
||||
}
|
||||
// use the default. IDENTITY or SEQUENCE.
|
||||
desc.setIdType(dbIdentity.getIdType());
|
||||
desc.setIdTypePlatformDefault();
|
||||
}
|
||||
|
||||
if (IdType.GENERATOR.equals(desc.getIdType())) {
|
||||
|
||||
@@ -73,6 +73,11 @@ public class DeployBeanDescriptor<T> {
|
||||
*/
|
||||
private IdType idType;
|
||||
|
||||
/**
|
||||
* Set to true if the identity is default for the platform.
|
||||
*/
|
||||
private boolean idTypePlatformDefault;
|
||||
|
||||
/**
|
||||
* The name of an IdGenerator (optional).
|
||||
*/
|
||||
@@ -545,6 +550,20 @@ public class DeployBeanDescriptor<T> {
|
||||
this.idType = idType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set when the identity type is the platform default.
|
||||
*/
|
||||
public void setIdTypePlatformDefault() {
|
||||
this.idTypePlatformDefault = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true when the identity is the platform default.
|
||||
*/
|
||||
public boolean isIdTypePlatformDefault() {
|
||||
return idTypePlatformDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the DB sequence name (can be null).
|
||||
*/
|
||||
@@ -685,6 +704,25 @@ public class DeployBeanDescriptor<T> {
|
||||
return Collections.unmodifiableSet(set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the primary key is a compound key or if it's database type
|
||||
* is non-numeric (and hence not suitable for db identity or sequence.
|
||||
*/
|
||||
public boolean isPrimaryKeyCompoundOrNonNumeric() {
|
||||
|
||||
List<DeployBeanProperty> ids = propertiesId();
|
||||
if (ids.size() != 1) {
|
||||
// compound key
|
||||
return true;
|
||||
}
|
||||
DeployBeanProperty p = ids.get(0);
|
||||
if (p instanceof DeployBeanPropertyAssocOne<?>) {
|
||||
return ((DeployBeanPropertyAssocOne<?>)p).isCompound();
|
||||
} else {
|
||||
return !p.isDbNumberType();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Primary Key column assuming it is a single column (not
|
||||
* compound). This is for the purpose of defining a sequence name.
|
||||
|
||||
@@ -625,6 +625,35 @@ public class DeployBeanProperty {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDbNumberType() {
|
||||
return isNumericType(dbType);
|
||||
}
|
||||
|
||||
private boolean isNumericType(int type) {
|
||||
switch (type) {
|
||||
case Types.BIGINT:
|
||||
return true;
|
||||
case Types.DECIMAL:
|
||||
return true;
|
||||
case Types.DOUBLE:
|
||||
return true;
|
||||
case Types.FLOAT:
|
||||
return true;
|
||||
case Types.INTEGER:
|
||||
return true;
|
||||
case Types.NUMERIC:
|
||||
return true;
|
||||
case Types.REAL:
|
||||
return true;
|
||||
case Types.SMALLINT:
|
||||
return true;
|
||||
case Types.TINYINT:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Return true if this property is based on a secondary table.
|
||||
*/
|
||||
|
||||
+8
@@ -30,6 +30,14 @@ public class DeployBeanPropertyAssocOne<T> extends DeployBeanPropertyAssoc<T> {
|
||||
return deployEmbedded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this has multiple properties (expected for embedded id).
|
||||
*/
|
||||
public boolean isCompound() {
|
||||
// just checking for compound and not doing numeric check at this stage
|
||||
return getDeployEmbedded().getPropertyColumnMap().size() > 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDbColumn() {
|
||||
DeployTableJoinColumn[] columns = tableJoin.columns();
|
||||
|
||||
@@ -112,12 +112,8 @@ public class DataBind {
|
||||
pstmt.setString(++pos, String.valueOf(v));
|
||||
}
|
||||
|
||||
public void setBlob(InputStream inputStream, long length) throws SQLException {
|
||||
pstmt.setBlob(++pos, inputStream, length);
|
||||
}
|
||||
|
||||
public void setBlob(InputStream inputStream) throws SQLException {
|
||||
pstmt.setBlob(++pos, inputStream);
|
||||
public void setBinaryStream(InputStream inputStream, long length) throws SQLException {
|
||||
pstmt.setBinaryStream(++pos, inputStream, length);
|
||||
}
|
||||
|
||||
public void setBlob(byte[] bytes) throws SQLException {
|
||||
|
||||
@@ -18,16 +18,8 @@ public interface DataReader {
|
||||
|
||||
byte[] getBinaryBytes() throws SQLException;
|
||||
|
||||
byte[] getBlobBytes() throws SQLException;
|
||||
|
||||
InputStream getBlobInputStream() throws SQLException;
|
||||
|
||||
String getStringFromStream() throws SQLException;
|
||||
|
||||
String getStringClob() throws SQLException;
|
||||
|
||||
Reader getClobReader() throws SQLException;
|
||||
|
||||
String getString() throws SQLException;
|
||||
|
||||
Boolean getBoolean() throws SQLException;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.Message;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Array;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Clob;
|
||||
import java.sql.Date;
|
||||
import java.sql.Ref;
|
||||
import java.sql.ResultSet;
|
||||
@@ -15,8 +15,6 @@ import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.Message;
|
||||
|
||||
public class RsetDataReader implements DataReader {
|
||||
|
||||
private static final int bufferSize = 512;
|
||||
@@ -57,10 +55,6 @@ public class RsetDataReader implements DataReader {
|
||||
return rset.getArray(pos());
|
||||
}
|
||||
|
||||
public InputStream getAsciiStream() throws SQLException {
|
||||
return rset.getAsciiStream(pos());
|
||||
}
|
||||
|
||||
public Object getObject() throws SQLException {
|
||||
return rset.getObject(pos());
|
||||
}
|
||||
@@ -168,28 +162,6 @@ public class RsetDataReader implements DataReader {
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getClobReader() throws SQLException {
|
||||
Clob clob = rset.getClob(pos());
|
||||
if (clob == null) {
|
||||
return null;
|
||||
}
|
||||
return clob.getCharacterStream();
|
||||
}
|
||||
|
||||
public String getStringClob() throws SQLException {
|
||||
|
||||
Clob clob = rset.getClob(pos());
|
||||
if (clob == null) {
|
||||
return null;
|
||||
}
|
||||
Reader reader = clob.getCharacterStream();
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
protected String readStringLob(Reader reader) throws SQLException {
|
||||
|
||||
char[] buffer = new char[clobBufferSize];
|
||||
@@ -212,23 +184,6 @@ public class RsetDataReader implements DataReader {
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
public byte[] getBlobBytes() throws SQLException {
|
||||
Blob blob = rset.getBlob(pos());
|
||||
if (blob == null) {
|
||||
return null;
|
||||
}
|
||||
InputStream in = blob.getBinaryStream();
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
public InputStream getBlobInputStream() throws SQLException {
|
||||
Blob blob = rset.getBlob(pos());
|
||||
if (blob == null) {
|
||||
return null;
|
||||
}
|
||||
return blob.getBinaryStream();
|
||||
}
|
||||
|
||||
protected byte[] getBinaryLob(InputStream in) throws SQLException {
|
||||
|
||||
try {
|
||||
|
||||
@@ -14,7 +14,7 @@ public class ScalarTypeBytesBlob extends ScalarTypeBytesBase {
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getBlobBytes();
|
||||
return dataReader.getBinaryBytes();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class ScalarTypeClob extends ScalarTypeBaseVarchar<String> {
|
||||
@Override
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getStringClob();
|
||||
return dataReader.getStringFromStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ScalarTypeFile extends ScalarTypeBase<File> {
|
||||
* Construct with reasonable defaults of Blob and 8096 buffer size.
|
||||
*/
|
||||
public ScalarTypeFile() {
|
||||
this(Types.BLOB, "db-", null, null, 8096);
|
||||
this(Types.LONGVARBINARY, "db-", null, null, 8096);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +92,7 @@ public class ScalarTypeFile extends ScalarTypeBase<File> {
|
||||
try {
|
||||
// stream from our file to the db
|
||||
InputStream fi = getInputStream(value);
|
||||
b.setBlob(fi, value.length());
|
||||
b.setBinaryStream(fi, value.length());
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error trying to set file inputStream", e);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.avaje.ebean.text.json.EJson;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
@@ -31,17 +30,11 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
@Override
|
||||
public Map read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Reader reader = dataReader.getClobReader();
|
||||
if (reader == null) {
|
||||
String content = dataReader.getStringFromStream();
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Map map = parse(reader);
|
||||
reader.close();
|
||||
return map;
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error reading Clob stream from DB", e);
|
||||
}
|
||||
return parse(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,15 +53,17 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
@Override
|
||||
public Map read(DataReader dataReader) throws SQLException {
|
||||
|
||||
InputStream is = dataReader.getBlobInputStream();
|
||||
InputStream is = dataReader.getBinaryStream();
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
InputStreamReader reader = new InputStreamReader(is);
|
||||
Map map = parse(reader);
|
||||
reader.close();
|
||||
return map;
|
||||
try {
|
||||
return parse(reader);
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error reading Blob stream from DB", e);
|
||||
}
|
||||
@@ -81,8 +76,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
|
||||
b.setNull(Types.BLOB);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
InputStream stream = new ByteArrayInputStream(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
b.setBlob(stream);
|
||||
b.setBytes(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
@@ -34,17 +33,11 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
|
||||
@Override
|
||||
public JsonNode read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Reader reader = dataReader.getClobReader();
|
||||
if (reader == null) {
|
||||
String content = dataReader.getStringFromStream();
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode tree = parse(reader);
|
||||
reader.close();
|
||||
return tree;
|
||||
} catch (IOException e) {
|
||||
throw new SQLException("Error reading Clob stream from DB", e);
|
||||
}
|
||||
return parse(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +63,7 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
|
||||
@Override
|
||||
public JsonNode read(DataReader dataReader) throws SQLException {
|
||||
|
||||
InputStream is = dataReader.getBlobInputStream();
|
||||
InputStream is = dataReader.getBinaryStream();
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -91,8 +84,7 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
|
||||
dataBind.setNull(Types.BLOB);
|
||||
} else {
|
||||
String rawJson = formatValue(value);
|
||||
InputStream stream = new ByteArrayInputStream(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
dataBind.setBlob(stream);
|
||||
dataBind.setBlob(rawJson.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user