Skip to content

Commit f977cbe

Browse files
committed
#118 Fix Error in Conditions & more ConditionsTest
1 parent fd5321a commit f977cbe

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

example/exam04_sql_conditions.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616

1717
$db->enableQueryConditions();
1818

19+
$select='SELECT {ifint lastdays}
20+
21+
event_date>=today()-{lastdays}
22+
23+
{else}
24+
25+
event_date=today()
26+
27+
{/if}';
28+
29+
30+
$statement = $db->selectAsync($select, $input_params);
31+
echo $statement->sql();
32+
echo "\n";
33+
34+
1935

2036
$select = '
2137
SELECT * FROM {from_table}
@@ -105,7 +121,7 @@
105121
{ifint lastdays}
106122
107123
108-
event_date>=today()-{lastdays}
124+
event_date>=today()-{lastdays}-{lastdays}-{lastdays}
109125
110126
111127
{else}
@@ -117,12 +133,12 @@
117133
{/if}
118134
";
119135

120-
121-
$select='{ifint s_empty}NOT_SHOW{/if}
122-
1: {ifbool int1}NOT_SHOW{else}OK{/if}
123-
2: {ifbool int30}NOT_SHOW{else}OK{/if}
124-
125-
';
136+
//
137+
//$select='{ifint s_empty}NOT_SHOW{/if}
138+
//1: {ifbool int1}NOT_SHOW{else}OK{/if}
139+
//2: {ifbool int30}NOT_SHOW{else}OK{/if}
140+
//
141+
//';
126142

