ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
ilSCORM13DB Class Reference
+ Collaboration diagram for ilSCORM13DB:

Public Member Functions

 __construct ($dsn, $login, $password, $type='mysql')
 
 init ($dsn, $login, $password, $type='mysql')
 
 addQueries ()
 
 getLastId ()
 
 getType ()
 
getRecord ($tableOrView, $idname, $idvalue)
 
 setRecord ($tableOrView, $row, $idname=null)
 
getRecords ($tableOrView, $idname=null, $idvalues=null, $order=null, $paging=null)
 
 setRecords ($tableOrView, $rows, $idname=null)
 
 removeRecord ($table, $idname, $idvalue)
 
 removeRecords ($tables, $idnames, $idvalues)
 
query ($query, $params=null, $order=null, $paging=null, $fetchType=PDO::FETCH_ASSOC)
 
 exec ($queries, $params=null, &$result=null)
 exec('delete...') exec('delete... More...
 
 begin ()
 
 commit ()
 
 rollback ()
 
 il_to_pdo_dsn ($il_dsn)
 

Static Public Attributes

static $DB
 

Private Member Functions

 getDSN ()
 
 getDB ()
 
 escapeName ($name)
 
 setLastId ($id)
 

Private Attributes

 $db
 
 $dsn
 
 $type
 
 $brackets
 
 $lastId
 

Static Private Attributes

static $DSN
 
static $TYPE
 
static $BRACKETS
 
static $LAST_ID
 
static $ERRORS
 
static $BRACKETS_LIST
 
static $errors
 
static $SQLCOMMAND = array()
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilSCORM13DB::__construct (   $dsn,
  $login,
  $password,
  $type = 'mysql' 
)

Definition at line 56 of file ilSCORM13DB.php.

References $dsn, and $type.

57  {
58  try {
59  self::$DB = new PDO($dsn, $login, $password);
60  } catch (PDOException $e) {
61  error_log("Error!: " . $e->getMessage());
62  }
63  $this->dsn = $dsn;
64  $this->brackets = self::$BRACKETS_LIST[$type];
65  $this->type = is_null($type) ? substr($dsn, 0, strpos($dsn, ':')) : $type;
66  $this->brackets = self::$BRACKETS_LIST[$this->type];
67 
68  }

Member Function Documentation

◆ addQueries()

ilSCORM13DB::addQueries ( )

Definition at line 82 of file ilSCORM13DB.php.

83  {
84  require_once("./Modules/Scorm2004/classes/ilSCORM13Player_mysql.php");
85  }

◆ begin()

ilSCORM13DB::begin ( )

Definition at line 358 of file ilSCORM13DB.php.

359  {
360  self::getDB()->beginTransaction();
361  self::$ERRORS = 0;
362  }

◆ commit()

ilSCORM13DB::commit ( )

Definition at line 364 of file ilSCORM13DB.php.

365  {
366  self::$ERRORS
367  ? self::getDB()->rollBack()
368  : self::getDB()->commit();
369  return self::$ERRORS;
370  }

◆ escapeName()

ilSCORM13DB::escapeName (   $name)
private

Definition at line 116 of file ilSCORM13DB.php.

117  {
118  $b = $this && $this instanceof ilSCORM13DB
119  ? $this->brackets
120  : self::$BRACKETS;
121  return $b[0] . preg_replace('/[^\w_.-]/', '_', $name) . $b[1];
122  }

◆ exec()

ilSCORM13DB::exec (   $queries,
  $params = null,
$result = null 
)

exec('delete...') exec('delete...

id=?', array(231)) exec('insert... id=?', array(array(231), array(130))) exec(array('select... id=?', 'select2... id=?'), array(array(231))) exec(array('select... id=?', 'select2... id=?'), array(array(231), array(130)))

erstelle gleich dimensionale arrays f�r $queries und $params

Definition at line 304 of file ilSCORM13DB.php.

References $d, $r, and $result.

305  {
306  if (!is_array($queries))
307  {
308  $r = self::exec(array($queries), $params);
309  return $r[0];
310  }
311  if (!is_array($params))
312  {
313  $params = array();
314  }
315  if (!is_array(current($params)))
316  {
317  $params = array($params);
318  }
319  //$d = new PDO(self::getDSN());
320  $d = self::getDB();
321  self::addQueries();
322  foreach ($queries as $i => &$q)
323  {
324  if ($s = $d->prepare($sql = (self::$SQLCOMMAND[$q] ? self::$SQLCOMMAND[$q] : $q)))
325  {
326  error_log("SQL-Command: ".self::$SQLCOMMAND[$q]);
327  $q = 0;
328  $r = array();
329  $ps = is_array($params) ? $params[$i % count($params)] : null;
330  if (!is_array(current($ps)))
331  {
332  $ps = array($ps);
333  }
334  foreach ($ps as $p)
335  {
336  $q+=$s->execute($p);
337  $arr = $s->errorInfo();
338  file_put_contents('/tmp/sql.log', implode("\n", array('', date('c'), $sql, var_export($p, true),var_export($arr,true))), FILE_APPEND);
339 
340  if (is_array($result))
341  {
342  count($queries)<2
343  ? $result = $s->fetchAll(PDO::FETCH_ASSOC)
344  : $result[] = $s->fetchAll(PDO::FETCH_ASSOC);
345  }
346  }
347  }
348  else
349  {
350  // prepare failed
351  }
352  }
353  unset($d);
354  return $queries;
355  }
$result
$r

◆ getDB()

ilSCORM13DB::getDB ( )
private

Definition at line 109 of file ilSCORM13DB.php.

110  {
111  return $this && $this instanceof ilSCORM13DB
112  ? $this->db
113  : self::$DB;
114  }

◆ getDSN()

ilSCORM13DB::getDSN ( )
private

Definition at line 102 of file ilSCORM13DB.php.

103  {
104  return $this && $this instanceof ilSCORM13DB
105  ? $this->dsn
106  : self::$DSN;
107  }

◆ getLastId()

ilSCORM13DB::getLastId ( )

Definition at line 87 of file ilSCORM13DB.php.

88  {
89  return self::getDB()->lastInsertId();
90  return $this && $this instanceof ilSCORM13DB
91  ? $this->lastId
92  : self::$LAST_ID;
93  }

◆ getRecord()

& ilSCORM13DB::getRecord (   $tableOrView,
  $idname,
  $idvalue 
)

Definition at line 131 of file ilSCORM13DB.php.

References $r.

132  {
133  if (!is_string($idname) || !is_numeric($idvalue))
134  {
135  return false;
136  }
137  $q = 'SELECT * FROM ' . self::escapeName($tableOrView) . ' WHERE ' . self::escapeName($idname) . '=' . $idvalue;
138  $r = self::query($q);
139  return $r[0];
140  }
$r

◆ getRecords()

& ilSCORM13DB::getRecords (   $tableOrView,
  $idname = null,
  $idvalues = null,
  $order = null,
  $paging = null 
)

Definition at line 148 of file ilSCORM13DB.php.

149  {
150  $tableOrView = self::escapeName($tableOrView);
151  $q = "SELECT * FROM $tableOrView";
152  if (is_string($idname) && is_array($idvalues))
153  {
154  $idname = self::escapeName($idname);
155  foreach ($idvalues as &$idvalue)
156  {
157  if (!is_numeric($idvalue)) return false;
158  $idvalue = "$idname=$idvalue";
159  }
160  $q .= ' ' . implode(' OR ', $idvalues);
161  }
162  return self::query($q, null, $order, $paging);
163  }

◆ getType()

ilSCORM13DB::getType ( )

Definition at line 95 of file ilSCORM13DB.php.

96  {
97  return $this && $this instanceof ilSCORM13DB
98  ? $this->type
99  : self::$TYPE;
100  }

◆ il_to_pdo_dsn()

ilSCORM13DB::il_to_pdo_dsn (   $il_dsn)

Definition at line 378 of file ilSCORM13DB.php.

379  {
380  $pattern = '/([a-z]+)(:\/\/)([^:]*)(:)([^@]*)(@)([^\/]+)(\/)(.*)/i';
381  preg_match($pattern, $il_dsn, $matches);
382  $pdo_dsn[0]=$matches[1].":dbname=".$matches[9].";host=".$matches[7];
383  $pdo_dsn[1]=$matches[3];
384  $pdo_dsn[2]=$matches[5];
385  return $pdo_dsn;
386  }

◆ init()

ilSCORM13DB::init (   $dsn,
  $login,
  $password,
  $type = 'mysql' 
)

Definition at line 70 of file ilSCORM13DB.php.

References $dsn, and $type.

71  {
72  try {
73  self::$DB = new PDO($dsn, $login, $password);
74  } catch (PDOException $e) {
75  error_log("Error!: " . $e->getMessage());
76  }
77  self::$DSN = $dsn;
78  self::$TYPE = is_null($type) ? substr($dsn, 0, strpos($dsn, ':')) : $type;
79  self::$BRACKETS = self::$BRACKETS_LIST[self::$TYPE];
80  }

◆ query()

& ilSCORM13DB::query (   $query,
  $params = null,
  $order = null,
  $paging = null,
  $fetchType = PDO::FETCH_ASSOC 
)

Definition at line 258 of file ilSCORM13DB.php.

References $d, $query, and $r.

259  {
260  $r = array();
261  $d = self::getDB();
262  self::addQueries();
263  $q = array(self::$SQLCOMMAND[$query] ? self::$SQLCOMMAND[$query] : $query);
264  if (is_array($order))
265  {
266  $o = array();
267  foreach ($order as $k => $v)
268  {
269  $o[] = self::escapeName($k) . ' ' . ($v ? 'ASC' : 'DESC');
270  }
271  $q[] = 'ORDER BY ' . implode(', ', $o);
272  }
273  if (is_array($paging))
274  {
275  if (is_int($o = $paging['count']))
276  {
277  // MySQL Style
278  $q[] = "LIMIT $o";
279  if (is_int($o = $paging['offset']))
280  {
281  $q[] = "OFFSET $o";
282  }
283  }
284  }
285  $q = implode(' ', $q);
286  $s = $d->prepare($q);
287  $s->execute($params);
288  $arr = $s->errorInfo();
289  file_put_contents('/tmp/sql.log', implode("\n", array('', date('c'), $q, var_export($params, true),var_export($arr,true))), FILE_APPEND);
290  $r = $s->fetchAll($fetchType);
291  unset($d);
292  return $r;
293  }
$r

◆ removeRecord()

ilSCORM13DB::removeRecord (   $table,
  $idname,
  $idvalue 
)

Definition at line 220 of file ilSCORM13DB.php.

221  {
222  self::removeRecords($table, $idname, array($idvalue));
223  }

◆ removeRecords()

ilSCORM13DB::removeRecords (   $tables,
  $idnames,
  $idvalues 
)

Definition at line 225 of file ilSCORM13DB.php.

References $d.

226  {
227  if (!is_array($idvalues))
228  {
229  return false;
230  }
231  //$d = new PDO(self::getDSN());
232  $d = self::getDB();
233  if (!is_array($tables))
234  {
235  $tables = array($tables);
236  }
237  if (!is_array($idnames))
238  {
239  $idnames = array($idnames);
240  }
241  $tables = array_reverse($tables);
242  foreach ($tables as $i => &$table)
243  {
244  $table = self::escapeName($table);
245  $idname = $idnames[$i % count($idnames)];
246  if (!is_string($idname)) return false;
247  $idname = self::escapeName($idname);
248  $q = "DELETE FROM $table WHERE $idname=?";
249  foreach ($idvalues as $idvalue)
250  {
251  $table = self::exec($q, $idvalue);
252  }
253  }
254  unset($d);
255  return array_reverse($tables);
256  }

◆ rollback()

ilSCORM13DB::rollback ( )

Definition at line 372 of file ilSCORM13DB.php.

373  {
374  self::getDB()->rollBack();
375  }

◆ setLastId()

ilSCORM13DB::setLastId (   $id)
private

Definition at line 124 of file ilSCORM13DB.php.

125  {
126  $this && $this instanceof ilSCORM13DB
127  ? $this->lastId = $id
128  : self::$LAST_ID = $id;
129  }

◆ setRecord()

ilSCORM13DB::setRecord (   $tableOrView,
  $row,
  $idname = null 
)

Definition at line 142 of file ilSCORM13DB.php.

References $r, and $row.

143  {
144  $r = self::setRecords($tableOrView, array($row), $idname);
145  return $r[0];
146  }
$r

◆ setRecords()

ilSCORM13DB::setRecords (   $tableOrView,
  $rows,
  $idname = null 
)

Definition at line 165 of file ilSCORM13DB.php.

References $d, $r, $row, and $type.

166  {
167  //$d = new PDO(self::getDSN());
168  $d = self::getDB();
169  $r = 0;
170  if (!is_array($row = $rows[0]))
171  {
172  return false;
173  }
174  $tableOrView = self::escapeName($tableOrView);
175  $q = array();
176  if (is_string($idname)) {
177  $idvalue = $row[$idname];
178  $idname = self::escapeName($idname);
179  }
180  $u = is_numeric($idvalue);
181  if ($u)
182  {
183  foreach (array_keys($row) as $k)
184  {
185  $q[] = self::escapeName($k) . '=?';
186  }
187  $q = implode(', ', $q);
188  $q = "UPDATE $tableOrView SET $q WHERE $idname=$idvalue";
189  }
190  else
191  {
192  foreach (array_keys($row) as $k)
193  {
194  $q[] = self::escapeName($k);
195  }
196  $q = implode(', ', $q);
197  $q = "INSERT INTO $tableOrView ($q) VALUES (" . str_pad('', count($row)*2-1, '?,') . ')';
198  }
199  //echo "<br>$q";
200  if ($s = $d->prepare($q))
201  {
202  $type = self::getType();
203  foreach ($rows as &$row)
204  {
205  $row = $s->execute(array_values($row));
206  $arr = $s->errorInfo();
207  file_put_contents('/tmp/sql.log', implode("\n", array('', date('c'), $sql, var_export($q, true),var_export($arr,true))), FILE_APPEND);
208 
209  if (!$u && is_string($idname) && $row)
210  {
211  $row = $d->lastInsertId();
212  self::setLastId($row);
213  }
214  }
215  }
216  unset($d);
217  return $rows;
218  }
$r

Field Documentation

◆ $BRACKETS

ilSCORM13DB::$BRACKETS
staticprivate

Definition at line 37 of file ilSCORM13DB.php.

◆ $brackets

ilSCORM13DB::$brackets
private

Definition at line 49 of file ilSCORM13DB.php.

◆ $BRACKETS_LIST

ilSCORM13DB::$BRACKETS_LIST
staticprivate
Initial value:
= array(
'mysql' => '``',
'sqlite' => '""',
)

Definition at line 40 of file ilSCORM13DB.php.

◆ $DB

ilSCORM13DB::$DB
static

Definition at line 34 of file ilSCORM13DB.php.

◆ $db

ilSCORM13DB::$db
private

Definition at line 46 of file ilSCORM13DB.php.

◆ $DSN

ilSCORM13DB::$DSN
staticprivate

Definition at line 35 of file ilSCORM13DB.php.

◆ $dsn

ilSCORM13DB::$dsn
private

Definition at line 47 of file ilSCORM13DB.php.

Referenced by __construct(), and init().

◆ $ERRORS

ilSCORM13DB::$ERRORS
staticprivate

Definition at line 39 of file ilSCORM13DB.php.

◆ $errors

ilSCORM13DB::$errors
staticprivate

Definition at line 51 of file ilSCORM13DB.php.

◆ $LAST_ID

ilSCORM13DB::$LAST_ID
staticprivate

Definition at line 38 of file ilSCORM13DB.php.

◆ $lastId

ilSCORM13DB::$lastId
private

Definition at line 50 of file ilSCORM13DB.php.

◆ $SQLCOMMAND

ilSCORM13DB::$SQLCOMMAND = array()
staticprivate

Definition at line 53 of file ilSCORM13DB.php.

◆ $TYPE

ilSCORM13DB::$TYPE
staticprivate

Definition at line 36 of file ilSCORM13DB.php.

◆ $type

ilSCORM13DB::$type
private

Definition at line 48 of file ilSCORM13DB.php.

Referenced by __construct(), init(), and setRecords().


The documentation for this class was generated from the following file: