Skip to content

Commit

Permalink
Remove duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
maikgreubel committed Aug 30, 2015
1 parent 1df772f commit e4ee30f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
19 changes: 12 additions & 7 deletions src/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}
}
}
18 changes: 3 additions & 15 deletions src/Type/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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");
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Type/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

$this->lockViaSql($orm, $table, $lockStatement);
$this->changeLockViaSql($orm, $table, $lockStatement);
}

/**
Expand Down

0 comments on commit e4ee30f

Please sign in to comment.