From f49ebc44f1ea68c32a228ccf77feec14c66d0195 Mon Sep 17 00:00:00 2001 From: rainbowsoft Date: Wed, 25 Oct 2023 17:39:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=BC=A0=E6=BC=A0=E7=9D=A1=E7=9A=84?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE=E4=B8=8B=E4=BF=AE=E6=94=B9=E4=BA=86SQL?= =?UTF-8?q?=E9=93=BE=E5=BC=8F=E5=BA=93;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/phpunit/ClassSQLGlobalTest.php | 24 +++- tests/phpunit/ClassSQL__MySQLTest.php | 14 ++ zb_system/function/c_system_version.php | 6 +- zb_system/function/lib/sql/global.php | 183 ++++++++++++++++-------- 4 files changed, 158 insertions(+), 69 deletions(-) diff --git a/tests/phpunit/ClassSQLGlobalTest.php b/tests/phpunit/ClassSQLGlobalTest.php index dace8be41..bc2d9b22c 100644 --- a/tests/phpunit/ClassSQLGlobalTest.php +++ b/tests/phpunit/ClassSQLGlobalTest.php @@ -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() diff --git a/tests/phpunit/ClassSQL__MySQLTest.php b/tests/phpunit/ClassSQL__MySQLTest.php index b723ae73c..2d6ecb913 100644 --- a/tests/phpunit/ClassSQL__MySQLTest.php +++ b/tests/phpunit/ClassSQL__MySQLTest.php @@ -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 + ); + + } } diff --git a/zb_system/function/c_system_version.php b/zb_system/function/c_system_version.php index 78295bc51..e1a96c9e7 100644 --- a/zb_system/function/c_system_version.php +++ b/zb_system/function/c_system_version.php @@ -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); diff --git a/zb_system/function/lib/sql/global.php b/zb_system/function/lib/sql/global.php index 6f8623be4..11604897d 100644 --- a/zb_system/function/lib/sql/global.php +++ b/zb_system/function/lib/sql/global.php @@ -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); @@ -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(); @@ -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(); + } + } } } @@ -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(); + } + } } } @@ -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() @@ -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;