Skip to content

Commit 68d7113

Browse files
added composer support
1 parent 50dea2b commit 68d7113

4 files changed

Lines changed: 107 additions & 74 deletions

File tree

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Daveismyname
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,36 @@ This PDO wrapper, is a collection of crud methods for working with a database th
44

55
## Install
66

7-
To install place this class into the project folder and include the class, then set the db credentials. Finally create an instance of the classes by calling it's get method.
7+
Using composer include the repository by typing the following into a terminal
88

9-
This wrapper makes use of a single database connection further connections attempts will reuse the already open connections, if not already connected.
10-
11-
````php
12-
include('database.php');
9+
```
10+
composer require daveismyname/pdo-wrapper
11+
```
1312

14-
//db properties
15-
define('DB_TYPE','mysql');
16-
define('DB_HOST','localhost');
17-
define('DB_USER','username');
18-
define('DB_PASS','password');
19-
define('DB_NAME','database name');
13+
Set the db credentials. Finally create an instance of the classes by calling it's get method.
2014

21-
// make a connection to mysql here
22-
$db = Database::get();
23-
````
15+
This wrapper makes use of a single database connection further connections attempts will reuse the already open connections, if not already connected.
2416

25-
To make a connection to another database pass an array containing the following:
17+
```php
18+
use Daveismyname\PdoWrapper\Database;
2619

27-
````
28-
$db = Database::get(array(
29-
'type' => 'mysql',
30-
'host' => 'localhost',
31-
'name' => 'dbname',
32-
'user' => 'dbusername'
33-
'pass' => 'password'
34-
));
35-
````
20+
// make a connection to mysql here
21+
$db = Database::get($username, $password, $database, $host = 'localhost', $type = 'mysql');
22+
```
3623

3724
# Usage examples
3825

3926
## Select:
4027

41-
````php
28+
```php
4229
$db->select("column FROM table");
43-
````
30+
```
4431

4532
To select data based on user data instead of passing the data to the query directly use a prepared statement, this is safer and stops any attempt at sql injections.
4633

47-
````php
34+
```php
4835
$db->select("username FROM members WHERE memberID = :id and email = :email", array(':id' => 1, ':email' => 'someone@domain.com'));
49-
````
36+
```
5037

5138
The above query will return the username from the members table where the memberID and email match. The memberID and email is passed seperartly in an array.
5239

@@ -56,67 +43,67 @@ Data returned from the query will be returns as an object this can be changed by
5643

5744
To use the object loop through it, a typical example:
5845

59-
````php
46+
```php
6047
$rows = $db->select("firstName, lastName FROM members ORDER BY firstName, lastName");
6148
foreach ($rows as $row) {
6249
echo "<p>$row->firstName $row->lastName</p>";
6350
}
64-
````
51+
```
6552

66-
## Select Signle Record:
53+
## Select Single Record:
6754

6855
Using find() will return only a single result. Like select it accepts params being passed in an array as a second argument.
6956

70-
````php
57+
```php
7158
$db->find("column FROM table where id=:id", [':id' => 23]);
72-
````
59+
```
7360

7461
# Raw
7562

7663
A raw query is a query that is not ran through a prepared statement and will execute the query passed directly. Useful when creating a table.
7764

78-
````php
65+
```php
7966
$db->raw("CREATE TABLE IF NOT EXISTS members (
8067
memberID INT(11) NOT NULL AUTO_INCREMENT,
8168
firstName VARCHAR(255) NOT NULL,
8269
lastnName VARCHAR(255) NOT NULL,
8370
email VARCHAR(255) NOT NULL,
8471
PRIMARY KEY (memberID))"
8572
);
86-
````
73+
```
8774

8875
## Insert
8976

9077
Data is inserted by calling the insert method it expects the table name followed by an array of key and values to insert in to the database.
9178

92-
````php
79+
```php
9380
$data = array(
9481
'firstName' => 'Joe',
9582
'lastnName' => 'Smith',
9683
'email' => 'someone@domain.com'
9784
);
9885
$db->insert('members', $data);
99-
````
86+
```
10087

10188
The insert automatically returns the last inserted id by returning 'lastInsertId' to collect the id:
10289

103-
````php
90+
```php
10491
$id = $db->insert('members', $data);
105-
````
92+
```
10693

10794
## Updating
10895

10996
To update an existing record the update method is called. This method expects the table, array of data to update and a second array containing the where condition.
11097

111-
````php
98+
```php
11299
$data = array(
113100
'firstName' => 'Joe',
114101
'lastnName' => 'Smith',
115102
'email' => 'someone@domain.com'
116103
);
117104
$where = array('memberID' => 2);
118105
$db->update('members', $data, $where);
119-
````
106+
```
120107
Or:
121108

