From: Tim Otten Date: Fri, 19 May 2023 06:57:39 +0000 (-0700) Subject: CRM_Utils_SQL_Insert - Allow "IGNORE" flag X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3c8cc03ec3cd3c057668483f105ed09b5cb0f2c9;p=civicrm-core.git CRM_Utils_SQL_Insert - Allow "IGNORE" flag --- diff --git a/CRM/Utils/SQL/Insert.php b/CRM/Utils/SQL/Insert.php index 7fc321ca30..bf63e24753 100644 --- a/CRM/Utils/SQL/Insert.php +++ b/CRM/Utils/SQL/Insert.php @@ -22,7 +22,11 @@ */ class CRM_Utils_SQL_Insert { - private $verb = 'INSERT INTO'; + /** + * @var string + * Ex: 'INSERT INTO', 'REPLACE INTO' + */ + private $verb; /** * @var string @@ -45,10 +49,12 @@ class CRM_Utils_SQL_Insert { * * @param string $table * Table-name and optional alias. + * @param string $verb + * Ex: 'INSERT INTO', 'REPLACE INTO' * @return CRM_Utils_SQL_Insert */ - public static function into($table) { - return new self($table); + public static function into($table, string $verb = 'INSERT INTO') { + return new self($table, $verb); } /** @@ -79,9 +85,12 @@ class CRM_Utils_SQL_Insert { * * @param string $table * Table-name and optional alias. + * @param string $verb + * Ex: 'INSERT INTO', 'REPLACE INTO' */ - public function __construct($table) { + public function __construct($table, string $verb = 'INSERT INTO') { $this->table = $table; + $this->verb = $verb; $this->rows = []; } diff --git a/tests/phpunit/CRM/Utils/SQL/InsertTest.php b/tests/phpunit/CRM/Utils/SQL/InsertTest.php index a7d781d55c..1653585ebc 100644 --- a/tests/phpunit/CRM/Utils/SQL/InsertTest.php +++ b/tests/phpunit/CRM/Utils/SQL/InsertTest.php @@ -54,7 +54,19 @@ class CRM_Utils_SQL_InsertTest extends CiviUnitTestCase { (CONCAT(@foo, @bar),"2b") '; $this->assertLike($expected, $insert->toSQL()); + } + public function testInsertIgnore() { + $insert = CRM_Utils_SQL_Insert::into('foo', 'INSERT IGNORE INTO') + ->allowLiterals() + ->row(['first' => new CRM_Utils_SQL_Literal('1+1'), 'second' => '2']) + ->row(['second' => '2b', 'first' => new CRM_Utils_SQL_Literal('CONCAT(@foo, @bar)')]); + $expected = ' + INSERT IGNORE INTO foo (`first`,`second`) VALUES + (1+1,"2"), + (CONCAT(@foo, @bar),"2b") + '; + $this->assertLike($expected, $insert->toSQL()); } }