Documentation

DbMasterData
in package

Class DbMasterData

This class provides methods for interacting with database metadata and performing operations on tables and their records. It includes functionality for retrieving table metadata, column names, and data items, as well as handling relationships between tables and enforcing column-specific rules.

Table of Contents

Properties

$config  : Config
$mySql  : MySql
$defaultColumns  : array<string|int, mixed>
$defaultColumnsRules  : array<string|int, mixed>
$defaultReadonlyColumns  : array<string|int, mixed>
$factColumns  : array<string|int, mixed>
$HTML_MYSQL_TYPE_RULES  : array<string|int, mixed>

Methods

__construct()  : mixed
getIdByCode()  : array{id: int}|null
Get `id` of item by Code Value - Returns the first matching row's `id`, or null if code is null or not found.
getItemByIdAppAdmin()  : array<string|int, mixed>
getRules()  : array<string, string>
Builds per-column validation rules for a table using INFORMATION_SCHEMA and local mappings.
getTableAppAdmin()  : array<string|int, mixed>
getTablesAppAdmin()  : array<string|int, mixed>
getUniqueKeyColumns()  : array<string|int, mixed>
refreshGroupedLinkedTablesModel()  : mixed
save()  : array<string|int, mixed>
findFreeNTupleStrings()  : array<int, string>
Finds free (unused) N-tuple strings by subtracting the used set from all possible combinations.
getAllowedTables()  : array<string|int, mixed>
getCodeIdPairs()  : mixed
getColumnNames()  : array<string|int, mixed>
getForeignKeysStructure()  : array<string|int, mixed>
getLinkedTablesModel()  : array<string|int, mixed>
getPrimaryKeyColumn()  : string
getReadonlyFields()  : array<string|int, mixed>
getUsedAndFreeCodes()  : mixed
isColumnNullable()  : bool
Checks whether a given column in the configured MySQL schema is nullable.
makeUniqueNTupleStrings()  : array<string|int, mixed>
sanitizeSqlIdentifier()  : string
Validates a SQL identifier (table/column) against a conservative pattern.

Properties

$defaultColumns

private array<string|int, mixed> $defaultColumns = ['id', 'sort_order', 'status', 'createddatetime', 'modifieddatetime', 'createdby', 'modifiedby']

$defaultColumnsRules

