rdbms: make Database::query() more readable and consistent
Mainly:
* Stash trxLevel as the variable $priorTransaction since
Database::replaceLostConnection might make it 0 when called.
* Factor out Database::beginIfImplied method and call it on
each query attempt of query(), not just the first one.
* Do not bother setting STATUS_TRX_ERROR if a query fails due to
connection issues and was recoverable since requiring ROLLBACK
in order to continue has no real advantage.
* Do not bother setting trxDoneWrites/lastWriteTime for temporary
table operations.
* Make Database::handleTransactionLoss() keep TransactionProfiler
cleaner by calling Database::transactionWritingOut().
Also:
* Make sure Database::wasKnownStatementRollbackError() calls are
right after the corresponding queries so it is easy to follow.
Having connection attempts in between seems fragile.
* Rename Database::doProfiledQuery => Database::attemptQuery and
move more logic to that method.
* Factor out Database::assertNeitherReplicaNorReadOnly method.
* Rename Database::assertOpen => Database::assertHasConnectionHandle.
* Fix wording of Database::wasKnownStatementRollbackError comments.
* Use $isEffectiveWrite variable name instead of $isNonTempWrite
and $isWrite in some places.
Bug: T218226
Change-Id: I2063e4080b41d5fc504f9207a56312ce92130ed7