Skip to content

Commit 6f1b3d2

Browse files
committed
where changes
1 parent 1edaf47 commit 6f1b3d2

File tree

3 files changed

+111
-30
lines changed

3 files changed

+111
-30
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Xdebug Cloud",
9+
"type": "php",
10+
"request": "launch",
11+
"xdebugCloudToken": ""
12+
},
13+
{
14+
"type": "chrome",
15+
"request": "launch",
16+
"name": "Launch Chrome against localhost",
17+
"url": "http://localhost:8080",
18+
"webRoot": "${workspaceFolder}"
19+
}
20+
]
21+
}

src/Where.php

Lines changed: 88 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,64 @@ public function add(String $field, $value, String $operator = "=", String $value
4141
{
4242
$operator = "";
4343
}
44-
44+
45+
$will_condition_apply = true;
46+
4547
switch(gettype($value))
4648
{
49+
case "integer":
50+
case "float":
51+
break;
52+
53+
case "boolean":
54+
$value = (int) $value;
55+
break;
56+
4757
case "string":
48-
switch($value_type)
49-
{
50-
case "string":
51-
$value = "'" . $value . "'";
52-
break;
58+
$value = trim($value);
5359

54-
case "date":
55-
$value = date("Y-m-d", strtotime($value));
56-
$value = "'" . $value . "'";
57-
break;
58-
59-
case "datetime":
60-
$value = date("Y-m-d H:i:s", strtotime($value));
61-
$value = "'" . $value . "'";
62-
break;
60+
$null_present = strpos(strtoupper($value), "NULL") >= 0;
6361

64-
case "bool":
65-
case "boolean":
66-
$value = (int) $value;
67-
break;
62+
if (!$null_present)
63+
{
64+
$value = $this->_parseStringValue($value);
65+
if ($value === false)
66+
{
67+
$will_condition_apply = false;
68+
}
6869
}
6970
break;
7071

71-
case "boolean":
72-
case "integer":
73-
$value = (int) $value;
74-
break;
72+
7573

7674
case "array":
77-
$value = "(" . implode(",", $value) . ")";
78-
$operator = "IN";
75+
if (empty($value))
76+
{
77+
$will_condition_apply = false;
78+
break;
79+
}
80+
81+
$arr = $value;
82+
83+
foreach($arr as $k => $v)
84+
{
85+
switch(gettype($v))
86+
{
87+
case "integer":
88+
case "float":
89+
break;
90+
91+
case "string":
92+
$arr[$k] = $v = $this->_parseStringValue($v);
93+
if ($v === false)
94+
{
95+
unset($arr[$k]);
96+
}
97+
break;
98+
}
99+
}
100+
101+
$value = "(" . implode(",", $arr) . ")";
79102
break;
80103

81104
case "NULL":
@@ -87,14 +110,49 @@ public function add(String $field, $value, String $operator = "=", String $value
87110
throw new \Exception("Query Builder : Un-Supported value type :" . gettype($value) );
88111
}
89112

90-
$this->fields[] = array(
91-
"field" => trim($field),
92-
"value" => $value,
93-
"op" => trim($operator)
94-
);
113+
if ($will_condition_apply)
114+
{
115+
$this->fields[] = array(
116+
"field" => trim($field),
117+
"value" => $value,
118+
"op" => trim($operator)
119+
);
120+
}
95121

96122
return $this;
97123
}
124+
125+
private function _parseStringValue($value, $value_type = "string")
126+
{
127+
if (is_string($value) && strlen($value) > 0)
128+
{
129+
switch($value_type)
130+
{
131+
case "string":
132+
$value = "'" . $value . "'";
133+
break;
134+
135+
case "date":
136+
$value = date("Y-m-d", strtotime($value));
137+
$value = "'" . $value . "'";
138+
break;
139+
140+
case "datetime":
141+
$value = date("Y-m-d H:i:s", strtotime($value));
142+
$value = "'" . $value . "'";
143+
break;
144+
145+
case "bool":
146+
case "boolean":
147+
$value = (int) $value;
148+
break;
149+
}
150+
151+
return $value;
152+
}
153+
154+
return false;
155+
}
98156

99157
/**
100158
* @param Array $arr

test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
Where::init("OR")
2828
->add("P.id", NULL, "", "")
2929
->add("O.id", NULL, "", "")
30+
->add("O.id", "IS NOT NULL", "", "")
31+
->add("O.id", ["a", 1], "NOT IN", "")
3032
)
3133
);
3234

0 commit comments

Comments
 (0)