127143
$input_params=[
128144
'lastdays'=>3,

src/Query/Degeneration/Conditions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static function __ifsets($matches, $markers)
2828
$content_false = '';
2929
$condition = '';
3030
$flag_else = '';
31-
31+
//print_r($matches);
3232
if (sizeof($matches) == 4) {
3333
list($condition, $preset, $variable, $content_true) = $matches;
3434
} elseif (sizeof($matches) == 6) {
@@ -75,13 +75,17 @@ public function process($sql)
7575
$markers = $this->bindings;
7676

7777
// ------ if/else conditions & if[set|int]/else conditions -----
78-
$sql = preg_replace_callback('#\{if(.{0,}?)\s([^\}]+?)}([^\{]+?)(\{else\}([^\{]+?)?)?\s*\{\/if}#sui', function ($matches) use ($markers) {
78+
$sql = preg_replace_callback('#\{if(.{0,}?)\s+([^\}]+?)\}(.+?)(\{else\}([^\{]+?)?)?\s*\{\/if}#sui', function ($matches) use ($markers) {
7979
return self::__ifsets($matches, $markers);
8080
}
8181
, $sql);
8282

8383
return $sql;
8484

85+
/*
86+
* $ifint var ELSE {ENDIF}
87+
*
88+
*/
8589

8690
// stackoverflow
8791
// if(whatever) { } else { adsffdsa } else if() { }

tests/ConditionsTest.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ private function getInputParams()
3030

3131
private function condTest($sql,$equal)
3232
{
33+
$equal=$equal.' FORMAT JSON';
3334
$input_params=$this->getInputParams();
35+
// echo "-----\n".$this->client->selectAsync($sql, $input_params)->sql()."\n----\n";
36+
3437
$this->assertEquals($equal,$this->client->selectAsync($sql, $input_params)->sql());
38+
39+
3540
}
3641
/**
3742
*
@@ -106,32 +111,34 @@ public function testSqlConditionsBig()
106111
$this->restartClickHouseClient();
107112
$this->client->enableQueryConditions();
108113
$input_params=$this->getInputParams();
114+
115+
109116
$this->assertNotContains(
110117
'NOT_SHOW',$this->client->selectAsync($select, $input_params)->sql()
111118
);
112119

113-
114120
}
115121
public function testSqlConditions1()
116122
{
117123
$this->restartClickHouseClient();
118124
$this->client->enableQueryConditions();
119125

120-
$this->condTest('{ifint s_empty}NOT_SHOW{/if}{ifbool int1}NOT_SHOW{else}OK{/if}{ifbool int30}NOT_SHOW{else}OK{/if}','OKOK FORMAT JSON');
121-
$this->condTest('{ifbool false}OK{/if}{ifbool true}OK{/if}{ifbool true}OK{else}NOT_SHOW{/if}','OKOK FORMAT JSON');
122-
$this->condTest('{ifstring s_empty}NOT_SHOW{else}OK{/if}{ifstring s_null}OK{else}NOT_SHOW{/if}','OKOK FORMAT JSON');
123-
$this->condTest('{ifint int1} OK {/if}',' OK FORMAT JSON');
124-
$this->condTest('{ifint s_empty}NOT_SHOW{/if}_1_','_1_ FORMAT JSON');
125-
$this->condTest('1_{ifint str0} NOT_SHOW {else}OK{/if}_2','1_OK_2 FORMAT JSON');
126-
$this->condTest('1_{if zero}OK{/if}_2','1_OK_2 FORMAT JSON');
127-
$this->condTest('1_{if empty}OK{/if}_2','1__2 FORMAT JSON');
128-
$this->condTest('1_{if s_false}OK{/if}_2','1_OK_2 FORMAT JSON');
129-
$this->condTest('1_{if qwert}NOT_SHOW{/if}_2','1__2 FORMAT JSON');
130-
$this->condTest('1_{ifset zero} NOT_SHOW {else}OK{/if}{ifset false} NOT_SHOW {/if}{ifset s_false} OK {/if}_2','1_OK OK_2 FORMAT JSON');
131-
$this->condTest('1_{ifint zero} NOT_SHOW {/if}{if zero}OK{/if}{ifint s_empty}NOT_SHOW{/if}_2','1_OK_2 FORMAT JSON');
132-
$this->condTest('1_{ifint s_null}NOT_SHOW{/if}{ifset null} NOT_SHOW {/if}_2','1__2 FORMAT JSON');
133-
134-
126+
$this->condTest('{ifint s_empty}NOT_SHOW{/if}{ifbool int1}NOT_SHOW{else}OK{/if}{ifbool int30}NOT_SHOW{else}OK{/if}','OKOK');
127+
$this->condTest('{ifbool false}OK{/if}{ifbool true}OK{/if}{ifbool true}OK{else}NOT_SHOW{/if}','OKOK');
128+
$this->condTest('{ifstring s_empty}NOT_SHOW{else}OK{/if}{ifstring s_null}OK{else}NOT_SHOW{/if}','OKOK');
129+
$this->condTest('{ifint int1} OK {/if}',' OK');
130+
$this->condTest('{ifint s_empty}NOT_SHOW{/if}_1_','_1_');
131+
$this->condTest('1_{ifint str0} NOT_SHOW {else}OK{/if}_2','1_OK_2');
132+
$this->condTest('1_{if zero}OK{/if}_2','1_OK_2');
133+
$this->condTest('1_{if empty}OK{/if}_2','1__2');
134+
$this->condTest('1_{if s_false}OK{/if}_2','1_OK_2');
135+
$this->condTest('1_{if qwert}NOT_SHOW{/if}_2','1__2');
136+
$this->condTest('1_{ifset zero} NOT_SHOW {else}OK{/if}{ifset false} NOT_SHOW {/if}{ifset s_false} OK {/if}_2','1_OK OK_2');
137+
$this->condTest('1_{ifint zero} NOT_SHOW {/if}{if zero}OK{/if}{ifint s_empty}NOT_SHOW{/if}_2','1_OK_2');
138+
$this->condTest('1_{ifint s_null}NOT_SHOW{/if}{ifset null} NOT_SHOW {/if}_2','1__2');
139+
$this->condTest("{ifint lastdays}\n\n\nevent_date>=today()-{lastdays}-{lastdays}-{lastdays}\n\n\n{else}\n\n\nevent_date>=today()\n\n\n{/if}", "\n\n\nevent_date>=today()-3-3-3\n\n\n");
140+
$this->condTest("1_{ifint lastdays}\n2_{lastdays}_\t{int1}_{str0}_{str1}\n_6{else}\n\n{/if}", "1_\n2_3_\t1_0_1\n_6");
141+
$this->condTest("1_{ifint qwer}\n\n\n\n_6{else}\n{int1}{str0}{str1}\n{/if}\n_77", "1_\n101\n_77");
135142

136143

137144
}

0 commit comments

Comments
 (0)