/** * @return bool */ public function evaluate() { $statement = DB::get()->from($this->arguments['model']); foreach ($this->match_fields as $column => $input_key) { $statement->where($column . ' = ?', $this->validator[$input_key]); } $this->instance = $statement->fetchRow(); return $this->instance instanceof Model; }
/** * @param \Wave\Config\Row $config */ public function __construct(ConfigRow $config) { /** @var DriverInterface $driver_class */ $driver_class = DB::getDriverClass($config->driver); $this->driver_class = $driver_class; $options = array(); if (isset($config->driver_options)) { // driver_options can be in two formats, either a key => value array (native) // or an array of [ key, value ], used to preserve types (i.e. integers as keys) if (isset($config->driver_options[0])) { foreach ($config->driver_options as $option) { list($key, $value) = $option; $options[$key] = $value; } } else { $options = $config->driver_options->getArrayCopy(); } } parent::__construct($driver_class::constructDSN($config), $config->username, $config->password, $options); $this->cache_enabled = isset($config->enable_cache) && $config->enable_cache; //Override the default PDOStatement $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('\\Wave\\DB\\Statement', array($this))); }
public static function getRelations(DB\Table $table) { //using namespace for the table identifier as there might be same name DBs on different servers $namespace = $table->getDatabase()->getNamespace(); $relation_cache = self::_getRelationCache($table); $relations = array(); //may not be any constraints if ($relation_cache !== null) { foreach ($relation_cache as $cached_row) { //--- check both ends of the relation can be built. $local_db = DB::getByDatabaseName($cached_row['TABLE_SCHEMA']); if ($local_db === null) { Wave\Log::write('mysql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['TABLE_SCHEMA']), Wave\Log::WARNING); continue; } $local_column = $local_db->getColumn($cached_row['TABLE_NAME'], $cached_row['COLUMN_NAME']); //skip if there's no referenced schema. This is because primary keys will be in the relation cache (no ref schema) if ($cached_row['REFERENCED_TABLE_SCHEMA'] === null) { continue; } $referenced_db = DB::getByDatabaseName($cached_row['REFERENCED_TABLE_SCHEMA']); if ($referenced_db === null) { Wave\Log::write('mysql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['REFERENCED_TABLE_SCHEMA']), Wave\Log::WARNING); continue; } $referenced_column = $referenced_db->getColumn($cached_row['REFERENCED_TABLE_NAME'], $cached_row['REFERENCED_COLUMN_NAME']); if ($referenced_column === null) { Wave\Log::write('mysql_driver', sprintf('Column [%s] is not referenced in the configuration - skipping building relations.', $cached_row['REFERENCED_COLUMN_NAME']), Wave\Log::WARNING); continue; } //----- if ($cached_row['REFERENCED_TABLE_SCHEMA'] != $cached_row['TABLE_SCHEMA']) { //print_r($cached_row); //exit; } $relation = DB\Relation::create($local_column, $referenced_column, $cached_row['CONSTRAINT_NAME'], isset($cached_row['REVERSE'])); if ($relation !== null) { $relations[$relation->getIdentifyingName()] = $relation; } else { Wave\Log::write('mysql_driver', sprintf('[%s.%s.%s] has duplicate relations.', $cached_row['TABLE_SCHEMA'], $cached_row['TABLE_NAME'], $cached_row['COLUMN_NAME']), Wave\Log::WARNING); } } } return $relations; }
/** * @param $relation_name * @param Model $object * @param bool $remove_relation * @return bool * @throws Exception * @throws Wave\Exception */ public function _removeRelationObject($relation_name, Model $object, $remove_relation = true) { if (isset($this->_data[$relation_name])) { foreach ($this->_data[$relation_name] as $key => $relation) { if ($relation->_getFingerprint() === $object->_getFingerprint()) { //Unset unset($this->_data[$relation_name][$key]); //Renumber (so it doesn't accidentally map later down the track). $this->_data[$relation_name] = array_values($this->_data[$relation_name]); } } } if ($remove_relation) { $relation_data = $this->_getRelation($relation_name); switch ($relation_data['relation_type']) { case Relation::MANY_TO_MANY: $related_class = $relation_data['related_class']; $db = Wave\DB::get($related_class::_getDatabaseNamespace()); $query = $db->from($related_class, $from_alias); //Add in all of the related columns foreach ($relation_data['local_columns'] as $position => $local_column) { //At this point, may as well return as there aren't null-relations if ($this->_data[$local_column] === null) { return false; } $query->where(sprintf('%s.%s = ?', $from_alias, $db->escape($relation_data['related_columns'][$position])), $this->_data[$local_column]); } //Add in all of the related columns foreach ($relation_data['target_relation']['related_columns'] as $position => $related_column) { //At this point, may as well return as there aren't null-relations if ($object->{$related_column} === null) { return false; } $query->where(sprintf('%s.%s = ?', $from_alias, $db->escape($relation_data['target_relation']['local_columns'][$position])), $object->{$related_column}); } if ($rc = $query->fetchRow()) { return $rc->delete(); } break; case Relation::ONE_TO_MANY: foreach ($relation_data['related_columns'] as $position => $related_column) { $object->{$related_column} = null; } return $object->save(); break; } } }
/** * @param bool $with_namespace * * @return string */ public function getClassName($with_namespace = false) { $prefix = $with_namespace ? '\\' . $this->database->getNamespace() . '\\' : ''; return $prefix . Wave\Inflector::camelize($this->table); }
/** * @param \Wave\DB $database * * @return string */ private static function getModelPath(Wave\DB $database) { $namespace = $database->getNamespace(false); $model_directory = Wave\Config::get('wave')->path->models; return $model_directory . DS . $namespace . DS; }
public static function getRelations(DB\Table $table) { //using namespace for the table identifier as there might be same name DBs on different servers $namespace = $table->getDatabase()->getNamespace(); $relation_cache = self::_getRelationCache($table); $relations = array(); //may not be any constraints if ($relation_cache !== null) { foreach ($relation_cache as $cached_row) { //--- check both ends of the relation can be built. $local_db = DB::getByConfig(array('database' => $cached_row['table_catalog'], 'schema' => $cached_row['table_schema'])); if ($local_db === null) { Log::write('pgsql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['table_catalog']), Log::WARNING); continue; } $local_column = $local_db->getColumn($cached_row['table_name'], $cached_row['column_name']); //skip if there's no referenced schema. This is because primary keys will be in the relation cache (no ref schema) if ($cached_row['referenced_table_schema'] === null) { continue; } $referenced_db = DB::getByConfig(array('database' => $cached_row['referenced_table_catalog'], 'schema' => $cached_row['referenced_table_schema'])); if ($referenced_db === null) { Log::write('pgsql_driver', sprintf('Database [%s] is not referenced in the configuration - skipping building relations.', $cached_row['referenced_table_schema']), Log::WARNING); continue; } $referenced_column = $referenced_db->getColumn($cached_row['referenced_table_name'], $cached_row['referenced_column_name']); //----- $relation = DB\Relation::create($local_column, $referenced_column, $cached_row['constraint_name'], isset($cached_row['reverse'])); if ($relation !== null) { $relations[$relation->getIdentifyingName()] = $relation; } else { Log::write('mysql_driver', sprintf('[%s.%s.%s] has duplicate relations.', $cached_row['table_schema'], $cached_row['table_name'], $cached_row['column_name']), Log::WARNING); } } } return $relations; }
/** * Escape a value using the PDO escaper for the current connection * @param $text * * @return string */ private function escape($text) { return $this->database->escape($text); }