private static IDataType NativeValueToDataType(ServerProcess process, NativeValue nativeValue) { NativeScalarValue nativeScalar = nativeValue as NativeScalarValue; if (nativeScalar != null) { return(NativeTypeNameToDataType(process.DataTypes, nativeScalar.DataTypeName)); } NativeListValue nativeList = nativeValue as NativeListValue; if (nativeList != null) { return (new ListType ( // Use the element type name if given !String.IsNullOrEmpty(nativeList.ElementDataTypeName) ? NativeTypeNameToDataType(process.DataTypes, nativeList.ElementDataTypeName) // If not, try to use the type of the first element : (nativeList.Elements != null && nativeList.Elements.Length > 0 && String.IsNullOrEmpty(nativeList.ElementDataTypeName)) ? NativeValueToDataType(process, nativeList.Elements[0]) // If not, revert to generic list : process.DataTypes.SystemGeneric )); } NativeRowValue nativeRow = nativeValue as NativeRowValue; if (nativeRow != null) { return(new Schema.RowType(NativeColumnsToColumns(process.DataTypes, nativeRow.Columns))); } NativeTableValue nativeTable = nativeValue as NativeTableValue; if (nativeTable != null) { TableType tableType = new TableType(); foreach (Column column in NativeColumnsToColumns(process.DataTypes, nativeTable.Columns)) { tableType.Columns.Add(column); } return(tableType); } throw new NotSupportedException(String.Format("Values of type \"{0}\" are not supported.", nativeTable.GetType().Name)); }
public static NativeValue DataTypeToNativeValue(IServerProcess process, IDataType dataType) { IScalarType scalarType = dataType as IScalarType; if (scalarType != null) { NativeScalarValue nativeScalar = new NativeScalarValue(); nativeScalar.DataTypeName = ScalarTypeToDataTypeName(process.DataTypes, scalarType); return(nativeScalar); } ListType listType = dataType as ListType; if (listType != null) { NativeListValue nativeList = new NativeListValue(); return(nativeList); } RowType rowType = dataType as RowType; if (rowType != null) { NativeRowValue nativeRow = new NativeRowValue(); nativeRow.Columns = ColumnsToNativeColumns(process.DataTypes, rowType.Columns); return(nativeRow); } TableType tableType = dataType as TableType; if (tableType != null) { NativeTableValue nativeTable = new NativeTableValue(); nativeTable.Columns = ColumnsToNativeColumns(process.DataTypes, tableType.Columns); return(nativeTable); } throw new NotSupportedException(String.Format("Values of type \"{0}\" are not supported.", dataType.Name)); }
private static string GetDataTypeName(NativeValue value) { NativeScalarValue scalarValue = value as NativeScalarValue; if (scalarValue != null) { return(scalarValue.DataTypeName); } NativeListValue listValue = value as NativeListValue; if (listValue != null) { return(String.Format("list({0})", listValue.ElementDataTypeName)); } NativeRowValue rowValue = value as NativeRowValue; if (rowValue != null) { var sb = new StringBuilder(); sb.Append("row{"); bool first = true; foreach (NativeColumn column in rowValue.Columns) { if (!first) { sb.Append(","); } else { first = false; } sb.AppendFormat("{0}:{1}", column.Name, column.DataTypeName); } sb.Append("}"); return(sb.ToString()); } NativeTableValue tableValue = value as NativeTableValue; if (tableValue != null) { var sb = new StringBuilder(); sb.Append("table{"); bool first = true; foreach (NativeColumn column in tableValue.Columns) { if (!first) { sb.Append(","); } else { first = false; } sb.AppendFormat("{0}:{1}", column.Name, column.DataTypeName); } sb.Append("}"); return(sb.ToString()); } throw new NotSupportedException("Non-scalar-valued attributes are not supported."); }
public static NativeValue DataParamToNativeValue(IServerProcess process, DataParam dataParam) { var scalarType = dataParam.DataType as IScalarType; if (scalarType != null) { NativeScalarValue nativeScalar = new NativeScalarValue(); nativeScalar.DataTypeName = ScalarTypeToDataTypeName(process.DataTypes, scalarType); nativeScalar.Value = dataParam.Value; return(nativeScalar); } var listType = dataParam.DataType as IListType; if (listType != null) { var listValue = dataParam.Value as ListValue; NativeListValue nativeList = new NativeListValue(); if (listValue != null) { nativeList.Elements = new NativeValue[listValue.Count()]; for (int index = 0; index < listValue.Count(); index++) { nativeList.Elements[index] = DataValueToNativeValue(process, listValue.GetValue(index)); } } return(nativeList); } var rowType = dataParam.DataType as IRowType; if (rowType != null) { var rowValue = dataParam.Value as IRow; NativeRowValue nativeRow = new NativeRowValue(); nativeRow.Columns = ColumnsToNativeColumns(process.DataTypes, rowType.Columns); if (rowValue != null) { nativeRow.Values = new NativeValue[nativeRow.Columns.Length]; for (int index = 0; index < nativeRow.Values.Length; index++) { nativeRow.Values[index] = DataValueToNativeValue(process, rowValue.GetValue(index)); } } return(nativeRow); } var tableType = dataParam.DataType as ITableType; if (tableType != null) { var tableValue = dataParam.Value as ITable; NativeTableValue nativeTable = new NativeTableValue(); nativeTable.Columns = ColumnsToNativeColumns(process.DataTypes, tableType.Columns); List <object[]> nativeRows = new List <object[]>(); if (!tableValue.BOF()) { tableValue.First(); } var valueTypes = new bool[tableType.Columns.Count]; for (int index = 0; index < tableType.Columns.Count; index++) { valueTypes[index] = tableType.Columns[index].DataType is IScalarType; } while (tableValue.Next()) { using (IRow currentRow = tableValue.Select()) { object[] nativeRow = new object[nativeTable.Columns.Length]; for (int index = 0; index < nativeTable.Columns.Length; index++) { if (valueTypes[index]) { nativeRow[index] = currentRow[index]; } else { nativeRow[index] = DataValueToNativeValue(process, currentRow.GetValue(index)); } } nativeRows.Add(nativeRow); } } nativeTable.Rows = nativeRows.ToArray(); return(nativeTable); } throw new NotSupportedException(String.Format("Values of type \"{0}\" are not supported.", dataParam.DataType.Name)); }
public static NativeValue DataValueToNativeValue(IServerProcess process, IDataValue dataValue) { if (dataValue == null) { return(null); } IScalar scalar = dataValue as IScalar; if (scalar != null) { NativeScalarValue nativeScalar = new NativeScalarValue(); nativeScalar.DataTypeName = ScalarTypeToDataTypeName(process.DataTypes, scalar.DataType); nativeScalar.Value = dataValue.IsNil ? null : scalar.AsNative; return(nativeScalar); } ListValue list = dataValue as ListValue; if (list != null) { NativeListValue nativeList = new NativeListValue(); if (!list.IsNil) { nativeList.Elements = new NativeValue[list.Count()]; for (int index = 0; index < list.Count(); index++) { nativeList.Elements[index] = DataValueToNativeValue(process, list.GetValue(index)); } } return(nativeList); } IRow row = dataValue as IRow; if (row != null) { NativeRowValue nativeRow = new NativeRowValue(); nativeRow.Columns = ColumnsToNativeColumns(process.DataTypes, row.DataType.Columns); if (!row.IsNil) { nativeRow.Values = new NativeValue[nativeRow.Columns.Length]; for (int index = 0; index < nativeRow.Values.Length; index++) { nativeRow.Values[index] = DataValueToNativeValue(process, row.GetValue(index)); } } return(nativeRow); } TableValue tableValue = dataValue as TableValue; TableValueScan scan = null; try { if (tableValue != null) { scan = new TableValueScan(tableValue); scan.Open(); dataValue = scan; } ITable table = dataValue as ITable; if (table != null) { NativeTableValue nativeTable = new NativeTableValue(); nativeTable.Columns = ColumnsToNativeColumns(process.DataTypes, table.DataType.Columns); List <object[]> nativeRows = new List <object[]>(); if (!table.BOF()) { table.First(); } bool[] valueTypes = new bool[nativeTable.Columns.Length]; for (int index = 0; index < nativeTable.Columns.Length; index++) { valueTypes[index] = table.DataType.Columns[index].DataType is IScalarType; } while (table.Next()) { using (IRow currentRow = table.Select()) { object[] nativeRow = new object[nativeTable.Columns.Length]; for (int index = 0; index < nativeTable.Columns.Length; index++) { if (valueTypes[index]) { nativeRow[index] = currentRow[index]; } else { nativeRow[index] = DataValueToNativeValue(process, currentRow.GetValue(index)); } } nativeRows.Add(nativeRow); } } nativeTable.Rows = nativeRows.ToArray(); return(nativeTable); } } finally { if (scan != null) { scan.Dispose(); } } throw new NotSupportedException(String.Format("Values of type \"{0}\" are not supported.", dataValue.DataType.Name)); }
public static IDataValue NativeValueToDataValue(ServerProcess process, NativeValue nativeValue) { NativeScalarValue nativeScalar = nativeValue as NativeScalarValue; if (nativeScalar != null) { return(new Scalar(process.ValueManager, (IScalarType)NativeTypeNameToDataType(process.DataTypes, nativeScalar.DataTypeName), nativeScalar.Value)); } NativeListValue nativeList = nativeValue as NativeListValue; if (nativeList != null) { // Create and fill the list ListValue list = new ListValue(process.ValueManager, (ListType)NativeValueToDataType(process, nativeValue), nativeList.Elements == null ? null : new NativeList()); if (nativeList.Elements != null && nativeList.Elements.Length > 0) { for (int index = 0; index < nativeList.Elements.Length; index++) { list.Add(NativeValueToDataValue(process, nativeList.Elements[index])); } } return(list); } NativeRowValue nativeRow = nativeValue as NativeRowValue; if (nativeRow != null) { Row row = new Row(process.ValueManager, new Schema.RowType(NativeColumnsToColumns(process.DataTypes, nativeRow.Columns))); if (nativeRow.Values == null) { row.AsNative = null; } else { for (int index = 0; index < nativeRow.Values.Length; index++) { row[index] = NativeValueToDataValue(process, nativeRow.Values[index]); } } return(row); } NativeTableValue nativeTable = nativeValue as NativeTableValue; if (nativeTable != null) { NativeTable internalTable = new NativeTable(process.ValueManager, NativeTableToTableVar(process, nativeTable)); TableValue table = new TableValue(process.ValueManager, internalTable.TableType, internalTable); if (nativeTable.Rows == null) { table.AsNative = null; } else { bool[] valueTypes = new bool[internalTable.TableType.Columns.Count]; for (int index = 0; index < internalTable.TableType.Columns.Count; index++) { valueTypes[index] = internalTable.TableType.Columns[index].DataType is IScalarType; } for (int index = 0; index < nativeTable.Rows.Length; index++) { Row row = new Row(process.ValueManager, internalTable.RowType); try { for (int columnIndex = 0; columnIndex < nativeTable.Rows[index].Length; columnIndex++) { if (valueTypes[columnIndex]) { row[columnIndex] = nativeTable.Rows[index][columnIndex]; } else { row[columnIndex] = NativeValueToDataValue(process, (NativeValue)nativeTable.Rows[index][columnIndex]); } } internalTable.Insert(process.ValueManager, row); } catch (Exception) { row.Dispose(); throw; } } } return(table); } throw new NotSupportedException(String.Format("Unknown native value type: \"{0}\".", nativeValue.GetType().Name)); }