/** * Compiles SQL term for use in WHERE clauses to select records matching all * properties in given filter. * * Resulting term is using parameter markers for values of those properties * to be matched. These values have to be given on querying data source. * * @param connection $source data source to use for quoting names * of filtering properties * @param array $filter set of "properties" and "values" * @return string SQL-term */ protected function _compileFilterTerm(connection $source, $filter) { return implode(' AND ', array_map(function ($n) use($source) { return $source->quoteName($n) . '=?'; }, $filter['properties'])); }
/** * Writes data on item to be created into provided datasource. * * The invocation of this method is wrapped in a transaction in context of * linked datasource. Thus this method may throw exceptions for rolling back * partial modifications in datasource. * * The method is considered to return array consisting of all properties * and related values used to identify created instances of current model. * * The method must be defined public for internal use in a closure. By design * it should be considered protected. * * @param connection $link link to datasource * @param array $arrProperties properties of new instance to write into datasource * @return array ID-components of created instance (to be used on loading this instance by model::select()) * @throws datasource_exception * @throws model_exception on missing parts of multidimensional ID */ public static function _onCreate(connection $link, $arrProperties) { $set = static::$set_prefix . static::$set; // auto-assign ID to item unless properties include explicit ID if (count(static::$id) === 1) { $idName = static::idName(0); if (!array_key_exists($idName, $arrProperties)) { $arrProperties[$idName] = $link->nextID($set); } } // validate and extract ID of item to create $item = array(); foreach (static::$id as $name) { if (array_key_exists($name, $arrProperties)) { $item[$name] = $arrProperties[$name]; } else { throw new model_exception('missing properties of created item\'s identifier'); } } // prepare SQL statement for inserting record on new item $columns = array_keys($arrProperties); $values = array_values($arrProperties); $marks = array_pad(array(), count($arrProperties), '?'); $columns = array_map(function ($n) use($link) { return $link->quoteName($n); }, $columns); $qSet = $link->qualifyDatasetName($set); $columns = implode(',', $columns); $marks = implode(',', $marks); // query datasource for inserting record of new item if ($link->test("INSERT INTO {$qSet} ({$columns}) VALUES ({$marks})", $values) === false) { throw new datasource_exception($link, 'failed to create item in datasource, model ' . $set); } return $item; }
/** * Qualifies and/or quotes provided property names. * * @param array $arrNames set of property names to process * @param string|null $strSetOrAlias name/alias of data set, provide for qualified names, omit for unqualified names * @param connection $source used optionally to quote names for use in querying connected data source * @return array set of qualified and/or quoted names of properties */ protected function _qualifyNames($arrNames, $strSetOrAlias = null, connection $source = null) { $parts = array(); if (is_string($strSetOrAlias)) { $parts[] = $source ? $source->qualifyDatasetName(trim($strSetOrAlias)) : $strSetOrAlias; } foreach ($arrNames as $key => $name) { $temp = $parts; $temp[] = $source ? $source->quoteName(trim($name)) : trim($name); $arrNames[$key] = implode('.', $temp); } return $arrNames; }
/** * Retrieves name of data set optionally combined with declared alias. * * On providing datasource connection either part of resulting name is * quoted (and qualified) according to quoting and qualification rules of * connected datasource. * * @param connection $db * @return string */ public function getFullName(connection $db = null) { $name = $this->getName(true); $alias = $this->alias ? $this->alias : null; if ($db) { $name = $db->qualifyDatasetName($name); if ($alias) { $alias = $db->quoteName($alias); } } return $alias ? $name . ' ' . $alias : $name; }