Page 8705 Table Information Card, source in 29

Source2829

src/System Application/App/Table Information/src/TableInformationCard.Page.al172 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.DataAdministration;

using System.Diagnostics;
using System.Environment;
using System.Reflection;

/// <summary>
/// Page which displays detailed database information for a given table.
/// </summary>
page 8705 "Table Information Card"
{
    PageType = Card;
    UsageCategory = Administration;
    ApplicationArea = All;
    AdditionalSearchTerms = 'Database,Size,Storage,Index,Key,Table';
    SourceTable = "Table Metadata";
    Caption = 'Index Management';
    DeleteAllowed = false;
    InsertAllowed = false;
    ModifyAllowed = false;
    DataCaptionExpression = StrSubstNo('%1 - %2', Rec.Name, Rec.ID);
    Permissions = tabledata "Table Metadata" = r,
                  tabledata "Database Index" = r;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field(TableNo; Rec.ID)
                {
                    Caption = 'Table No.';
                    Editable = false;
                    ToolTip = 'Specifies the number for the table.';
                }
                field(TableName; Rec.Name)
                {
                    Caption = 'Table Name';
                    Editable = false;
                    ToolTip = 'Specifies the name of the table.';
                }
                field(Company; SetCompanyName)
                {
                    Enabled = PerCompany;
                    Caption = 'Company Name';
                    TableRelation = Company.Name;
                    ToolTip = 'Specifies the name of the company the table belongs to if the table is per-company. Changing this value will update the data shown on this page to reflect the selected company.';

                    trigger OnValidate()
                    begin
                        SetBasedOnCompanyName(SetCompanyName);

                        CurrPage.Update(false);
                    end;
                }
                field(IndexSize; IndexSizeKB)
                {
                    Caption = 'Combined Index Size (kB)';
                    Editable = false;
                    ToolTip = 'Specifies the combined size of all indexes on the table for this company, presented in kilobytes.';
                }
                field(RowCount; RowCount)
                {
                    Caption = 'Table Row Count';
                    Editable = false;
                    ToolTip = 'Specifies the number of rows in the table for this company.';
                }
                field("Database start time"; SqlServerRestartTime)
                {
                    Caption = 'Database start time';
                    Editable = false;
                    ToolTip = 'Specifies the last time the database engine was started. Index statistics are reset when SQL Server is restarted.';
                }
            }
            part(IndexLines; "Indexes List Part")
            {
                SubPageLink = TableId = field(ID);
            }
            part(IndexDetails; "Index Details")
            {
                Provider = IndexLines;
                SubPageLink = TableId = field(TableId),
                              "Company Name" = field("Company Name"),
                              "Source App ID" = field("Source App ID"),
                              "Index Name" = field("Index Name"),
                              "Index Type" = field("Index Type");
            }
        }
    }

    trigger OnOpenPage()
    begin
        SetBasedOnCompanyName(Rec.CurrentCompany());
    end;

    local procedure SetBasedOnCompanyName(NewCompanyName: Text)
    var
        AllObjWithCaption: Record AllObjWithCaption;
        DatabaseIndex: Record "Database Index";
        TableMetadata: Record "Table Metadata";
        Objects: Page System.Reflection.Objects;
        Recref: RecordRef;
        TableId: Integer;
    begin
        if Rec.ID <> 0 then
            TableId := Rec.ID
        else
            if Evaluate(TableId, Rec.GetFilter("ID")) and (TableId <> 0) then
                TableId := TableId
            else begin
                // Originally the page was opened via Table Information,
                // but we want to allow opening the page directly, so we need to prompt the user to select a table.

                AllObjWithCaption.SetRange("Object Type", AllObjWithCaption."Object Type"::Table);
                Objects.SetRecord(AllObjWithCaption);
                Objects.SetTableView(AllObjWithCaption);
                Objects.Caption(SelectTableLbl);
                Objects.LookupMode(true);

                if Objects.RunModal() <> Action::LookupOK then
                    Error(NoTableSelectedErr);

                Objects.GetRecord(AllObjWithCaption);
                TableId := AllObjWithCaption."Object ID";

                Rec.SetRange(Id, TableId);
            end;

        if not TableMetadata.Get(TableId) then
            Error(TableNotFoundErr, TableId);

        // By-default show data for the current company.
        if TableMetadata.DataPerCompany and (NewCompanyName <> '') then begin
            SetCompanyName := NewCompanyName;
            PerCompany := true;

            DatabaseIndex.SetRange(DatabaseIndex."Company Name", SetCompanyName);
        end;

        Recref.Open(TableId, false, SetCompanyName);

        RowCount := Recref.Count();
        IndexSizeKB := 0;
        SqlServerRestartTime := 0DT;
        DatabaseIndex.SetRange(DatabaseIndex.TableId, TableId);
        if DatabaseIndex.FindSet() then
            repeat
                if SqlServerRestartTime = 0DT then
                    SqlServerRestartTime := DatabaseIndex."Database Start time";

                IndexSizeKB += DatabaseIndex."Index Size (KB)";
            until DatabaseIndex.Next() = 0;

        CurrPage.IndexLines.Page.SetCompanyFilter(SetCompanyName);
    end;

    var
        IndexSizeKB: BigInteger;
        RowCount: Integer;
        SetCompanyName: Text;
        SqlServerRestartTime: DateTime;
        PerCompany: Boolean;
        NoTableSelectedErr: Label 'No table selected. Please select a table to view its information.';
        SelectTableLbl: Label 'Select Table';
        TableNotFoundErr: Label 'Table %1 could not be found.', Comment = '%1 = table ID';
}