1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34: class cDbDriverMysql extends cDbDriverAbstract {
35:
36: 37: 38:
39: public function check() {
40: return function_exists("mysql_connect");
41: }
42:
43: 44: 45:
46: public function connect() {
47: if (isset($this->_dbCfg['connection'])) {
48: $connectConfig = $this->_dbCfg['connection'];
49: }
50: if (empty($connectConfig) || !isset($connectConfig['host']) || !isset($connectConfig['user']) || !isset($connectConfig['password'])) {
51: $this->_handler->halt('Database connection settings incomplete');
52:
53: return NULL;
54: }
55:
56:
57: $dbHandler = mysql_connect($connectConfig['host'], $connectConfig['user'], $connectConfig['password']);
58: if (!$dbHandler || !is_resource($dbHandler)) {
59: $this->_handler->halt('Error during establishing a connection with database.');
60:
61: return NULL;
62: }
63:
64: if (isset($connectConfig['database'])) {
65: if (!mysql_select_db($connectConfig['database'], $dbHandler)) {
66: $this->_handler->halt('Can not use database ' . $connectConfig['database']);
67:
68: return NULL;
69: } else {
70:
71: if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
72: if (!mysql_set_charset($connectConfig['charset'], $dbHandler)) {
73: $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
74:
75: return NULL;
76: }
77: }
78: }
79: }
80:
81: return $dbHandler;
82: }
83:
84: 85: 86:
87: public function buildInsert($tableName, array $fields) {
88: $fieldList = '';
89: $valueList = '';
90:
91: foreach ($fields as $field => $value) {
92: $fieldList .= '`' . $field . '`, ';
93: if (is_int($value)) {
94: $valueList .= $value . ', ';
95: } else {
96: $valueList .= "'" . $this->escape($value) . "', ";
97: }
98: }
99:
100: $fieldList = substr($fieldList, 0, -2);
101: $valueList = substr($valueList, 0, -2);
102:
103: return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
104: }
105:
106: 107: 108:
109: public function buildUpdate($tableName, array $fields, array $whereClauses) {
110: $updateList = '';
111: $whereList = '';
112:
113: foreach ($fields as $field => $value) {
114: $updateList .= '`' . $field . '`=';
115: if (is_int($value)) {
116: $updateList .= $value . ', ';
117: } else {
118: $updateList .= "'" . $this->escape($value) . "', ";
119: }
120: }
121:
122: foreach ($whereClauses as $field => $value) {
123: $whereList .= '`' . $field . '`=';
124: if (is_int($value)) {
125: $whereList .= $value . ' AND ';
126: } else {
127: $whereList .= "'" . $this->escape($value) . "' AND ";
128: }
129: }
130:
131: $updateList = substr($updateList, 0, -2);
132: $whereList = substr($whereList, 0, -5);
133:
134: return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
135: }
136:
137: 138: 139:
140: public function query($query) {
141: $linkId = $this->_handler->getLinkId();
142: $queryId = @mysql_query($query, $linkId);
143:
144: $this->_handler->setQueryId($queryId);
145: $this->_handler->setRow(0);
146: $this->_handler->setErrorNumber($this->getErrorNumber());
147: $this->_handler->setErrorMessage($this->getErrorMessage());
148: }
149:
150: 151: 152:
153: public function nextRecord() {
154: $queryId = $this->_handler->getQueryId();
155: $record = @mysql_fetch_array($queryId);
156:
157: $this->_handler->setRecord($record);
158: $this->_handler->incrementRow();
159: $this->_handler->setErrorNumber($this->getErrorNumber());
160: $this->_handler->setErrorMessage($this->getErrorMessage());
161:
162: return is_array($record);
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function getResultObject($className = NULL) {
172: $result = NULL;
173: $queryId = $this->_handler->getQueryId();
174:
175: if (is_resource($queryId)) {
176: if ($className == NULL) {
177: $result = mysql_fetch_object($queryId);
178: } else {
179: $result = mysql_fetch_object($queryId, $className);
180: }
181: }
182:
183: return $result;
184: }
185:
186: 187: 188:
189: public function affectedRows() {
190: $linkId = $this->_handler->getLinkId();
191:
192: return ($linkId) ? mysql_affected_rows($linkId) : 0;
193: }
194:
195: 196: 197:
198: public function numRows() {
199: $queryId = $this->_handler->getQueryId();
200:
201: return ($queryId) ? mysql_num_rows($queryId) : 0;
202: }
203:
204: 205: 206:
207: public function numFields() {
208: $queryId = $this->_handler->getQueryId();
209:
210: return ($queryId) ? mysql_num_fields($queryId) : 0;
211: }
212:
213: 214: 215:
216: public function free() {
217: @mysql_free_result($this->_handler->getQueryId());
218: $this->_handler->setQueryId(0);
219: }
220:
221: 222: 223:
224: public function escape($string) {
225: $linkId = $this->_handler->getLinkId();
226:
227: return mysql_real_escape_string($string, $linkId);
228: }
229:
230: 231: 232: 233: 234:
235: public function seek($pos = 0) {
236: $queryId = $this->_handler->getQueryId();
237:
238: $status = @mysql_data_seek($queryId, $pos);
239: if ($status) {
240: $this->_handler->setRow($pos);
241: } else {
242: return 0;
243: }
244:
245: return 1;
246: }
247:
248: 249: 250:
251: public function getMetaData($tableName, $full = false) {
252: $res = array();
253:
254: $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
255: $id = $this->_handler->getQueryId();
256: if (!$id) {
257: $this->_handler->halt('Metadata query failed.');
258:
259: return false;
260: }
261:
262:
263: $count = @mysql_num_fields($id);
264: for ($i = 0; $i < $count; $i++) {
265: $res[$i]['table'] = @mysql_field_table($id, $i);
266: $res[$i]['name'] = @mysql_field_name($id, $i);
267: $res[$i]['type'] = @mysql_field_type($id, $i);
268: $res[$i]['len'] = @mysql_field_len($id, $i);
269: $res[$i]['flags'] = @mysql_field_flags($id, $i);
270: if ($full) {
271: $res['meta'][$res[$i]['name']] = $i;
272: }
273: }
274: if ($full) {
275: $res['num_fields'] = $count;
276: }
277:
278: $this->free();
279:
280: return $res;
281: }
282:
283: 284: 285:
286: public function getTableNames() {
287: $return = array();
288:
289: if ($this->query('SHOW TABLES')) {
290: while ($this->nextRecord()) {
291: $record = $this->getRecord();
292: $return[] = array(
293: 'table_name' => $record[0], 'tablespace_name' => $this->_dbCfg['connection']['database'], 'database' => $this->_dbCfg['connection']['database'],
294: );
295: }
296:
297: $this->free();
298: }
299:
300: return $return;
301: }
302:
303: 304: 305:
306: public function getServerInfo() {
307: $linkId = $this->_handler->getLinkId();
308:
309: if (is_resource($linkId)) {
310: $arr = array();
311: $arr['description'] = mysql_get_server_info($linkId);
312:
313: return $arr;
314: }
315:
316: return NULL;
317: }
318:
319: 320: 321:
322: public function getErrorNumber() {
323: $linkId = $this->_handler->getLinkId();
324:
325: if (is_resource($linkId)) {
326: return mysql_errno($linkId);
327: } else {
328: return mysql_errno();
329: }
330: }
331:
332: 333: 334:
335: public function getErrorMessage() {
336: $linkId = $this->_handler->getLinkId();
337:
338: if (is_resource($linkId)) {
339: return mysql_error($linkId);
340: } else {
341: return mysql_error();
342: }
343: }
344:
345: 346: 347:
348: public function disconnect() {
349: mysql_close($this->_handler->getLinkId());
350: }
351:
352: }