1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opeyemiabiodun\PotatoORM\Connections; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
trait DatabaseTransactionsTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Creates a record in the database. |
11
|
|
|
* |
12
|
|
|
* @param string $table The table where the a new record is made. |
13
|
|
|
* @param array $record The record to be made in the database. |
14
|
|
|
* |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
|
|
public function createRecord($table, $record) |
18
|
|
|
{ |
19
|
|
|
if (gettype($table) !== 'string') { |
20
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (gettype($record) !== 'array') { |
24
|
|
|
throw new InvalidArgumentException("The parameter {$record} is not an array. An array is required instead."); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$count = count($record); |
28
|
|
|
|
29
|
|
|
$sql = "INSERT INTO {$table} ("; |
30
|
|
View Code Duplication |
foreach ($record as $key => $value) { |
|
|
|
|
31
|
|
|
|
32
|
|
|
$count--; |
33
|
|
|
|
34
|
|
|
if ($key === $this->getPrimaryKey($table)) { |
|
|
|
|
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($count > 0) { |
39
|
|
|
$sql = $sql."{$key}, "; |
40
|
|
|
} else { |
41
|
|
|
$sql = $sql."{$key}) "; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$count = count($record); |
46
|
|
|
|
47
|
|
|
$sql .= 'VALUES ('; |
48
|
|
|
foreach ($record as $key => $value) { |
49
|
|
|
|
50
|
|
|
$count--; |
51
|
|
|
|
52
|
|
|
if ($key === $this->getPrimaryKey($table)) { |
|
|
|
|
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($count > 0) { |
57
|
|
|
$sql = (empty($value)) ? $sql."NULL, " : $sql."'{$value}', "; |
58
|
|
|
} else { |
59
|
|
|
$sql = (empty($value)) ? $sql."NULL " : $sql."'{$value}') "; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->getPdo()->prepare($sql)->execute(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Remove a record in the database. |
68
|
|
|
* |
69
|
|
|
* @param string $table The table where the record is removed in the database. |
70
|
|
|
* @param string $pk The primary key value of the record. |
71
|
|
|
* |
72
|
|
|
* @return bool Returns boolean true if the record was successfully deleted or else it returns false. |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function deleteRecord($table, $pk) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (gettype($table) !== 'string') { |
77
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->getPdo()->prepare("DELETE FROM {$table} |
|
|
|
|
81
|
|
|
WHERE {$this->getPrimaryKey($table)}={$pk}")->execute(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a particular record in a table. |
86
|
|
|
* |
87
|
|
|
* @param string $table The table of the record. |
88
|
|
|
* @param string $pk The primary key value of the record. |
89
|
|
|
* |
90
|
|
|
* @return array An array containing the particular record. |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function findRecord($table, $pk) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if (gettype($table) !== 'string') { |
95
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->getPdo()->query("SELECT * FROM {$table} |
|
|
|
|
99
|
|
|
WHERE {$this->getPrimaryKey($table)}={$pk}")->fetchAll(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns all the records in a table. |
104
|
|
|
* |
105
|
|
|
* @param string $table The table inspected for all its records. |
106
|
|
|
* |
107
|
|
|
* @return array All the records in the table. |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function getAllRecords($table) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if (gettype($table) !== 'string') { |
112
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->getPdo()->query("SELECT * FROM {$table}")->fetchAll(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Update a record in the database. |
120
|
|
|
* |
121
|
|
|
* @param string $table The table where the record update is being made. |
122
|
|
|
* @param string $pk The primary key value of the record to be updated. |
123
|
|
|
* @param array $record The updates to be made to the record in the database. |
124
|
|
|
* |
125
|
|
|
* @return bool Returns boolean true if the record was successfully updated or else it returns false. |
126
|
|
|
*/ |
127
|
|
|
public function updateRecord($table, $pk, $record) |
128
|
|
|
{ |
129
|
|
|
if (gettype($table) !== 'string') { |
130
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (gettype($pk) !== 'string') { |
134
|
|
|
throw new InvalidArgumentException("The parameter {$pk} is not a string. A string is required instead."); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (gettype($record) !== 'array') { |
138
|
|
|
throw new InvalidArgumentException("The parameter {$record} is not an array. An array is required instead."); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$count = count($record); |
142
|
|
|
|
143
|
|
|
$sql = "UPDATE {$table} SET "; |
144
|
|
View Code Duplication |
foreach ($record as $key => $value) { |
|
|
|
|
145
|
|
|
if ($count > 1) { |
146
|
|
|
$sql = $sql."{$key}={$value}, "; |
147
|
|
|
} else { |
148
|
|
|
$sql = $sql."{$key}={$value} "; |
149
|
|
|
} |
150
|
|
|
$count--; |
151
|
|
|
} |
152
|
|
|
$sql .= "WHERE {$this->getPrimaryKey($table)}='{$pk}'"; |
|
|
|
|
153
|
|
|
|
154
|
|
|
return $this->getPdo()->prepare($sql)->execute(); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.