Skip to content

Commit 05d1fd2

Browse files
committed
Initial commit
0 parents  commit 05d1fd2

18 files changed

+884
-0
lines changed

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "gjhuerte/php-dbwrapper",
3+
"description": "Database wrapper for PHP",
4+
"type": "library",
5+
"license": "GPLv2",
6+
"authors": [
7+
{
8+
"name": "Gabriel Jay Huerte",
9+
"email": "gjhuerte@gmail.com"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"require": {
14+
"php": ">=5.3.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"App\\": "src/"
19+
}
20+
}
21+
}

composer.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/DatabaseWrapper.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace App\Database;
4+
5+
use App\Database\Traits\Orderable;
6+
use App\Database\Traits\Selectable;
7+
use App\Database\Traits\Filterable;
8+
use App\Database\Traits\Executable;
9+
use App\Database\Interfaces\DatabaseInterface;
10+
11+
class DatabaseWrapper implements DatabaseInterface
12+
{
13+
14+
use Selectable, Filterable, Orderable, Executable;
15+
16+
protected static $db;
17+
protected static $table;
18+
19+
private static $instance;
20+
private static $query;
21+
22+
private static $selectArguments;
23+
private static $whereArguments = [];
24+
private static $orderByArguments = [];
25+
26+
/**
27+
* Initialize database connection
28+
*
29+
* @param string $host
30+
* @param string $db
31+
* @param string $charset
32+
* @return instance
33+
*/
34+
public static function init($host, $db_name, $charset)
35+
{
36+
$dsn = 'mysql:host=' . $db_host . ';dbname=' . $db_name . ';charset=' . $charset . ';';
37+
$options = [
38+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
39+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
40+
PDO::ATTR_EMULATE_PREPARES => false,
41+
];
42+
43+
try {
44+
self::$db = new PDO($dsn, DB_USERNAME, DB_PASSWORD, $options);
45+
}
46+
47+
catch(\PDOException $e) {
48+
throw new \PDOException($e->getMessage(), (int) $e->getCode());
49+
}
50+
51+
if(is_null(self::$instance)) {
52+
self::$instance = new self();
53+
}
54+
55+
return self::$instance;
56+
}
57+
58+
/**
59+
* Sets the value for the table
60+
*
61+
* @param string $name
62+
* @return void
63+
*/
64+
public static function table($name)
65+
{
66+
self::init();
67+
self::$table = $name;
68+
69+
if(is_null(self::$instance)) {
70+
self::$instance = new self();
71+
}
72+
73+
return self::$instance;
74+
}
75+
76+
/**
77+
* Limits the number of rows the query returns
78+
*
79+
* @param integer $columnNumber
80+
* @param integer $offset
81+
* @return void
82+
*/
83+
public function limit(int $columnNumber, int $offset)
84+
{
85+
$this->limitArgument = 'LIMIT ' . $coumnNumber . ( $offset ?? '');
86+
}
87+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Database;
4+
5+
interface DatabaseInterface
6+
{
7+
8+
/**
9+
* Initialize database connection
10+
*
11+
* @param string $host
12+
* @param string $db
13+
* @param string $charset
14+
* @return instance
15+
*/
16+
static function init($host, $db_name, $charset);
17+
18+
/**
19+
* Sets the value for the table
20+
*
21+
* @param string $name
22+
* @return void
23+
*/
24+
static function table($name);
25+
26+
/**
27+
* Select query
28+
*
29+
* @param array ...$args
30+
* @return instance
31+
*/
32+
function select(...$args);
33+
34+
/**
35+
* Returns the number of rows the query has
36+
*
37+
* @return instance
38+
*/
39+
public function selectCount();
40+
41+
/**
42+
* Returns all the columns
43+
*
44+
* @return instance
45+
*/
46+
public function selectAll();
47+
48+
/**
49+
* Where clause for sql query
50+
*
51+
* @return void
52+
*/
53+
function where($column, $operator, $value);
54+
55+
/**
56+
* order by clause for sql query
57+
*
58+
* @return void
59+
*/
60+
function orderBy($column, $type);
61+
62+
/**
63+
* Execute the statement
64+
*
65+
* @return instance
66+
*/
67+
function get();
68+
69+
/**
70+
* Limits the number of rows the query returns
71+
*
72+
* @param integer $columnNumber
73+
* @param integer $offset
74+
* @return void
75+
*/
76+
public function limit(int $columnNumber, int $offset);
77+
}

src/Database/Traits/Executable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Database\Traits;
4+
5+
trait Executable
6+
{
7+
8+
/**
9+
* Execute the statement
10+
*
11+
* @return instance
12+
*/
13+
public function get()
14+
{
15+
$query = $this->selectQuery . ' ' . implode(' ', $this->whereArguments) . ' '. implode(' ', $this->orderByArguments);
16+
return self::$db->query($query);
17+
}
18+
}

src/Database/Traits/Filterable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Database\Traits;
4+
5+
trait Filterable
6+
{
7+
8+
/**
9+
* Where clause for sql query
10+
*
11+
* @return void
12+
*/
13+
public function where($column, $operator, $value)
14+
{
15+
$this->whereArguments[] = 'where ' . $column . ' ' . $operator . ' ' . $value;
16+
return $this;
17+
}
18+
}

src/Database/Traits/Orderable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Database\Traits;
4+
5+
trait Orderable
6+
{
7+
8+
/**
9+
* order by clause for sql query
10+
*
11+
* @return void
12+
*/
13+
public function orderBy($column, $type)
14+
{
15+
$this->orderByArguments[] = 'order by ' . $column . ' ' . $type;
16+
return $this;
17+
}
18+
}

src/Database/Traits/Selectable.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Database\Traits;
4+
5+
trait Selectable
6+
{
7+
8+
/**
9+
* Select query
10+
*
11+
* @param array ...$args
12+
* @return instance
13+
*/
14+
public function select(...$args)
15+
{
16+
$this->selectQuery = 'SELECT ' . implode(',', $args) . ' FROM ' . self::$table;
17+
return $this;
18+
}
19+
20+
/**
21+
* Returns the number of rows the query has
22+
*
23+
* @return instance
24+
*/
25+
public function selectCount()
26+
{
27+
$this->selectQuery = 'SELECT COUNT(*)' . ' FROM ' . self::$table;
28+
return $this;
29+
}
30+
31+
/**
32+
* Returns all the columns
33+
*
34+
* @return instance
35+
*/
36+
public function selectAll()
37+
{
38+
$this->selectQuery = 'SELECT *' . ' FROM ' . self::$table;
39+
return $this;
40+
}
41+
}

src/tests/.gitkeep

Whitespace-only changes.

vendor/autoload.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer/autoload_real.php';
6+
7+
return ComposerAutoloaderInitd8f8425448e5be7bb5fde1b283a3b2f2::getLoader();

0 commit comments

Comments
 (0)