NaifyDb\Adapter\Mysql クラス

extends NaifyDb\Adapter\Adapter
implements NaifyDb\Connection\ConnectionConfigInterface

MySQL / MariaDB 接続設定ビルダー

host / port / charset / collation まで fluent に組み立て、 Connection や NaifyDb へ渡す Adapter 設定を 1 オブジェクトにまとめる。

使用例

$config = Mysql::configure()
    ->host('127.0.0.1')
    ->port(3306)
    ->database('mydb')
    ->username('root')
    ->password('secret')
    ->charset('utf8mb4')
    ->collation('utf8mb4_unicode_ci');

NaifyDb::configure()->loadConfig('main', $config)->apply();
$db = NaifyDb::db('main');

設計意図

PDO::MYSQL_ATTR_INIT_COMMAND は PHP 8.5 で deprecated。整数値 1002 で defined() 分岐

プロパティ

名前型説明
private $host string 接続先ホスト
private $port int 接続先ポート
private $database string データベース名
private $username string 接続ユーザー名
private $password string 接続パスワード
private $charset string 接続 charset
private $collation string 接続直後に適用する照合順序

メソッド

メソッド説明
public host(string $host): static 接続先ホストを設定する
public port(int $port): static 接続先ポートを設定する
public database(string $database): static データベース名を設定する
public username(string $username): static 接続ユーザー名を設定する
public password(string $password): static 接続パスワードを設定する
public charset(string $charset): static 接続 charset を設定する
public collation(string $collation): static 接続直後に適用する collation を設定する
public override toDsn(): string MySQL / MariaDB 用 DSN を生成する
public override getUsername(): ?string 接続ユーザー名を返す 8.4 → property hooks
public override getPassword(): ?string 接続パスワードを返す 8.4 → property hooks
public override getOptions(): array PDO オプションを返す
charset / collation の初期化 SQL もここで付与する。
PDO::MYSQL_ATTR_INIT_COMMAND は PHP 8.5 で deprecated。整数値 1002 で defined() 分岐
8.5 → Pdo\Mysql::ATTR_INIT_COMMAND
public override quoteIdentifier(string $identifier): string MySQL 形式で識別子をクオートする
public override getMetadata(): MetadataInterface MySQL 用 metadata オブジェクトを返す
public override describeTable(PDO $pdo, string $tableName): array MySQL / MariaDB のカラム情報を直接取得する

継承プロパティ

名前型説明継承元
protected $extra array ドライバー固有の追加設定置き場 Adapter

継承メソッド

メソッド説明継承元
public static configure(): static インスタンスを生成する Adapter
public set(): static 任意のプロパティを設定する Adapter
public profiler(): static クエリプロファイラを設定する Adapter
public getProfiler(): ?QueryProfilerInterface プロファイラのインスタンスを取得する Adapter
public toArray(): array 設定を配列へエクスポートする Adapter
protected parseTypeAndSize(): array 型文字列からデータ型とサイズを分離する Adapter