| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* No direct access. |
| 8 |
*/ |
| 9 |
defined('ABSPATH') or die("Access denied"); |
| 10 |
|
| 11 |
/** |
| 12 |
* Import helpers that may be used by models. |
| 13 |
*/ |
| 14 |
require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/single-table-simple-view.inc.php'; |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
*/ |
| 19 |
abstract class CJTTable { |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected $dbDriver = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
* @var mixed |
| 32 |
*/ |
| 33 |
protected $fields = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* put your comment there... |
| 37 |
* |
| 38 |
* @var mixed |
| 39 |
*/ |
| 40 |
protected $table = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @var CJTMYSQLQueueDriver |
| 46 |
*/ |
| 47 |
public function __construct(&$dbDriver, $table) { |
| 48 |
// Set table name. |
| 49 |
$this->dbDriver = $dbDriver; |
| 50 |
$this->table = $this->dbDriver->getTableName($table); |
| 51 |
// Read table fields. |
| 52 |
$this->fields = $this->dbDriver->getColumns($this->table); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* put your comment there... |
| 57 |
* |
| 58 |
*/ |
| 59 |
public function getDBDriver() { |
| 60 |
return $this->dbDriver; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* put your comment there... |
| 65 |
* |
| 66 |
*/ |
| 67 |
public function getFields() { |
| 68 |
return $this->fields; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* put your comment there... |
| 73 |
* |
| 74 |
*/ |
| 75 |
public function getName() { |
| 76 |
return $this->table; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* put your comment there... |
| 81 |
* |
| 82 |
*/ |
| 83 |
public function getNextId($offset = 0) { |
| 84 |
$query = "SHOW TABLE STATUS |
| 85 |
LIKE '{$this->table}';"; |
| 86 |
$newId = $this->dbDriver->row($query)->Auto_increment; |
| 87 |
// Add offset. |
| 88 |
$newId = $newId + $offset; |
| 89 |
return $newId; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* put your comment there... |
| 94 |
* |
| 95 |
* @todo Delete method and use CJTMYSQLQuery instead. |
| 96 |
* |
| 97 |
* @param mixed $parameters |
| 98 |
*/ |
| 99 |
protected function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) { |
| 100 |
$prepared = array(); |
| 101 |
// For every parameter esacape name value. |
| 102 |
foreach ($parameters as $name => $value) { |
| 103 |
if (!$excludeNulls || ($value !== null)) { |
| 104 |
if (array_key_exists($name, $this->fields) === FALSE) { |
| 105 |
throw new Exception("Field:{$name} is not found!"); |
| 106 |
} |
| 107 |
else { |
| 108 |
$field = $this->fields[$name]; |
| 109 |
// Escape field name and value. |
| 110 |
$value = $this->dbDriver->escapeValue($value, $field); |
| 111 |
// Get name-value operator. |
| 112 |
$operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator; |
| 113 |
$prepared[] = "`{$name}`{$operator}{$value}"; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
return $prepared; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
} // End class. |