This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Simple PDO Wrapper

2.2.1 2017-04-18 14:21 UTC

README

Features

  1. MySQL 5.7.x JSON type support(partial).

Prerequisites

  1. PHP > 5.6
  2. MySQL 5.7 for JSON type support

Installation

composer require lovelock/spw

The you implement ConfigInterface to provide your Database configuration. Config files like ini/yaml/xml or any other formats are OK only if you implement the interface.

Api

There are two kinds of methods, one is for querying conditions, the other is for executing queries.

Querying conditions methods

  1. from($table) Set table name of the query

  2. into($table)
    Alias of from($table)

  3. where(array $conditions)
    Set where clause of query.

  4. orderBy($col, $asc = 'desc')
    Set sequence of result, this can be called multiple times for multiple orders.

  5. limit($limit)
    Set limit number of result.

  6. getNumRows() Get number of rows with specified conditions.

  7. groupBy() Set group by clause.

where() method accepts an array as parameter. The array can be various formats.

  1. where(['id' => 20]) means where id = 20
  2. where(['id' => ['IN', [2, 3, 4]]]) means where id in (2, 3, 4)
  3. where(['id' => ['NOT IN', [2, 3, 4]]]) means where id not in (2, 3, 4)
  4. where(['id' => ['IS', 'NULL']]) means where id is null
  5. where(['id' => ['IS', 'NOT NULL']]) means where id is not null
  6. where(['id' => ['BETWEEN', [2, 4]]]) means where id between (2, 4)

Executing queries methods

  1. select($col)
    Execute select query with specified columns.

  2. update($values)
    Execute update query with specified columns and values.

  3. insert($values)
    Execute insert query with specified columns and values.

  4. delete()
    Execute delete query with specified conditions.

  5. replace() Execute replace query with specified data.

Usage

You're highly recommended to use this library with DIC(Dependency Injection Container), thus Spw can be used as a service. Take a Slim application as example.

<?php

// App initialization
$settings = [...];
$container = new \Slim\Container($settings);
$container['db'] = function ($c) {
    return new \Spw\Connection(new DevConfig());
};

$app = new \Slim\App($container);
$app->run();
.....
<?php
class IoC
{
    private $c;

    public function __construct(ContainerInterface $c)
    {
        $this->c = $c;
    }

    public function __get($name)
    {
        return $this->c->get($name);
    }
}
<?php

/**
 * @property ConnectionInterface db
 */
class ABC extends IoC
{
    public function foo()
    {
        $this->db->from('tablename')
        ->where(['id' => 1])
        ->select();
    }
}

TODO

  1. Cache system integration.
  2. Decoupling of DIC.
  3. Improve JSON support.

Contribution

PR are welcome to improve this project.

Donation

  1. WeChat
  2. Alipay