public insertData ( string $query, array $params = [] ) : integer | false | ||
$query | string | The query to execute. |
$params | array | Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
return | integer | false | The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not, and false on failure. |
public function insertData($query, array $params = [])
{
if ($this->logger) {
$this->logger->info("DB query {$query}");
}
$connection = $this->getConnection('write');
$this->invalidateQueryCache();
$this->executeQuery($query, $connection, $params);
return (int) $connection->lastInsertId();
}
/** * Creates a table {prefix}{$namespace}_lock that is used as a mutex. * * @param string $namespace Allows having separate locks for separate processes * @return bool */ public function lock($namespace) { $this->assertNamespace($namespace); if (!$this->isLocked($namespace)) { // Lock it $this->db->insertData("CREATE TABLE {$this->db->getTablePrefix()}{$namespace}_lock (id INT)"); $this->logger->info("Locked mutex for {$namespace}"); return true; } $this->logger->warn("Cannot lock mutex for {$namespace}: already locked."); return false; }