Library for SQL database management to be used by several providers at the same time.
- Installation
- Requirements
- Quick Start and Examples
- Available Methods
- Usage
- Select
- Insert
- Update
- Replace
- Delete
- Create
- Truncate
- Drop
- Tests
- TODO
- Exception Handler
- Contribute
- Repository
- License
- Copyright
The preferred way to install this extension is through composer.
To install PHP Database library, simply:
$ composer require Josantonius/Database
The previous command will only install the necessary files, if you prefer to download the entire source code (including tests, vendor folder, exceptions not used, docs...) you can use:
$ composer require Josantonius/Database --prefer-source
Or you can also clone the complete repository with Git:
$ git clone https://github.com/Josantonius/PHP-Database.git
This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.
To use this class, simply:
require __DIR__ . '/vendor/autoload.php';
use Josantonius\Database\Database;
Available methods in this library:
Database::getConnection();
Database->query();
Database->create();
Database->select();
Database->insert();
Database->update();
Database->replace();
Database->delete();
Database->truncate();
Database->drop();
Database->in();
Database->table();
Database->from();
Database->where();
Database->order();
Database->limit();
Database->execute();
Example of use for this library:
<?php
require __DIR__ . '/vendor/autoload.php';
use Josantonius\Database\Database;
$db = Database::getConnection(
'identifier', # Unique identifier for the connection
'PDOprovider', # Database provider name
'localhost', # Database server
'db-user', # Database user
'db-name', # Database name
'password', # Database password
array('charset' => 'utf8')
);
// And once the connection is established:
$db = Database::getConnection('identifier');
Select data from database.
$db->select()->from()->where()->order()->limit()->execute();
select($columns)
$columns → (array|string|empty) Names of columns to select. If left blank select all fields.
from($table)
$table → (string) Database table name.
where($clauses, $statements) (Optional)
$clauses → (array|string) → Where clauses.
$statements → (array) (Optional) → Prepared statements.
order($params) (Optional)
$params → (array|string) → Parameters to sort query.
limit($number) (Optional)
$number → (int) → Rows number limit.
execute($dataType)
$dataType → (string|empty) → Accepted parameters: 'obj', 'array_num', 'array_assoc' & 'rows'.
SELECT query example. For more examples see the DatabaseSelectTest class.
$statements[] = [':id', 1, 'int'];
$statements[] = [':name', 'Manny', 'str'];
$clauses = ['id = :id', 'name = :name'];
$query = $db->select('name')
->from('test')
->where($clauses, $statements);
->order('id DESC')
->limit(1);
$result = $query->execute('obj');
Insert data into database.
$db->insert()->in()->execute();
insert($data, $statements)
$data → (array) → Name of columns and values to be inserted.
$statements → (array) (Optional) → Prepared statements.
in($table)
$table → (string) Database table name.
execute($dataType)
$dataType → (string|empty) → Accepted parameters: 'rows' & 'id'.
INSERT query example. For more examples see the DatabaseInsertTest class.
$statements[] = [1, "Isis"];
$statements[] = [2, "isis@email.com"];
$data = [
"name" => "?",
"email" => "?"
];
$query = $db->insert($data, $statements)
->in('test');
$result = $query->execute('id');
Update fields in the database.
$db->update()->in()->where()->execute();
update($data, $statements)
$data → (array) → Name of columns and values to be inserted.
$statements → (array) (Optional) → Prepared statements.
where($clauses, $statements) (Optional)
$clauses → (array|string) → Where clauses.
$statements → (array) (Optional) → Prepared statements.
execute($dataType)
$dataType → (string|empty) → Accepted parameters: 'rows' & 'id'.
UPDATE query example. For more examples see the DatabaseUpdateTest class.
$data = [
'name' => ':new_name',
'email' => ':new_email'
];
$statements['data'][] = [':new_name', 'Manny', 'str'];
$statements['data'][] = [':new_email', 'manny@email.com', 'str'];
$clauses = 'id = :id AND name = :name1 OR name = :name2';
$statements['clauses'][] = [':id', 1, 'int'];
$statements['clauses'][] = [':name1', 'Isis', 'str'];
$statements['clauses'][] = [':name2', 'Manny', 'str'];
$query = $db->update($data, $statements['data'])
->in('test')
->where($clauses, $statements['clauses']);
$result = $query->execute();
Replace a row in a table if it exists or insert a new row in a table if not exist.
$db->replace()->from()->execute();
replace($data, $statements)
$data → (array) → Name of columns and values to be replaced.
$statements → (array) (Optional) → Prepared statements.
from($table)
$table → (string) Database table name.
execute($dataType)
$dataType → (string|empty) → Accepted parameters: 'rows' & 'id'.
REPLACE example. For more examples see the DatabaseReplaceTest class.
$data = [
'id' => 1,
'name' => 'Manny',
'email' => 'manny@email.com'
];
$query = $db->replace($data)
->from('test');
$result = $query->execute();
Delete fields in the database.
$db->delete()->from()->where()->execute();
delete()
This method has no attributes.
from($table)
$table → (string) Database table name.
where($clauses, $statements) (Optional)
$clauses → (array|string) → Where clauses.
$statements → (array) (Optional) → Prepared statements.
execute($dataType)
$dataType → (string|empty) → Accepted parameters: 'rows' & 'id'.
DELETE query example. For more examples see the DatabaseDeleteTest class.
$query = $db->delete()
->from('test')
->where('id = 1');
$result = $query->execute();
Create table in database.
$db->create()->table()->execute();
create($params)
$params → (array) → Parameters of configuration for the columns.
table($table)
$table → (string) Database table name.
foreing($foreing_key) (Optional)
$foreing_key → (string) Foreing key.
reference($reference) (Optional)
$reference → (string) Column reference.
on($table) (Optional)
$table → (string) Table reference.
actions($actions) (Optional)
$actions → (string) Actions when delete or update for foreing key.
engine($engine) (Optional)
$engine → (string) Database engine.
charset($charset) (Optional)
$charset → (string) Database charset.
execute()
This method has no attributes.
CREATE query example. For more examples see the DatabaseCreateTest class.
$params = [
'id' => 'INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY',
'name' => 'VARCHAR(30) NOT NULL',
'email' => 'VARCHAR(50)',
'reg_date' => 'TIMESTAMP'
];
$query = static::$db->create($params)
->table('test');
$result = $query->execute();
Truncate table in database.
$db->truncate()->table()->execute();
truncate()
This method has no attributes.
table($table)
$table → (string) Database table name.
execute()
This method has no attributes.
TRUNCATE query example. For more examples see the DatabaseTruncateTest class.
$query = $db->truncate()
->table('test');
$result = $query->execute();
Drop table in database.
$db->drop()->table()->execute();
drop()
This method has no attributes.
table($table)
$table → (string) Database table name.
execute()
This method has no attributes.
DROP query example. For more examples see the DatabaseDropTest class.
$query = $db->drop()
->table('test');
$result = $query->execute();
To run tests simply:
$ git clone https://github.com/Josantonius/PHP-Database.git
$ cd PHP-Database
$ mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS phpunit CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 'travis'@'127.0.0.1' IDENTIFIED BY ''; GRANT ALL ON phpunit.* TO 'travis'@'127.0.0.1'; FLUSH PRIVILEGES;"
$ phpunit
- Create tests
- Improve documentation
This library uses exception handler that you can customize.
- Check for open issues or open a new issue to start a discussion around a bug or feature.
- Fork the repository on GitHub to start making your changes.
- Write one or more tests for the new feature or that expose the bug.
- Make code changes to implement the feature or fix the bug.
- Send a pull request to get your changes merged and published.
This is intended for large and long-lived objects.
All files in this repository were created and uploaded automatically with Reposgit Creator.
This project is licensed under MIT license. See the LICENSE file for more info.
2017 Josantonius, josantonius.com
If you find it useful, let me know 😉