コード例 #1
0
        public static object DataValueToValue(IServerProcess process, IDataValue dataValue)
        {
            if (dataValue == null)
            {
                return(null);
            }

            IScalar scalar = dataValue as IScalar;

            if (scalar != null)
            {
                return(ScalarTypeNameToValue(dataValue.DataType.Name, scalar));
            }

            ListValue list = dataValue as ListValue;

            if (list != null)
            {
                var listValue = new List <object>();
                if (!list.IsNil)
                {
                    for (int index = 0; index < list.Count(); index++)
                    {
                        listValue.Add(DataValueToValue(process, list.GetValue(index)));
                    }
                }
                return(listValue);
            }

            IRow row = dataValue as IRow;

            if (row != null)
            {
                var rowValue = new Dictionary <string, object>();

                if (!row.IsNil)
                {
                    for (int index = 0; index < row.DataType.Columns.Count; index++)
                    {
                        var data = row.GetValue(index);
                        data.DataType.Name = DataTypeToDataTypeName(process.DataTypes, row.DataType.Columns[index].DataType);
                        rowValue.Add(row.DataType.Columns[index].Name, DataValueToValue(process, data));
                    }
                }
                return(rowValue);
            }

            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)
                {
                    var resultTable = new List <object>();

                    if (!table.BOF())
                    {
                        table.First();
                    }

                    bool[] valueTypes = new bool[table.DataType.Columns.Count];
                    for (int index = 0; index < table.DataType.Columns.Count; index++)
                    {
                        valueTypes[index] = table.DataType.Columns[index].DataType is IScalarType;
                    }

                    while (table.Next())
                    {
                        using (IRow currentRow = table.Select())
                        {
                            object[] nativeRow = new object[table.DataType.Columns.Count];
                            for (int index = 0; index < table.DataType.Columns.Count; index++)
                            {
                                if (valueTypes[index])
                                {
                                    resultTable.Add(currentRow[index]);
                                }
                                else
                                {
                                    resultTable.Add(DataValueToValue(process, currentRow.GetValue(index)));
                                }
                            }
                        }
                    }

                    return(resultTable);
                }
            }
            finally
            {
                if (scan != null)
                {
                    scan.Dispose();
                }
            }

            throw new NotSupportedException(string.Format("Values of type \"{0}\" are not supported.", dataValue.DataType.Name));
        }
コード例 #2
0
ファイル: NativeMarshal.cs プロジェクト: laszlo-kiss/Dataphor
        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));
        }