示例#1
0
 /**
  * {@inheritdoc}
  * @throws \br\gov\sial\core\persist\exception\PersistException
  * */
 protected function _valid(array $config)
 {
     $require = array('adapter');
     foreach ($require as $key) {
         PersistException::throwsExceptionIfParamIsNull($this->exists($key), sprintf(self::CONFIG_MANDATORY_PROPERTY, $key));
     }
 }
示例#2
0
 /**
  * {@inheritdoc}
  * @throws \br\gov\sial\core\persist\exception\PersistException
  * */
 protected function _valid(array $config)
 {
     $require = array('hostname', 'password', 'port', 'source', 'username');
     foreach ($require as $key) {
         PersistException::throwsExceptionIfParamIsNull($this->exists($key), sprintf(self::CONFIG_MANDATORY_PROPERTY, $key));
     }
 }
示例#3
0
 /**
  * {@inheritdoc}
  * */
 protected function _connect(persistConfig $config)
 {
     # por algum motivo desonhecido existe momomentos que o hash obtido em $config->hash()
     # por isso foi necessario recuperar os valores de $config e coloca-los em um array limpo
     $data = array('adapter' => $config->get('adapter'), 'driver' => $config->get('driver'));
     $hash = md5(json_encode($data));
     if (!isset(self::$_sqlConnn[$hash])) {
         try {
             $resource = new \PDO($config->getDSN());
             $resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
             $resource->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
             $resource->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
             PersistException::throwsExceptionIfParamIsNull($resource, self::CONNECT_CANNOT_CONNECT);
             self::$_sqlConnn[$hash] = $resource;
         } catch (\PDOException $pdoe) {
             # @todo um log com o error do PDO devera ser guardo;
             // @codeCoverageIgnoreStart
             throw new PersistException($pdoe->getMessage(), $pdoe->getCode());
             // @codeCoverageIgnoreEnd
         }
     }
     return self::$_sqlConnn[$hash];
 }
示例#4
0
 /**
  * Fábrica de Persist.
  * @example Persist::factory
  * @code
  * <?php
  *     ...
  *     Persist::factory('\lib\Persist', $persistConfig);
  *     ...
  * ?>
  * @endcode
  * @param [string | Model] $namespace
  * @param  PersistConfig $config
  * @return Persist
  * @throws PersistException
  * */
 public static function factory($namespace = NULL, PersistConfig $config)
 {
     if (is_string($namespace)) {
         self::exists($namespace, TRUE);
         return new $namespace($config);
     }
     PersistException::throwsExceptionIfParamIsNull($namespace instanceof ModelAbstract, self::PERSIST_NAMESPACE_OR_MODEL_REQUIRED);
     # get the funcionality namespace
     $tmpNSFunc = current(explode(self::NAMESPACE_SEPARATOR . 'mvcb', $namespace->getNamespace()));
     $arrNS = explode(self::NAMESPACE_SEPARATOR, $tmpNSFunc);
     # get the funcionality name
     $tmpFuncy = end($arrNS);
     $tpl = '%1$s%2$spersist%2$s%3$s%2$s%4$sPersist';
     $namespace = sprintf($tpl, $tmpNSFunc, self::NAMESPACE_SEPARATOR, $config->get('adapter'), ucfirst($tmpFuncy));
     self::exists($namespace, TRUE);
     # instancia o ModuleDatabasePersist
     $tmpPersist = new $namespace($config);
     return $tmpPersist;
 }
示例#5
0
 /**
  * deleta dados no repositorio
  *
  * @param ValueObjectAbstract $valueObject
  * @return br\gov\sial\core\persist\Persist
  * */
 public function delete(ValueObjectAbstract $valueObject)
 {
     $attrs = $this->_persist->persistAttr($valueObject, 'delete');
     $annon = $valueObject->annotation()->load();
     $fieldData = array();
     $tmpOperator = array('WHERE', 'AND');
     $filter = NULL;
     foreach ($attrs as $attr) {
         $fieldData[$attr->database] = $this->_persist->getValue($valueObject, $attr->get, $attr->type);
         if (TRUE == empty($fieldData[$attr->database]->value)) {
             unset($fieldData[$attr->database]);
             // @codeCoverageIgnoreStart
             continue;
             // @codeCoverageIgnoreEnd
         }
         $filter .= sprintf(' %1$s %2$s = :%2$s', $tmpOperator[(bool) $filter], $attr->database);
     }
     PersistException::throwsExceptionIfParamIsNull(NULL !== $filter, self::PERSIT_PRIMARYKEY_NOT_FOUND);
     // @codeCoverageIgnoreStart
     $queryDelete = sprintf('DELETE FROM %s%s', $annon->entity, $filter);
     return $this->execute($queryDelete, $fieldData);
 }
