Skip to content

Commit

Permalink
在漠漠睡的建议下修改了SQL链式库;
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowsoft committed Oct 25, 2023
1 parent 29837f5 commit f49ebc4
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 69 deletions.
24 changes: 17 additions & 7 deletions tests/phpunit/ClassSQLGlobalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,26 @@ public function testIssue121InitializeNewInstance()
public function testUnion()
{
self::$db = new SQL__MySQL($GLOBALS['zbp']->db);
self::$db->unionall(

$this->assertEquals(
'SELECT * FROM zbp_table UNION ALL SELECT * FROM zbp_table3 UNION ALL SELECT * FROM zbp_table2 ',
self::$db->unionall(
self::$db->select('zbp_table')->sql,
self::$db->select('zbp_table3')->sql,
self::$db->select('zbp_table2')->sql
);
$this->assertEquals('SELECT * FROM zbp_table UNION ALL SELECT * FROM zbp_table2 ', self::$db->sql);
self::$db->union(
)
->sql
);

$this->assertEquals(
'SELECT * FROM zbp_table UNION SELECT * FROM zbp_table2 UNION SELECT * FROM zbp_table3 ',
self::$db->union(
self::$db->select('zbp_table')->sql,
self::$db->select('zbp_table2')->sql
);
$this->assertEquals('SELECT * FROM zbp_table UNION SELECT * FROM zbp_table2 ', self::$db->sql);
self::$db->select('zbp_table2')->sql,
self::$db->select('zbp_table3')->sql
)
->sql
);
}

public function testGroupbyHaving()
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/ClassSQL__MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,18 @@ public function testOption()
->option(array('collate' => 'utf8_general_ci'));
$this->assertEquals('CREATE TEMPORARY TABLE IF NOT EXISTS zbp_post2 ( a int(11) NOT NULL AUTO_INCREMENT, i tinyint(1) NOT NULL DEFAULT \'0\', j char(250) NOT NULL DEFAULT \'\', k varchar(250) NOT NULL DEFAULT \'\', o longtext NOT NULL , p longtext NOT NULL , PRIMARY KEY (a) ) ENGINE=Memory DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;', self::$db->sql);
}

