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
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
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
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
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
The first of many changes decoupling SQL building blocks from Database
class.
Bug: T299691
Depends-On: I5d1d5b9b875bced7bda234f45d6d22ed59db4871
Change-Id: I784e78361f5ee629d31c68629d669ee0ddddf929
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
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
Pass the error number as an argument, similar to isConnectionError()
Fix related mysql documentation links
Change-Id: Id32ef2fd27de65376960de3f5138ffdf7654ff71
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
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
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
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
Aside from having no callers, it is not part of the documented interface
of IDatabase, or even Database.
Change-Id: I85e64fbb6527ab26494d9a5e5cec4bb518878b99
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
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
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
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
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
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
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
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
Make related DatabaseSqlite::listTables() method directly use query()
rather than high-level wrapper.
Change-Id: I253f83091fb62fd0a449ac78d8846fb69bb808f6
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
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