livestatus: Stop relying on broken short-cut
[ninja.git] / application / helpers / sql.php
blob6212575823a577744f37e54368635cae9ee90965
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * SQL Help class
4 */
5 class sql_Core
7 /**
8 * Parse the limit string and split into
9 * more sql standard LIMIT (val) OFFSET (val)
11 public function limit_parse($str)
13 $str = trim($str);
14 if (empty($str))
15 return false;
16 $limit_str = false;
17 if (strstr($str, ',')) {
18 $limit_parts = explode(',', $str);
19 if (!empty($limit_parts) && count($limit_parts)==2) {
20 $limit = $limit_parts[1];
21 $offset = $limit_parts[0];
22 $limit_str = " LIMIT ".$limit." OFFSET ".$offset;
24 } elseif(is_numeric($str)) {
25 $limit_str = !empty($str) ? ' LIMIT '.(int)$str : '';
28 return $limit_str;
31 /**
32 * Concatenate arguments for use in sql query
33 * Since we are using 3 arguments, this method
34 * handles just this and nothing else.
35 * Arguments 1 and 3 are assumed to be field names
36 * and argument 2 i assumed to be a string.
38 public static function concat($arg1, $arg2, $arg3)
40 switch (Kohana::config('database.default.connection.type'))
42 case 'mysql':
43 return " CONCAT(".$arg1.", '".$arg2."', ".$arg3.") ";
44 break;
45 default:
46 return " ".$arg1."||'".$arg2."'||".$arg3." ";
50 /**
51 * Given one SQL function F and N elements E, return (E_0) F (E_1) F (E_2) F ... (E_N)
52 * Handles empty arguments just fine.
53 * Is helpful for ANDing or ORing long expressions without having to resort to hacks like
54 * WHERE 1=1
56 public static function combine() {
57 $args = func_get_args();
58 $function = array_shift($args);
59 $res = false;
60 foreach ($args as $arg) {
61 if (!empty($arg))
62 $res[] = $arg;
64 if (!empty($res))
65 $res = '(' . implode(') '.$function.' (', $res) . ')';
66 return $res;