示例#6
0
 /**
  * @return string[]
  * @throws PersistException
  * */
 public function getClassDoc()
 {
     $comments = parent::getClassDoc();
     $pattern = '/(?<pname>\\w+)="(?P<pval>[^"]+)"/';
     $message = sprintf(self::ANNONTATION_COMMENT_NOT_FOUND, $this->_reflection->getName());
     PersistException::throwsExceptionIfParamIsNull(trim($comments), $message);
     $result = array();
     preg_match_all(self::ANNONTATION_ER_DEF_ENTITY_AND_SCHEMA, $comments, $result);
     $classDef = next($result);
     $comments = array();
     # identificador da definicao
     # @ident(...)
     foreach ($classDef as $key => $ident) {
         # recupera os valores definidos em cada propriedade
         # @...(name="value")
         preg_match_all($pattern, $result[0][$key], $resAnnon);
         if (1 < count($resAnnon['pname'])) {
             $attrs = array();
             foreach ($resAnnon['pval'] as $idx => $val) {
                 $attrs[$resAnnon['pname'][$idx]] = $val;
             }
             $comments[$ident][] = $attrs;
         } else {
             $comments[$ident] = $resAnnon['pval'][0];
         }
     }
     return $comments;
 }
 /**
  * Busca para Graphico Mensal
  * @param ReportValueObject $voReport
  */
 public function update(ValueObjectAbstract $voVinculoFuncional)
 {
     $attrs = $voVinculoFuncional->annotation()->load();
     $query = "UPDATE %s.%s SET %s WHERE %s = :%s";
     $arrCmp = $arrVlw = array();
     $prmKey = NULL;
     $prmPK = NULL;
     foreach ($attrs->attrs as $key => $value) {
         $getMethod = $value->get;
         if (isset($value->primaryKey)) {
             $prmKey = $value->database;
             $prmPK = $key;
             $arrVlw[$key] = (object) array('type' => $value->type, 'value' => $voVinculoFuncional->{$getMethod}());
             continue;
         }
         if ($voVinculoFuncional->{$getMethod}() == "") {
             continue;
         }
         $arrCmp[] = " {$value->database} = :{$key}";
         $arrVlw[$key] = (object) array('type' => $value->type, 'value' => $voVinculoFuncional->{$getMethod}());
     }
     $query = sprintf($query, $attrs->schema, $attrs->entity, implode(',', $arrCmp), $prmKey, $prmPK);
     try {
         PersistException::throwsExceptionIfParamIsNull($prmPK, "A Primary Key não foi informada.");
         return $this->execute($query, $arrVlw);
     } catch (\Exception $excp) {
         throw new PersistException($excp->getMessage());
     }
 }
示例#8
0
 /**
  * Descarta toda as operações pendentes e desfaz a transação.
  *
  * @return Persist
  * @throws PersistException
  * */
 public function rollback()
 {
     PersistException::throwsExceptionIfParamIsNull($this->_resource->rollBack(), self::TRANSATION_ROLLBACK_ERROR);
     return $this;
 }
示例#9
0
 /**
  * {@inheritdoc}
  *
  * @throws PersistException
  * @return ResultSet
  * */
 public function retrieve()
 {
     $this->_adminAuthenticate();
     PersistException::throwsExceptionIfParamIsNull($this->_ldapAuth, self::CONNECT_MANDATORY_AUTH);
     $tmpSource = $this->_hasDirectory() ? "{$this->_directory},{$this->_source}" : $this->_source;
     return new ResultSet($this, ldap_search($this->_resource, $tmpSource, $this->_getFilter(), $this->_ldapParam));
 }
示例#10
0
 /**
  * Recupera o nome da entidade que será persistida.
  *
  * @param \stdClass $annon
  * @return string
  * */
 public function getEntityName(\stdClass $annon)
 {
     # monta o nome da entidade, caso o drvier do banco nao deh suprote a namespace
     # especialize este a classe na pasta de driver especifica
     PersistException::throwsExceptionIfParamIsNull(isset($annon->entity), self::PERSIST_UNREGISTERED_ENTITY);
     $entity = $annon->entity;
     if (isset($annon->schema) && trim($annon->schema)) {
         $entity = "{$annon->schema}.{$annon->entity}";
     }
     return $entity;
 }
示例#11
0
 /**
  * Deleta dados no repositório.
  *
  * @param ValueObjectAbstract $valueObject
  * @return br\gov\sial\core\persist\Persist
  * @throws PersistException
  * */
 public function delete(ValueObjectAbstract $valueObject)
 {
     $annon = $valueObject->annotation()->load();
     $tmpFilter = NULL;
     # tecnica explicada no metodo findByParam
     $tmpOperator = array('WHERE', 'AND');
     foreach ($annon->attrs as $field) {
         if (!isset($field->database) || !isset($field->primaryKey)) {
             // @codeCoverageIgnoreStart
             continue;
             // @codeCoverageIgnoreEnd
         }
         $tmpFilter .= sprintf(' %1$s %2$s = :%2$s', $tmpOperator[(bool) $tmpFilter], $field->database);
         $params[$field->database] = self::_getValue($valueObject, $field->get, $field->type);
     }
     # por padrao nao eh possivel atualizar toda a entidade, se realmente  for necessario realizar
     # esta operacao sobreescreve este metodo
     PersistException::throwsExceptionIfParamIsNull($tmpFilter, sprintf(self::PERSIST_UNAVAILABLE_DELETE_FILTER, $annon->entity));
     $query = sprintf('DELETE FROM %s %s', $annon->entity, $tmpFilter);
     $this->_persist->getConnect()->prepare($query, $params)->update();
 }