/** * 返回用于查询关联表数据的 SQL 语句 * * @param string $sql * @param string $in * * @return string */ function _getFindSQLBase($sql, $in) { if ($in) { $sql .= " WHERE {$this->qforeignKey} {$in}"; } if ($this->conditions) { if (is_array($this->conditions)) { $conditions = FLEA_Db_SqlHelper::parseConditions($this->conditions, $this->assocTDG); if (is_array($conditions)) { $conditions = $conditions[0]; } } else { $conditions =& $this->conditions; } if ($conditions) { $sql .= " AND {$conditions}"; } } if ($this->sort && $this->countOnly == false) { $sql .= " ORDER BY {$this->sort}"; } return $sql; }
/** * 根据关联表的 counterCache 选项更新统计信息 * * @param array $row */ function _updateCounterCache(&$row) { foreach (array_keys($this->links) as $linkKey) { $link =& $this->links[$linkKey]; /* @var $link FLEA_Db_TableLink */ if ($link->type != BELONGS_TO || !$link->enabled || !$link->counterCache) { continue; } $link->init(); $f = $link->assocTDG->qfield($link->counterCache); if (isset($row[$link->foreignKey])) { $fkv = $this->dbo->qstr($row[$link->foreignKey]); } else { $pkv = $this->dbo->qstr($row[$this->primaryKey]); $sql = "SELECT {$link->foreignKey} FROM {$this->qtableName} WHERE {$this->qpk} = {$pkv}"; $fkv = $this->dbo->getOne($sql); } $conditions = "{$link->qforeignKey} = {$fkv}"; if ($link->conditions) { if (is_array($link->conditions)) { $conditions = FLEA_Db_SqlHelper::parseConditions($link->conditions, $link->assocTDG); if (is_array($conditions)) { $conditions = $conditions[0]; } } else { $conditions =& $link->conditions; } if ($conditions) { $conditions = "{$link->qforeignKey} = {$fkv} AND {$conditions}"; } } $sql = "UPDATE {$link->assocTDG->qtableName} SET {$f} = (SELECT COUNT(*) FROM {$this->qtableName} WHERE {$conditions}) WHERE {$link->assocTDG->qpk} = {$fkv}"; $this->dbo->execute($sql); } }
/** * 分析查询条件,返回 WHERE 子句 * * @param array $conditions * @param boolean $queryLinks * * @return string */ function getWhere($conditions, $queryLinks = true) { // 处理查询条件 $where = FLEA_Db_SqlHelper::parseConditions($conditions, $this); $sqljoin = ''; $distinct = ''; do { if (!is_array($where)) { $whereby = $where != '' ? " WHERE {$where}" : ''; break; } $arr = $where; list($where, $linksWhere) = $arr; unset($arr); if (!$this->autoLink || !$queryLinks) { $whereby = $where != '' ? " WHERE {$where}" : ''; break; } foreach ($linksWhere as $linkid => $lws) { if (!isset($this->links[$linkid]) || !$this->links[$linkid]->enabled) { continue; } $link =& $this->links[$linkid]; /* @var $link FLEA_Db_TableLink */ if (!$link->init) { $link->init(); } $distinct = 'DISTINCT '; switch ($link->type) { case HAS_ONE: case HAS_MANY: /* @var $link FLEA_Db_HasOneLink */ $sqljoin .= "LEFT JOIN {$link->assocTDG->qtableName} ON {$link->mainTDG->qpk} = {$link->qforeignKey} "; break; case BELONGS_TO: /* @var $link FLEA_Db_BelongsToLink */ $sqljoin .= "LEFT JOIN {$link->assocTDG->qtableName} ON {$link->assocTDG->qpk} = {$link->qforeignKey} "; break; case MANY_TO_MANY: /* @var $link FLEA_Db_ManyToManyLink */ $sqljoin .= "INNER JOIN {$link->qjoinTable} ON {$link->qforeignKey} = {$this->qpk} INNER JOIN {$link->assocTDG->qtableName} ON {$link->assocTDG->qpk} = {$link->qassocForeignKey} "; break; } $lw = reset($lws); if (isset($lw[3])) { $whereby = $where != '' ? " WHERE {$where} {$lw[3]} " : ' WHERE'; } else { $whereby = $where != '' ? " WHERE {$where} AND " : ' WHERE'; } foreach ($lws as $lw) { list($field, $value, $op, $expr, $isCommand) = $lw; if (!$isCommand) { $field = $link->assocTDG->qfield($field); $value = $this->dbo->qstr($value); $whereby .= " {$field} {$op} {$value} {$expr}"; } else { $whereby .= " {$value} {$expr}"; } } $whereby = substr($whereby, 0, -(strlen($expr) + 1)); unset($link); } $whereby = " {$sqljoin} {$whereby}"; } while (false); if ($queryLinks) { return array($whereby, $distinct); } else { return $whereby; } }