1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-22 13:30:55 +01:00

Update PhabricatorLiskDAO::chunkSQL() for new %Q semantics

Summary:
Ref T13217. This method is slightly tricky:

  - We can't safely return a string: return an array instead.
  - It no longer makes sense to accept glue. All callers use `', '` as glue anyway, so hard-code that.

Then convert all callsites.

Test Plan: Browsed around, saw fewer "unsafe" errors in error log.

Reviewers: amckinley

Reviewed By: amckinley

Subscribers: yelirekim, PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13217

Differential Revision: https://secure.phabricator.com/D19784
This commit is contained in:
epriestley 2018-11-06 16:57:55 -08:00
parent 315d857a8a
commit da40f80741
21 changed files with 59 additions and 60 deletions

View file

@ -45,7 +45,7 @@ foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (boardPHID, columnPHID, objectPHID, sequence) 'INSERT INTO %T (boardPHID, columnPHID, objectPHID, sequence)
VALUES %Q', VALUES %LQ',
id(new PhabricatorProjectColumnPosition())->getTableName(), id(new PhabricatorProjectColumnPosition())->getTableName(),
$chunk); $chunk);
} }

View file

@ -22,12 +22,12 @@ foreach (new LiskRawMigrationIterator($conn_w, 'file') as $row) {
} }
if ($sql) { if ($sql) {
foreach (PhabricatorLiskDAO::chunkSQL($sql, ', ') as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T 'INSERT INTO %T
(id, mailKey, phid, byteSize, storageEngine, storageFormat, (id, mailKey, phid, byteSize, storageEngine, storageFormat,
storageHandle, dateCreated, dateModified, metadata) VALUES %Q '. storageHandle, dateCreated, dateModified, metadata) VALUES %LQ '.
'ON DUPLICATE KEY UPDATE mailKey = VALUES(mailKey)', 'ON DUPLICATE KEY UPDATE mailKey = VALUES(mailKey)',
$table_name, $table_name,
$chunk); $chunk);

View file

@ -34,10 +34,10 @@ foreach ($chunk_iter as $chunk) {
continue; continue;
} }
foreach (PhabricatorLiskDAO::chunkSQL($sql, ', ') as $sql_chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $sql_chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT IGNORE INTO %T (id, phid) VALUES %Q 'INSERT IGNORE INTO %T (id, phid) VALUES %LQ
ON DUPLICATE KEY UPDATE phid = VALUES(phid)', ON DUPLICATE KEY UPDATE phid = VALUES(phid)',
$diff_table->getTableName(), $diff_table->getTableName(),
$sql_chunk); $sql_chunk);

View file

@ -38,7 +38,7 @@ final class PhabricatorKeyValueDatabaseCache
$conn_w, $conn_w,
'INSERT INTO %T 'INSERT INTO %T
(cacheKeyHash, cacheKey, cacheFormat, cacheData, (cacheKeyHash, cacheKey, cacheFormat, cacheData,
cacheCreated, cacheExpires) VALUES %Q cacheCreated, cacheExpires) VALUES %LQ
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
cacheKey = VALUES(cacheKey), cacheKey = VALUES(cacheKey),
cacheFormat = VALUES(cacheFormat), cacheFormat = VALUES(cacheFormat),

View file

@ -245,7 +245,7 @@ final class PhabricatorCalendarNotificationEngine
$conn, $conn,
'INSERT IGNORE INTO %T 'INSERT IGNORE INTO %T
(eventPHID, targetPHID, utcInitialEpoch, didNotifyEpoch) (eventPHID, targetPHID, utcInitialEpoch, didNotifyEpoch)
VALUES %Q', VALUES %LQ',
$table->getTableName(), $table->getTableName(),
$chunk); $chunk);
} }

View file

@ -106,8 +106,8 @@ final class DiffusionUpdateCoverageConduitAPIMethod
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn, $conn,
'INSERT INTO %T (branchID, pathID, commitID, coverage) VALUES %Q'. 'INSERT INTO %T (branchID, pathID, commitID, coverage) VALUES %LQ'.
' ON DUPLICATE KEY UPDATE coverage=VALUES(coverage)', ' ON DUPLICATE KEY UPDATE coverage = VALUES(coverage)',
$table_name, $table_name,
$chunk); $chunk);
} }

