-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBPDO.php
More file actions
139 lines (129 loc) · 3.47 KB
/
LudoDBPDO.php
File metadata and controls
139 lines (129 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* User: Alf Magne
* Date: 31.01.13
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* PDO Mysql Adapter. The default and preferred DB adapter to use.
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
class LudoDBPDO extends LudoDB implements LudoDBAdapter
{
/**
* Database connection resource reference
* @var PDO
*/
protected static $conn;
/**
*
* Connect to database
* @throws LudoDBConnectionException
*/
public function connect()
{
try{
$connectionString = "mysql:host=".self::getHost();
if(self::getDb())$connectionString.=";dbname=".self::getDb();
self::$conn = new PDO($connectionString, self::getUser(), self::getPassword());
}catch(PDOException $e){
throw new LudoDBConnectionException("Could not connect to database because ". $e->getMessage(), 400);
}
}
/**
* Escape string - nothing to do here since we're using prepared statements.
* @param $string
* @return mixed
*/
public function escapeString($string)
{
return $string;
}
/**
* Execute query and return resource.
* @param $sql
* @param array $params
* @return bool|mysqli_result|resource|PDOStatement
* @throws Exception
*/
public function query($sql, $params = array())
{
if (self::$logSQLs) $this->log($sql, $params);
if (self::$loggingEnabled) {
self::$queryCounter++;
}
if(!is_array($params))$params = array($params);
$stmt = self::$conn->prepare($sql);
if(!$stmt->execute($params)){
throw new Exception("Invalid PDO query ". $sql . " (". implode(",", $params).")");
}
return $stmt;
}
/**
* Get one row.
* @param $sql
* @param array $params
* @return array|null
*/
public function one($sql, $params = array())
{
$res = $this->query($sql . " limit 1", $params);
$row = $res->fetch(PDO::FETCH_ASSOC);
return $row ? $row : null;
}
/**
* Return number of rows.
* @param $sql
* @param array $params
* @return int
*/
public function countRows($sql, $params = array())
{
$res = $this->query($sql, $params);
return $res->rowCount();
}
/**
* Get last insert id
* @return int
*/
public function getInsertId()
{
return self::$conn->lastInsertId();
}
/**
* Go to next row
* @param mysqli_result|resource|PDOStatement $result
* @return array
*/
public function nextRow($result)
{
return $result->fetch(PDO::FETCH_ASSOC);
}
/**
* Return value of first column in a query
* @param $sql
* @param array $params
* @return null|array
*/
public function getValue($sql, $params = array())
{
$result = $this->query($sql . " limit 1", $params);
$row = $result->fetch(PDO::FETCH_NUM);
return (isset($row)) ? $row[0] : null;
}
/**
* Return table definition, column names and column types for a table.
* @param String $tableName
* @return array
*/
public function getTableDefinition($tableName){
$res = $this->query($this->getSqlForTableDef($tableName));
$ret = array();
while($row = $this->nextRow($res)){
$ret[$row['Field']] = $row['Type'];
}
return $ret;
}
}