Skip to content

Commit

Permalink
Move lock methods to BaseRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 21, 2017
1 parent 2391eeb commit f45d637
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 63 deletions.
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ UPGRADE TO 4.0.x

13. Trigger methods like `beforeCreate`, `beforeUpdate`, `afterUpdate` are moved to BaseRepo.

14. `lockWrite` => `writeLock`, `lockRead` => `readLock`


UPGRADE TO 2.0
======================
Expand Down
72 changes: 9 additions & 63 deletions src/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,6 @@ static protected function _stmFetch($stm, $args)
return $obj;
}

/**
* Create a record object from array.
*/
public static function fromArray(array $array)
{
$record = new static;
$record->setData($array);
return $record;
}

/**
* Delete current record, the record should be loaded already.
Expand Down Expand Up @@ -1130,13 +1121,6 @@ public function fetchManyToManyRelationCollection($relationId)
);
}

public function __clone()
{
$d = $this->getData();
$this->setData($d);
$this->autoReload = $this->autoReload;
}

public function asCreateAction(array $args = array(), array $options = array())
{
// the create action requires empty args
Expand Down Expand Up @@ -1179,6 +1163,7 @@ public function getRecordActionClass($type)
return \ActionKit\RecordAction\BaseRecordAction::createCRUDClass($class, $type);
}

// =========================== REPO METHODS ===========================

/**
* defaultRepo method creates the Repo instance class with the default data source IDs
Expand Down Expand Up @@ -1219,7 +1204,7 @@ static public function repo($write = null, $read = null)
* @param Connection $read
* @return BaseRepo
*/
static protected function createRepo($write, $read)
static public function createRepo($write, $read)
{
return new BaseRepo($write, $read);
}
Expand All @@ -1237,52 +1222,13 @@ public function unserialize($str)
$this->setData(unserialize($str));
}


public function lockWrite($alias = null)
{
if (!$alias) {
$alias = $this->alias;
}
// the ::table consts is in the child class.
if ($alias) {
$sql = 'LOCK TABLES '.$this->table.' AS '.$alias.' WRITE';
} else {
$sql = 'LOCK TABLES '.$this->table.' WRITE';
}
$this->getWriteConnection()->query($sql);
}

public function lockRead($alias = null)
{
if (!$alias) {
$alias = $this->alias;
}
// the ::table consts is in the child class.
if ($alias) {
$sql = 'LOCK TABLES '.$this->table.' AS '.$alias.' READ';
} else {
$sql = 'LOCK TABLES '.$this->table.' READ';
}
$this->getReadConnection()->query($sql);
}

public function unlock()
{
$readDsId = $this->readSourceId;
$writeDsId = $this->writeSourceId;
if ($readDsId === $writeDsId) {
$this->getReadConnection()->query('UNLOCK TABLES;');
} else {
$this->getReadConnection()->query('UNLOCK TABLES;');
$this->getWriteConnection()->query('UNLOCK TABLES;');
}
}

public function free()
/**
* Create a record object from array.
*/
public static function fromArray(array $array)
{
if ($this->_preparedCreateStms) {
$this->_preparedCreateStms->closeCursor();
$this->_preparedCreateStms = null;
}
$record = new static;
$record->setData($array);
return $record;
}
}
40 changes: 40 additions & 0 deletions src/BaseRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,46 @@ static public function _validateColumn(RuntimeColumn $column, $val, array $args,
}




// ================= Locks =====================
public function lockWrite($alias = null)
{
if (!$alias) {
$alias = $this->alias;
}
$table = $this->getTable();
if ($alias) {
$this->write->query("LOCK TABLES $table AS $alias WRITE");
} else {
$this->write->query("LOCK TABLES $table WRITE");
}
}

public function lockRead($alias = null)
{
if (!$alias) {
$alias = $this->alias;
}
$table = $this->getTable();
if ($alias) {
$this->read->query("LOCK TABLES $table AS $alias READ");
} else {
$this->read->query("LOCK TABLES $table READ");
}
}

public function unlockAll()
{
if ($this->readSourceId === $this->writeSourceId) {
$this->read->query('UNLOCK TABLES');
} else {
$this->read->query('UNLOCK TABLES');
$this->write->query('UNLOCK TABLES');
}
}


// ================= TRIGGER METHODS ===================

/**
Expand Down

0 comments on commit f45d637

Please sign in to comment.