Skip to content

Commit be31f1e

Browse files
authored
Merge pull request joomla#70 from photodude/patch-8
Port new functions from CMS
2 parents adc46e2 + b094232 commit be31f1e

File tree

4 files changed

+268
-1
lines changed

4 files changed

+268
-1
lines changed

src/DatabaseQuery.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ abstract class DatabaseQuery
187187
*/
188188
protected $union = null;
189189

190+
/**
191+
* The unionAll element.
192+
*
193+
* @var Query\QueryElement
194+
* @since __DEPLOY_VERSION__
195+
*/
196+
protected $unionAll = null;
197+
190198
/**
191199
* Magic method to provide method alias support for quote() and quoteName().
192200
*
@@ -646,6 +654,27 @@ public function currentTimestamp()
646654
return 'CURRENT_TIMESTAMP()';
647655
}
648656

657+
/**
658+
* Add to the current date and time.
659+
* Usage:
660+
* $query->select($query->dateAdd());
661+
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
662+
* Note: Not all drivers support all units.
663+
*
664+
* @param mixed $date The date to add to. May be date or datetime
665+
* @param string $interval The string representation of the appropriate number of units
666+
* @param string $datePart The part of the date to perform the addition on
667+
*
668+
* @return string The string with the appropriate sql for addition of dates
669+
*
670+
* @see http://dev.mysql.com/doc/en/date-and-time-functions.html
671+
* @since __DEPLOY_VERSION__
672+
*/
673+
public function dateAdd($date, $interval, $datePart)
674+
{
675+
return trim("DATE_ADD('" . $date . "', INTERVAL " . $interval . ' ' . $datePart . ')');
676+
}
677+
649678
/**
650679
* Returns a PHP date() function compliant date format for the database driver.
651680
*
@@ -767,6 +796,26 @@ public function exec($columns)
767796
return $this;
768797
}
769798

799+
/**
800+
* Find a value in a varchar used like a set.
801+
*
802+
* Ensure that the value is an integer before passing to the method.
803+
*
804+
* Usage:
805+
* $query->findInSet((int) $parent->id, 'a.assigned_cat_ids')
806+
*
807+
* @param string $value The value to search for.
808+
* @param string $set The set of values.
809+
*
810+
* @return string Returns the find_in_set() MySQL function and must be translated in each driver.
811+
*
812+
* @since __DEPLOY_VERSION__
813+
*/
814+
public function findInSet($value, $set)
815+
{
816+
return '';
817+
}
818+
770819
/**
771820
* Add a table to the FROM clause of the query.
772821
*
@@ -1503,7 +1552,7 @@ public function __clone()
15031552
public function union($query, $distinct = false, $glue = '')
15041553
{
15051554
// Clear any ORDER BY clause in UNION query
1506-
// See http://dev.mysql.com/doc/refman/5.0/en/union.html
1555+
// See http://dev.mysql.com/doc/en/union.html
15071556
if (!is_null($this->order))
15081557
{
15091558
$this->clear('order');
@@ -1535,6 +1584,44 @@ public function union($query, $distinct = false, $glue = '')
15351584
return $this;
15361585
}
15371586

1587+
/**
1588+
* Add a query to UNION ALL with the current query.
1589+
* Multiple unions each require separate statements and create an array of unions.
1590+
*
1591+
* Usage:
1592+
* $query->union('SELECT name FROM #__foo')
1593+
* $query->union(array('SELECT name FROM #__foo','SELECT name FROM #__bar'))
1594+
*
1595+
* @param mixed $query The JDatabaseQuery object or string to union.
1596+
* @param boolean $distinct Not used - ignored.
1597+
* @param string $glue Not used - ignored.
1598+
*
1599+
* @return Query\QueryElement Returns this object to allow chaining.
1600+
*
1601+
* @see union
1602+
*
1603+
* @since __DEPLOY_VERSION__
1604+
*/
1605+
public function unionAll($query, $distinct = false, $glue = '')
1606+
{
1607+
$glue = ')' . PHP_EOL . 'UNION ALL (';
1608+
$name = 'UNION ALL ()';
1609+
1610+
// Get the QueryElement if it does not exist
1611+
if (is_null($this->unionAll))
1612+
{
1613+
$this->unionAll = new Query\QueryElement($name, $query, "$glue");
1614+
}
1615+
1616+
// Otherwise append the second UNION.
1617+
else
1618+
{
1619+
$this->unionAll->append($query);
1620+
}
1621+
1622+
return $this;
1623+
}
1624+
15381625
/**
15391626
* Add a query to UNION DISTINCT with the current query. Simply a proxy to Union with the Distinct clause.
15401627
*

src/Mysqli/MysqliQuery.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,61 @@ public function setLimit($limit = 0, $offset = 0)
211211

212212
return $this;
213213
}
214+
215+
/**
216+
* Return correct regexp operator for mysqli.
217+
*
218+
* Ensure that the regexp operator is mysqli compatible.
219+
*
220+
* Usage:
221+
* $query->where('field ' . $query->regexp($search));
222+
*
223+
* @param string $value The regex pattern.
224+
*
225+
* @return string Returns the regex operator.
226+
*
227+
* @since __DEPLOY_VERSION__
228+
*/
229+
public function regexp($value)
230+
{
231+
return ' REGEXP ' . $value;
232+
}
233+
234+
/**
235+
* Return correct rand() function for Mysqli.
236+
*
237+
* Ensure that the rand() function is Mysqli compatible.
238+
*
239+
* Usage:
240+
* $query->Rand();
241+
*
242+
* @return string The correct rand function.
243+
*
244+
* @since __DEPLOY_VERSION__
245+
*/
246+
public function Rand()
247+
{
248+
return ' RAND() ';
249+
}
250+
251+
/**
252+
* Find a value in a varchar used like a set.
253+
*
254+
* Ensure that the value is an integer before passing to the method.
255+
*
256+
* Usage:
257+
* $query->findInSet((int) $parent->id, 'a.assigned_cat_ids')
258+
*
259+
* @param string $value The value to search for.
260+
*
261+
* @param string $set The set of values.
262+
*
263+
* @return string Returns the find_in_set() Mysql translation.
264+
*
265+
* @since __DEPLOY_VERSION__
266+
*/
267+
public function findInSet($value, $set)
268+
{
269+
return ' find_in_set(' . $value . ', ' . $set . ')';
270+
}
214271
}

