ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Db.php
Go to the documentation of this file.
1<?php
2
30define('CAS_PGT_STORAGE_DB_DEFAULT_TABLE', 'cas_pgts');
31
47{
56 private $_pdo;
57
63 private function _getPdo()
64 {
65 return $this->_pdo;
66 }
67
71 private $_dsn;
72 private $_username;
73 private $_password;
75
79 private $_table;
80
86 private function _getTable()
87 {
88 return $this->_table;
89 }
90
91 // ########################################################################
92 // DEBUGGING
93 // ########################################################################
94
101 public function getStorageType()
102 {
103 return "db";
104 }
105
113 public function getStorageInfo()
114 {
115 return 'table=`' . $this->_getTable() . '\'';
116 }
117
118 // ########################################################################
119 // CONSTRUCTOR
120 // ########################################################################
121
138 public function __construct(
139 $cas_parent,
140 $dsn_or_pdo,
141 $username = '',
142 $password = '',
143 $table = '',
144 $driver_options = null
145 ) {
147 // call the ancestor's constructor
148 parent::__construct($cas_parent);
149
150 // set default values
151 if (empty($table)) {
153 }
154 if (!is_array($driver_options)) {
155 $driver_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
156 }
157
158 // store the specified parameters
159 if ($dsn_or_pdo instanceof PDO) {
160 $this->_pdo = $dsn_or_pdo;
161 } else {
162 $this->_dsn = $dsn_or_pdo;
163 $this->_username = $username;
164 $this->_password = $password;
165 $this->_driver_options = $driver_options;
166 }
167
168 // store the table name
169 $this->_table = $table;
170
172 }
173
174 // ########################################################################
175 // INITIALIZATION
176 // ########################################################################
177
183 public function init()
184 {
186 // if the storage has already been initialized, return immediatly
187 if ($this->isInitialized()) {
188 return;
189 }
190
191 // initialize the base object
192 parent::init();
193
194 // create the PDO object if it doesn't exist already
195 if (!($this->_pdo instanceof PDO)) {
196 try {
197 $this->_pdo = new PDO(
198 $this->_dsn,
199 $this->_username,
200 $this->_password,
201 $this->_driver_options
202 );
203 } catch (PDOException $e) {
204 phpCAS::error('Database connection error: ' . $e->getMessage());
205 }
206 }
207
209 }
210
211 // ########################################################################
212 // PDO database interaction
213 // ########################################################################
214
219 private $_errMode;
220
226 private function _setErrorMode()
227 {
228 // get PDO object and enable exception error mode
229 $pdo = $this->_getPdo();
230 $this->_errMode = $pdo->getAttribute(PDO::ATTR_ERRMODE);
231 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
232 }
233
239 private function _resetErrorMode()
240 {
241 // get PDO object and reset the error mode to what it was originally
242 $pdo = $this->_getPdo();
243 $pdo->setAttribute(PDO::ATTR_ERRMODE, $this->_errMode);
244 }
245
246 // ########################################################################
247 // database queries
248 // ########################################################################
249 // these queries are potentially unsafe because the person using this library
250 // can set the table to use, but there is no reliable way to escape SQL
251 // fieldnames in PDO yet
252
258 protected function createTableSql()
259 {
260 return 'CREATE TABLE ' . $this->_getTable()
261 . ' (pgt_iou VARCHAR(255) NOT NULL PRIMARY KEY, pgt VARCHAR(255) NOT NULL)';
262 }
263
270 protected function storePgtSql()
271 {
272 return 'INSERT INTO ' . $this->_getTable()
273 . ' (pgt_iou, pgt) VALUES (:pgt_iou, :pgt)';
274 }
275
283 protected function retrievePgtSql()
284 {
285 return 'SELECT pgt FROM ' . $this->_getTable() . ' WHERE pgt_iou = :pgt_iou';
286 }
287
294 protected function deletePgtSql()
295 {
296 return 'DELETE FROM ' . $this->_getTable() . ' WHERE pgt_iou = :pgt_iou';
297 }
298
299 // ########################################################################
300 // PGT I/O
301 // ########################################################################
302
308 public function createTable()
309 {
311
312 // initialize this PGTStorage object if it hasn't been initialized yet
313 if (!$this->isInitialized()) {
314 $this->init();
315 }
316
317 // initialize the PDO object for this method
318 $pdo = $this->_getPdo();
319 $this->_setErrorMode();
320
321 try {
322 $pdo->beginTransaction();
323
324 $query = $pdo->query($this->createTableSQL());
325 $query->closeCursor();
326
327 $pdo->commit();
328 } catch (PDOException $e) {
329 // attempt rolling back the transaction before throwing a phpCAS error
330 try {
331 $pdo->rollBack();
332 } catch (PDOException $e) {
333 }
334 phpCAS::error('error creating PGT storage table: ' . $e->getMessage());
335 }
336
337 // reset the PDO object
338 $this->_resetErrorMode();
339
341 }
342
352 public function write($pgt, $pgt_iou)
353 {
355
356 // initialize the PDO object for this method
357 $pdo = $this->_getPdo();
358 $this->_setErrorMode();
359
360 try {
361 $pdo->beginTransaction();
362
363 $query = $pdo->prepare($this->storePgtSql());
364 $query->bindValue(':pgt', $pgt, PDO::PARAM_STR);
365 $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
366 $query->execute();
367 $query->closeCursor();
368
369 $pdo->commit();
370 } catch (PDOException $e) {
371 // attempt rolling back the transaction before throwing a phpCAS error
372 try {
373 $pdo->rollBack();
374 } catch (PDOException $e) {
375 }
376 phpCAS::error('error writing PGT to database: ' . $e->getMessage());
377 }
378
379 // reset the PDO object
380 $this->_resetErrorMode();
381
383 }
384
393 public function read($pgt_iou)
394 {
396 $pgt = false;
397
398 // initialize the PDO object for this method
399 $pdo = $this->_getPdo();
400 $this->_setErrorMode();
401
402 try {
403 $pdo->beginTransaction();
404
405 // fetch the pgt for the specified pgt_iou
406 $query = $pdo->prepare($this->retrievePgtSql());
407 $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
408 $query->execute();
409 $pgt = $query->fetchColumn(0);
410 $query->closeCursor();
411
412 // delete the specified pgt_iou from the database
413 $query = $pdo->prepare($this->deletePgtSql());
414 $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
415 $query->execute();
416 $query->closeCursor();
417
418 $pdo->commit();
419 } catch (PDOException $e) {
420 // attempt rolling back the transaction before throwing a phpCAS error
421 try {
422 $pdo->rollBack();
423 } catch (PDOException $e) {
424 }
425 phpCAS::trace('error reading PGT from database: ' . $e->getMessage());
426 }
427
428 // reset the PDO object
429 $this->_resetErrorMode();
430
432 return $pgt;
433 }
434
436}
const CAS_PGT_STORAGE_DB_DEFAULT_TABLE
Definition: Db.php:30
while(count($oldTaskList) > 0) foreach(array_keys( $newTaskList) as $task) init()
Definition: build.php:77
An exception for terminatinating execution or to throw for unit testing.
Basic class for PGT storage The CAS_PGTStorage_AbstractStorage class is a generic class for PGT stora...
Basic class for PGT database storage The CAS_PGTStorage_Db class is a class for PGT database storage.
Definition: Db.php:47
$password
Definition: cron.php:14
$_table
the table to use for storing/retrieving pgt's
Definition: Db.php:79
read($pgt_iou)
This method reads a PGT corresponding to a PGT Iou and deletes the corresponding db entry.
Definition: Db.php:393
_resetErrorMode()
this method will reset the error mode on the PDO object
Definition: Db.php:239
$_errMode
attribute that stores the previous error mode for the PDO handle while processing a transaction
Definition: Db.php:219
_getPdo()
This method returns the PDO object to use for database interactions.
Definition: Db.php:63
getStorageType()
This method returns an informational string giving the type of storage used by the object (used for d...
Definition: Db.php:101
write($pgt, $pgt_iou)
This method stores a PGT and its corresponding PGT Iou in the database.
Definition: Db.php:352
createTableSql()
This method returns the query used to create a pgt storage table.
Definition: Db.php:258
_getTable()
This method returns the table to use when storing/retrieving PGT's.
Definition: Db.php:86
init()
This method is used to initialize the storage.
Definition: Db.php:183
getStorageInfo()
This method returns an informational string giving informations on the parameters of the storage.
Definition: Db.php:113
storePgtSql()
This method returns the query used to store a pgt.
Definition: Db.php:270
$_pdo
the PDO object to use for database interactions
Definition: Db.php:56
createTable()
This method creates the database table used to store pgt's and pgtiou's.
Definition: Db.php:308
_setErrorMode()
This method will enable the Exception error mode on the PDO object.
Definition: Db.php:226
retrievePgtSql()
This method returns the query used to retrieve a pgt.
Definition: Db.php:283
__construct( $cas_parent, $dsn_or_pdo, $username='', $password='', $table='', $driver_options=null)
The class constructor.
Definition: Db.php:138
$_dsn
database connection options to use when creating a new PDO object
Definition: Db.php:71
deletePgtSql()
This method returns the query used to delete a pgt.
Definition: Db.php:294
isInitialized()
This method tells if the storage has already been intialized.
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:599
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:658
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode.
Definition: CAS.php:611
static error($msg)
This method is used by interface methods to print an error and where the function was originally call...
Definition: CAS.php:563
$pdo
Definition: migrateto20.php:62
$query
if(empty($password)) $table
Definition: pwgen.php:24