Commit graph

151 commits

Author SHA1 Message Date
Aaron Schulz
da9e4b52a5 rdbms: Add multi-statement query support to Database
Add Database::queryMulti(), which will execute an array of
queries as a batch with minimal roundtrips.

SQLite fallbacks to looping through each statement and
invoking doQuery().

Add QueryStatus class to reduce complexity in Database.

Rewrite doQuery() as doSingleStatementQuery().

Change-Id: I3d51083e36ab06fcc1d94558e51b38e106f71bb9
2022-06-09 11:45:38 +10:00
jenkins-bot
2198eec936 Merge "rdbms: Move selectSQLText to SQLPlatform" 2022-06-02 19:54:08 +00:00
Amir Sarabadani
dd9b44b167 rdbms: Move selectSQLText to SQLPlatform
Bug: T307616
Change-Id: I5218bca6633ad4d2704267e0c40926ca171fb1f7
2022-06-02 21:35:04 +02:00
Aaron Schulz
496332e4ce rdbms: avoid DBReadOnlyRoleError in Database::doFlushSession()
Fix and simplify the flags to executeQuery(). The QUERY_CHANGE_ROWS flag
caused the DB_REPLICA role check to be violated.

Remove the UNLOCK TABLES query since lockTables()/unlockTables() have
since been removed.

Bug: T306632
Change-Id: I8fcf940028abd70c0d66bec1780308d3c2367b61
2022-05-31 23:09:41 +00:00
Tim Starling
472d60d249 rdbms: Deprecate DBO_SSL
Using a bitfield breaks server array template merging in LBFactoryMulti.

The only nice thing about flags compared to boolean constructor
parameters is the existence of setFlag(), clearFlag() and
restoreFlags(). But that is not useful for parameters that are used
during connect.

Bug: T134809
Change-Id: I088ef4a0e4c11a2cfe17415b325b455164004c1e
2022-05-27 14:57:31 +00:00
Amir Sarabadani
d83cbce70e rdbms: Move four more functions from Database to SQLPlatform
Bug: T307616
Change-Id: I0ff6318bcbca79830f013a16597d04d1e97ccf6d
2022-05-20 17:06:49 +02:00
jenkins-bot
f8117f9165 Merge "rdbms: fix Postgres return type for lastErrno()" 2022-05-18 08:38:10 +00:00
Amir Sarabadani
8c833750d0 rdbms: Prepare for moving Database::selectSQLText to SQLPlatform
This moves a lot of methods used in selectSQLText method so the move
would be easier. I tried moving it but the patch basically exploded.

Bug: T307616
Change-Id: Ib8c68b4506055b63c769926d6d7cfd1069d951f4
2022-05-12 07:59:22 +02:00
Amir Sarabadani
332b530e8a rdbms: Move out more functions from Database to SQLPlatform
Bug: T307616
Depends-On: Id1f477f5ca4c837cf50ee5ef450873735b732a42
Change-Id: I63308df985b9d596110718450997565de2b06aca
2022-05-11 16:51:53 +00:00
Aaron Schulz
fbdd56da35 rdbms: fix Postgres return type for lastErrno()
The error codes are strings (e.g. "55P03"). Casting to an integer
breaks comparisons by callers (all internal) and makes error strings
confusing.

Use "00000" instead of 0 to reduce this confusion (does not affect
current callers).

Follow-up to 478c4892.

Change-Id: I3dd8929f4c8e604eef669710e684724977381c74
2022-05-10 17:42:56 -07:00
Amir Sarabadani
ca5b162c23 rdbms: Start using SQLPlatform and move more methods there
The first of many changes decoupling SQL building blocks from Database
class.

Bug: T299691
Depends-On: I5d1d5b9b875bced7bda234f45d6d22ed59db4871
Change-Id: I784e78361f5ee629d31c68629d669ee0ddddf929
2022-05-04 18:34:13 +00:00
Aaron Schulz
9a9f441dc5 rdbms: remove lockTables()/unlockTables()
Bug: T294969
Change-Id: I94a2b1916c3d798f412ad2b89d8c31e09439d6c7
2022-05-03 16:03:57 -07:00
Amir Sarabadani
236a0941c0 rdmbs: Start of SQLPlatform to split out of Database
This is the first step to split parts of Database that doesn't require a
connection and are used for query parts.

