PHP code example of haoa / mix-database
1. Go to this page and download the library: Download haoa/mix-database library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
haoa / mix-database example snippets
$db = new Haoa\MixDatabase\Database('mysql:host=127.0.0.1;port=3306;charset=utf8mb4;dbname=test', 'username', 'password');
$db->insert('users', [
'name' => 'foo',
'balance' => 0,
]);
$db->table('users')->where('id = ?', 1)->first();
$db->table('users')->where('id = ?', 1)->update('name', 'foo1');
$db->table('users')->where('id = ?', 1)->delete();
$db = new Haoa\MixDatabase\Database(
'pgsql:host=127.0.0.1;port=5432;dbname=test',
'username',
'password',
[PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
true // 对普通表名、字段名加驱动对应的引号
);
/** 用户数据模型。 */
class UserModel extends Haoa\MixDatabase\Context\Model
{
public static string $tableName = 'users';
// 示例表没有自动时间字段,关闭自动填充。
protected string $createTimeField = '';
protected string $updateTimeField = '';
}
$modelDb = new Haoa\MixDatabase\Context\Database(
'pgsql:host=127.0.0.1;port=5432;dbname=test',
'username',
'password',
[PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);
$users = UserModel::db($modelDb)->where('id', 'IN', [1, 2])->get();
$maxOpen = 50; // 最大开启连接数
$maxIdle = 20; // 最大闲置连接数
$maxLifetime = 3600; // 连接的最长生命周期
$waitTimeout = 0.0; // 从池获取连接等待的时间, 0为一直等待
$db->startPool($maxOpen, $maxIdle, $maxLifetime, $waitTimeout);
Swoole\Runtime::enableCoroutine(); // 示例先初始化连接池,再开启协程 Hook
Swoole\Coroutine\run(function () use ($db) {
$users = $db->table('users')->limit(10)->get();
});
$db->poolStats(); // array, fields: total, idle, active
$data = [
'name' => 'foo',
'balance' => 0,
];
$db->insert('users', $data);
$data = [
'name' => 'foo',
'balance' => 0,
];
$insertId = $db->insert('users', $data)->lastInsertId();
$data = [
'name' => 'foo',
'balance' => 0,
];
$db->insert('users', $data, 'REPLACE INTO');
$data = [
[
'name' => 'foo',
'balance' => 0,
],
[
'name' => 'foo1',
'balance' => 0,
]
];
$db->batchInsert('users', $data);
$data = [
'name' => 'foo',
'balance' => 0,
'add_time' => new Haoa\MixDatabase\Expr('CURRENT_TIMESTAMP'),
];
$db->insert('users', $data);
$db->table('users')->where('id = ? AND name = ?', 1, 'foo')->get();
$db->table('users')->where('id = ?', 1)->where('name = ?', 'foo')->get();
$db->table('users')->where('id = ? OR id = ?', 1, 2)->get();
$db->table('users')->where('id = ?', 1)->or('id = ?', 2)->get();
$db->table('users')->where('id IN (?)', [1, 2])->get();
$db->table('users')->where('id NOT IN (?)', [1, 2])->get();
$db->table('users')->select('id, name')->get();
$db->table('users')->select('id', 'name')->get();
$db->table('users')->select('name AS n')->get();
$db->table('users')->order('id', 'desc')->get();
$db->table('users')->order('id', 'desc')->order('name', 'asc')->get();
$db->table('users')->limit(5)->get();
$db->table('users')->offset(10)->limit(5)->get();
$db->table('news')->select('uid, COUNT(*) AS total')->group('uid')->having('COUNT(*) > ?', 0)->get();
$db->table('news')->select('uid, COUNT(*) AS total')->group('uid')->having('COUNT(*) > ? AND COUNT(*) < ?', 0, 10)->get();
$db->table('news AS n')->select('n.*, u.name')->join('users AS u', 'n.uid = u.id')->get();
$db->table('news AS n')->select('n.*, u.name')->leftJoin('users AS u', 'n.uid = u.id AND u.balance > ?', 10)->get();
$db->table('users')->where('id = ?', 1)->update('name', 'foo1');
$rowsAffected = $db->table('users')->where('id = ?', 1)->update('name', 'foo1')->rowCount();
$data = [
'name' => 'foo1',
'balance' => 100,
];
$db->table('users')->where('id = ?', 1)->updates($data);
$db->table('users')->where('id = ?', 1)->update('balance', new Haoa\MixDatabase\Expr('balance + ?', 1));
$data = [
'balance' => new Haoa\MixDatabase\Expr('balance + ?', 1),
];
$db->table('users')->where('id = ?', 1)->updates($data);
$db->table('users')->where('id = ?', 1)->update('add_time', new Haoa\MixDatabase\Expr('CURRENT_TIMESTAMP'));
$data = [
'add_time' => new Haoa\MixDatabase\Expr('CURRENT_TIMESTAMP'),
];
$db->table('users')->where('id = ?', 1)->updates($data);
$db->table('users')->where('id = ?', 1)->delete();
$rowsAffected = $db->table('users')->where('id = ?', 1)->delete()->rowCount();
$db->raw('SELECT * FROM users WHERE id = ?', 1)->first();
$db->exec('DELETE FROM users WHERE id = ?', 1)->rowCount();
$tx = $db->beginTransaction();
try {
$data = [
'name' => 'foo',
'balance' => 0,
];
$tx->insert('users', $data);
$tx->commit();
} catch (\Throwable $ex) {
$tx->rollback();
throw $ex;
}
$db->transaction(function (Haoa\MixDatabase\Transaction $tx) {
$data = [
'name' => 'foo',
'balance' => 0,
];
$tx->insert('users', $data);
});
$db->debug(function (Haoa\MixDatabase\ConnectionInterface $conn) {
var_dump($conn->queryLog()); // array, fields: time, sql, bindings
})
->table('users')
->where('id = ?', 1)
->get();
$db->setLogger($logger);
interface LoggerInterface
{
public function trace(float $time, string $sql, array $bindings, int $rowCount, ?\Throwable $exception): void;
}