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: public function getResultObject($className = NULL) {
169: $result = NULL;
170: $queryId = $this->_handler->getQueryId();
171:
172: if (is_resource($queryId)) {
173: if ($className == NULL) {
174: $result = mysql_fetch_object($queryId);
175: } else {
176: $result = mysql_fetch_object($queryId, $className);
177: }
178: }
179:
180: return $result;
181: }
182:
183: 184: 185:
186: public function affectedRows() {
187: $linkId = $this->_handler->getLinkId();
188:
189: return ($linkId) ? mysql_affected_rows($linkId) : 0;
190: }
191:
192: 193: 194:
195: public function numRows() {
196: $queryId = $this->_handler->getQueryId();
197:
198: return ($queryId) ? mysql_num_rows($queryId) : 0;
199: }
200:
201: 202: 203:
204: public function numFields() {
205: $queryId = $this->_handler->getQueryId();
206:
207: return ($queryId) ? mysql_num_fields($queryId) : 0;
208: }
209:
210: 211: 212:
213: public function free() {
214: @mysql_free_result($this->_handler->getQueryId());
215: $this->_handler->setQueryId(0);
216: }
217:
218: 219: 220:
221: public function escape($string) {
222: $linkId = $this->_handler->getLinkId();
223:
224: return mysql_real_escape_string($string, $linkId);
225: }
226:
227: 228: 229:
230: public function seek($pos = 0) {
231: $queryId = $this->_handler->getQueryId();
232:
233: $status = @mysql_data_seek($queryId, $pos);
234: if ($status) {
235: $this->_handler->setRow($pos);
236: } else {
237: return 0;
238: }
239:
240: return 1;
241: }
242:
243: 244: 245:
246: public function getMetaData($tableName, $full = false) {
247: $res = array();
248:
249: $this->query('SELECT * FROM `%s` LIMIT 1', $tableName);
250: $id = $this->getQueryId();
251: if (!$id) {
252: $this->_handler->halt('Metadata query failed.');
253:
254: return false;
255: }
256:
257: $count = @mysql_num_fields($id);
258:
259:
260: for ($i = 0; $i < $count; $i++) {
261: $res[$i]['table'] = @mysql_field_table($id, $i);
262: $res[$i]['name'] = @mysql_field_name($id, $i);
263: $res[$i]['type'] = @mysql_field_type($id, $i);
264: $res[$i]['len'] = @mysql_field_len($id, $i);
265: $res[$i]['flags'] = @mysql_field_flags($id, $i);
266: if ($full) {
267: $res['meta'][$res[$i]['name']] = $i;
268: }
269: }
270: if ($full) {
271: $res['num_fields'] = $count;
272: }
273:
274: $this->free();
275:
276: return $res;
277: }
278:
279: 280: 281:
282: public function getTableNames() {
283: $return = array();
284:
285: if ($this->query('SHOW TABLES')) {
286: while ($this->nextRecord()) {
287: $record = $this->getRecord();
288: $return[] = array(
289: 'table_name' => $record[0], 'tablespace_name' => $this->_dbCfg['connection']['database'], 'database' => $this->_dbCfg['connection']['database'],
290: );
291: }
292:
293: $this->free();
294: }
295:
296: return $return;
297: }
298:
299: 300: 301:
302: public function getServerInfo() {
303: $linkId = $this->_handler->getLinkId();
304:
305: if (is_resource($linkId)) {
306: $arr = array();
307: $arr['description'] = mysql_get_server_info($linkId);
308:
309: return $arr;
310: }
311:
312: return NULL;
313: }
314:
315: 316: 317:
318: public function getErrorNumber() {
319: $linkId = $this->_handler->getLinkId();
320:
321: if (is_resource($linkId)) {
322: return mysql_errno($linkId);
323: } else {
324: return mysql_errno();
325: }
326: }
327:
328: 329: 330:
331: public function getErrorMessage() {
332: $linkId = $this->_handler->getLinkId();
333:
334: if (is_resource($linkId)) {
335: return mysql_error($linkId);
336: } else {
337: return mysql_error();
338: }
339: }
340:
341: 342: 343:
344: public function disconnect() {
345: mysql_close($this->_handler->getLinkId());
346: }
347:
348: }