Bug: T299691
Change-Id: I140aa4328865994499926f898233867ce383908c
2022-04-24 16:07:44 +00:00
Aaron Schulz
db36853718 rdbms: make automatic connection recovery more robust
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Track session state errors caused by the loss of named locks or
temp tables (e.g. during connection loss). Such errors will prevent
further queries except for rollback() and flushSession(), which must
be issued to resolve the error.

Add flushPrimarySessions() methods to LBFactory/LoadBalancer
and use it in places where session state loss is meant to be
safely aknowledged.

Change-Id: I60532f86e629c83b357d4832d1963eca17752944
2022-04-14 11:09:31 +10:00
jenkins-bot
04426c6a05 Merge "rdbms: rename wasKnownStatementRollbackError() to isKnownStatementRollbackError()" 2022-03-22 23:16:04 +00:00
Aaron Schulz
e0a53cba15 rdbms: rename wasKnownStatementRollbackError() to isKnownStatementRollbackError()
Pass the error number as an argument, similar to isConnectionError()

Fix related mysql documentation links

Change-Id: Id32ef2fd27de65376960de3f5138ffdf7654ff71
2022-03-22 22:59:09 +00:00
Umherirrender
1f71eccf63 phan: Disable null_casts_as_any_type setting
Make phan stricter about null types by setting null_casts_as_any_type to
false (the default in mediawiki-phan-config)
Remaining false positive issues are suppressed.
The suppression and the setting change can only be done together

Bug: T242536
Bug: T301991
Change-Id: I0f295382b96fb3be8037a01c10487d9d591e7e01
2022-03-21 18:25:07 +00:00
Ladsgroup
31c1ca8658 Revert "rdbms: make automatic connection recovery apply to more cases"
This reverts commit 4cac31de4e.

Reason for revert: Blocking the train, reverting the chain.

Change-Id: I7f275b3a25379c6f3256e90947c8eed4b232c0f4
2022-03-17 20:11:10 +01:00
jenkins-bot
8d2e09d2bf Merge "database: Fix various scalar and null types" 2022-03-10 05:56:31 +00:00
Umherirrender
478c4892f6 database: Fix various scalar and null types
Fix return values to match documentation
Provide fallback if no null value is allowed
Return from inside of try/finally makes it clear for static analyzer

Change-Id: I8bdfa453fc56dc50749cbd324b3d0d36e8e7065e
2022-03-09 20:28:00 +01:00
Aaron Schulz
4cac31de4e rdbms: make automatic connection recovery apply to more cases
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Add methods and class constants to track session state errors
caused by the loss of named locks or temp tables. Such errors can
be resolved by a "session flush" method.

Make assertQueryIsCurrentlyAllowed() better distinguish ROLLBACK
queries from ROLLBACK TO SAVEPOINT queries. For some scenarios,
only full tranasction ROLLBACK queries should be allowed.

Add flushSession() method to Database and flushPrimarySessions()
methods to LBFactory/LoadBalancer.

Also:
* Rename wasKnownStatementRollbackError() and make it take the
  error number as an argument, similar to wasConnectionError().
  Add mysql error codes for query timeouts since they only cause
  statement rollbacks.
* Rename wasConnectionError() and mark it as protected. This is an
  internal method with no outside callers.
* Rename wasQueryTimeout(), remove some HHVM-specific code, and
  simplify the arguments.
* Make executeQuery() use a for loop for the query retry logic
  to reduce code duplication.
* Move the error state setting logic in executeQueryAttempt() up
  in order to reduce code duplication.
* Move the beginIfImplied() call in executeQueryAttempt() up to the
  retry loop in executeQuery(). This narrows the executeQueryAttempt()
  concerns to sending a single query and updating tracking fields.
* Make closeConnection() and doHandleSessionLossPreconnect() in
  DatabaseSqlite more consistent with the base class by releasing named locks.
* Mark trxStatus() as @internal.

