项目作者: ulrack

项目描述 :
Database Abstraction Layer PDO implementation.
高级语言: PHP
项目地址: git://github.com/ulrack/dbal-pdo.git
创建时间: 2019-04-26T21:05:11Z
项目社区:https://github.com/ulrack/dbal-pdo

开源协议:MIT License

下载


DEPRECATION NOTICE: this package has been moved and improved at grizz-it/dbal-pdo

Build Status

Ulrack DBAL PDO

Ulrack DBAL PDO provides a PDO implementation for Ulrack DBAL.
This package only implements the database connection and transaction part.

Installation

To install the package run the following command:

  1. composer require ulrack/dbal-pdo

Usage

Creating a connection

To establish a connection with the database through the
Ulrack\Dbal\Pdo\Component\Connection\PdoConnection object, it is preferred to use the
supplied factory in the package.

First of, initialize the factory by adding:

  1. <?php
  2. use Ulrack\Dbal\Pdo\Factory\PdoConnectionFactory;
  3. $factory = new PdoConnectionFactory;

Then proceed to create an instance of PdoConnection by calling the create method.

  1. <?php
  2. $connection = $factory->create(
  3. 'mysql:dbname=test;host=localhost',
  4. 'test',
  5. 'test'
  6. );

The factory will then create a PDO object, inject it into the PdoConnection
object and return the connection object.

The create method has the following parameters:

string $dsn

This parameter expects a DSN string.
The Data Source Name, or DSN, contains the information required to connect to
the database. An example of how to compose such a string for MySQL can be found
here.

string $username

This string expects the username of the database user through which you wish to
connect your application to the database.

string $password (optional)

The password field expects the password for the previously mentioned user.

array $options (optional)

This parameter expects driver specific connection options in key value pairs.
These can be found through the PDO drivers
page on PHP.net.

array $attributes (optional)

This parameter expects PDO attributes to be set in key value pairs.
These options can be found here.
All these options are immediately set on the PDO object after initialisation.

Calling the database

After the connection has been established with the database it is possible to
send query objects to the PdoConnection, which will run their queries on the
database.

After a query object is fully assembled it can be executed in the following ways:

  1. // Immediate call to the database.
  2. $result = $connection->query($queryObject);
  3. // Transactional call to the database.
  4. $connection->startTransaction();
  5. $result = $connection->query($queryObject);
  6. $connection->commit();
  7. // It is also possible to rollback a transaction before it is committed:
  8. $connection->rollback();

If the query was an insertion of a new record, the insert ID can be retrieved
by calling lastTransactionId:

  1. $connection->lastTransactionId();

Reading the result

The results of a query are send back to the application through an instance of
the Ulrack\Dbal\Pdo\Component\Result\PdoQueryResult class. It is possible to iterate over
this object by running it in a foreach loop. If all records should be retrieved
at once, then the fetchAll() method can be called on the result object.

The amount of affected records can be retrieved by “counting” the object:

  1. count($result); //Returns affected rows.

To assert the success of the query, the isSuccess method can be called on the
result object.

The status code and errors (as a result of a failing query) can be retrieved by
calling getErrors and getStatusCode on the result object.

All SQLState status codes can be found here.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

MIT License

Copyright (c) GrizzIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.