Rev 3186 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Mapper\Common;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Db\Adapter\Driver\ResultInterface;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use Laminas\Db\Sql\Sql;use Laminas\Db\Sql\Select;use Laminas\Db\Sql\Insert;use Laminas\Db\Sql\Update;use Laminas\Db\Sql\Delete;class MapperCommon{const SQL_DUPLICATE_RECORD = 1062;const SQL_CANNOT_ADD_OR_UPDATE_A_CHILD = 1452;const SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW = 1451;/**** @var AdapterInterface*/protected $adapter;/**** @var Sql*/protected $sql;/**** @var integer*/protected $affectedRows;/**** @var integer*/protected $lastInsertId;/**** @var integer*/protected $errno;/**** @var String*/protected $error;/**** @var MapperCommon*/private static $_instance;/**** @param AdapterInterface $adapter*/public function __construct($adapter){$this->sql = new Sql($adapter);$this->adapter = $adapter;}/**** @param AdapterInterface $adapter* @return MapperCommon*/public static function getInstance($adapter){if(self::$_instance == null) {self::$_instance = new MapperCommon($adapter);}return self::$_instance;}/**** @return Sql*/public function getSql(){return $this->sql;}/**** @return integer*/public function getAffectedRows(){return $this->affectedRows;}/**** @return integer*/public function getErrno(){return $this->errno;}/**** @return string*/public function getError(){return $this->error;}/**** @return integer*/public function getLastInsertId(){return $this->lastInsertId;}private function initRespose(){$this->errno = 0;$this->error = '';$this->affectedRows = 0;$this->lastInsertId = 0;}/**** @param Insert $insert* @return boolean*/protected function executeInsert($insert){$this->initRespose();try{$stmt = $this->sql->prepareStatementForSqlObject($insert);$result = $stmt->execute();if ($result) {$this->lastInsertId = $result->getGeneratedValue();$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($insert->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return false;}/**** @param Update $update* @return boolean*/protected function executeUpdate($update){$this->initRespose();try{$stmt = $this->sql->prepareStatementForSqlObject($update);$result = $stmt->execute();if($result) {$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($update->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return false;}/**** @param Delete $delete* @return boolean*/protected function executeDelete($delete){$this->initRespose();try{$stmt = $this->sql->prepareStatementForSqlObject($delete);$result = $stmt->execute();if($result) {$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($delete->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return false;}protected function executeDeleteUsingSentenceWithParameters($sql, $parameters = []){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result) {$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return false;}/**** @param Select $select* @param Object $prototype* @return array*/protected function executeFetchAllObject($select, $prototype){$this->initRespose();try {$stmt = $this->sql->prepareStatementForSqlObject($select);$result = $stmt->execute();if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$hydrator = new ObjectPropertyHydrator();$records = [];foreach($result as $row){$record = clone $prototype;$hydrator->hydrate($row, $record);array_push($records, $record);}return $records;}}catch(\Exception $e){error_log($select->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return [];}/**** @param Select $select* @return array*/protected function executeFetchAllArray($select){$this->initRespose();try {$stmt = $this->sql->prepareStatementForSqlObject($select);$result = $stmt->execute();if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$records = [];foreach($result as $record){array_push($records, $record);}return $records;}}catch(\Exception $e){error_log($select->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return [];}/**** @param Select $select* @param Object $prototype* @return void|Object*/protected function executeFetchOneObject($select, $prototype){$this->initRespose();try {$select->limit(1);$stmt = $this->sql->prepareStatementForSqlObject($select);$result = $stmt->execute();if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$hydrator = new ObjectPropertyHydrator();$hydrator->hydrate($result->current(), $prototype);return $prototype;}}catch(\Exception $e){error_log($select->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return;}/**** @param Select $select* @return void|array*/protected function executeFetchOneArray($select){$this->initRespose();try {$select->limit(1);$stmt = $this->sql->prepareStatementForSqlObject($select);$result = $stmt->execute();if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {return $result->current();}}catch(\Exception $e){error_log($select->getSqlString($this->adapter->platform) . ' ' . $e->getMessage());$this->processError($e);}return;}/**** @param string $sql* @param array $parameters* @return boolean*/protected function executeInsertUsingSentenceWithParameters($sql, $parameters = []){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result) {$this->lastInsertId = $result->getGeneratedValue();$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return false;}/**** @param string $sql* @param array $parameters* @return boolean*/protected function executeSentenceWithParameters($sql, $parameters = []){$this->initRespose();try{$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if($result) {$this->affectedRows = $result->getAffectedRows();return true;}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return false;}/**** @param Object $prototype* @param string $sql* @param array $parameters* @return array*/protected function executeFetchAllObjectUsingParameters($prototype, $sql, $parameters = []){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$hydrator = new ObjectPropertyHydrator();$records = [];foreach($result as $row){$record = clone $prototype;$hydrator->hydrate($row, $record);array_push($records, $record);}return $records;}}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return [];}/**** @param string $sql* @param array $parameters* @return array*/protected function executeFetchAllArrayUsingParameters($sql, $parameters = [] ){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$records = [];foreach($result as $row){array_push($records, $row);}return $records;;}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return [];}/**** @param Object $prototype* @param string $sql* @param array $parameters* @return void|Object*/protected function executeFetchOneObjectUsingParameters($prototype, $sql, $parameters = []){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {$hydrator = new ObjectPropertyHydrator();$hydrator->hydrate($result->current(), $prototype);return $prototype;}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return;}/**** @param string $sql* @param array $parameters* @return void|array*/protected function executeFetchOneArrayUsingParameters($sql, $parameters = []){$this->initRespose();try {$stmt = $this->adapter->createStatement($sql);$stmt->prepare();$result = $stmt->execute($parameters);if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {return $result->current();}}catch(\Exception $e){error_log($sql . ' (' . implode(',', $parameters) . ') ' . $e->getMessage());$this->processError($e);}return;}private function processError($e){$message = $e->getMessage();if (strpos($message, strval(self::SQL_DUPLICATE_RECORD) ) !== false) {$this->errno = self::SQL_DUPLICATE_RECORD;$this->error = 'ERROR_SQL_DUPLICATE_RECORD';}else if (strpos($message, strval(self::SQL_CANNOT_ADD_OR_UPDATE_A_CHILD) ) !== false) {$this->errno = self::SQL_CANNOT_ADD_OR_UPDATE_A_CHILD ;$this->error = 'ERROR_SQL_CANNOT_ADD_OR_UPDATE_A_CHILD';}else if (strpos($message, strval(self::SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW) ) !== false) {$this->errno = self::SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW;$this->error = 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW';} else {$this->errno = 0;$this->error = 'ERROR_THERE_WAS_AN_ERROR';}}/**** @param array $values* @param boolean $onlyNull* @return array*/protected function removeEmpty($values, $onlyNull = false){if($onlyNull) {$values = array_filter($values, function($v) {return !is_null($v);});} else {$values = array_filter($values, function($v) {return !empty($v);});}unset($values['added_on']);unset($values['updated_on']);return $values;}}