00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBDriverMysql implements IDBDriver {
00009
00010
00011
00012 const PRIMARY = 0;
00013
00014
00015
00016 const SECONDARY = 1;
00017
00018
00019
00020
00021
00022 protected $type;
00023 protected $db_handle = false;
00024 protected static $transaction_count = 0;
00025 protected $connect_params;
00026
00027
00028
00029
00030
00031 public function get_driver_name() {
00032 return 'mysql';
00033 }
00034
00035
00036
00037
00038
00039
00040 public function get_host() {
00041 return Arr::get_item($this->connect_params, 'host', '');
00042 }
00043
00044
00045
00046
00047
00048
00049 public function get_db_name() {
00050 return Arr::get_item($this->connect_params, 'db', '');
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 public function initialize($dbname, $user = '', $password = '', $host = 'localhost', $params = false) {
00065 $this->connect_params = array(
00066 'host' => $host,
00067 'user' => $user,
00068 'pwd' => $password,
00069 'db' => $dbname,
00070 );
00071 $this->type = Arr::get_item($params, 'type', self::PRIMARY);
00072 }
00073
00074
00075
00076
00077
00078
00079 protected function connect() {
00080 if ($this->db_handle === false) {
00081 $err = new Status();
00082 $this->db_handle = mysql_connect(
00083 $this->connect_params['host'],
00084 $this->connect_params['user'],
00085 $this->connect_params['pwd']
00086 );
00087 if ($this->db_handle) {
00088 if ($this->type == self::PRIMARY) {
00089 $err->merge($this->make_default());
00090 }
00091 if ($err->is_ok()) {
00092
00093 if (GyroLocale::get_charset() == 'UTF-8') {
00094 $this->execute("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
00095 }
00096
00097 }
00098 }
00099 else {
00100 $err->append(tr(
00101 'Could not connect to server %host',
00102 'core',
00103 array('%host' => $this->connect_params['host'])
00104 ));
00105 }
00106 if ($err->is_error()) {
00107 throw new Exception($err->to_string(Status::OUTPUT_PLAIN));
00108 }
00109 }
00110 }
00111
00112
00113
00114
00115
00116
00117 public function quote($value) {
00118 return "'" . $this->escape($value) . "'";
00119 }
00120
00121
00122
00123
00124
00125
00126 public function escape_database_entity($obj, $type = self::FIELD) {
00127 $ret = '';
00128 if ($type === self::TABLE) {
00129 $ret .= '`' . $this->get_db_name() . '`.';
00130 }
00131 $ret .= '`' . $obj . '`';
00132 return $ret;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function escape($value) {
00142 $this->connect();
00143 return mysql_real_escape_string(Cast::string($value), $this->db_handle);
00144 }
00145
00146
00147
00148
00149
00150
00151 public function get_status() {
00152 $this->connect();
00153 $ret = new Status();
00154 if (mysql_errno($this->db_handle)) {
00155 $ret->append(mysql_error($this->db_handle));
00156 }
00157 return $ret;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166 public function execute($sql) {
00167 $this->connect();
00168 mysql_query($sql, $this->db_handle);
00169 return $this->get_status();
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public function query($sql) {
00179 $this->connect();
00180 $handle = mysql_query($sql, $this->db_handle);
00181 $status = $this->get_status();
00182 return new DBResultSetMysql($handle, $status);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 public function explain($sql) {
00192 $ret = false;
00193 if (strtolower(substr($sql, 0, 6)) === 'select') {
00194 $sql = 'EXPLAIN ' . $sql;
00195 $ret = $this->query($sql);
00196 }
00197 return $ret;
00198 }
00199
00200
00201
00202
00203
00204
00205 public function make_default() {
00206 $ret = new Status();
00207 $this->connect();
00208 if (!mysql_select_db($this->connect_params['db'], $this->db_handle)) {
00209 $ret->append(tr(
00210 'Could not connect to database %db on server %host',
00211 'core',
00212 array('%db' => $this->connect_params['db'], '%host' => $this->connect_params['host'])
00213 ));
00214 }
00215 return $ret;
00216 }
00217
00218
00219
00220
00221 public function trans_start() {
00222 if (self::$transaction_count >= 0) {
00223 if (self::$transaction_count == 0) {
00224
00225
00226 $this->connect();
00227 mysql_query('START TRANSACTION', $this->db_handle);
00228 }
00229 self::$transaction_count++;
00230 }
00231 }
00232
00233
00234
00235
00236 public function trans_commit() {
00237 if (self::$transaction_count > 0) {
00238 self::$transaction_count--;
00239 if (self::$transaction_count == 0) {
00240 mysql_query('COMMIT', $this->db_handle);
00241 }
00242 }
00243 }
00244
00245
00246
00247
00248 public function trans_rollback() {
00249 if (self::$transaction_count > 0) {
00250 self::$transaction_count--;
00251 if (self::$transaction_count == 0) {
00252
00253 mysql_query('ROLLBACK', $this->db_handle);
00254 }
00255 }
00256
00257
00258
00259 }
00260
00261
00262
00263
00264 public function last_insert_id() {
00265 return mysql_insert_id($this->db_handle);
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 public function has_feature($feature) {
00275 switch ($feature) {
00276 case self::FEATURE_REPLACE:
00277 return true;
00278 default:
00279 return false;
00280 }
00281 }
00282 }