private array<string|int, mixed> $defaultColumnsRules = [ 'id' => 'int|default:-1', //if missing, then no error error from Validator (required but passed as empty string (null->empty conversion by Validator)) 'table_name_code' => 'string|required|default:to be set by additional logic and table code_range', // Rest is done by logic in methods 'sort_order' => 'int|nullable', 'status' => 'string|required|in:active,test,do not display,under development,to be deleted,deactivated', 'createddatetime' => 'string|default:to be set by system or pass through', //|date:Y-m-d H:i:s 'modifieddatetime' => 'string|default:to be set by system or pass through', //|date:Y-m-d H:i:s 'createdby' => 'string|default:to be set by system or pass through', 'modifiedby' => 'string|default:to be set by system or pass through', ]

$defaultReadonlyColumns

private array<string|int, mixed> $defaultReadonlyColumns = [ //'id', 'createddatetime', 'modifieddatetime', 'createdby', 'modifiedby', ]

$factColumns

private array<string|int, mixed> $factColumns = ['qty', 'amount_cur', 'amount_pct', 'amount_text']

$HTML_MYSQL_TYPE_RULES

private array<string|int, mixed> $HTML_MYSQL_TYPE_RULES = ['bigint' => 'int', 'int' => 'int', 'float' => 'float', 'varchar' => 'string', 'text' => 'string', 'tinyint' => 'bool', 'datetime' => 'string']

Methods

getIdByCode()

Get `id` of item by Code Value - Returns the first matching row's `id`, or null if code is null or not found.

public getIdByCode(array{foreign_table_name: non-empty-string, column_name: non-empty-string, code_value: scalar|null} $data) : array{id: int}|null
Parameters
$data : array{foreign_table_name: non-empty-string, column_name: non-empty-string, code_value: scalar|null}
Tags
throws
PDOException
Return values
array{id: int}|null

Returns ['id' => ] on match, otherwise null.

getItemByIdAppAdmin()

public getItemByIdAppAdmin(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
$data : array<string|int, mixed>
Return values
array<string|int, mixed>

getRules()

Builds per-column validation rules for a table using INFORMATION_SCHEMA and local mappings.

public getRules(non-empty-string $tableName) : array<string, string>

Validatopn Rules are according to convention of DataValidator.php

Workflow:

  • Reads COLUMN_NAME/DATA_TYPE/CHARACTER_MAXIMUM_LENGTH/IS_NULLABLE from INFORMATION_SCHEMA.
  • Seeds rules from $this->defaultColumnsRules (incl. "_code" via 'table_name_code').
  • Detects *_code / *_id pairs via getColumnTuplesFKs() and assigns string/int rules with required|nullable.
  • Maps remaining columns via $this->HTML_MYSQL_TYPE_RULES, then appends required|nullable, min_length/max_length.
  • Adds suffix-based constraints: *_webpage → url, *_email → email, *_date → date:d.m.Y.
  • Merges optional table-specific overrides from config ('data-validation-rules') via array_replace().
  • Parameters
    $tableName : non-empty-string
    Tags
    throws
    PDOException
    Return values
    array<string, string>

    Map of column name => pipe-delimited rule string.

    getTableAppAdmin()

    public getTableAppAdmin(array<string|int, mixed> $data) : array<string|int, mixed>
    Parameters
    $data : array<string|int, mixed>
    Return values
    array<string|int, mixed>

    getTablesAppAdmin()

    public getTablesAppAdmin() : array<string|int, mixed>
    Return values
    array<string|int, mixed>

    getUniqueKeyColumns()

    public getUniqueKeyColumns(string $tableName) : array<string|int, mixed>
    Parameters
    $tableName : string
    Return values
    array<string|int, mixed>

    refreshGroupedLinkedTablesModel()

    public refreshGroupedLinkedTablesModel(array<string|int, mixed> $junctionDropdownDataInput) : mixed
    Parameters
    $junctionDropdownDataInput : array<string|int, mixed>

    save()

    public save(array<string|int, mixed> $data) : array<string|int, mixed>
    Parameters
    $data : array<string|int, mixed>
    Return values
    array<string|int, mixed>

    findFreeNTupleStrings()

    Finds free (unused) N-tuple strings by subtracting the used set from all possible combinations.

    private findFreeNTupleStrings(array<int, string> $usedCodesCombinationsStrings, array<int, string> $allCodesCombinationsStrings[, bool $isReturnFirst = false ]) : array<int, string>
    • Builds a hash set of used combinations for O(1) lookups.
    • Scans $allCodesCombinationsStrings in order and collects those not present in the used set.
    • If $isReturnFirst is true, returns as soon as the first free combination is found.
    Parameters
    $usedCodesCombinationsStrings : array<int, string>

    Joined tuples that are already used.

    $allCodesCombinationsStrings : array<int, string>

    All possible joined tuples (search order preserved).

    $isReturnFirst : bool = false

    If true, stop after the first free tuple.

    Return values
    array<int, string>

    Free (unused) tuple strings; empty list if none.

    getAllowedTables()

    private getAllowedTables() : array<string|int, mixed>
    Return values
    array<string|int, mixed>

    getCodeIdPairs()

    private getCodeIdPairs(string $tableName, array<string|int, mixed> $columnNames) : mixed
    Parameters
    $tableName : string
    $columnNames : array<string|int, mixed>

    getColumnNames()

    private getColumnNames(string $tableName) : array<string|int, mixed>
    Parameters
    $tableName : string
    Return values
    array<string|int, mixed>

    getForeignKeysStructure()

    private getForeignKeysStructure(string $tableName) : array<string|int, mixed>
    Parameters
    $tableName : string
    Return values
    array<string|int, mixed>

    getLinkedTablesModel()

    private getLinkedTablesModel(string $tableName, array<string|int, mixed> $codeIdPairs) : array<string|int, mixed>
    Parameters
    $tableName : string
    $codeIdPairs : array<string|int, mixed>
    Return values
    array<string|int, mixed>

    getPrimaryKeyColumn()

    private getPrimaryKeyColumn(string $tableName) : string
    Parameters
    $tableName : string
    Return values
    string

    getReadonlyFields()

    private getReadonlyFields(string $tableName[, array<string|int, mixed> $additionalReadonlyList = [] ]) : array<string|int, mixed>
    Parameters
    $tableName : string
    $additionalReadonlyList : array<string|int, mixed> = []
    Return values
    array<string|int, mixed>

    getUsedAndFreeCodes()

    private getUsedAndFreeCodes(string $tableName, array<string|int, mixed> $columnNames, array<string|int, mixed> $columnNamesReferencedColumns, array<string|int, mixed> $foreignTablesNames, int $callingNumber[, array<string|int, mixed> $givenValues = [] ]) : mixed
    Parameters
    $tableName : string
    $columnNames : array<string|int, mixed>
    $columnNamesReferencedColumns : array<string|int, mixed>
    $foreignTablesNames : array<string|int, mixed>
    $callingNumber : int
    $givenValues : array<string|int, mixed> = []

    isColumnNullable()

    Checks whether a given column in the configured MySQL schema is nullable.

    private isColumnNullable(array{table_name: string, column_name: string} $data) : bool
    Parameters
    $data : array{table_name: string, column_name: string}

    Table and column identifiers.

    Tags
    throws
    PDOException

    On database or query execution errors.

    Return values
    bool

    True if the column is nullable, false otherwise.

    makeUniqueNTupleStrings()

    private makeUniqueNTupleStrings(array<string|int, mixed> $codesCombinations[, bool $isPdoReturnStructure = true ][, mixed $separator = '_DELIMIT_' ]) : array<string|int, mixed>
    Parameters
    $codesCombinations : array<string|int, mixed>
    $isPdoReturnStructure : bool = true
    $separator : mixed = '_DELIMIT_'
    Return values
    array<string|int, mixed>

    sanitizeSqlIdentifier()

    Validates a SQL identifier (table/column) against a conservative pattern.

    private sanitizeSqlIdentifier(string $sqlIdentifier) : string

    Accepts only letters, digits, underscores, and hyphens. Optionally intended for table names ($isTable) to enable whitelist enforcement (currently commented).

    Parameters
    $sqlIdentifier : string

    Raw identifier to validate

    Tags
    throws
    InvalidArgumentException

    If the identifier format is invalid

    Return values
    string

    The validated identifier (unchanged)

    
            
    On this page

    Search results