MysqlTableTrait uses trait:short, \oihana\models\pdo\PDOTrait
Provides operations for managing and inspecting MySQL tables in the current database.
Includes methods for listing, dropping, renaming, optimizing, repairing, and sizing tables.
Requires a connected PDO instance and uses MysqlAssertionsTrait for input validation.
Tags
Table of Contents
Methods
- dropTable() : bool
- Drops a table in the current database.
- getTableSize() : int
- Returns the size of a table in bytes.
- listCurrentTables() : array<int, string>
- Lists all tables in the current database.
- optimizeTable() : bool
- Optimizes a table.
- renameTable() : bool
- Renames a table in the current database.
- repairTable() : bool
- Repairs a table.
- tableExists() : bool
- Checks if a table exists in the current database.
- assertHost() : void
- Validates a MySQL host string.
- assertIdentifier() : void
- Validates a MySQL identifier such as a database name, user name, or table name.
Methods
dropTable()
Drops a table in the current database.
public
dropTable(string $table) : bool
Parameters
- $table : string
-
Table name.
Return values
bool —True on success.
getTableSize()
Returns the size of a table in bytes.
public
getTableSize(string $table) : int
Parameters
- $table : string
-
Table name.
Tags
Return values
int —Table size in bytes.
listCurrentTables()
Lists all tables in the current database.
public
listCurrentTables([bool $throwable = false ]) : array<int, string>
Parameters
- $throwable : bool = false
Return values
array<int, string> —Array of table names.
optimizeTable()
Optimizes a table.
public
optimizeTable(string $table) : bool
Parameters
- $table : string
-
Table name.
Return values
bool —True on success.
renameTable()
Renames a table in the current database.
public
renameTable(string $from, string $to) : bool
Parameters
- $from : string
-
Current table name.
- $to : string
-
New table name.
Return values
bool —True if renamed successfully.
repairTable()
Repairs a table.
public
repairTable(string $table) : bool
Parameters
- $table : string
-
Table name.
Return values
bool —True on success.
tableExists()
Checks if a table exists in the current database.
public
tableExists(string $table) : bool
Parameters
- $table : string
-
Table name.
Return values
bool —True if the table exists.
assertHost()
Validates a MySQL host string.
protected
assertHost(string $host) : void
A valid host string may contain:
- letters (a–z, A–Z)
- digits (0–9)
- dots (.)
- hyphens (-)
- underscores (_) and percent signs (%) for wildcards
Parameters
- $host : string
-
The host name or IP to validate (e.g., 'localhost', '127.0.0.1', '%.example.com').
Tags
assertIdentifier()
Validates a MySQL identifier such as a database name, user name, or table name.
protected
assertIdentifier(string $name) : void
A valid identifier consists of letters (a–z, A–Z), digits (0–9), and underscores (_). This ensures safe usage in SQL queries without risk of injection or syntax errors.
Parameters
- $name : string
-
The identifier to validate.