122109
```php
@@ -137,37 +124,46 @@ $db->update('members', $update['data'], $update['where']);
137124

138125
To delete records call the delete method. This method expects the table name and an array of the where condition.
139126

140-
````php
127+
```php
141128
$where = array('memberID' => 2);
142129
$db->delete('members', $where);
143-
````
130+
```
144131

145132
This will delete a single record to set the limit pass a third parameters containing the number to limit to, or to remove the limit pass null as a third param.
146133

147-
````php
134+
```php
148135
$db->delete('members', $where, 10); //delete 10 records matcing the where
149136
$db->delete('members', $where, null); //delete all records matching the where
150137
```
151138

139+
## Delete multiple IN
140+
141+
To delete multiple records where ids are in a specific column, this uses WHERE id IN (4,5,6)
142+
143+
```php
144+
$db->deleteByIds('users', 'id', '4,5,6');
145+
```
146+
152147
## Truncate
153148

154149
To empty a table of all contents call the truncate method. Passing only the table name.
155150

156-
````php
151+
```php
157152
$db->truncate('members');
158-
````
153+
```
159154

160155

161156
## Count
162157

163158
To count records call the count method. This method expects the table name and column name (optional).
164159

165-
````php
160+
```php
166161
$db->count('members');
167-
````
162+
```
168163

169164
If table has no column `id`
170-
````
165+
166+
```php
171167
$db->count('members', 'member_id');
172-
````
168+
```
173169

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "daveismyname/pdo-wrapper",
3+
"description": "A crud wrapper for PDO",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "David Carr",
8+
"email": "dave@daveismyname.com",
9+
"homepage": "http://daveismyname.blog",
10+
"role": "Developer"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.2"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Daveismyname\\PdoWrapper\\": "/src"
19+
}
20+
},
21+
"minimum-stability": "dev"
22+
}

database.php renamed to src/Database.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
<?php
2+
namespace Daveismyname\PdoWrapper;
3+
4+
use PDO;
25

36
class Database extends PDO
47
{
58
/**
69
* @var array Array of saved databases for reusing
710
*/
8-
protected static $instances = array();
11+
protected static $instances = [];
912

1013
/**
1114
* Static method get
1215
*
1316
* @param array $group
14-
* @return \helpers\database
17+
* @return database
1518
*/
16-
public static function get($group = false)
19+
public static function get(string $username, string $password, string $database, string $host = 'localhost', string $type = 'mysql')
1720
{
18-
// Determining if exists or it's not empty, then use default group defined in config
19-
$group = !$group ? array (
20-
'type' => DB_TYPE,
21-
'host' => DB_HOST,
22-
'name' => DB_NAME,
23-
'user' => DB_USER,
24-
'pass' => DB_PASS
25-
) : $group;
26-
27-
// Group information
28-
$type = $group['type'];
29-
$host = $group['host'];
30-
$name = $group['name'];
31-
$user = $group['user'];
32-
$pass = $group['pass'];
33-
34-
// ID for database based on the group information
35-
$id = "$type.$host.$name.$user.$pass";
21+
// ID for database based on the credentials
22+
$id = "$type.$host.$database.$username.$password";
3623

3724
// Checking if the same
3825
if (isset(self::$instances[$id])) {
3926
return self::$instances[$id];
4027
}
4128

42-
$instance = new Database("$type:host=$host;dbname=$name;charset=utf8", $user, $pass);
29+
$instance = new Database("$type:host=$host;dbname=$database;charset=utf8", $username, $password);
4330
$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
4431

4532
// Setting Database into $instances to avoid duplication
@@ -69,7 +56,7 @@ public function raw($sql)
6956
* @param string $single when set will return only 1 record
7057
* @return array returns an array of records
7158
*/
72-
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '', $single = null)
59+
public function select($sql, $array = [], $fetchMode = PDO::FETCH_OBJ, $class = '', $single = null)
7360
{
7461
// Append select if it isn't appended.
7562
if (strtolower(substr($sql, 0, 7)) !== 'select ') {
@@ -102,7 +89,7 @@ public function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $cla
10289
* @param string $class class name
10390
* @return array returns a single record
10491
*/
105-
public function find($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
92+
public function find($sql, $array = [], $fetchMode = PDO::FETCH_OBJ, $class = '')
10693
{
10794
return $this->select($sql, $array, $fetchMode, $class, true);
10895
}
@@ -221,6 +208,13 @@ public function delete($table, $where, $limit = 1)
221208
return $stmt->rowCount();
222209
}
223210

211+
public function deleteByIds(string $table, string $column, string $ids)
212+
{
213+
$stmt = $this->prepare("DELETE FROM $table WHERE $column IN ($ids)");
214+
$stmt->execute();
215+
return $stmt->rowCount();
216+
}
217+
224218
/**
225219
* truncate table
226220
* @param string $table table name

0 commit comments

Comments
 (0)