1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage database
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 *
8 * This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning FOFTable objects
9 * instead of plain stdClass objects
10 */
11
12 // Protect from unauthorized access
13 defined('FOF_INCLUDED') or die;
14
15 /**
16 * Query Building Class.
17 *
18 * @since 11.1
19 */
20 class FOFDatabaseQueryMysqli extends FOFDatabaseQuery implements FOFDatabaseQueryLimitable
21 {
22 /**
23 * @var integer The offset for the result set.
24 * @since 12.1
25 */
26 protected $offset;
27
28 /**
29 * @var integer The limit for the result set.
30 * @since 12.1
31 */
32 protected $limit;
33
34 /**
35 * Method to modify a query already in string format with the needed
36 * additions to make the query limited to a particular number of
37 * results, or start at a particular offset.
38 *
39 * @param string $query The query in string format
40 * @param integer $limit The limit for the result set
41 * @param integer $offset The offset for the result set
42 *
43 * @return string
44 *
45 * @since 12.1
46 */
47 public function processLimit($query, $limit, $offset = 0)
48 {
49 if ($limit > 0 || $offset > 0)
50 {
51 $query .= ' LIMIT ' . $offset . ', ' . $limit;
52 }
53
54 return $query;
55 }
56
57 /**
58 * Concatenates an array of column names or values.
59 *
60 * @param array $values An array of values to concatenate.
61 * @param string $separator As separator to place between each value.
62 *
63 * @return string The concatenated values.
64 *
65 * @since 11.1
66 */
67 public function concatenate($values, $separator = null)
68 {
69 if ($separator)
70 {
71 $concat_string = 'CONCAT_WS(' . $this->quote($separator);
72
73 foreach ($values as $value)
74 {
75 $concat_string .= ', ' . $value;
76 }
77
78 return $concat_string . ')';
79 }
80 else
81 {
82 return 'CONCAT(' . implode(',', $values) . ')';
83 }
84 }
85
86 /**
87 * Sets the offset and limit for the result set, if the database driver supports it.
88 *
89 * Usage:
90 * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
91 * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
92 *
93 * @param integer $limit The limit for the result set
94 * @param integer $offset The offset for the result set
95 *
96 * @return FOFDatabaseQuery Returns this object to allow chaining.
97 *
98 * @since 12.1
99 */
100 public function setLimit($limit = 0, $offset = 0)
101 {
102 $this->limit = (int) $limit;
103 $this->offset = (int) $offset;
104
105 return $this;
106 }
107
108 /**
109 * Return correct regexp operator for mysqli.
110 *
111 * Ensure that the regexp operator is mysqli compatible.
112 *
113 * Usage:
114 * $query->where('field ' . $query->regexp($search));
115 *
116 * @param string $value The regex pattern.
117 *
118 * @return string Returns the regex operator.
119 *
120 * @since 11.3
121 */
122 public function regexp($value)
123 {
124 return ' REGEXP ' . $value;
125 }
126
127 /**
128 * Return correct rand() function for Mysql.
129 *
130 * Ensure that the rand() function is Mysql compatible.
131 *
132 * Usage:
133 * $query->Rand();
134 *
135 * @return string The correct rand function.
136 *
137 * @since 3.5
138 */
139 public function Rand()
140 {
141 return ' RAND() ';
142 }
143 }
144