public function __construct($page_id, ExtendedPdoInterface $connection) { $this->connection = $connection; $page = $connection->fetchOne("SELECT * FROM pages WHERE page_id = :page_id", ['page_id' => $page_id]); if ($page) { $this->fields = $page; if ($page['node_id']) { parent::__construct($page['node_id'], $connection); } } }
/** * Gets cached data. * * @param string $table Table. * @param mixed $cache_key Key. * @param string|null $sql Fallback sql used to populate cache on the fly. * @param array $values Fallback values used together with above sql. * * @return array|boolean */ public function getFromCache($table, $cache_key, $sql = null, array $values = array()) { if (isset($this->_cache[$table][$cache_key])) { return $this->_cache[$table][$cache_key]; } if (isset($sql)) { $result = $this->_database->fetchOne($sql, $values); if ($result !== false) { $this->setIntoCache($table, $cache_key, $result); return $this->_cache[$table][$cache_key]; } } return false; }
/** * Frees consumed memory manually. * * @return void * * @codeCoverageIgnore */ protected function freeMemoryManually() { $profiler = $this->database->getProfiler(); if (is_object($profiler) && $profiler->isActive()) { $profiler->resetProfiles(); } }
/** * {@inheritdoc} */ public function attachRelationships(Collection $collection, $include = null, ServerRequestInterface $request = null) { if (count($collection) === 0) { return; } if (is_null($include)) { return; } $rels = $collection->getIterator()->current()->getRelationshipMap(); $rules = $request instanceof ServerRequestInterface ? $this->parseQueryString($request->getUri()->getQuery()) : []; foreach ($this->getRelationshipMap() as $key => $map) { if (is_array($include) && !in_array($key, $include)) { continue; } $binds = $this->getRelationshipBinds($collection, $key, $map['defined_in']['entity']); if (empty($binds)) { continue; } $query = sprintf('SELECT * FROM %s LEFT JOIN %s ON %s.%s = %s.%s WHERE %s.%s IN (:relationships)', $map['defined_in']['table'], $map['target']['table'], $map['target']['table'], $map['target']['primary'], $map['defined_in']['table'], $map['target']['relationship'], $map['defined_in']['table'], $map['defined_in']['primary']); // @todo allow for further filtering of rels via request if (array_key_exists('sort', $rules)) { $whitelist = []; if (array_key_exists($key, $rels)) { $entity = $rels[$key]; $entity = new $entity(); $mapping = $entity->getMapping(); $whitelist = array_keys($mapping); } $query .= $this->buildSortPart($rules['sort'], $map['target']['table'], $whitelist); } $result = $this->dbal->fetchAll($query, ['relationships' => $binds]); $this->attachRelationshipsToCollection($collection, $key, $result); } }
public function update(array $aWhere, array $aToSet) { $this->formatToDb($aWhere); $this->formatToDb($aToSet); list($sQuery, $aParameters) = $this->buildUpdateQuery($this->get('entity'), $aWhere, $aToSet); try { $oPdoStmt = $this->oPdo->perform($sQuery, $aParameters); } catch (\PDOException $oException) { $sErrMsg = $oException->getMessage() . "\n Entity: " . $this->get('entity') . "\n Query: {$sQuery}" . "\n Parameters: " . print_r($aParameters, true); throw new \RuntimeException($sErrMsg); } return $oPdoStmt; }
/** * Checks, that database table contains given number of records. * * @param string $table_name Table name. * @param integer $expected_record_count Expected record count. * * @return void */ protected function assertTableCount($table_name, $expected_record_count) { $profiler = $this->database->getProfiler(); if (is_object($profiler)) { $profiler->setActive(false); } $sql = 'SELECT COUNT(*) FROM ' . $table_name; $actual_record_count = $this->database->fetchValue($sql); if (is_object($profiler)) { $profiler->setActive(true); } $this->assertEquals($expected_record_count, $actual_record_count, 'The "' . $table_name . '" table contains ' . $expected_record_count . ' records'); }
/** * Adds a relation. * * @param integer $class_id Class ID. * @param string $related_class Related class. * @param integer $relation_type Relation type. * @param boolean $is_internal Is internal. * @param array $old_relations Old relations. * * @return string */ protected function addRelation($class_id, $related_class, $relation_type, $is_internal, array $old_relations) { $insert_sql = ' INSERT INTO ClassRelations (ClassId, RelatedClass, RelatedClassId, RelationType) VALUES (:class_id, :related_class, :related_class_id, :relation_type)'; $update_sql = ' UPDATE ClassRelations SET RelationType = :relation_type WHERE ClassId = :class_id AND RelatedClassId = :related_class_id'; if ($is_internal) { $related_class_id = 0; } else { $related_class_file = realpath(ReflectionEngine::locateClassFile($related_class)); $sql = 'SELECT Id FROM Classes WHERE FileId = :file_id AND Name = :name'; $related_class_id = $this->db->fetchValue($sql, array('file_id' => $this->processFile($related_class_file), 'name' => $related_class)); } $this->db->perform(in_array($related_class, $old_relations) ? $update_sql : $insert_sql, array('class_id' => $class_id, 'related_class' => $related_class, 'related_class_id' => $related_class_id, 'relation_type' => $relation_type)); return $related_class; }
/** * {@inheritdoc} */ public function attachRelationships(Collection $collection, $include = null, ServerRequestInterface $request = null) { if (count($collection) === 0 || is_null($include)) { return; } $bind = []; $rels = $collection->getIterator()->current()->getRelationshipMap(); $rules = $request instanceof ServerRequestInterface ? $this->parseQueryString($request->getUri()->getQuery()) : []; foreach ($this->getRelationshipMap() as $key => $map) { if (!array_key_exists('include', $rules) || array_key_exists('include', $rules) && !array_key_exists($key, $rules['include'])) { continue; } $binds = $this->getRelationshipBinds($collection, $key, $map['defined_in']['entity']); if (empty($binds)) { continue; } $query = sprintf('SELECT * FROM %s LEFT JOIN %s ON %s.%s = %s.%s WHERE %s.%s IN (:relationships)', $map['defined_in']['table'], $map['target']['table'], $map['target']['table'], $map['target']['primary'], $map['defined_in']['table'], $map['target']['relationship'], $map['defined_in']['table'], $map['defined_in']['primary']); $options = array_key_exists('include', $rules) ? $rules['include'][$key] : []; if (!empty($options['filter'])) { $query .= $this->buildRelationshipFilterQueryPart($map, $options['filter']); foreach ($options['filter'] as $filter) { $bind[$filter['binding']] = $filter['value']; } } if (array_key_exists('sort', $rules)) { $whitelist = []; if (array_key_exists($key, $rels)) { $entity = $rels[$key]; $whitelist = array_keys((new $entity())->getMapping()); } $query .= $this->buildSortPart($rules['sort'], $map['target']['table'], $whitelist); } if (array_key_exists('limit', $options) && !is_null($options['limit'])) { $query .= ' LIMIT ' . (int) $options['limit']; } $bind['relationships'] = $binds; $result = $this->dbal->fetchAll($query, $bind); $this->attachRelationshipsToCollection($collection, $key, $result); } }
/** * Adds ref to commit and commit to project. * * @param integer $revision Revision. * @param integer $ref_id Ref ID. * * @return void */ public function addCommitToRef($revision, $ref_id) { $sql = 'INSERT INTO CommitRefs (Revision, RefId) VALUES (:revision, :ref_id)'; $this->database->perform($sql, array('revision' => $revision, 'ref_id' => $ref_id)); }
/** * * Sets a PDO attribute value. * * @param mixed $attribute The PDO::ATTR_* constant. * * @param mixed $value The value for the attribute. * * @return bool * */ public function setAttribute($attribute, $value) { return $this->pdo->setAttribute($attribute, $value); }