1
0
mirror of https://github.com/beestat/app.git synced 2025-06-01 20:56:44 -04:00

Optimized away a slow query into a transaction.

This commit is contained in:
Jon Ziebell 2021-02-26 14:51:59 -05:00
parent 1c65cd6fcf
commit 089cbb69e3
4 changed files with 35 additions and 13 deletions

View File

@ -19,9 +19,7 @@ final class api_log extends crud {
* @return int The ID of the inserted row. * @return int The ID of the inserted row.
*/ */
public function create($attributes) { public function create($attributes) {
// Insert using the transactionless connection. $this->request->queue_create($this->resource, $attributes);
$database = database::get_transactionless_instance();
return $database->create($this->resource, $attributes);
} }
/** /**

View File

@ -713,7 +713,9 @@ final class database extends \mysqli {
* return the newly created row (does a database read). Specifying id will * return the newly created row (does a database read). Specifying id will
* return just the ID of the created row. * return just the ID of the created row.
* *
* @return int The primary key of the inserted row. * @return mixed Either the primary key of the inserted row or the inserted
* row. If the row is not needed it's faster to return the ID to avoid
* another select.
*/ */
public function create($resource, $attributes, $return_mode = 'row') { public function create($resource, $attributes, $return_mode = 'row') {
$table = $this->get_table($resource); $table = $this->get_table($resource);

View File

@ -99,6 +99,13 @@ final class request {
*/ */
private $current_working_directory; private $current_working_directory;
/**
* A list of create calls queued up to run at the end of the request.
*
* @var array
*/
private $queued_creates = [];
/** /**
* Save the request variables for use later on. If unset, they are defaulted * Save the request variables for use later on. If unset, they are defaulted
* to null. Any of these values being null will throw an exception as soon * to null. Any of these values being null will throw an exception as soon
@ -581,15 +588,21 @@ final class request {
try { try {
$this->total_time = (microtime(true) - $this->begin_timestamp); $this->total_time = (microtime(true) - $this->begin_timestamp);
$database = database::get_instance();
// Fix the current working directory. See documentation on this class // Fix the current working directory. See documentation on this class
// variable for details. // variable for details.
chdir($this->current_working_directory); chdir($this->current_working_directory);
// Run any queued creates.
foreach($this->queued_creates as $queued_create) {
$database->create($queued_create['resource'], $queued_create['attributes'], 'id');
}
// If I didn't catch an error/exception with my handlers, look here...this // If I didn't catch an error/exception with my handlers, look here...this
// will catch fatal errors that I can't. // will catch fatal errors that I can't.
$error = error_get_last(); $error = error_get_last();
if($error !== null) { if($error !== null) {
$this->error_detail['file'] = $error['file']; $this->error_detail['file'] = $error['file'];
$this->error_detail['line'] = $error['line']; $this->error_detail['line'] = $error['line'];
$this->error_detail['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $this->error_detail['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@ -602,7 +615,6 @@ final class request {
); );
try { try {
$database = database::get_instance();
$this->error_detail['queries'] = $database->get_queries(); $this->error_detail['queries'] = $database->get_queries();
} catch(Exception $e) {} } catch(Exception $e) {}
} }
@ -658,7 +670,6 @@ final class request {
); );
try { try {
$database = database::get_instance();
$this->error_detail['queries'] = $database->get_queries(); $this->error_detail['queries'] = $database->get_queries();
} catch(Exception $e) {} } catch(Exception $e) {}
@ -668,4 +679,19 @@ final class request {
} }
} }
/**
* Queue a database create to happen at the end of the request. Generally to
* be used for logging things as these will not be affected by transaction
* rollbacks in the event of an exception.
*
* @param string $resource
* @param array $attributes
*/
public function queue_create($resource, $attributes) {
$this->queued_creates[] = [
'resource' => $resource,
'attributes' => $attributes
];
}
} }

View File

@ -8,8 +8,7 @@
class external_api_log extends cora\crud { class external_api_log extends cora\crud {
/** /**
* Insert an item into the log table using the transactionless database * Queue a create to happen at the end of the request.
* connection.
* *
* @param array $attributes The attributes to insert. * @param array $attributes The attributes to insert.
* *
@ -17,10 +16,7 @@ class external_api_log extends cora\crud {
*/ */
public function create($attributes) { public function create($attributes) {
$attributes['user_id'] = $this->session->get_user_id(); $attributes['user_id'] = $this->session->get_user_id();
$this->request->queue_create($this->resource, $attributes);
// Insert using the transactionless connection.
$database = cora\database::get_transactionless_instance();
return $database->create($this->resource, $attributes, 'id');
} }
} }