View file

@ -101,11 +101,11 @@ final class DivinerLivePublisher extends DivinerPublisher {
$strings[] = qsprintf($conn_w, '%s', $hash); $strings[] = qsprintf($conn_w, '%s', $hash);
} }
foreach (PhabricatorLiskDAO::chunkSQL($strings, ', ') as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($strings) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'UPDATE %T SET graphHash = NULL, nodeHash = NULL 'UPDATE %T SET graphHash = NULL, nodeHash = NULL
WHERE graphHash IN (%Q)', WHERE graphHash IN (%LQ)',
$symbol_table->getTableName(), $symbol_table->getTableName(),
$chunk); $chunk);
} }

View file

@ -189,7 +189,7 @@ final class PhabricatorFactDaemon extends PhabricatorDaemon {
$conn, $conn,
'INSERT INTO %T 'INSERT INTO %T
(keyID, objectID, dimensionID, value, epoch) (keyID, objectID, dimensionID, value, epoch)
VALUES %Q', VALUES %LQ',
$table_name, $table_name,
$chunk); $chunk);
} }

View file

@ -75,7 +75,7 @@ abstract class PhabricatorFactDimension extends PhabricatorFactDAO {
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn, $conn,
'INSERT IGNORE INTO %T (%C) VALUES %Q', 'INSERT IGNORE INTO %T (%C) VALUES %LQ',
$this->getTableName(), $this->getTableName(),
$column, $column,
$chunk); $chunk);

View file

@ -531,7 +531,7 @@ final class ManiphestTransactionEditor
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn, $conn,
'INSERT INTO %T (id, %Q, subpriority) VALUES %Q 'INSERT INTO %T (id, %Q, subpriority) VALUES %LQ
ON DUPLICATE KEY UPDATE subpriority = VALUES(subpriority)', ON DUPLICATE KEY UPDATE subpriority = VALUES(subpriority)',
$task->getTableName(), $task->getTableName(),
implode(', ', array_keys($extra_columns)), implode(', ', array_keys($extra_columns)),

View file

@ -85,7 +85,7 @@ final class PhabricatorUserCache extends PhabricatorUserDAO {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (userPHID, cacheIndex, cacheKey, cacheData, cacheType) 'INSERT INTO %T (userPHID, cacheIndex, cacheKey, cacheData, cacheType)
VALUES %Q VALUES %LQ
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
cacheData = VALUES(cacheData), cacheData = VALUES(cacheData),
cacheType = VALUES(cacheType)', cacheType = VALUES(cacheType)',

View file

@ -467,7 +467,7 @@ final class PhabricatorProject extends PhabricatorProjectDAO
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (projectID, token) VALUES %Q', 'INSERT INTO %T (projectID, token) VALUES %LQ',
$table, $table,
$chunk); $chunk);
} }

View file

@ -161,7 +161,7 @@ final class PhabricatorRepositoryManagementParentsWorkflow
foreach (PhabricatorLiskDAO::chunkSQL($delete_sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($delete_sql) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'DELETE FROM %T WHERE childCommitID IN (%Q)', 'DELETE FROM %T WHERE childCommitID IN (%LQ)',
PhabricatorRepository::TABLE_PARENTS, PhabricatorRepository::TABLE_PARENTS,
$chunk); $chunk);
} }
@ -169,7 +169,7 @@ final class PhabricatorRepositoryManagementParentsWorkflow
foreach (PhabricatorLiskDAO::chunkSQL($insert_sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($insert_sql) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (childCommitID, parentCommitID) VALUES %Q', 'INSERT INTO %T (childCommitID, parentCommitID) VALUES %LQ',
PhabricatorRepository::TABLE_PARENTS, PhabricatorRepository::TABLE_PARENTS,
$chunk); $chunk);
} }

View file

@ -151,7 +151,7 @@ abstract class PhabricatorRepositoryCommitChangeParserWorker
'INSERT INTO %T 'INSERT INTO %T
(repositoryID, pathID, commitID, targetPathID, targetCommitID, (repositoryID, pathID, commitID, targetPathID, targetCommitID,
changeType, fileType, isDirect, commitSequence) changeType, fileType, isDirect, commitSequence)
VALUES %Q', VALUES %LQ',
PhabricatorRepository::TABLE_PATHCHANGE, PhabricatorRepository::TABLE_PATHCHANGE,
$chunk); $chunk);
} }

