|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ganga\Potato; |
|
4
|
|
|
|
|
5
|
|
|
use SQLite3; |
|
6
|
|
|
use mysqli; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class to define connection to sqlite database; |
|
10
|
|
|
*/ |
|
11
|
|
|
class Connection |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $conn; |
|
14
|
|
|
public static $dbType; |
|
15
|
|
|
public static $dbName; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
* Declare an instance of SQLite class |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($config) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!is_array($config)) { |
|
24
|
|
|
throw new PotatoException("Database Config must be an array"); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (!isset($config['type']) || empty($config['type'])) { |
|
28
|
|
|
throw new PotatoException("You must specify a database type in the config"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!isset($config['database']) || empty($config['database'])) { |
|
32
|
|
|
throw new PotatoException("You must provide a database in the config"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
self::$dbName = $config['database']; |
|
36
|
|
|
self::$dbType = strtolower($config['type']); |
|
37
|
|
|
|
|
38
|
|
|
switch (self::$dbType) { |
|
39
|
|
|
case 'sqlite': |
|
40
|
|
|
self::$conn = $this->connectSQLite($config); |
|
41
|
|
|
break; |
|
42
|
|
|
case 'mysql': |
|
43
|
|
|
self::$conn = $this->connectMySQL($config); |
|
44
|
|
|
break; |
|
45
|
|
|
case 'postgres': |
|
46
|
|
|
case 'pgssql': |
|
47
|
|
|
|
|
48
|
|
|
break; |
|
49
|
|
|
case 'mongo': |
|
50
|
|
|
|
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get an instance of the db connection |
|
57
|
|
|
* @return SQLite3 connection |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function db() |
|
60
|
|
|
{ |
|
61
|
|
|
return self::$conn; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function connectMySQL($config) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!isset($config['host']) || empty($config['host'])) { |
|
67
|
|
|
throw new PotatoException("You must provide a host to connect to MySQL database"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!isset($config['user']) || empty($config['user'])) { |
|
71
|
|
|
throw new PotatoException("You must provide a user for MySQL database"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!isset($config['password']) || empty($config['password'])) { |
|
75
|
|
|
throw new PotatoException("You must provide a password for MySQL database"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$db = new mysqli($config['host'], $config['user'], $config['password'], $config['database']); |
|
79
|
|
|
|
|
80
|
|
|
if($db->connect_errno > 0){ |
|
81
|
|
|
die('Unable to connect to database [' . $db->connect_error . ']'); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $db; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function connectSQLite($config) |
|
88
|
|
|
{ |
|
89
|
|
|
$conn = new SQLite3($config['database']); |
|
90
|
|
|
if (!$conn) { |
|
91
|
|
|
return $conn->lastErrorMsg(); |
|
92
|
|
|
} else { |
|
93
|
|
|
return $conn; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function connectPostrges() |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.