Bug: T281451
Bug: T293859
Change-Id: I200f90e413b8a725828745f81925b54985c72180
2022-03-09 15:49:38 +11:00
Umherirrender
db9e0391d7 Fix signature of DatabasePostgres::buildGroupConcatField
The method in the postgres layer has an extra argument in the middle,
making the last parameter unused and the join condition is not passed to
build the subquery.
For usage of that function with a join the join condition are missing
and making the subquery returning a cartesian product, in case of change
tags with all existing tags in the wiki.

The code is there since the addition of the function in a974025
It is first used in a in a5500e7 for the change tags

Bug: T195807
Bug: T256401
Change-Id: I4e73e37d176a6368e65aeb21e5dbed14edd24f19
2022-01-22 11:40:28 +01:00
Alexander Vorwerk
78ee5ed0d1 Replace remaining usages of deprecated IDatabase methods
Bug: T286694
Change-Id: I15c782ee8d4137604e27fd05a6b87ce2f9975a07
2022-01-21 00:06:29 +01:00
Aaron Schulz
014369874b rdbms: cleanup the use of QUERY_ flags to query() in Database
Change-Id: I9c538f392687db942250624d53ab564ebb2177a0
2022-01-20 10:55:10 +11:00
Alexander Vorwerk
358bc6ec45 Replace remaining usages of IDatabase::fetchObject()
Bug: T299471
Change-Id: I5acae5e72de2f85fd6e68e391297895c86ffb10e
2022-01-19 01:42:16 +01:00
jenkins-bot
ead3e8d20f Merge "rdbms: Remove a cluster of unused and deprecated functions" 2022-01-04 05:14:16 +00:00
Siddharth VP
191cadb6a5 Fix typos in comments (C-D)
Change-Id: I568fb93b53feb83f026d485136dd0d116d677f4f
2021-12-30 18:00:02 +05:30
Amir Sarabadani
8f4a057d6b rdbms: Remove a cluster of unused and deprecated functions
See RELEASE NOTES for details.

Bug: T286694
Change-Id: I8e2951c037e06d20b2f5854f0046e0ae032d86d3
2021-12-22 17:19:42 +01:00
Aaron Schulz
9b59b6c75c rdbms: Remove redundant use of QUERY_IGNORE_DBO_TRX constant
Change-Id: I254b19aebedc1ccb42fb9e53d93b12d4912b7afb
2021-12-16 18:54:13 +00:00
jenkins-bot
b44af6c975 Merge "rdbms: Drop IDatabase::aggregateValue()" 2021-12-12 02:06:50 +00:00
Aaron Schulz
99bd8bddec rdbms: remove pointless DatabasePostgres::tableName() method
Change-Id: I89d92755c6e662a58dcebe70493a29ecbebf6fbd
2021-12-09 17:52:40 -08:00
Amir Sarabadani
76c761a845 rdbms: Drop IDatabase::aggregateValue()
Unused

Bug: T296960
Change-Id: I098d8e8d47709d2c754c1bb107a9a57e811ec6f4
2021-12-09 15:54:11 +01:00
Aaron Schulz
66ca4ecef0 rdbms: rename Database::wasConnectionError() and make it protected
This method is for internal use only

Change-Id: I4c0bd1530dc76b521ce9cbfc75baf0321ee1e9a0
2021-11-10 15:01:08 -08:00
Aaron Schulz
252d21e037 rdbms: remove unused DatabasePostgres::hasConstraint() method
Aside from having no callers, it is not part of the documented interface
of IDatabase, or even Database.

Change-Id: I85e64fbb6527ab26494d9a5e5cec4bb518878b99
2021-08-03 13:45:13 -07:00
Tim Starling
bc76602493 DBMS-specific ResultWrapper subclasses
Cleanup after the switch of Database::query() to return ResultWrapper
instead of resource.

* Soft-deprecate the IResultWrapper accessors in IDatabase.
* Move relevant DBMS-specific functionality to ResultWrapper subclasses.
  The deprecated methods in IResultWrapper become short and simple.
  ResultWrapper is now abstract (b/c break).