src/Postgresql/PostgresqlQuery.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,89 @@ public function processLimit($query, $limit, $offset = 0)
695695

696696
return $query;
697697
}
698+
699+
/**
700+
* Add to the current date and time in Postgresql.
701+
* Usage:
702+
* $query->select($query->dateAdd());
703+
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
704+
*
705+
* @param datetime $date The date to add to
706+
* @param string $interval The string representation of the appropriate number of units
707+
* @param string $datePart The part of the date to perform the addition on
708+
*
709+
* @return string The string with the appropriate sql for addition of dates
710+
*
711+
* @since __DEPLOY_VERSION__
712+
* @note Not all drivers support all units. Check appropriate references
713+
* @link http://www.postgresql.org/docs/9.0/static/functions-datetime.html.
714+
*/
715+
public function dateAdd($date, $interval, $datePart)
716+
{
717+
if (substr($interval, 0, 1) != '-')
718+
{
719+
return "timestamp '" . $date . "' + interval '" . $interval . " " . $datePart . "'";
720+
}
721+
else
722+
{
723+
return "timestamp '" . $date . "' - interval '" . ltrim($interval, '-') . " " . $datePart . "'";
724+
}
725+
}
726+
727+
/**
728+
* Return correct regexp operator for Postgresql.
729+
*
730+
* Ensure that the regexp operator is Postgresql compatible.
731+
*
732+
* Usage:
733+
* $query->where('field ' . $query->regexp($search));
734+
*
735+
* @param string $value The regex pattern.
736+
*
737+
* @return string Returns the regex operator.
738+
*
739+
* @since __DEPLOY_VERSION__
740+
*/
741+
public function regexp($value)
742+
{
743+
return ' ~* ' . $value;
744+
}
745+
746+
/**
747+
* Return correct rand() function for Postgresql.
748+
*
749+
* Ensure that the rand() function is Postgresql compatible.
750+
*
751+
* Usage:
752+
* $query->Rand();
753+
*
754+
* @return string The correct rand function.
755+
*
756+
* @since __DEPLOY_VERSION__
757+
*/
758+
public function Rand()
759+
{
760+
return ' RANDOM() ';
761+
}
762+
763+
/**
764+
* Find a value in a varchar used like a set.
765+
*
766+
* Ensure that the value is an integer before passing to the method.
767+
*
768+
* Usage:
769+
* $query->findInSet((int) $parent->id, 'a.assigned_cat_ids')
770+
*
771+
* @param string $value The value to search for.
772+
*
773+
* @param string $set The set of values.
774+
*
775+
* @return string Returns the find_in_set() postgresql translation.
776+
*
777+
* @since __DEPLOY_VERSION__
778+
*/
779+
public function findInSet($value, $set)
780+
{
781+
return " $value = ANY (string_to_array($set, ',')::integer[]) ";
782+
}
698783
}

src/Sqlsrv/SqlsrvQuery.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,42 @@ public function group($columns)
270270

271271
return $this;
272272
}
273+
274+
/**
275+
* Return correct rand() function for MSSQL.
276+
*
277+
* Ensure that the rand() function is MSSQL compatible.
278+
*
279+
* Usage:
280+
* $query->Rand();
281+
*
282+
* @return string The correct rand function.
283+
*
284+
* @since __DEPLOY_VERSION__
285+
*/
286+
public function Rand()
287+
{
288+
return ' NEWID() ';
289+
}
290+
291+
/**
292+
* Find a value in a varchar used like a set.
293+
*
294+
* Ensure that the value is an integer before passing to the method.
295+
*
296+
* Usage:
297+
* $query->findInSet((int) $parent->id, 'a.assigned_cat_ids')
298+
*
299+
* @param string $value The value to search for.
300+
*
301+
* @param string $set The set of values.
302+
*
303+
* @return string Returns the find_in_set() Mysql translation.
304+
*
305+
* @since __DEPLOY_VERSION__
306+
*/
307+
public function findInSet($value, $set)
308+
{
309+
return "CHARINDEX(',$value,', ',' + $set + ',') > 0";
310+
}
273311
}

0 commit comments

Comments
 (0)