Exemplo n.º 1
0
        private static void PrintTableContent(RealmTable table, Func <int, int, string> getBacklinkTarget, int indent)
        {
            var spec  = table.Spec;
            var count = Math.Min(table.RowCount, PrintCount);

            for (var rowIndex = 0; rowIndex < count; rowIndex++)
            {
                for (var columnIndex = 0; columnIndex < spec.ColumnCount; columnIndex++)
                {
                    var columnSpec = spec.GetColumn(columnIndex);
                    var columnName = columnSpec.Name;
                    var columnType = columnSpec.Type;

                    object value;
                    switch (columnType)
                    {
                    case ColumnType.Int:
                        value = columnSpec.Nullable
                                ? table.GetNullableInt(columnIndex, rowIndex)
                                : table.GetInt(columnIndex, rowIndex);
                        break;

                    case ColumnType.Bool:
                        value = columnSpec.Nullable
                                ? table.GetNullableBool(columnIndex, rowIndex)
                                : table.GetBool(columnIndex, rowIndex);
                        break;

                    case ColumnType.String:
                        value = table.GetString(columnIndex, rowIndex);
                        break;

                    case ColumnType.Binary:
                        var bin = table.GetBinary(columnIndex, rowIndex);
                        if (bin == null)
                        {
                            value = null;
                        }
                        else
                        {
                            value = string.Concat(bin.Select(x => x.ToString("x2")).Prepend("0x"));
                        }
                        break;

                    case ColumnType.Table:
                        var subtableRowCount = table.GetSubtable(columnIndex, rowIndex)?.RowCount ?? 0;
                        value = $"Subtable ({Math.Min(subtableRowCount, PrintCount)}/{subtableRowCount})";
                        break;

                    case ColumnType.OldDateTime:
                        value = columnSpec.Nullable
                                ? table.GetNullableOldDateTime(columnIndex, rowIndex)
                                : table.GetOldDateTime(columnIndex, rowIndex);
                        break;

                    case ColumnType.Timestamp:
                        value = table.GetTimestamp(columnIndex, rowIndex);
                        break;

                    case ColumnType.Float:
                        value = columnSpec.Nullable
                                ? table.GetNullableFloat(columnIndex, rowIndex)
                                : table.GetFloat(columnIndex, rowIndex);
                        break;

                    case ColumnType.Double:
                        value = columnSpec.Nullable
                                ? table.GetNullableDouble(columnIndex, rowIndex)
                                : table.GetDouble(columnIndex, rowIndex);
                        break;

                    case ColumnType.Link:
                        value = table.GetLink(columnIndex, rowIndex);
                        break;

                    case ColumnType.LinkList:
                        value = string.Join(", ", table.GetLinkList(columnIndex, rowIndex));
                        break;

                    case ColumnType.BackLink:
                        columnName = "<- " + getBacklinkTarget(spec.GetLinkTargetTableIndex(columnIndex), spec.GetBacklinkOriginColumnIndex(columnIndex));
                        value      = string.Join(", ", table.GetBacklinks(columnIndex, rowIndex));
                        break;

                    default:
                        value = "(unsupported type)";
                        break;
                    }

                    for (var i = 0; i < indent; i++)
                    {
                        Console.Write("  ");
                    }

                    Console.WriteLine(
                        "{0} {1}: {2}",
                        columnIndex == 0 ? "-" : " ",
                        columnName,
                        value ?? "(null)");

                    if (columnType == ColumnType.Table)
                    {
                        var subtable = table.GetSubtable(columnIndex, rowIndex);
                        if (subtable != null)
                        {
                            PrintTableContent(
                                table.GetSubtable(columnIndex, rowIndex),
                                getBacklinkTarget,
                                indent + 2
                                );
                        }
                    }
                }
            }
        }