1: <?php
2: /**
3: * You may not change or alter any portion of this comment or credits
4: * of supporting developers from this source code or any supporting source code
5: * which is considered copyrighted (c) material of the original comment or credit authors.
6: *
7: * This program is distributed in the hope that it will be useful,
8: * but WITHOUT ANY WARRANTY; without even the implied warranty of
9: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: /**
13: * connection to a mysql database - legacy support only
14: *
15: * PHP version 5.3
16: *
17: * @category Xoops\Class\Database\MySQLDatabase
18: * @package MySQLDatabase
19: * @author Kazumi Ono <onokazu@xoops.org>
20: * @author readheadedrod <redheadedrod@hotmail.com>
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2013 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @version Release: 2.6
25: * @link http://xoops.org
26: * @since 2.6.0
27: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
28: *
29: */
30: class XoopsMySQLDatabase extends XoopsDatabase
31: {
32:
33: /**
34: * @var object keep track of last result since we need it for getAffectedRows
35: */
36: private $lastResult = null;
37:
38: /**
39: * Database connection
40: *
41: * @var resource
42: */
43: public $conn;
44:
45: /**
46: * Database connection
47: *
48: * @var resource
49: */
50: private $connect = false;
51:
52: /**
53: * Database connection
54: *
55: * @var resource
56: */
57: private $selectdb;
58:
59: /**
60: * Issue a deprecated warning once per session
61: *
62: * @return void
63: */
64: protected function deprecated()
65: {
66: static $warning_issued = false;
67: if (!$warning_issued) {
68: $warning_issued = true;
69: $stack = debug_backtrace();
70: $frame = $stack[1];
71: Xoops::getInstance()->deprecated(
72: 'Legacy XoopsDB is deprecated since 2.6.0; all calls should be using Doctrine through $xoops->db(). '
73: . 'Called from ' . $frame['function'] . '() in ' . $frame['file'] . ' line '. $frame['line']
74: );
75: }
76: }
77:
78:
79: /**
80: * connect to the database
81: *
82: * @param bool $selectdb select the database now?
83: *
84: * @return bool successful?
85: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
86: */
87: public function connect($selectdb = true)
88: {
89: $this->connect = (is_object($this->conn));
90: $this->selectdb = $selectdb;
91: $this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] !== 'GET');
92: return $this->connect;
93: }
94:
95:
96: /**
97: * generate an ID for a new row
98: *
99: * This is for compatibility only. Will always return 0, because MySQL supports
100: * autoincrement for primary keys.
101: *
102: * @param string $sequence name of the sequence from which to get the next ID
103: *
104: * @return int always 0, because mysql has support for autoincrement
105: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
106: */
107: public function genId($sequence)
108: {
109: $this->deprecated();
110: return 0; // will use auto_increment
111: }
112:
113: /**
114: * Get a result row as an enumerated array
115: *
116: * @param resource $result resource to get result from
117: *
118: * @return array
119: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
120: */
121: public function fetchRow($result)
122: {
123: $this->deprecated();
124: if (!is_object($result)) {
125: return null;
126: }
127: return $result->fetch(\PDO::FETCH_NUM);
128: }
129:
130: /**
131: * Fetch a result row as an associative array
132: *
133: * @param resource $result resource to get result from
134: *
135: * @return array
136: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
137: */
138: public function fetchArray($result)
139: {
140: $this->deprecated();
141:
142: if (!is_object($result)) {
143: return null;
144: }
145: return $result->fetch(\PDO::FETCH_ASSOC);
146: }
147:
148: /**
149: * Fetch a result row as an associative array
150: *
151: * @param resource $result resource to get result from
152: *
153: * @return array
154: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
155: */
156: public function fetchBoth($result)
157: {
158: $this->deprecated();
159:
160: if (!is_object($result)) {
161: return null;
162: }
163: return $result->fetch(\PDO::FETCH_BOTH);
164: }
165:
166: /**
167: * Fetch a result row as an object
168: *
169: * @param resource $result resource to get result from
170: *
171: * @return object|stdClass
172: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
173: */
174: public function fetchObject($result)
175: {
176: $this->deprecated();
177:
178: if (!is_object($result)) {
179: return null;
180: }
181: return $result->fetch(\PDO::FETCH_OBJ);
182: }
183:
184: /**
185: * Get the ID generated from the previous INSERT operation
186: *
187: * @return int
188: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
189: */
190: public function getInsertId()
191: {
192: $this->deprecated();
193: return $this->conn->lastInsertId();
194: }
195:
196: /**
197: * Get number of rows in result
198: *
199: * @param resource $result the resource containing the number of rows
200: *
201: * @return int the number of rows to return
202: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
203: */
204: public function getRowsNum($result)
205: {
206: Xoops::getInstance()->deprecated('getRowsNum is deprecated and not dependable.');
207: //$this->deprecated();
208: return $result->rowCount();
209: }
210:
211: /**
212: * Get number of affected rows
213: *
214: * @return int
215: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
216: */
217: public function getAffectedRows()
218: {
219: $this->deprecated();
220: return $this->lastResult->rowCount();
221: }
222:
223: /**
224: * Close MySQL connection
225: *
226: * @return void
227: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
228: */
229: public function close()
230: {
231: $this->deprecated();
232: return $this->conn->close();
233: }
234:
235: /**
236: * will free all memory associated with the result identifier result.
237: *
238: * @param resource $result query result
239: *
240: * @return bool TRUE on success or FALSE on failure.
241: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
242: */
243: public function freeRecordSet($result)
244: {
245: $this->deprecated();
246:
247: return $result->closeCursor();
248: }
249:
250: /**
251: * Returns the text of the error message from previous MySQL operation
252: *
253: * @return bool Returns the error text from the last MySQL function,
254: * or '' (the empty string) if no error occurred.
255: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
256: */
257: public function error()
258: {
259: $this->deprecated();
260:
261: return $this->conn->errorInfo();
262: }
263:
264: /**
265: * Returns the numerical value of the error message from previous
266: * MySQL operation
267: *
268: * @return int Returns the error number from the last MySQL function
269: * , or 0 (zero) if no error occurred.
270: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
271: */
272: public function errno()
273: {
274: $this->deprecated();
275:
276: return $this->conn->errorCode();
277: }
278:
279: /**
280: * Returns escaped string text with single
281: * quotes around it to be safely stored in database
282: *
283: * @param string $str unescaped string text
284: *
285: * @return string escaped string text with single quotes around
286: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
287: */
288: public function quoteString($str)
289: {
290: $this->deprecated();
291:
292: return $this->quote($str);
293: }
294:
295: /**
296: * Quotes a string for use in a query.
297: *
298: * @param string $string string to quote
299: *
300: * @return string
301: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
302: */
303: public function quote($string)
304: {
305: $this->deprecated();
306:
307: return $this->conn->quote($string);
308: }
309:
310: /**
311: * Escapes a string for use in a query. Does not add quotes.
312: *
313: * @param string $string string to escape
314: *
315: * @return string
316: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
317: */
318: public function escape($string)
319: {
320: $this->deprecated();
321:
322: $string = $this->quote($input);
323: return substr($string, 1, -1);
324: }
325:
326: /**
327: * perform a query on the database
328: *
329: * @param string $sql a valid MySQL query
330: * @param int $limit number of records to return
331: * @param int $start offset of first record to return
332: *
333: * @return bool|resource query result or FALSE if successful
334: * or TRUE if successful and no result
335: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
336: */
337: public function queryF($sql, $limit = 0, $start = 0)
338: {
339: $this->deprecated();
340:
341: if (!empty($limit)) {
342: if (empty($start)) {
343: $start = 0;
344: }
345: $sql = $sql . ' LIMIT ' . (int) $start . ', ' . (int) $limit;
346: }
347: $events = \Xoops::getInstance()->events();
348: $events->triggerEvent('core.database.query.start');
349: try {
350: $result = $this->conn->query($sql);
351: } catch (Exception $e) {
352: $result=false;
353: }
354: $this->lastResult = $result;
355: $events->triggerEvent('core.database.query.end');
356:
357: if ($result) {
358: $events->triggerEvent('core.database.query.success', (array($sql)));
359: return $result;
360: } else {
361: $events->triggerEvent('core.database.query.failure', (array($sql, $this)));
362: return false;
363: }
364: }
365:
366: /**
367: * perform a query
368: *
369: * This method is empty and does nothing! It should therefore only be
370: * used if nothing is exactly what you want done! ;-)
371: *
372: * @param string $sql a valid MySQL query
373: * @param int $limit number of records to return
374: * @param int $start offset of first record to return
375: *
376: * @return this returns nothing
377: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
378: */
379: public function query($sql, $limit = 0, $start = 0)
380: {
381: $this->deprecated();
382:
383: }
384:
385: /**
386: * perform queries from SQL dump file in a batch
387: *
388: * @param string $file file path to an SQL dump file
389: *
390: * @return bool FALSE if failed reading SQL file or TRUE
391: * if the file has been read and queries executed
392: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
393: */
394: public function queryFromFile($file)
395: {
396: $this->deprecated();
397:
398: if (false !== ($fp = fopen($file, 'r'))) {
399: $sql_queries = trim(fread($fp, filesize($file)));
400: SqlUtility::splitMySqlFile($pieces, $sql_queries);
401: foreach ($pieces as $query) {
402: // [0] contains the prefixed query
403: // [4] contains unprefixed table name
404: $prefixed_query
405: = SqlUtility::prefixQuery(trim($query), $this->prefix());
406: if ($prefixed_query != false) {
407: $this->query($prefixed_query[0]);
408: }
409: }
410: return true;
411: }
412: return false;
413: }
414:
415: /**
416: * Get field name
417: *
418: * @param resource $result query result
419: * @param int $offset numerical field index
420: *
421: * @return string
422: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
423: */
424: public function getFieldName($result, $offset)
425: {
426: $this->deprecated();
427:
428: try {
429: $temp = $result->getColumnMeta($offset);
430: return $temp['name'];
431: } catch (PDOException $e) {
432: return null;
433: }
434:
435: }
436:
437: /**
438: * Get field type
439: *
440: * @param resource $result query result
441: * @param int $offset numerical field index
442: *
443: * @return string
444: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
445: */
446: public function getFieldType($result, $offset)
447: {
448: $this->deprecated();
449:
450: try {
451: $temp = ($result->getColumnMeta($offset));
452: $t = $temp['native_type'];
453:
454: $temp = (string)(
455: ((($t === 'STRING') || ($t === 'VAR_STRING') ) ? 'string' : '' ) .
456: ( (in_array($t, array('TINY', 'SHORT', 'LONG', 'LONGLONG', 'INT24'))) ? 'int' : '' ) .
457: ( (in_array($t, array('FLOAT', 'DOUBLE', 'DECIMAL', 'NEWDECIMAL'))) ? 'real' : '' ) .
458: ( ($t === 'TIMESTAMP') ? 'timestamp' : '' ) .
459: ( ($t === 'YEAR') ? 'year' : '') .
460: ( (($t === 'DATE') || ($t === 'NEWDATE') ) ? 'date' : '' ) .
461: ( ($t === 'TIME') ? 'time' : '' ) .
462: ( ($t === 'SET') ? 'set' : '' ) .
463: ( ($t === 'ENUM') ? 'enum' : '' ) .
464: ( ($t === 'GEOMETRY') ? 'geometry' : '' ) .
465: ( ($t === 'DATETIME') ? 'datetime' : '' ) .
466: ( (in_array($t, array('TINY_BLOB', 'BLOB', 'MEDIUM_BLOB', 'LONG_BLOB' ))) ? 'blob' : '' ) .
467: ( ($t === 'NULL') ? 'null' : '' )
468: );
469: return $temp;
470: } catch (PDOException $e) {
471: return null;
472: }
473: }
474:
475: /**
476: * Get number of fields in result
477: *
478: * @param resource $result query result
479: *
480: * @return int
481: * @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
482: */
483: public function getFieldsNum($result)
484: {
485: $this->deprecated();
486:
487: return $result->columnCount();
488: }
489: }
490: