Recommend this page to a friend! |
Classes of Josantonius | PHP Database | README.md | Download |
|
DownloadPHP Database librarySQL database management to be used by several providers at the same time.
RequirementsThis library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher. InstallationThe preferred way to install this extension is through Composer. To install PHP Database library, simply:
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
You can also clone the complete repository with Git:
Or install it manually: Download Database.php, Provider.php, PDOprovider.php, MSSQLprovider.php and DBException.php:
Get connection- Get connection:
| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $id | Database unique ID. | string | Yes | | | $provider | Name of provider class. | string | No | null | | $host | Database host. | string | No | null | | $user | Database user. | string | No | null | | $name | Database name. | string | No | null | | $password | Database password . | string | No | null | | Attribute | Key | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | $settings | | Database options. | array | No | null | | $settings | 'port' | Database port. | string | No | | | $settings | 'charset' | Database charset. | string | No | | # Return (object) ? object with the connection
Query- Process query and prepare it for the provider:
| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $query | Query. | string | Yes | | | $statements | Statements. | array | No | null | | $result | Query result; 'obj', 'array_num', 'array_assoc', 'rows', 'id'. | string | No | 'obj' | # Return (mixed) ? result as object, array, int... # throws [DBException] ? invalid query type
CREATE TABLE- CREATE TABLE statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $data | Column name and configuration for data types. | array | Yes | | | table() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | foreing() | | Set foreing key. | method | No | | | | $id | Column id. | string | Yes | | | reference() | | Set reference for foreing keys. | method | No | | | | $table | Table name. | array | Yes | | | on() | | Set database table name. | method | No | | | | $table | Table name. | array | Yes | | | actions() | | Set actions when delete or update for foreing key. | method | No | | | | $action | Action when delete or update. | array | Yes | | | engine() | | Set table engine. | method | No | | | | $type | Engine type. | string | Yes | | | charset() | | Set table charset. | method | No | | | | $type | Charset type. | string | Yes | | | execute() | | Execute query. | method | Yes | | # Return (boolean)
SELECT- SELECT statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $columns | Column/s name. | mixed | No | '*' | | from() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | where() | | Where clauses. | method | No | | | | $clauses | Column name and value. | mixed | Yes | | | | $statements | Statements. | array | No | null | | order() | | Order. | method | No | | | | $type | Query sort parameters. | string | Yes | | | limit() | | Limit. | method | No | | | | $number | Number. | int | Yes | | | execute() | | Execute query. | method | Yes | | | | $result | Query result; 'obj', 'array_num', 'array_assoc', 'rows'. | string | No | 'obj' | # Return (mixed) ? query result (object, array, int...) or rows affected
INSERT INTO- INSERT INTO statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $data | Column name and value. | array | Yes | | | | $statements | Statements. | array | No | null | | in() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | execute() | | Execute query. | method | Yes | | | | $result | Query result; 'rows', 'id'. | string | No | 'rows' | # Return (int) ? rows affected or last row affected ID
UPDATE- UPDATE statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $data | Column name and value. | array | Yes | | | | $statements | Statements. | array | No | null | | in() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | where() | | Where clauses. | method | No | | | | $clauses | Column name and value. | mixed | Yes | | | | $statements | Statements. | array | No | null | | execute() | | Execute query. | method | Yes | | # Return (int) ? rows affected
REPLACE- Replace a row in a table if it exists or insert a new row if not exist:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $data | Column name and value. | array | Yes | | | | $statements | Statements. | array | No | null | | from() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | execute() | | Execute query. | method | Yes | | | | $result | Query result; 'rows', 'id'. | string | No | 'rows' | # Return (int) ? rows affected or last row affected ID
DELETE- DELETE statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | | $data | Column name and value. | array | Yes | | | | $statements | Statements. | array | No | null | | from() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | where() | | Where clauses. | method | No | | | | $clauses | Column name and value. | mixed | Yes | | | | $statements | Statements. | array | No | null | | execute() | | Execute query. | method | Yes | | # Return (int) ? rows affected
TRUNCATE TABLE- TRUNCATE TABLE statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | table() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | execute() | | Execute query. | method | Yes | | # Return (boolean)
DROP TABLE- DROP TABLE statement:
| Method | Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | table() | | Set database table name. | method | Yes | | | | $table | Table name. | string | Yes | | | execute() | | Execute query. | method | Yes | | # Return (boolean)
Quick StartTo use this class with Composer:
Or If you installed it manually, use it:
TestsTo run tests you just need composer and to execute the following:
Run unit tests with PHPUnit:
Run PSR2 code standard tests with PHPCS:
Run PHP Mess Detector tests to detect inconsistencies in code style:
Run all previous tests:
? TODO
Exception HandlerThis library uses exception handler that you can customize. ContributeIf you would like to help, please take a look at the list of issues or the To Do checklist. Pull requests
RepositoryThe file structure from this repository was created with PHP-Skeleton. LicenseThis project is licensed under MIT license. See the LICENSE file for more info. Copyright2017 Josantonius, josantonius.com If you find it useful, let me know :wink: |