database->adapter, $config->database->params->toArray()); Qs_Db::$_instance->getConnection()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (isset($config->database->initCommands)) { foreach ($config->database->initCommands->toArray() as $sql) { Qs_Db::$_instance->query($sql); } } } return Qs_Db::$_instance; } public static function getConfig() { if (null === Qs_Db::$_config) { if (!Zend_Registry::isRegistered('config')) { throw new Qs_Db_Exception('config is not registered in Zend_Registry'); } Qs_Db::$_config = Zend_Registry::get('config'); } return Qs_Db::$_config; } public static function getTableName($shortName, $isService = false) { if (!isset(Qs_Db::$_tableNames[(int) $isService][$shortName])) { $config = Qs_Db::getConfig(); Qs_Db::$_tableNames[(int) $isService][$shortName] = $config->database->tablePrefix . (($isService) ? $config->database->servicePrefix : '') . $shortName; } return Qs_Db::$_tableNames[(int) $isService][$shortName]; } public static function filter(Zend_Db_Select $select, array $filter, $alias) { if (empty($filter)) { return; } $tableInfo = Qs_Array::get($select->getPart(Zend_Db_Select::FROM), $alias); if (null === $tableInfo) { throw new Qs_Db_Exception( 'Alias "' . $alias . '" not found in select "FROM" part' . print_r($select->getPart(Zend_Db_Select::FROM), true) ); } $table = new Qs_Db_Table(array('name' => $tableInfo['tableName'], 'schema' => $tableInfo['schema'])); $metaData = $table->getMetaData(); foreach ($filter as $field => $value) { if (!array_key_exists($field, $metaData)) { continue; } $_field = $select->getAdapter()->quoteIdentifier($alias . '.' . $field); if (is_null($value)) { $_conditionAndValue = 'IS NULL'; } else { $type = Qs_Db::_getMetaTypeToType(Qs_Array::get($metaData, $field . '[DATA_TYPE]')); $_condition = is_array($value) ? ' IN (?)' : '= ?'; $_conditionAndValue = $select->getAdapter()->quoteInto($_condition, $value, $type); } $select->where($_field . ' ' . $_conditionAndValue); } } /** * Modified version of function Zend_Db_Adapter_Abstract->_whereExpr * * Convert an array, string, or Zend_Db_Expr object * into a string to put in a WHERE clause. * * @param array|string|Zend_Db_Expr $where * @return string */ public static function getWhereSql($where) { if (empty($where)) { return 1; } $db = Qs_Db::getInstance(); $where = (array) $where; foreach ($where as $condition => &$term) { if (is_int($condition)) { if ($term instanceof Zend_Db_Expr) { $term = $term->__toString(); } } else { $term = $db->quoteInto($condition, $term); } $term = '(' . $term . ')'; } $where = implode(' AND ', $where); return $where; } protected static function _getMetaTypeToType($type) { switch (strtolower($type)) { case 'mediumint': // break was intentionally omitted case 'smallint': // break was intentionally omitted case 'tinyint': // break was intentionally omitted case 'bit': // break was intentionally omitted case 'bool': // break was intentionally omitted case 'boolean': // break was intentionally omitted case 'int': $type = Qs_Db::INT_TYPE; break; case 'serial': // break was intentionally omitted case 'bingint': $type = Qs_Db::BIGINT_TYPE; break; case 'decimal': // break was intentionally omitted case 'double': // break was intentionally omitted case 'float': // break was intentionally omitted case 'real': $type = Qs_Db::FLOAT_TYPE; break; default: $type = null; break; } return $type; } public static function getPairExpr($tableAlias, $alias = null) { $pair = self::getPair($tableAlias, $alias); $db = self::getInstance(); return new Zend_Db_Expr($db->quoteIdentifier(current($pair)) . ' AS ' . $db->quoteIdentifier(key($pair))); } public static function getPair($tableAlias, $alias = null) { if (null === $alias) { $alias = $tableAlias; } return array($alias => self::getTableName($tableAlias)); } public static function getField($field, $alias = null) { return ((null === $alias) ? '' : $alias . '.') . $field; } public static function quoteField($field, $alias = null) { return Qs_Db::getInstance()->quoteIdentifier(self::getField($field, $alias)); } }