| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | include_once('DB.php'); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class Ethna_DB_PEAR extends Ethna_DB |
|---|
| 22 | { |
|---|
| 23 | Â Â |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | var $db; |
|---|
| 29 | |
|---|
| 30 | Â Â |
|---|
| 31 | var  $transaction = array(); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | Â Â |
|---|
| 35 | var $logger; |
|---|
| 36 | |
|---|
| 37 | Â Â |
|---|
| 38 | var $sql; |
|---|
| 39 | |
|---|
| 40 | Â Â |
|---|
| 41 | var $type; |
|---|
| 42 | |
|---|
| 43 | Â Â |
|---|
| 44 | var $dsn; |
|---|
| 45 | |
|---|
| 46 | Â Â |
|---|
| 47 | var $persistent; |
|---|
| 48 | |
|---|
| 49 | Â Â |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | *  Ethna_DB_PEAR���饹�Υ��󥹥ȥ饯�� |
|---|
| 54 | Â Â Â * |
|---|
| 55 |    *  @access  public |
|---|
| 56 |    *  @param  object  Ethna_Controller  &$controllerÂ Â ï¿½ï¿½ï¿½ï¿½í¡¼ï¿½é¥ªï¿½Ö¥ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Â Â Â *  @param  string  $dsn                DSN |
|---|
| 57 |    *  @param  bool  $persistent              ����� |
|---|
| 58 | Â Â Â */ |
|---|
| 59 | Â Â function Ethna_DB_PEAR(&$controller, $dsn, $persistent) |
|---|
| 60 | Â Â { |
|---|
| 61 | Â Â Â Â parent::Ethna_DB($controller, $dsn, $persistent); |
|---|
| 62 | |
|---|
| 63 | Â Â Â Â $this->db = null; |
|---|
| 64 | Â Â Â Â $this->logger =& $controller->getLogger(); |
|---|
| 65 | Â Â Â Â $this->sql =& $controller->getSQL(); |
|---|
| 66 | |
|---|
| 67 | Â Â Â Â $dsninfo = DB::parseDSN($dsn); |
|---|
| 68 | Â Â Â Â $this->type = $dsninfo['phptype']; |
|---|
| 69 | Â Â } |
|---|
| 70 | |
|---|
| 71 | Â Â |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | function connect() |
|---|
| 77 | Â Â { |
|---|
| 78 | Â Â Â Â $this->db =& DB::connect($this->dsn, $this->persistent); |
|---|
| 79 | Â Â Â Â if (DB::isError($this->db)) { |
|---|
| 80 |       $error = Ethna::raiseError('DB�³���顼: %s', E_DB_CONNECT, $this->db->getUserInfo()); |
|---|
| 81 | Â Â Â Â Â Â $error->addUserInfo($this->db); |
|---|
| 82 | Â Â Â Â Â Â $this->db = null; |
|---|
| 83 | Â Â Â Â Â Â return $error; |
|---|
| 84 | Â Â Â Â } |
|---|
| 85 | |
|---|
| 86 | Â Â Â Â return 0; |
|---|
| 87 | Â Â } |
|---|
| 88 | |
|---|
| 89 | Â Â |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | function disconnect() |
|---|
| 94 | Â Â { |
|---|
| 95 | Â Â Â Â if (is_null($this->db)) { |
|---|
| 96 | Â Â Â Â Â Â return; |
|---|
| 97 | Â Â Â Â } |
|---|
| 98 | Â Â Â Â $this->db->disconnect(); |
|---|
| 99 | Â Â } |
|---|
| 100 | |
|---|
| 101 | Â Â |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | function isValid() |
|---|
| 108 | Â Â { |
|---|
| 109 | Â Â Â Â if (is_null($this->db)) { |
|---|
| 110 | Â Â Â Â Â Â return false; |
|---|
| 111 | Â Â Â Â } else { |
|---|
| 112 | Â Â Â Â Â Â return true; |
|---|
| 113 | Â Â Â Â } |
|---|
| 114 | Â Â } |
|---|
| 115 | |
|---|
| 116 | Â Â |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | function begin() |
|---|
| 122 | Â Â { |
|---|
| 123 | Â Â Â Â if (count($this->transaction) > 0) { |
|---|
| 124 | Â Â Â Â Â Â $this->transaction[] = true; |
|---|
| 125 | Â Â Â Â Â Â return 0; |
|---|
| 126 | Â Â Â Â } |
|---|
| 127 | |
|---|
| 128 | Â Â Â Â $r = $this->query('BEGIN;'); |
|---|
| 129 | Â Â Â Â if (Ethna::isError($r)) { |
|---|
| 130 | Â Â Â Â Â Â return $r; |
|---|
| 131 | Â Â Â Â } |
|---|
| 132 | Â Â Â Â $this->transaction[] = true; |
|---|
| 133 | |
|---|
| 134 | Â Â Â Â return 0; |
|---|
| 135 | Â Â } |
|---|
| 136 | |
|---|
| 137 | Â Â |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | function rollback() |
|---|
| 143 | Â Â { |
|---|
| 144 | Â Â Â Â if (count($this->transaction) == 0) { |
|---|
| 145 | Â Â Â Â Â Â return 0; |
|---|
| 146 | Â Â Â Â } |
|---|
| 147 | |
|---|
| 148 | Â Â Â Â |
|---|
| 149 | if (Ethna::isError($r)) { |
|---|
| 150 | Â Â Â Â Â Â return $r; |
|---|
| 151 | Â Â Â Â } |
|---|
| 152 | Â Â Â Â $this->transaction = array(); |
|---|
| 153 | |
|---|
| 154 | Â Â Â Â return 0; |
|---|
| 155 | Â Â } |
|---|
| 156 | |
|---|
| 157 | Â Â |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | function commit() |
|---|
| 163 | Â Â { |
|---|
| 164 | Â Â Â Â if (count($this->transaction) == 0) { |
|---|
| 165 | Â Â Â Â Â Â return 0; |
|---|
| 166 | Â Â Â Â } else if (count($this->transaction) > 1) { |
|---|
| 167 | Â Â Â Â Â Â array_pop($this->transaction); |
|---|
| 168 | Â Â Â Â Â Â return 0; |
|---|
| 169 | Â Â Â Â } |
|---|
| 170 | |
|---|
| 171 | Â Â Â Â $r = $this->query('COMMIT;'); |
|---|
| 172 | Â Â Â Â if (Ethna::isError($r)) { |
|---|
| 173 | Â Â Â Â Â Â return $r; |
|---|
| 174 | Â Â Â Â } |
|---|
| 175 | Â Â Â Â array_pop($this->transaction); |
|---|
| 176 | |
|---|
| 177 | Â Â Â Â return 0; |
|---|
| 178 | Â Â } |
|---|
| 179 | |
|---|
| 180 | Â Â |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | function getMetaData($table) |
|---|
| 187 | Â Â { |
|---|
| 188 | Â Â Â Â return $this->db->tableInfo($table); |
|---|
| 189 | Â Â } |
|---|
| 190 | Â Â |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | *  DB�����פ�� |
|---|
| 194 | Â Â Â * |
|---|
| 195 |    *  @access  public |
|---|
| 196 |    *  @return  string  DB������   */ |
|---|
| 197 | Â Â function getType() |
|---|
| 198 | Â Â { |
|---|
| 199 | Â Â Â Â return $this->type; |
|---|
| 200 | Â Â } |
|---|
| 201 | |
|---|
| 202 | Â Â |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | function &query($query) |
|---|
| 209 | Â Â { |
|---|
| 210 | Â Â Â Â return $this->_query($query); |
|---|
| 211 | Â Â } |
|---|
| 212 | |
|---|
| 213 | Â Â |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | function getInsertId() |
|---|
| 220 | Â Â { |
|---|
| 221 | Â Â Â Â if ($this->isValid() == false) { |
|---|
| 222 | Â Â Â Â Â Â return null; |
|---|
| 223 | Â Â Â Â } else if ($this->type == 'mysql') { |
|---|
| 224 | Â Â Â Â Â Â return mysql_insert_id($this->db->connection); |
|---|
| 225 | Â Â Â Â } |
|---|
| 226 | |
|---|
| 227 | Â Â Â Â return null; |
|---|
| 228 | Â Â } |
|---|
| 229 | |
|---|
| 230 | Â Â |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | function affectedRows() |
|---|
| 235 | Â Â { |
|---|
| 236 | Â Â Â Â return $this->db->affectedRows(); |
|---|
| 237 | Â Â } |
|---|
| 238 | Â Â |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | *  SQLʸ��ꥯ����ȯ�Ԥ��� * |
|---|
| 242 |    *  @access  public |
|---|
| 243 |    *  @param  string  $sqlid    SQL-ID(+��   *  @return  mixed  DB_Result:�����֥�������Ethna_Error:���顼 |
|---|
| 244 | Â Â Â */ |
|---|
| 245 | Â Â function &sqlquery($sqlid) |
|---|
| 246 | Â Â { |
|---|
| 247 | Â Â Â Â $args = func_get_args(); |
|---|
| 248 | Â Â Â Â array_shift($args); |
|---|
| 249 | Â Â Â Â $query = $this->sql->get($sqlid, $args); |
|---|
| 250 | |
|---|
| 251 | Â Â Â Â return $this->_query($query); |
|---|
| 252 | Â Â } |
|---|
| 253 | |
|---|
| 254 | Â Â |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | function sql($sqlid) |
|---|
| 261 | Â Â { |
|---|
| 262 | Â Â Â Â $args = func_get_args(); |
|---|
| 263 | Â Â Â Â array_shift($args); |
|---|
| 264 | Â Â Â Â $query = $this->sql->get($sqlid, $args); |
|---|
| 265 | |
|---|
| 266 | Â Â Â Â return $query; |
|---|
| 267 | Â Â } |
|---|
| 268 | |
|---|
| 269 | Â Â |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | function lock($tables) |
|---|
| 276 | Â Â { |
|---|
| 277 | Â Â Â Â $this->message = null; |
|---|
| 278 | |
|---|
| 279 | Â Â Â Â $sql = ""; |
|---|
| 280 | Â Â Â Â foreach (to_array($tables) as $table) { |
|---|
| 281 | Â Â Â Â Â Â if ($sql != "") { |
|---|
| 282 | Â Â Â Â Â Â Â Â $sql .= ", "; |
|---|
| 283 | Â Â Â Â Â Â } |
|---|
| 284 | Â Â Â Â Â Â $sql .= "$table WRITE"; |
|---|
| 285 | Â Â Â Â } |
|---|
| 286 | |
|---|
| 287 | Â Â Â Â return $this->query("LOCK TABLES $sql"); |
|---|
| 288 | Â Â } |
|---|
| 289 | |
|---|
| 290 | Â Â |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | function unlock() |
|---|
| 296 | Â Â { |
|---|
| 297 | Â Â Â Â $this->message = null; |
|---|
| 298 | Â Â Â Â return $this->query("UNLOCK TABLES"); |
|---|
| 299 | Â Â } |
|---|
| 300 | |
|---|
| 301 | Â Â |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | function &_query($query) |
|---|
| 308 | Â Â { |
|---|
| 309 | Â Â Â Â $this->logger->log(LOG_DEBUG, "$query"); |
|---|
| 310 | Â Â Â Â $r =& $this->db->query($query); |
|---|
| 311 | Â Â Â Â if (DB::isError($r)) { |
|---|
| 312 | Â Â Â Â Â Â if ($r->getCode() == DB_ERROR_ALREADY_EXISTS) { |
|---|
| 313 |         $error = Ethna::raiseNotice('�����������顼 SQL[%s]', E_DB_DUPENT, $query, $this->db->errorNative(), $r->getUserInfo()); |
|---|
| 314 | Â Â Â Â Â Â } else { |
|---|
| 315 |         $error = Ethna::raiseError('�����ꥨ�顼 SQL[%s] CODE[%d] MESSAGE[%s]', E_DB_QUERY, $query, $this->db->errorNative(), $r->getUserInfo()); |
|---|
| 316 | Â Â Â Â Â Â } |
|---|
| 317 | Â Â Â Â Â Â return $error; |
|---|
| 318 | Â Â Â Â } |
|---|
| 319 | Â Â Â Â return $r; |
|---|
| 320 | Â Â } |
|---|
| 321 | Â Â |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | ?> |
|---|