Oihana Php Mysql

MysqlDatabaseTrait uses trait:short, \oihana\models\pdo\PDOTrait

Provides high-level operations for managing MySQL databases.

Includes methods to create, drop, inspect, and maintain databases, as well as retrieve metadata like charset, collation, and size.

Requires a connected PDO instance and uses helper traits for assertions and bindings.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

createDatabase()  : bool
Creates a new MySQL database with given charset and collation.
databaseExists()  : bool
Checks if a MySQL database exists.
dropDatabase()  : bool
Drops a database if it exists.
getDatabaseCharset()  : array{Charset: string, Collation: string}|null
Returns the default character set and collation of a database.
getDatabaseSize()  : int
Returns the size of a database in bytes.
listDatabases()  : array<int, string>
Lists all available databases.
optimizeDatabase()  : bool
Optimizes all tables in a database.
repairDatabase()  : bool
Repairs all tables in a database.
assertHost()  : void
Validates a MySQL host string.
assertIdentifier()  : void
Validates a MySQL identifier such as a database name, user name, or table name.
getRecommendedCollation()  : string
Returns the recommended collation for the given charset and server version.

Methods

createDatabase()

Creates a new MySQL database with given charset and collation.

public createDatabase(string $name[, string|null $charset = null ][, string|null $collation = null ]) : bool
Parameters
$name : string

The name of the database.

$charset : string|null = null

The character set to use (default: 'utf8mb4').

$collation : string|null = null

The collation to use (auto-selected if null).

Return values
bool

True on success, false otherwise.

databaseExists()

Checks if a MySQL database exists.

public databaseExists(string $name) : bool
Parameters
$name : string

Database name.

Return values
bool

True if the database exists.

dropDatabase()

Drops a database if it exists.

public dropDatabase(string $name) : bool
Parameters
$name : string

The name of the database to drop.

Return values
bool

True on success, false otherwise.

getDatabaseCharset()

Returns the default character set and collation of a database.

public getDatabaseCharset(string $dbname) : array{Charset: string, Collation: string}|null
Parameters
$dbname : string
Tags
throws
ContainerExceptionInterface
NotFoundExceptionInterface
DependencyException
NotFoundException
ReflectionException
Return values
array{Charset: string, Collation: string}|null

getDatabaseSize()

Returns the size of a database in bytes.

public getDatabaseSize(string $dbname) : int
Parameters
$dbname : string
Tags
throws
Exception
Return values
int

Size in bytes.

listDatabases()

Lists all available databases.

public listDatabases([bool $excludeSystem = true ]) : array<int, string>
Parameters
$excludeSystem : bool = true

Exclude system databases like 'information_schema', 'mysql', etc.

Tags
throws
Exception
Return values
array<int, string>

List of database names.

optimizeDatabase()

Optimizes all tables in a database.

public optimizeDatabase(string $dbname) : bool
Parameters
$dbname : string
Tags
throws
Exception
Return values
bool

True if all tables optimized successfully.

repairDatabase()

Repairs all tables in a database.

public repairDatabase(string $dbname) : bool
Parameters
$dbname : string
Tags
throws
Exception
Return values
bool

True if all tables repaired successfully.

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
throws
InvalidArgumentException

If the host string contains disallowed characters.

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.

Tags
throws
InvalidArgumentException

If the identifier contains invalid characters.

getRecommendedCollation()

Returns the recommended collation for the given charset and server version.

protected getRecommendedCollation(string $charset) : string
Parameters
$charset : string
Return values
string
On this page

Search results