use of javax.persistence.Column in project siesta by cadenzauk.
the class EmbeddedTest method embeddedInWhereClause.
@Test
void embeddedInWhereClause() {
Database database = testDatabaseBuilder().defaultSchema("SIESTA").table(PartRow.class, t -> t.embedded(MoneyAmount.class, PartRow::purchasePrice, pp -> pp.columnName("PURCH_PRICE").column(MoneyAmount::amount, "PURCHASE_PRICE_AMT")).embedded(MoneyAmount.class, PartRow::retailPrice, rp -> rp.column(MoneyAmount::amount, "RETAIL_PRICE_AMT"))).build();
database.from(PartRow.class).select(Aggregates.countDistinct(column(PartRow::purchasePrice).dot(MoneyAmount::amount))).where(PartRow::purchasePrice).dot(MoneyAmount::currency).isEqualTo("USD").or(PartRow::retailPrice).dot(MoneyAmount::currency).isEqualTo("NZD").optional(sqlExecutor);
Mockito.verify(sqlExecutor).query(sql.capture(), args.capture(), any());
assertThat(sql.getValue(), is("select count(distinct PART.PURCHASE_PRICE_AMT) as count_PART_PURCHASE_PRICE_AMT " + "from SIESTA.PART PART where PART.PURCH_PRICE_CCY = ? or PART.RETAIL_PRICE_CCY = ?"));
assertThat(args.getValue(), is(toArray("USD", "NZD")));
}
use of javax.persistence.Column in project siesta by cadenzauk.
the class MethodUtilTest method annotationsNotPresent.
@Test
void annotationsNotPresent() {
Method method = MethodUtil.fromReference(TestDerivedClass.class, TestClass::method2);
Stream<Column> annotations = MethodUtil.annotations(Column.class, method);
assertThat(annotations.count(), is(0L));
}
use of javax.persistence.Column in project cassandra-driver-mapping by valchkou.
the class EntityTypeParser method getColumnDataType.
/**
* by default data type retrieved from javaTypeToDataType.
*
* @Column columnDefinition may override datatype.
*/
private static DataType.Name getColumnDataType(Field f) {
Class<?> t = f.getType();
DataType.Name dataType = javaTypeToDataType.get(t);
if (t.isEnum()) {
// enum is a special type.
dataType = javaTypeToDataType.get(Enum.class);
}
Annotation columnA = f.getAnnotation(Column.class);
if (columnA instanceof Column) {
String typedef = ((Column) columnA).columnDefinition();
if (typedef != null && typedef.length() > 0) {
DataType.Name dt = DataType.Name.valueOf(typedef.toUpperCase());
if (dt != null) {
dataType = dt;
}
}
}
return dataType;
}
use of javax.persistence.Column in project jirm by agentgt.
the class SqlParameterDefinition method parameterDef.
static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
final SqlParameterDefinition definition;
String sn = null;
ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
if (manyToOne != null) {
Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
checkState(!od.getIdParameters().isEmpty(), "No id parameters");
if (joinColumn != null)
sn = joinColumn.name();
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
FetchType fetch = manyToOne.fetch();
int depth;
if (FetchType.LAZY == fetch) {
depth = 1;
} else {
depth = config.getMaximumLoadDepth();
}
SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
} else {
Column col = getAnnotation(objectType, parameterName, Column.class);
if (col != null && !isNullOrEmpty(col.name()))
sn = col.name();
Id id = getAnnotation(objectType, parameterName, Id.class);
Version version = getAnnotation(objectType, parameterName, Version.class);
GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
boolean idFlag = id != null;
boolean versionFlag = version != null;
boolean generatedFlag = generated != null;
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
}
return definition;
}
use of javax.persistence.Column in project cosmic by MissionCriticalCloud.
the class GenericDaoBase method prepareAttribute.
@DB()
protected void prepareAttribute(final int j, final PreparedStatement pstmt, final Attribute attr, Object value) throws SQLException {
if (attr.is(Attribute.Flag.DaoGenerated) && value == null) {
value = generateValue(attr);
if (attr.field == null) {
pstmt.setObject(j, value);
return;
}
}
if (attr.field.getType() == String.class) {
final String str = (String) value;
if (str == null) {
pstmt.setString(j, null);
return;
}
final Column column = attr.field.getAnnotation(Column.class);
final int length = column != null ? column.length() : 255;
// to support generic localization, utilize MySql UTF-8 support
if (length < str.length()) {
try {
if (attr.is(Attribute.Flag.Encrypted)) {
pstmt.setBytes(j, DBEncryptionUtil.encrypt(str.substring(0, length)).getBytes("UTF-8"));
} else {
pstmt.setBytes(j, str.substring(0, length).getBytes("UTF-8"));
}
} catch (final UnsupportedEncodingException e) {
// no-way it can't support UTF-8 encoding
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
}
} else {
try {
if (attr.is(Attribute.Flag.Encrypted)) {
pstmt.setBytes(j, DBEncryptionUtil.encrypt(str).getBytes("UTF-8"));
} else {
pstmt.setBytes(j, str.getBytes("UTF-8"));
}
} catch (final UnsupportedEncodingException e) {
// no-way it can't support UTF-8 encoding
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
}
}
} else if (attr.field.getType() == Date.class) {
final Date date = (Date) value;
if (date == null) {
pstmt.setObject(j, null);
return;
}
if (attr.is(Attribute.Flag.Date)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
} else if (attr.is(Attribute.Flag.TimeStamp)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
} else if (attr.is(Attribute.Flag.Time)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
}
} else if (attr.field.getType() == Calendar.class) {
final Calendar cal = (Calendar) value;
if (cal == null) {
pstmt.setObject(j, null);
return;
}
if (attr.is(Attribute.Flag.Date)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
} else if (attr.is(Attribute.Flag.TimeStamp)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
} else if (attr.is(Attribute.Flag.Time)) {
pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
}
} else if (attr.field.getType().isEnum()) {
final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
final EnumType type = (enumerated == null) ? EnumType.STRING : enumerated.value();
if (type == EnumType.STRING) {
pstmt.setString(j, value == null ? null : value.toString());
} else if (type == EnumType.ORDINAL) {
if (value == null) {
pstmt.setObject(j, null);
} else {
pstmt.setInt(j, ((Enum<?>) value).ordinal());
}
}
} else if (attr.field.getType() == URI.class) {
pstmt.setString(j, value == null ? null : value.toString());
} else if (attr.field.getType() == URL.class) {
pstmt.setURL(j, (URL) value);
} else if (attr.field.getType() == byte[].class) {
pstmt.setBytes(j, (byte[]) value);
} else if (attr.field.getType() == Ip.class) {
final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
final EnumType type = (enumerated == null) ? EnumType.ORDINAL : enumerated.value();
if (type == EnumType.STRING) {
pstmt.setString(j, value == null ? null : value.toString());
} else if (type == EnumType.ORDINAL) {
if (value == null) {
pstmt.setObject(j, null);
} else {
pstmt.setLong(j, (value instanceof Ip) ? ((Ip) value).longValue() : NetUtils.ip2Long((String) value));
}
}
} else {
pstmt.setObject(j, value);
}
}
Aggregations