Skip to content

Commit

Permalink
Remove more code duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
maikgreubel committed Aug 30, 2015
1 parent 77c1ccb commit 1df772f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ protected function getPrimaryColumnViaSql(Orm $orm, $table, $sql)
}

return $name;
}

/**
* Lock 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
*
* @throws OrmException
*/
protected function lockViaSql(Orm $orm, $table, $sql)
{
$connection = $orm->getConnection();

try {
if ($connection->exec($sql) === false) {
throw new OrmException("Could not lock table {table}", array('table' => $table));
}
} catch (\PDOException $ex) {
throw OrmException::fromPrevious($ex, "Could not lock table");
}
}
}
12 changes: 2 additions & 10 deletions src/Type/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getPrimaryKeyColumn($table, Orm $orm)
'schema' => $orm->getSchema()
));

return parent::getPrimaryColumnViaSql($orm, $table, $sql);
return $this->getPrimaryColumnViaSql($orm, $table, $sql);
}

/**
Expand All @@ -60,15 +60,7 @@ public function lock($table, $lockType, Orm $orm)
$lock = "WRITE";
}

$connection = $orm->getConnection();
$lockStatement = sprintf("LOCK TABLES `%s` %s", $table, $lock);
try {
if ($connection->exec($lockStatement) === false) {
throw new OrmException("Could not lock table {table}", array('table' => $table));
}
} catch (\PDOException $ex) {
throw OrmException::fromPrevious($ex, "Could not lock table");
}
$this->lockViaSql($orm, $table, sprintf("LOCK TABLES `%s` %s", $table, $lock));
}

/**
Expand Down
11 changes: 2 additions & 9 deletions src/Type/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getPrimaryKeyColumn($table, Orm $orm)
'schema' => $orm->getSchema()
));

return parent::getPrimaryColumnViaSql($orm, $table, $sql);
return $this->getPrimaryColumnViaSql($orm, $table, $sql);
}

/**
Expand All @@ -71,14 +71,7 @@ public function lock($table, $lockType, Orm $orm)
$mode
);

$connection = $orm->getConnection();
try {
if ($connection->exec($lockStatement) === false) {
throw new OrmException("Could not lock table {table}", array('table' => $table));
}
} catch (\PDOException $ex) {
throw OrmException::fromPrevious($ex, "Could not lock table");
}
$this->lockViaSql($orm, $table, $lockStatement);
}

/**
Expand Down

0 comments on commit 1df772f

Please sign in to comment.