diff --git a/src/Type/AbstractType.php b/src/Type/AbstractType.php index 215fc94..25e13e2 100644 --- a/src/Type/AbstractType.php +++ b/src/Type/AbstractType.php @@ -37,14 +37,19 @@ protected function interp($string, $context) * * @param Orm $orm The Orm instance * @param string $table The name of tablee - * @param string $sql The sql query which retrieves the column name + * @param string $query The sql query which retrieves the column name * * @throws OrmException * * @return string The column name */ - protected function getPrimaryColumnViaSql(Orm $orm, $table, $sql) + protected function getPrimaryColumnViaSql(Orm $orm, $table, $query) { + $sql = $this->interp($query, array( + 'table' => $table, + 'schema' => $orm->getSchema() + )); + $name = null; try { $stmt = $orm->getConnection()->query($sql); @@ -69,24 +74,24 @@ protected function getPrimaryColumnViaSql(Orm $orm, $table, $sql) } /** - * Lock table or row via sql + * Change the locks on table or row via sql * * @param Orm $orm The Orm instance * @param string $table The table name - * @param string $sql The sql to execute for locking + * @param string $sql The sql to execute for changing the lock level * * @throws OrmException */ - protected function lockViaSql(Orm $orm, $table, $sql) + protected function changeLockViaSql(Orm $orm, $table, $sql) { $connection = $orm->getConnection(); try { if ($connection->exec($sql) === false) { - throw new OrmException("Could not lock table {table}", array('table' => $table)); + throw new OrmException("Could not change lock type table {table}", array('table' => $table)); } } catch (\PDOException $ex) { - throw OrmException::fromPrevious($ex, "Could not lock table"); + throw OrmException::fromPrevious($ex, "Could not change lock type of table"); } } } diff --git a/src/Type/MySQL.php b/src/Type/MySQL.php index 83a1b6b..a4859f1 100644 --- a/src/Type/MySQL.php +++ b/src/Type/MySQL.php @@ -41,12 +41,7 @@ public function getPrimaryKeyColumn($table, Orm $orm) $query = "SELECT `COLUMN_NAME` as column_name FROM `information_schema`.`columns` " . // "WHERE `TABLE_NAME` = '{table}' AND `TABLE_SCHEMA` = '{schema}' AND `COLUMN_KEY` = 'PRI'"; - $sql = $this->interp($query, array( - 'table' => $table, - 'schema' => $orm->getSchema() - )); - - return $this->getPrimaryColumnViaSql($orm, $table, $sql); + return $this->getPrimaryColumnViaSql($orm, $table, $query); } /** @@ -60,7 +55,7 @@ public function lock($table, $lockType, Orm $orm) $lock = "WRITE"; } - $this->lockViaSql($orm, $table, sprintf("LOCK TABLES `%s` %s", $table, $lock)); + $this->changeLockViaSql($orm, $table, sprintf("LOCK TABLES `%s` %s", $table, $lock)); } /** @@ -69,14 +64,7 @@ public function lock($table, $lockType, Orm $orm) */ public function unlock($table, Orm $orm) { - $connection = $orm->getConnection(); - try { - if ($connection->exec("UNLOCK TABLES") === false) { - throw new OrmException("Could not unlock table {table}", array('table' => $table)); - } - } catch(\PDOException $ex) { - throw OrmException::fromPrevious($ex, "Could not unlock table"); - } + $this->changeLockViaSql($orm, $table, "UNLOCK TABLES"); } /** diff --git a/src/Type/Postgres.php b/src/Type/Postgres.php index 1f26055..0b2e599 100644 --- a/src/Type/Postgres.php +++ b/src/Type/Postgres.php @@ -44,12 +44,7 @@ public function getPrimaryKeyColumn($table, Orm $orm) "inner join information_schema.table_constraints tc on ccu.constraint_name = tc.constraint_name " . "where tc.table_catalog = '{schema}' AND tc.table_name = '{table}'"; - $sql = $this->interp($query, array( - 'table' => $table, - 'schema' => $orm->getSchema() - )); - - return $this->getPrimaryColumnViaSql($orm, $table, $sql); + return $this->getPrimaryColumnViaSql($orm, $table, $query); } /** @@ -71,7 +66,7 @@ public function lock($table, $lockType, Orm $orm) $mode ); - $this->lockViaSql($orm, $table, $lockStatement); + $this->changeLockViaSql($orm, $table, $lockStatement); } /**