kickpeach/database

此包的最新版本(dev-master)没有提供许可证信息。

数据库组件

dev-master 2019-02-06 13:28 UTC

This package is not auto-updated.

Last update: 2024-09-27 10:50:37 UTC


README

基于PDO的kickpeach框架的数据库组件,也可以独立使用,支持MySQL以及PostgreSQL数据库

如何使用

安装

    composer require kickpeach/database -vvv

使用

具体使用方法,请参考tests

获取连接

  • MySQL
$dsn = sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', '127.0.0.1', 3306, 'es_demo', 'utf8mb4');
$conn = new MySqlConnection($dsn, 'root', 123123);
  • PostgreSQL
$dsn = sprintf('pgsql:host=%s;port=%s;dbname=%s', '127.0.0.1', 3306, 'es_demo');
$conn = new PostgresConnection($dsn, 'root', 123123);

插入并获取插入ID

$insertId = $conn->table('profile')->insertGetId([
    'name'      => 'test-name',
    'gender'    => 1,
    'birthday'  => '1988-12-01 01:00:01', //DATETIME
    'memo'      => 'this is a test memo',
    'lat'       => '30.54916000', //DECIMAL(10,8)
    'lng'       => '104.06761000' //DECIMAL(11,8)
]);

插入并获取影响的行数

$affectNum = $conn->table('profile')->insert([
    [
        'name'      => 'test-name',
        'gender'    => 1,
        'birthday'  => '1988-12-01 01:00:01',
        'memo'      => 'this is a test memo',
        'lat'       => '30.54916000',
        'lng'       => '104.06761000'
    ],
    [
        'name'      => 'test-name-1',
        'gender'    => 1,
        'birthday'  => '2010-12-01 01:00:01',
        'memo'      => 'this is another test memo',
        'lat'       => '30.54916000',
        'lng'       => '104.06761000'
    ],
]);

更新并获取影响的行数

affectNum = $conn->update('update profile set name = :name, memo = :memo where id = :id', [
    ':name'     => 'test-name',
    ':memo'     => 'this is another memo',
    ':id'       => $id,
]);

选择

$records = $conn->select('select * from profile where id = :id', [
    ':id'   => $id,
]);

删除并获取影响的行数

$affectNum = $conn->delete('delete from profile where id = :id', [
    ':id'       => $id,
]);

事务

$conn->transaction(function ($conn) {
    //do something...
});

获取查询日志

$queryLogs = $conn->getQueryLog();

以“空转”模式执行给定的回调

$conn->pretend(function ($conn) {
    //do something...
});

许可证

MIT