public function testJoin(): void
{
$this->assertEquals(
'SELECT log_ID FROM zbp_post AS p LEFT JOIN zbp_postrelation AS pr ON p.log_ID = pr.pr_PostID LEFT JOIN zbp_postrelation2 AS pr2 ON p.log_ID = pr2.pr_PostID AND p.log_DD = pr2.pr_DD WHERE 1 = 1',
self::$db->selectany('log_ID')
->from(array('zbp_post'=>'p'))
->leftjoin(array('zbp_postrelation'=>'pr'), 'p.log_ID = pr.pr_PostID')
->leftjoin('zbp_postrelation2 AS pr2', array('p.log_ID = pr2.pr_PostID', 'p.log_DD = pr2.pr_DD'))
->where('1 = 1')
->sql
);

}
}
6 changes: 3 additions & 3 deletions zb_system/function/c_system_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*/
define('ZC_VERSION_MAJOR', '1');
define('ZC_VERSION_MINOR', '7');
define('ZC_VERSION_BUILD', '3');
define('ZC_VERSION_COMMIT', '3290');
define('ZC_VERSION_CODENAME', 'Finch');
define('ZC_VERSION_BUILD', '4');
define('ZC_VERSION_COMMIT', '3300');
define('ZC_VERSION_CODENAME', 'Beta');
define('ZC_VERSION', ZC_VERSION_MAJOR . '.' . ZC_VERSION_MINOR . '.' . ZC_VERSION_BUILD . '.' . ZC_VERSION_COMMIT);
if (strcasecmp(ZC_VERSION_CODENAME, 'Beta') == 0 || strcasecmp(ZC_VERSION_CODENAME, 'Alpha') == 0) {
define('ZC_VERSION_DISPLAY', ZC_VERSION_MAJOR . '.' . ZC_VERSION_MINOR . '.' . ZC_VERSION_BUILD . '.' . ZC_VERSION_COMMIT . ' ' . ZC_VERSION_CODENAME);
Expand Down
183 changes: 124 additions & 59 deletions zb_system/function/lib/sql/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ public function __call($callName, $argu)

return $this;
} elseif (in_array($upperKeyword, $this->extendKeyword)) {
$this->extend[$upperKeyword] = $argu;
if (in_array($upperKeyword, array('INNERJOIN', 'LEFTJOIN', 'RIGHTJOIN', 'JOIN', 'FULLJOIN'))) {
$this->extend[$upperKeyword][] = $argu;
} else {
$this->extend[$upperKeyword] = $argu;
}
if ($upperKeyword == 'DISTINCT' || $upperKeyword == 'DISTINCTROW' || $upperKeyword == 'SELECTANY') {
foreach ($argu as $key => $value) {
$this->column($value);
Expand Down Expand Up @@ -875,25 +879,14 @@ protected function buildSelect()

if (array_key_exists('JOIN', $this->extend)) {
$this->buildJOIN();
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
} elseif (array_key_exists('INNERJOIN', $this->extend)) {
$this->buildINNERJOIN();
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
} elseif (array_key_exists('LEFTJOIN', $this->extend)) {
$this->buildLEFTJOIN();
$this->buildON();
} elseif (array_key_exists('RIGHTJOIN', $this->extend)) {
$this->buildRIGHTJOIN();
$this->buildON();
} elseif (array_key_exists('FULLJOIN', $this->extend)) {
$this->buildFULLJOIN();
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}

$this->buildBeforeWhere();
Expand Down Expand Up @@ -1014,18 +1007,34 @@ protected function buildON()
$sql = &$this->pri_sql;
$sql[] = 'ON';
$sql[] = implode(' AND ', $this->extend['ON']);
unset($this->extend['ON']);
}

protected function buildJOIN()
{
$sql = &$this->pri_sql;
$sql[] = 'JOIN';
if (is_array($this->extend['JOIN'][0]) == true) {
$sql[] = key($this->extend['JOIN'][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['JOIN'][0]);
} else {
$sql[] = implode(' ,', $this->extend['JOIN']);
foreach ($this->extend['JOIN'] as $key => $value) {
$sql[] = 'JOIN';
if (is_array($this->extend['JOIN'][$key][0]) == true) {
$sql[] = key($this->extend['JOIN'][$key][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['JOIN'][$key][0]);
} else {
$sql[] = $this->extend['JOIN'][$key][0];
}
if (count($this->extend['JOIN'][$key]) > 1) {
$sql[] = 'ON';
if (is_array($this->extend['JOIN'][$key][1]) == true) {
$array = $this->extend['JOIN'][$key][1];
$sql[] = implode(' AND ', $array);
} else {
$sql[] = $this->extend['JOIN'][$key][1];
}
} else {
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}
}
}

Expand All @@ -1038,52 +1047,112 @@ protected function buildINNERJOIN()
$s = 'STRAIGHT_JOIN';
}
}
$sql[] = $s;
if (is_array($this->extend['INNERJOIN'][0]) == true) {
$sql[] = key($this->extend['INNERJOIN'][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['INNERJOIN'][0]);
} else {
$sql[] = implode(' ,', $this->extend['INNERJOIN']);
foreach ($this->extend['INNERJOIN'] as $key => $value) {
$sql[] = $s;
if (is_array($this->extend['INNERJOIN'][$key][0]) == true) {
$sql[] = key($this->extend['INNERJOIN'][$key][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['INNERJOIN'][$key][0]);
} else {
$sql[] = $this->extend['INNERJOIN'][$key][0];
}
if (count($this->extend['INNERJOIN'][$key]) > 1) {
$sql[] = 'ON';
if (is_array($this->extend['INNERJOIN'][$key][1]) == true) {
$array = $this->extend['INNERJOIN'][$key][1];
$sql[] = implode(' AND ', $array);
} else {
$sql[] = $this->extend['INNERJOIN'][$key][1];
}
} else {
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}
}
}

protected function buildLEFTJOIN()
{
$sql = &$this->pri_sql;
$sql[] = 'LEFT JOIN';
if (is_array($this->extend['LEFTJOIN'][0]) == true) {
$sql[] = key($this->extend['LEFTJOIN'][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['LEFTJOIN'][0]);
} else {
$sql[] = implode(' ,', $this->extend['LEFTJOIN']);
foreach ($this->extend['LEFTJOIN'] as $key => $value) {
$sql[] = 'LEFT JOIN';
if (is_array($this->extend['LEFTJOIN'][$key][0]) == true) {
$sql[] = key($this->extend['LEFTJOIN'][$key][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['LEFTJOIN'][$key][0]);
} else {
$sql[] = $this->extend['LEFTJOIN'][$key][0];
}
if (count($this->extend['LEFTJOIN'][$key]) > 1) {
$sql[] = 'ON';
if (is_array($this->extend['LEFTJOIN'][$key][1]) == true) {
$array = $this->extend['LEFTJOIN'][$key][1];
$sql[] = implode(' AND ', $array);
} else {
$sql[] = $this->extend['LEFTJOIN'][$key][1];
}
} else {
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}
}
}

protected function buildRIGHTJOIN()
{
$sql = &$this->pri_sql;
$sql[] = 'RIGHT JOIN';
if (is_array($this->extend['RIGHTJOIN'][0]) == true) {
$sql[] = key($this->extend['RIGHTJOIN'][0]);
$sql[] = ' AS ';
$sql[] = current($this->extend['RIGHTJOIN'][0]);
} else {
$sql[] = implode(' ,', $this->extend['RIGHTJOIN']);
foreach ($this->extend['RIGHTJOIN'] as $key => $value) {
$sql[] = 'RIGHT JOIN';
if (is_array($this->extend['RIGHTJOIN'][$key][0]) == true) {
$sql[] = key($this->extend['RIGHTJOIN'][$key][0]);
$sql[] = ' AS ';
$sql[] = current($this->extend['RIGHTJOIN'][$key][0]);
} else {
$sql[] = $this->extend['RIGHTJOIN'][$key][0];
}
if (count($this->extend['RIGHTJOIN'][$key]) > 1) {
$sql[] = 'ON';
if (is_array($this->extend['RIGHTJOIN'][$key][1]) == true) {
$array = $this->extend['RIGHTJOIN'][$key][1];
$sql[] = implode(' AND ', $array);
} else {
$sql[] = $this->extend['RIGHTJOIN'][$key][1];
}
} else {
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}
}
}

protected function buildFULLJOIN()
{
$sql = &$this->pri_sql;
$sql[] = 'FULL JOIN';
if (is_array($this->extend['FULLJOIN'][0]) == true) {
$sql[] = key($this->extend['FULLJOIN'][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['FULLJOIN'][0]);
} else {
$sql[] = implode(' ,', $this->extend['FULLJOIN']);
foreach ($this->extend['FULLJOIN'] as $key => $value) {
$sql[] = 'FULL JOIN';
if (is_array($this->extend['FULLJOIN'][$key][0]) == true) {
$sql[] = key($this->extend['FULLJOIN'][$key][0]);
$sql[] = 'AS';
$sql[] = current($this->extend['FULLJOIN'][$key][0]);
} else {
$sql[] = $this->extend['FULLJOIN'][$key][0];
}
if (count($this->extend['FULLJOIN'][$key]) > 1) {
$sql[] = 'ON';
if (is_array($this->extend['FULLJOIN'][$key][1]) == true) {
$array = $this->extend['FULLJOIN'][$key][1];
$sql[] = implode(' AND ', $array);
} else {
$sql[] = $this->extend['FULLJOIN'][$key][1];
}
} else {
if (array_key_exists('ON', $this->extend)) {
$this->buildON();
}
}
}
}

Expand All @@ -1106,9 +1175,14 @@ protected function buildUnion()
{
$sql = &$this->pri_sql;
$sql = array();
$sql[] = $this->extend['UNION'][0];
$sql[] = ' UNION ';
$sql[] = $this->extend['UNION'][1];
$sql[] = implode(' UNION ', $this->extend['UNION']);
}

protected function buildUnionALL()
{
$sql = &$this->pri_sql;
$sql = array();
$sql[] = implode(' UNION ALL ', $this->extend['UNIONALL']);
}

protected function buildTransaction()
Expand All @@ -1127,15 +1201,6 @@ protected function buildTransaction()
}
}

protected function buildUnionALL()
{
$sql = &$this->pri_sql;
$sql = array();
$sql[] = $this->extend['UNIONALL'][0];
$sql[] = ' UNION ALL ';
$sql[] = $this->extend['UNIONALL'][1];
}

protected function buildUpdate()
{
$sql = &$this->pri_sql;
Expand Down

0 comments on commit f49ebc4

Please sign in to comment.