* Move the implementation of fieldName(), numFields() and one of the
  fieldInfo() implementations to the ResultWrapper subclass in order to
  avoid ResultWrapper::unwrap() calls.
* Make Database::doQuery() return a ResultWrapper subclass instead of
  underlying result data, so that the Database parent class does not
  need to be aware of wrapper construction.
* Hard-deprecate ResultWrapper::unwrap(),
  DatabaseMysqlBase::fieldType(), DatabasePostgres::fieldType().
* Fix the inefficient seeking method in SQLite.
* Make FakeResultWrapper extend ResultWrapper with an implementation
  similar to the SQLite one. This is possible because ResultWrapper does
  not depend on IDatabase anymore.
* Resolve fixme in DatabasePostgres: from studying the source,
  neither pg_fetch_object() nor pg_num_rows() can set an error
  retrievable with pg_last_error(). Removed unnecessary warning
  suppression.
* ResultWrapperTest didn't make sense as a unit test anymore, so I
  adapted it as an integration test against the current DBMS.

This change also means that ResultWrapper::key() always gives the
correct offset, even if Iterator methods are not being used.

Bug: T286694
Change-Id: I935835316c0bd7d3d061bd8fde9c9ce99ce756ec
2021-07-21 06:54:26 -07:00
Aaron Schulz
bfda754d35 rdbms: track the acquisition timestamps of named locks in Database
This is useful assigning timestamps to critical section operations
without the risk of second-level clock drift from application servers

Bug: T274174
Change-Id: I2fa8af3632f5edb608113b401c6089d8476b06dd
2021-07-19 12:10:48 -07:00
Aaron Schulz
99d5d2e8cc rdbms: cleanup getServer() and connection parameter fields in Database
Make getServer() always return a string, as documented, even with new
Database::NEW_UNCONNECTED handles that have yet to call open(). If the
'host' parameter to __construct() is ''/null, getServer() now returns
'localhost' instead of null. This avoids problems like fatal errors in
calls to TransactionProfiler::recordConnection().

Use Database constants for "connectionParams" field keys for better
static analysis.

Also:
* Add Database::getServerName() method that returns "readable" server
  names in the style of LoadBalancer::getServerName(). Note that the
  "hostName" field is already passed in from LoadBalancer.
* Migrate most getServer() callers to getServerName() for easier
  debugging and more readable logging.
* Also, normalize Database/LoadBalancer SPI logging context to use
  "db_server" and reduce logging code duplication in LoadBalancer.

Bug: T277056
Change-Id: I00ed4049ebb45edab1ea07561c47e226a423ea3b
2021-05-05 19:44:02 +00:00
Ammarpad
30c207650d Postgres: Deprecate table name remapping parameter and function
These are no longer necessary, with abstract schema, the table
names should now be in sync with the names used in MySQL/SQlite.
Postgres-reserved identifiers need to be quoted where necessary.

Bug: T164898
Change-Id: I3bb11a021062d062ca645291c3c82cc318886bd9
2021-04-26 22:13:16 +01:00
Ammarpad
a4a954a626 rdbms: Fix property type documentations
Change-Id: Id4d3c74ef2e2f3b20573fd527ee9dabdf1d610a5
2021-03-28 21:19:13 +00:00
Ammar Abdulhamid
42ef6cb2f8 rdbms: Remove outer parentheses in insert query for Postgres
On Postgres < 9.4, the query will be sent serially for each row
and so the values (for a row) cannot be enclosed in extra parentheses.
For batched query, it's possible even though not required there too.

Bug: T269964
Change-Id: Id3c0307d8999577529715017bba7ed43811f7fe3
2020-12-14 16:44:57 +00:00
Umherirrender
d5259a9449 rdbms: Remove is_array check on typed argument in DatabasePostgres
Change-Id: I0d5b79c1453c4600cb7351aefd1e07d7d2f59044
2020-06-20 20:52:01 +00:00
Umherirrender
88fe512c22 Pass function name to database functions (rdbms/installer)
Change-Id: Ia37d37f2471960c1d9d04fd9bd86224058e88413
2020-06-07 21:06:47 +02:00
Tim Starling
47a1619027 Remove terminating line breaks from debug messages
A terminating line break has not been required in wfDebug() since 2014,
however no migration was done. Some of these line breaks found their way
into LoggerInterface::debug() calls, where they mess up the formatting
of the debug log.

