use of com.google.cloud.spanner.Type.StructField in project spanner-jdbc by olavloite.
the class CloudSpannerResultSetMetaDataTest method getFooTestResultSet.
private CloudSpannerResultSet getFooTestResultSet(CloudSpannerStatement statement) throws SQLException {
List<Struct> rows = new ArrayList<>(4);
for (int row = 1; row <= 4; row++) {
Struct.Builder builder = Struct.newBuilder();
for (TestColumn col : TEST_COLUMNS) {
builder.set(col.name).to(getDefaultValue(col.type, row));
}
rows.add(builder.build());
}
StructField[] fields = new StructField[TEST_COLUMNS.size()];
int index = 0;
for (TestColumn col : TEST_COLUMNS) {
fields[index] = StructField.of(col.name, col.type);
index++;
}
ResultSet rs = ResultSets.forRows(Type.struct(fields), rows);
return new CloudSpannerResultSet(statement, rs, SELECT_ALL_FROM_FOO);
}
use of com.google.cloud.spanner.Type.StructField in project jans by JanssenProject.
the class SpannerOperationServiceImpl method buildSelectAttributes.
private List<SelectItem> buildSelectAttributes(TableMapping tableMapping, String key, String... attributes) throws SearchException {
String tableName = tableMapping.getTableName();
Map<String, StructField> columTypes = tableMapping.getColumTypes();
// Table alias for columns
// Column dn
Column selectDnColumn = new Column(tableAlias, DN);
SelectExpressionItem selectDnItem = new SelectExpressionItem(selectDnColumn);
// Column doc_id
Column selectDocIdColumn = new Column(tableAlias, DOC_ID);
SelectExpressionItem selectDocIdItem = new SelectExpressionItem(selectDocIdColumn);
if (ArrayHelper.isEmpty(attributes)) {
// Select all columns
AllTableColumns allColumns = new AllTableColumns(tableAlias);
List<SelectItem> selectColumns = new ArrayList<SelectItem>();
selectColumns.add(allColumns);
// Add columns from child tables
List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
selectColumns.addAll(selectChildColumns);
return selectColumns;
} else if ((attributes.length == 1) && StringHelper.isEmpty(attributes[0])) {
// Compatibility with base persistence layer when application pass attributes new String[] { "" }
List<SelectItem> selectColumns = Arrays.asList(selectDnItem, selectDocIdItem);
// Add columns from child tables
List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
selectColumns.addAll(selectChildColumns);
return selectColumns;
}
List<SelectItem> expresisons = new ArrayList<SelectItem>(attributes.length + 2);
boolean hasDn = false;
for (String attributeName : attributes) {
StructField attributeType = columTypes.get(attributeName.toLowerCase());
SelectExpressionItem selectExpressionItem;
// If column not inside table we should check if there is child table
if (attributeType == null) {
TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
if (childTableMapping == null) {
throw new SearchException(String.format("Failed to build select attributes. Column '%s' is undefined", attributeName));
}
// Add columns from child table
selectExpressionItem = buildSelectAttributeFromChildTable(tableName, attributeName);
} else {
Column selectColumn = new Column(tableAlias, attributeName);
selectExpressionItem = new SelectExpressionItem(selectColumn);
}
expresisons.add(selectExpressionItem);
hasDn |= StringHelper.equals(attributeName, DN);
}
if (!hasDn) {
expresisons.add(selectDnItem);
}
expresisons.add(selectDocIdItem);
return expresisons;
}
use of com.google.cloud.spanner.Type.StructField in project jans by JanssenProject.
the class SpannerFilterConverter method getStructField.
private StructField getStructField(TableMapping tableMapping, String attributeName) throws SearchException {
String attributeNameLower = attributeName.toLowerCase();
StructField structField = tableMapping.getColumTypes().get(attributeNameLower);
if (structField == null) {
TableMapping childTableMapping = tableMapping.getChildTableMappingForAttribute(attributeNameLower);
if (childTableMapping != null) {
structField = childTableMapping.getColumTypes().get(attributeNameLower);
}
}
if (structField == null) {
throw new SearchException(String.format("Unknown column name '%s' in table/child table '%s'", attributeName, tableMapping.getTableName()));
}
return structField;
}
use of com.google.cloud.spanner.Type.StructField in project jans by JanssenProject.
the class SpannerFilterConverter method isMultiValue.
protected Boolean isMultiValue(TableMapping tableMapping, String attributeName, Filter currentGenericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap) throws SearchException {
StructField structField = getStructField(tableMapping, attributeName);
boolean hasChildTableForAttribute = tableMapping.hasChildTableForAttribute(attributeName.toLowerCase());
Code columnTypeCode = structField.getType().getCode();
if ((Code.ARRAY == columnTypeCode) || hasChildTableForAttribute) {
boolean multiValuedDetected = (Boolean.TRUE.equals(currentGenericFilter.getMultiValued()) || Boolean.TRUE.equals(determineMultiValuedByType(currentGenericFilter.getAttributeName(), propertiesAnnotationsMap)));
if (!multiValuedDetected) {
LOG.warn(String.format("Сolumn name '%s' was defined as multi valued in table/child table '%s' but detected as single value", attributeName, tableMapping.getTableName()));
}
return true;
}
return false;
}
use of com.google.cloud.spanner.Type.StructField in project jans by JanssenProject.
the class SpannerOperationServiceImpl method addEntryImpl.
private boolean addEntryImpl(TableMapping tableMapping, String key, Collection<AttributeData> attributes) throws PersistenceException {
try {
MessageDigest messageDigest = getMessageDigestInstance();
Map<String, StructField> columTypes = tableMapping.getColumTypes();
WriteBuilder mutationBuilder = Mutation.newInsertOrUpdateBuilder(tableMapping.getTableName());
List<Mutation> mutations = new LinkedList<>();
for (AttributeData attribute : attributes) {
String attributeName = attribute.getName();
StructField attributeType = columTypes.get(attributeName.toLowerCase());
// If column not inside table we should check if there is child table
if (attributeType == null) {
TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
if (childTableMapping == null) {
throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
}
Map<String, StructField> childColumTypes = childTableMapping.getColumTypes();
if (childColumTypes == null) {
throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
}
StructField childAttributeType = childColumTypes.get(attributeName.toLowerCase());
// Build Mutation for child table
for (Object value : attribute.getValues()) {
// Build Mutation for child table
String dictDocId = getStringUniqueKey(messageDigest, value);
WriteBuilder childMutationBuilder = Mutation.newInsertOrUpdateBuilder(childTableMapping.getTableName());
childMutationBuilder.set(SpannerOperationService.DOC_ID).to(key).set(SpannerOperationService.DICT_DOC_ID).to(dictDocId);
setMutationBuilderValue(childMutationBuilder, childAttributeType, value);
mutations.add(childMutationBuilder.build());
}
} else {
setMutationBuilderValue(mutationBuilder, attributeType, attribute.getValues());
}
}
mutations.add(0, mutationBuilder.build());
databaseClient.write(mutations);
return true;
} catch (SpannerException | IllegalStateException ex) {
throw new PersistenceException("Failed to add entry", ex);
}
}
Aggregations