internal static NodeMap TryGetNodeMap(DbRow row) { var map = GetNodeMap(row.NodeID); if (map.ID.Equals(DB3.Default)) { DbRow.ThrowInvalidDbRowException(row.GetType()); } return(map); }
internal static View ConvertDbRowData(IEnumerable <DbRow> rows, bool rkOnly = false, bool withRowID = false) { var first = rows.Where(row => row != null).First(); var nodeMap = DbMapping.GetNodeMap(first.NodeID); if (nodeMap.ID.Equals(DB3.Default)) { DbRow.ThrowInvalidDbRowException(first.GetType()); } var rowCount = 0; var columns = new List <ViewColumnInfo>(); var type = first.GetType(); var properties = type.GetSortedWritableProperties(withRowID); var numberOfProperties = properties.Length; if (numberOfProperties == 0) { throw new QueryTalkException("ViewConverter.ToView<T>", QueryTalkExceptionType.InvalidDataClass, String.Format("data class = {0}", type)); } List <IPropertyAccessor> getters; bool cached; if (withRowID) { cached = Cache.IRowAccessors.TryGetValue(type, out getters); } else { cached = Cache.PropertyAccessors.TryGetValue(type, out getters); } if (!cached) { getters = _GetDbRowsGetters(withRowID, type, properties); } var sql = Text.GenerateSql(500) .Append(Text.Select).S(); int i = 0; i = _BuildDbRowsOuterSelect(rkOnly, withRowID, nodeMap, columns, sql, i); if (withRowID) { sql.NewLineIndent(Text.Comma); AppendColumn(sql, String.Format("CAST({0} AS [int])", Filter.DelimitNonAsterix(String.Format("{0}{1}", Text.ColumnShortName, i + 1))), Text.Reserved.QtRowIDColumnName); } sql.S() .NewLine(Text.From).S() .Append(Text.LeftBracket).S(); bool firstRow = true; foreach (var row in rows) { if (row == null) { continue; } if (!firstRow) { sql.NewLineIndent(Text.UnionAll).S() .Append(Text.Select).S(); } else { sql.NewLineIndent(Text.Select).S(); } var originalValues = row.GetOriginalRKValues(); var gi = 0; // getter index int j = 0; _BuildDbRowsValues(rkOnly, withRowID, nodeMap, getters, sql, firstRow, row, originalValues, ref j, ref gi); if (withRowID) { var value = getters[gi].GetValue(row); sql.Append(Text.Comma); AppendColumnValue(sql, value, j + 1); } firstRow = false; ++rowCount; } sql.NewLine(Text.RightBracket).S() .Append(Text.As).S() .Append(Text.DelimitedTargetAlias); return(new View(sql.ToString(), typeof(DbRow), columns.ToArray(), rowCount)); }
private static void _BuildClassOuterSelect(Type type, ref Type clrType, ref QueryTalkException exception, List <ViewColumnInfo> columns, bool isEmpty, PropertyInfo[] properties, out List <IPropertyAccessor> getters, out int numberOfProperties, StringBuilder sqlOuter, StringBuilder sqlEmpty) { numberOfProperties = properties.Length; if (numberOfProperties == 0) { throw new QueryTalkException("ViewConverter.ToView<T>", QueryTalkExceptionType.InvalidDataClass, String.Format("data class = {0}", type)); } bool cached = Cache.PropertyAccessors.TryGetValue(type, out getters); if (!cached) { getters = new List <IPropertyAccessor>(); } NodeMap rowMap = null; bool isDbRow = type.IsDbRow(); if (isDbRow) { rowMap = DbMapping.GetNodeMap(type); if (rowMap.ID.Equals(DB3.Default)) { DbRow.ThrowInvalidDbRowException(type); } } // outer select: int i = 0; foreach (var property in properties) { string column; var clrTypeMatch = Mapping.CheckClrCompliance(property.PropertyType, out clrType, out exception); if (clrTypeMatch != Mapping.ClrTypeMatch.ClrMatch) { continue; } ViewColumnInfo columnInfo; if (isDbRow) { var rowColumn = rowMap.Columns.Where(a => a.ID.ColumnZ == i + 1).First(); column = rowColumn.Name.Part1; columnInfo = new ViewColumnInfo(column, rowColumn.DataType, rowColumn.IsNullable); columns.Add(columnInfo); } else { column = property.Name; columnInfo = new ViewColumnInfo(column, property.PropertyType, clrType); columns.Add(columnInfo); } if (i != 0) { sqlOuter.NewLineIndent(Text.Comma); sqlEmpty.Append(Text.Comma); } var dataType = Mapping.ProvideDataType(columnInfo.DataType, clrType); AppendOuterColumn(sqlOuter, dataType, i + 1, column); if (isEmpty) { AppendNullValueColumn(sqlEmpty, i + 1); } if (!cached) { getters.Add(PropertyAccessor.Create(type, property)); } ++i; } numberOfProperties = i; if (numberOfProperties == 0) { ThrowInvalidDataClassException(type); } if (!cached) { Cache.PropertyAccessors[type] = getters; } }