-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.php
More file actions
95 lines (89 loc) · 2.31 KB
/
Helper.php
File metadata and controls
95 lines (89 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Conditions Helper
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright Copyright (c) 2012 Satoshi Nishimura
*/
require_once __DIR__ . '/Util.php';
require_once __DIR__ . '/Where.php';
/**
* short cut function of sql 'AND'
*
* @package Tsukiyo
*/
function _Tsukiyo_Helper_and(){
return new Tsukiyo_WhereTree('and');
}
/**
* short cut function of sql 'OR'
*
* @package Tsukiyo
*/
function _Tsukiyo_Helper_or(){
return new Tsukiyo_WhereTree('or');
}
/**
* Conditions Helper
*
* Make sub expression of sql
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright Copyright (c) 2012 Satoshi Nishimura
*/
class Tsukiyo_Helper
{
const _AND = '_Tsukiyo_Helper_and';
const _OR = '_Tsukiyo_Helper_or';
// shortcut: extract(Tsukiyo_Helper::$all);
public static $and = self::_AND;
public static $or = self::_OR;
public static $all = array('and' => self::_AND,
'or' => self::_OR);
public static function like($where, $left, $right, $not = false){
$where = self::prepareLike($where, $left, $right);
if ($not)
$op = 'not like';
else
$op = 'like';
return self::where($op, $where);
}
public static function where($op, $where){
$ret = null;
foreach ($where as $k => $v){
$w = new Tsukiyo_WhereNode($op, $k, $v);
if ($ret)
$ret = $ret->add($w);
else
$ret = $w;
}
return $ret;
}
public static function noParamWhere($op, $where){
$ret = null;
$where = (array)$where;
foreach ($where as $k){
$w = new Tsukiyo_WhereNode($op, $k, null, true);
if ($ret)
$ret = $ret->add($w);
else
$ret = $w;
}
return $ret;
}
public static function prepareLike($where, $left, $right){
foreach ($where as $k => $v){
$v = str_replace('\\', '\\\\', $v);
$v = str_replace('%', '\\%', $v);
$v = str_replace('_', '\\_', $v);
if ($left)
$v = '%' . $v;
if ($right)
$v = $v . '%';
$where[$k] = $v;
}
return $where;
}
}