So, remove terminating line breaks from wfDebug() and
LoggerInterface::debug() calls.

Also:
* Fix the stripping of leading line breaks from the log header emitted
  by Setup.php. This feature, accidentally broken in 2014, allows
  requests to be distinguished in the log file.
* Avoid using the global variable $self.
* Move the logging of the client IP back to Setup.php. It was moved to
  WebRequest in the hopes that it would not always be needed, however
  $wgRequest->getIP() is now called unconditionally a few lines up in
  Setup.php. This means that it is put in its proper place after the
  "start request" message.
* Wrap the log header code in a closure so that variables like $name do
  not leak into global scope.
* In Linker.php, remove a few instances of an unnecessary second
  parameter to wfDebug().

Change-Id: I96651d3044a95b9d210b51cb8368edc76bebbb9e
2020-06-03 12:01:16 +10:00
Aaron Schulz
27cf5ace45 rdbms: add IDatabase::QUERY_* flags to obviate isWriteQuery()
This reduces regex overhead and reliance on brittle assumptions.
This will also be useful for complex write queries involving WITH.
Some RDBMS types allow writes with in the WITH aliases themselves,
in addition to the main query itself. Checking raw SQL strings for
such things would get fairly complex.

Change-Id: I8ac4bc4d671abf02f97e82c5daf7b21271b85e5e
2020-04-28 00:49:11 +00:00
Aaron Schulz
cbc700e186 rbms: optimize and rename truncateTable() to truncate()
Allow truncation of multiple tables. This also provides for
a way to avoid risky keywords like CASCADE for Postgres.

For Postgres, use RESTART IDENTITY, which has been supported
since Postgres 8.4.

Avoid TRUNCATE/DELETE queries for empty temp tables, which is
useful for integrations tests that frequently call this method.

Reorganize and tweak the regexes in Database::getTempWrites().
It now recognizes multi-table DROP/TRUNCATE (Postgres-style).

Change-Id: Idd49f118b20ea5a0f7a3e8c00369aabcd45dd44e
2020-04-21 01:26:18 -07:00
Aaron Schulz
303ec64395 rdbms: convert "Exception" try/catch blocks to "Throwable" or "RuntimeException"
Change-Id: I95c204c5c436c3504dcab9e7fe2ae27936ffb590
2020-04-02 01:14:17 +00:00
jenkins-bot
20a0c8ab5a Merge "rdbms: add QUERY_IGNORE_DBO_TRX to duplicateTableStructure() queries" 2020-03-29 16:42:34 +00:00
Aaron Schulz
d42ad1e215 rdbms: add QUERY_IGNORE_DBO_TRX to duplicateTableStructure() queries
Make related DatabaseSqlite::listTables() method directly use query()
rather than high-level wrapper.

Change-Id: I253f83091fb62fd0a449ac78d8846fb69bb808f6
2020-03-27 01:40:24 +00:00
Aaron Schulz
e1a8c4e2a0 rdbms: add IMaintainableDatabase::truncateTable()
Use it in MediaWikiIntegrationTestCase for resetting tables.

Also create Database::resetSequencesForTable() helper method from
the resetSequenceForTable() methods in the SQLite/Postgres classes.

Change-Id: I20945e20590e69340b1ce75f6bb2f6972375b00c
2020-03-25 01:36:06 +00:00
Aaron Schulz
13b11a946e rdbms: reduce duplication in Database via helper methods
Add several new internal methods to help with wrangling
the various formats that rows, conditions, options, and
unique key lists can come in. Remove now unused method
isMultiRowArray().

Add various sanity checks and logging for parameters to
upsert(), replace(), insert(), and insertSelect().

Move DatabasePostgresTest to the integration/ directory.

Change-Id: If5988a6f0816e8da2cbf2fd612e1a3e3a2e9c52f
2020-03-10 22:26:04 +00:00