View file

@ -201,7 +201,7 @@ final class PhabricatorFerretFulltextEngineExtension
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx( queryfx(
$conn, $conn,
'INSERT INTO %T (documentID, ngram) VALUES %Q', 'INSERT INTO %T (documentID, ngram) VALUES %LQ',
$engine->getNgramsTableName(), $engine->getNgramsTableName(),
$chunk); $chunk);
} }

View file

@ -123,7 +123,7 @@ final class PhabricatorSearchManagementNgramsWorkflow
queryfx( queryfx(
$conn, $conn,
'INSERT IGNORE INTO %T (ngram, needsCollection) 'INSERT IGNORE INTO %T (ngram, needsCollection)
VALUES %Q', VALUES %LQ',
$engine->getCommonNgramsTableName(), $engine->getCommonNgramsTableName(),
$chunk); $chunk);
} }

View file

@ -153,7 +153,7 @@ final class PhabricatorSystemActionEngine extends Phobject {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (actorHash, actorIdentity, action, score, epoch) 'INSERT INTO %T (actorHash, actorIdentity, action, score, epoch)
VALUES %Q', VALUES %LQ',
$log->getTableName(), $log->getTableName(),
$chunk); $chunk);
} }

View file

@ -303,7 +303,7 @@ final class PhabricatorCustomFieldList extends Phobject {
foreach (PhabricatorLiskDAO::chunkSQL($sql_list) as $chunk) { foreach (PhabricatorLiskDAO::chunkSQL($sql_list) as $chunk) {
queryfx( queryfx(
$conn_w, $conn_w,
'INSERT INTO %T (objectPHID, indexKey, indexValue) VALUES %Q', 'INSERT INTO %T (objectPHID, indexKey, indexValue) VALUES %LQ',
$table, $table,
$chunk); $chunk);
} }

View file

@ -7,15 +7,15 @@ final class QueryFormattingTestCase extends PhabricatorTestCase {
$this->assertEqual( $this->assertEqual(
'NULL', 'NULL',
qsprintf($conn, '%nd', null)); (string)qsprintf($conn, '%nd', null));
$this->assertEqual( $this->assertEqual(
'0', '0',
qsprintf($conn, '%nd', 0)); (string)qsprintf($conn, '%nd', 0));
$this->assertEqual( $this->assertEqual(
'0', '0',
qsprintf($conn, '%d', 0)); (string)qsprintf($conn, '%d', 0));
$raised = null; $raised = null;
try { try {
@ -29,39 +29,39 @@ final class QueryFormattingTestCase extends PhabricatorTestCase {
$this->assertEqual( $this->assertEqual(
"'<S>'", "'<S>'",
qsprintf($conn, '%s', null)); (string)qsprintf($conn, '%s', null));
$this->assertEqual( $this->assertEqual(
'NULL', 'NULL',
qsprintf($conn, '%ns', null)); (string)qsprintf($conn, '%ns', null));
$this->assertEqual( $this->assertEqual(
"'<S>', '<S>'", "'<S>', '<S>'",
qsprintf($conn, '%Ls', array('x', 'y'))); (string)qsprintf($conn, '%Ls', array('x', 'y')));
$this->assertEqual( $this->assertEqual(
"'<B>'", "'<B>'",
qsprintf($conn, '%B', null)); (string)qsprintf($conn, '%B', null));
$this->assertEqual( $this->assertEqual(
'NULL', 'NULL',
qsprintf($conn, '%nB', null)); (string)qsprintf($conn, '%nB', null));
$this->assertEqual( $this->assertEqual(
"'<B>', '<B>'", "'<B>', '<B>'",
qsprintf($conn, '%LB', array('x', 'y'))); (string)qsprintf($conn, '%LB', array('x', 'y')));
$this->assertEqual( $this->assertEqual(
'<C>', '<C>',
qsprintf($conn, '%T', 'x')); (string)qsprintf($conn, '%T', 'x'));
$this->assertEqual( $this->assertEqual(
'<C>', '<C>',
qsprintf($conn, '%C', 'y')); (string)qsprintf($conn, '%C', 'y'));
$this->assertEqual( $this->assertEqual(
'<C>.<C>', '<C>.<C>',
qsprintf($conn, '%R', new AphrontDatabaseTableRef('x', 'y'))); (string)qsprintf($conn, '%R', new AphrontDatabaseTableRef('x', 'y')));
} }

View file

@ -196,14 +196,11 @@ abstract class PhabricatorLiskDAO extends LiskDAO {
* INSERT, previously built with @{function:qsprintf}) into chunks which will * INSERT, previously built with @{function:qsprintf}) into chunks which will
* fit under the MySQL 'max_allowed_packet' limit. * fit under the MySQL 'max_allowed_packet' limit.
* *
* Chunks are glued together with `$glue`, by default ", ".
*
* If a statement is too large to fit within the limit, it is broken into * If a statement is too large to fit within the limit, it is broken into
* its own chunk (but might fail when the query executes). * its own chunk (but might fail when the query executes).
*/ */
public static function chunkSQL( public static function chunkSQL(
array $fragments, array $fragments,
$glue = ', ',
$limit = null) { $limit = null) {
if ($limit === null) { if ($limit === null) {
@ -216,9 +213,13 @@ abstract class PhabricatorLiskDAO extends LiskDAO {
$chunk = array(); $chunk = array();
$len = 0; $len = 0;
$glue_len = strlen($glue); $glue_len = strlen(', ');
foreach ($fragments as $fragment) { foreach ($fragments as $fragment) {
$this_len = strlen($fragment); if ($fragment instanceof PhutilQueryString) {
$this_len = strlen($fragment->getUnmaskedString());
} else {
$this_len = strlen($fragment);
}
if ($chunk) { if ($chunk) {
// Chunks after the first also imply glue. // Chunks after the first also imply glue.
@ -232,7 +233,7 @@ abstract class PhabricatorLiskDAO extends LiskDAO {
if ($chunk) { if ($chunk) {
$result[] = $chunk; $result[] = $chunk;
} }
$len = strlen($fragment); $len = ($this_len - $glue_len);
$chunk = array($fragment); $chunk = array($fragment);
} }
} }
@ -241,10 +242,6 @@ abstract class PhabricatorLiskDAO extends LiskDAO {
$result[] = $chunk; $result[] = $chunk;
} }
foreach ($result as $key => $fragment_list) {
$result[$key] = implode($glue, $fragment_list);
}
return $result; return $result;
} }

View file

@ -15,13 +15,15 @@ final class LiskChunkTestCase extends PhabricatorTestCase {
$this->assertEqual( $this->assertEqual(
array( array(
'aa', array('a'),
'bb', array('a'),
'ccc', array('b'),
'dd', array('b'),
'e', array('ccc'),
array('dd'),
array('e'),
), ),
PhabricatorLiskDAO::chunkSQL($fragments, '', 2)); PhabricatorLiskDAO::chunkSQL($fragments, 2));
$fragments = array( $fragments = array(
@ -37,11 +39,11 @@ final class LiskChunkTestCase extends PhabricatorTestCase {
$this->assertEqual( $this->assertEqual(
array( array(
'a, a, a', array('a', 'a', 'a'),
'XX, a, a', array('XX', 'a', 'a'),
'a, a', array('a', 'a'),
), ),
PhabricatorLiskDAO::chunkSQL($fragments, ', ', 8)); PhabricatorLiskDAO::chunkSQL($fragments, 8));
$fragments = array( $fragments = array(
@ -55,12 +57,12 @@ final class LiskChunkTestCase extends PhabricatorTestCase {
$this->assertEqual( $this->assertEqual(
array( array(
'xxxxxxxxxx', array('xxxxxxxxxx'),
'yyyyyyyyyy', array('yyyyyyyyyy'),
'a, b, c', array('a', 'b', 'c'),
'zzzzzzzzzz', array('zzzzzzzzzz'),
), ),
PhabricatorLiskDAO::chunkSQL($fragments, ', ', 8)); PhabricatorLiskDAO::chunkSQL($fragments, 8));
} }
} }