use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class ErrorTableEntry method getMatch.
public static ErrorTableEntry getMatch(PackageInfo packageInfo, int currentIP, final BStruct error) {
ErrorTableAttributeInfo errorTable = (ErrorTableAttributeInfo) packageInfo.getAttributeInfo(AttributeInfo.Kind.ERROR_TABLE);
List<ErrorTableEntry> errorTableEntries = errorTable != null ? errorTable.getErrorTableEntriesList() : new ArrayList<>();
List<MatchedEntry> rangeMatched = new ArrayList<>();
errorTableEntries.stream().filter(errorTableEntry -> errorTableEntry.matchRange(currentIP)).forEach(errorTableEntry -> {
MatchedEntry entry = new MatchedEntry();
entry.errorTableEntry = errorTableEntry;
entry.ipSize = errorTableEntry.ipTo - errorTableEntry.ipFrom;
if (errorTableEntry.getErrorStructCPIndex() == -1) {
// match any.
entry.status = 2;
rangeMatched.add(entry);
} else if (errorTableEntry.getError().getType().equals(error.getType())) {
// exact match.
entry.status = 0;
rangeMatched.add(entry);
} else if (CPU.checkStructEquivalency((BStructType) error.getType(), (BStructType) errorTableEntry.getError().getType())) {
entry.status = 1;
rangeMatched.add(entry);
}
});
if (rangeMatched.size() == 0) {
return null;
}
if (rangeMatched.size() == 1) {
return rangeMatched.get(0).errorTableEntry;
}
MatchedEntry[] matchedEntries = rangeMatched.stream().sorted(Comparator.comparingInt(o -> o.ipSize)).toArray(MatchedEntry[]::new);
int currentSize = 0;
ErrorTableEntry errorTableEntry = null;
for (int i = 0; i < matchedEntries.length; i++) {
MatchedEntry entry = matchedEntries[i];
if (currentSize < entry.ipSize) {
if (errorTableEntry == null) {
// Expand scope.
currentSize = entry.ipSize;
} else {
// Return best match.
return errorTableEntry;
}
}
if (entry.status == 0) {
// Best case.
return entry.errorTableEntry;
} else {
if (errorTableEntry == null) {
errorTableEntry = entry.errorTableEntry;
} else {
if (errorTableEntry.priority > entry.errorTableEntry.priority) {
// found a high order entry
errorTableEntry = entry.errorTableEntry;
}
}
}
}
return errorTableEntry;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class SQLDataIterator method generateNext.
@Override
public BStruct generateNext() {
if (this.type == null) {
throw new BallerinaException("the expected struct type is not specified in action");
}
BStruct bStruct = new BStruct(this.type);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int blobRegIndex = -1;
int refRegIndex = -1;
int index = 0;
String columnName = null;
try {
for (ColumnDefinition columnDef : columnDefs) {
if (columnDef instanceof SQLColumnDefinition) {
SQLColumnDefinition def = (SQLColumnDefinition) columnDef;
columnName = def.getName();
int sqlType = def.getSqlType();
++index;
switch(sqlType) {
case Types.ARRAY:
Array dataArray = rs.getArray(index);
bStruct.setRefField(++refRegIndex, getDataArray(dataArray));
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
String sValue = rs.getString(index);
bStruct.setStringField(++stringRegIndex, sValue);
break;
case Types.BLOB:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
Blob value = rs.getBlob(index);
if (value != null) {
bStruct.setBlobField(++blobRegIndex, value.getBytes(1L, (int) value.length()));
} else {
bStruct.setBlobField(++blobRegIndex, new byte[0]);
}
break;
case Types.CLOB:
String clobValue = SQLDatasourceUtils.getString((rs.getClob(index)));
bStruct.setStringField(++stringRegIndex, clobValue);
break;
case Types.NCLOB:
String nClobValue = SQLDatasourceUtils.getString(rs.getNClob(index));
bStruct.setStringField(++stringRegIndex, nClobValue);
break;
case Types.DATE:
Date date = rs.getDate(index);
int fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String dateValue = SQLDatasourceUtils.getString(date);
bStruct.setStringField(++stringRegIndex, dateValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(date.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, date.getTime());
}
break;
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
Time time = rs.getTime(index, utcCalendar);
fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String timeValue = SQLDatasourceUtils.getString(time);
bStruct.setStringField(++stringRegIndex, timeValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(time.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, time.getTime());
}
break;
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
Timestamp timestamp = rs.getTimestamp(index, utcCalendar);
fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String timestmpValue = SQLDatasourceUtils.getString(timestamp);
bStruct.setStringField(++stringRegIndex, timestmpValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(timestamp.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, timestamp.getTime());
}
break;
case Types.ROWID:
BValue strValue = new BString(new String(rs.getRowId(index).getBytes(), "UTF-8"));
bStruct.setStringField(++stringRegIndex, strValue.stringValue());
break;
case Types.TINYINT:
case Types.SMALLINT:
long iValue = rs.getInt(index);
bStruct.setIntField(++longRegIndex, iValue);
break;
case Types.INTEGER:
case Types.BIGINT:
long lValue = rs.getLong(index);
bStruct.setIntField(++longRegIndex, lValue);
break;
case Types.REAL:
case Types.FLOAT:
double fValue = rs.getFloat(index);
bStruct.setFloatField(++doubleRegIndex, fValue);
break;
case Types.DOUBLE:
double dValue = rs.getDouble(index);
bStruct.setFloatField(++doubleRegIndex, dValue);
break;
case Types.NUMERIC:
case Types.DECIMAL:
double decimalValue = 0;
BigDecimal bigDecimalValue = rs.getBigDecimal(index);
if (bigDecimalValue != null) {
decimalValue = bigDecimalValue.doubleValue();
}
bStruct.setFloatField(++doubleRegIndex, decimalValue);
break;
case Types.BIT:
case Types.BOOLEAN:
boolean boolValue = rs.getBoolean(index);
bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
break;
case Types.STRUCT:
Struct structdata = (Struct) rs.getObject(index);
BType structFieldType = this.type.getStructFields()[index - 1].getFieldType();
fieldType = structFieldType.getTag();
if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createUserDefinedType(structdata, (BStructType) structFieldType));
}
break;
default:
throw new BallerinaException("unsupported sql type " + sqlType + " found for the column " + columnName + " index:" + index);
}
}
}
} catch (Throwable e) {
throw new BallerinaException("error in retrieving next value for column: " + columnName + ": at index:" + index + ":" + e.getMessage());
}
return bStruct;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class AbstractSQLAction method getStructType.
protected BStructType getStructType(Context context) {
BStructType structType = null;
BTypeDescValue type = (BTypeDescValue) context.getNullableRefArgument(2);
if (type != null) {
structType = (BStructType) type.value();
}
return structType;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class Call method execute.
@Override
public void execute(Context context) {
try {
BStruct bConnector = (BStruct) context.getRefArgument(0);
String query = context.getStringArgument(0);
BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
BStructType structType = getStructType(context);
SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
Map<String, String> tags = new HashMap<>();
tags.put(TAG_KEY_DB_STATEMENT, query);
tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
executeProcedure(context, datasource, query, parameters, structType);
} catch (Throwable e) {
context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
SQLDatasourceUtils.handleErrorOnTransaction(context);
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class SQLDatasourceUtils method getStructData.
private static Object[] getStructData(BValue value, Connection conn) throws SQLException {
if (value == null || value.getType().getTag() != TypeTags.STRUCT_TAG) {
return new Object[] { null, null };
}
String structuredSQLType = value.getType().getName().toUpperCase(Locale.getDefault());
BStructType.StructField[] structFields = ((BStructType) value.getType()).getStructFields();
int fieldCount = structFields.length;
Object[] structData = new Object[fieldCount];
int intFieldIndex = 0;
int floatFieldIndex = 0;
int stringFieldIndex = 0;
int booleanFieldIndex = 0;
int blobFieldIndex = 0;
int refFieldIndex = 0;
for (int i = 0; i < fieldCount; ++i) {
BStructType.StructField field = structFields[i];
int typeTag = field.getFieldType().getTag();
switch(typeTag) {
case TypeTags.INT_TAG:
structData[i] = ((BStruct) value).getIntField(intFieldIndex);
++intFieldIndex;
break;
case TypeTags.FLOAT_TAG:
structData[i] = ((BStruct) value).getFloatField(floatFieldIndex);
++floatFieldIndex;
break;
case TypeTags.STRING_TAG:
structData[i] = ((BStruct) value).getStringField(stringFieldIndex);
++stringFieldIndex;
break;
case TypeTags.BOOLEAN_TAG:
structData[i] = ((BStruct) value).getBooleanField(booleanFieldIndex) > 0;
++booleanFieldIndex;
break;
case TypeTags.BLOB_TAG:
structData[i] = ((BStruct) value).getBlobField(blobFieldIndex);
++blobFieldIndex;
break;
case TypeTags.STRUCT_TAG:
Object structValue = ((BStruct) value).getRefField(refFieldIndex);
if (structValue instanceof BStruct) {
Object[] internalStructData = getStructData((BStruct) structValue, conn);
Object[] dataArray = (Object[]) internalStructData[0];
String internalStructType = (String) internalStructData[1];
structValue = conn.createStruct(internalStructType, dataArray);
}
structData[i] = structValue;
++refFieldIndex;
break;
default:
throw new BallerinaException("unsupported data type for struct parameter: " + structuredSQLType);
}
}
return new Object[] { structData, structuredSQLType };
}
Aggregations