1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opeyemiabiodun\PotatoORM\Connections; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOException; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\Connection; |
9
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\LoadEnvVariablesTrait; |
10
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\DatabaseTransactionsTrait; |
11
|
|
|
|
12
|
|
|
final class MySqlConnection implements Connection |
13
|
|
|
{ |
14
|
|
|
use LoadEnvVariablesTrait, DatabaseTransactionsTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The method called in the constructor. |
18
|
|
|
* |
19
|
|
|
* @return void |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
private function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->useDbEnv(); |
24
|
|
|
|
25
|
|
|
$dsn = 'mysql:host='.$this->_host |
26
|
|
|
.';port='.$this->_port |
27
|
|
|
.';dbname='.$this->_database; |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$this->_pdo = new PDO($dsn, $this->_username, $this->_password); |
|
|
|
|
31
|
|
|
} catch (PDOException $e) { |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the columns of a table. |
37
|
|
|
* |
38
|
|
|
* @param string $table The table inspected for its columns. |
39
|
|
|
* |
40
|
|
|
* @return array The columns of the table. |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function getColumns($table) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (gettype($table) !== 'string') { |
45
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead."); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->getPdo()->query("SELECT COLUMN_NAME |
49
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS |
50
|
|
|
WHERE TABLE_NAME = N'{$table}' |
51
|
|
|
AND TABLE_SCHEMA = N'{$this->_database}'")->fetchAll(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the Connection's PDO. |
56
|
|
|
* |
57
|
|
|
* @return PDO PHP Data Objects |
58
|
|
|
*/ |
59
|
|
|
public function getPdo() |
60
|
|
|
{ |
61
|
|
|
return $this->_pdo; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the primary key of a table. |
66
|
|
|
* |
67
|
|
|
* @param string $table The table inspected for its primary key. |
68
|
|
|
* |
69
|
|
|
* @return string The primary key of the table. |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function getPrimaryKey($table) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
if (gettype($table) !== 'string') { |
74
|
|
|
throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead."); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->getPdo()->query("SHOW KEYS FROM {$table} WHERE Key_name = 'PRIMARY'")->fetchAll()[0]['Column_name']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function load() |
81
|
|
|
{ |
82
|
|
|
return new MySqlConnection(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.