-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBPDOOracle.php
More file actions
197 lines (180 loc) · 4.6 KB
/
LudoDBPDOOracle.php
File metadata and controls
197 lines (180 loc) · 4.6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* User: Alf Magne Kalleland
* Date: 24.04.13
* Time: 00:01
* @package LudoDB
*/
/**
* LudoDB PDO adapter for Oracle database connections.
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
class LudoDBPDOOracle extends LudoDB implements LudoDBAdapter
{
/**
* Database connection resource reference
* @var PDO
*/
protected static $conn;
/**
*
* Connect to database
* @throws LudoDBConnectionException
*/
public function connect()
{
try {
$connectionString = "dbname=//" . self::getHost() . ":" . self::getPort() . "/" . self::getSID() . "/" . self::getInstanceName();
self::$conn = new PDO($connectionString, self::getUser(), self::getPassword());
} catch (PDOException $e) {
throw new LudoDBConnectionException("Could not connect to database because " . $e->getMessage(), 400);
}
}
/**
* Return port of Oracle conection.
* @return String
*/
public function getPort()
{
return LudoDBRegistry::get('DB_PORT');
}
/**
* Set port for Oracle connection.
* @param int $port
*/
public function setPort($port)
{
LudoDBRegistry::set('DB_PORT', $port);
}
/**
* Return SID for Oracle connection
* @return String
*/
public function getSID()
{
return LudoDBRegistry::get('DB_SID');
}
/**
* Set SID for oracle connection
* @param String
*/
public function setSID($sid)
{
LudoDBRegistry::set('DB_SID', $sid);
}
/**
* Return instance name for Oracle connectoin
* @return String
*/
public function getInstanceName()
{
return LudoDBRegistry::get('DB_INSTANCE');
}
/**
* Set instance name for Oracle connection
* @param $instance
*/
public function setInstanceName($instance)
{
LudoDBRegistry::set('DB_INSTANCE', $instance);
}
/**
* 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, $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, $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;
}
protected function getSqlForTableDef()
{
return "SELECT column_name FROM user_tab_cols WHERE table_name = ?";
}
}