示例#1
0
文件: Dao.php 项目: hisaboh/w2t
 protected final function parse_column_annotation()
 {
     if (!isset($this->_class_id_)) {
         $this->_class_id_ = $this->_class_;
     }
     if (isset(self::$DAO[$this->_class_id_])) {
         $this->_columns_ = self::$DAO[$this->_class_id_]->columns;
         $this->_self_columns_ = self::$DAO[$this->_class_id_]->self_columns;
         $this->_conds_ = self::$DAO[$this->_class_id_]->conds;
         $this->_has_many_conds_ = self::$DAO[$this->_class_id_]->has_many_conds;
         $this->_alias_ = self::$DAO[$this->_class_id_]->alias;
         foreach (self::$DAO[$this->_class_id_]->has_dao as $name => $dao) {
             $this->{$name}($dao);
         }
         return;
     }
     $count = $this->_class_id_ === $this->_class_ ? 0 : (int) substr($this->_class_id_, strpos($this->_class_id_, "___") + 3);
     $has_hierarchy = isset($this->_hierarchy_) ? $this->_hierarchy_ - 1 : $this->_has_hierarchy_;
     $root_table_alias = "t" . $count++;
     $has_dao = array();
     foreach ($this->access_members() as $name => $value) {
         if ($this->a($name, "extra") !== "true") {
             $anon_cond = $this->a($name, "cond");
             $column_type = $this->a($name, "type");
             $column = new Column();
             $column->name($name);
             $column->column($this->a($name, "column", $name));
             $column->column_alias("c" . $count++);
             if ($anon_cond === null) {
                 if (class_exists($column_type) && is_subclass_of($column_type, "Dao")) {
                     throw new Exception("undef " . $name . " annotation 'cond'");
                 }
                 $column->table($this->_table_);
                 $column->table_alias($root_table_alias);
                 $column->primary($this->a($name, "primary"));
                 $column->auto($column_type === "serial");
                 $this->_self_columns_[$name] = $this->_columns_[] = $column;
                 $this->_alias_[$column->column_alias()] = $name;
             } else {
                 if (false !== strpos($anon_cond, "(")) {
                     $is_has = class_exists($column_type) && is_subclass_of($column_type, "Dao");
                     $is_has_many = $is_has && $this->a($name, "attr") === "a";
                     if ((!$is_has || $has_hierarchy > 0) && preg_match("/^(.+)\\((.*)\\)(.*)\$/", $anon_cond, $match)) {
                         list(, $self_var, $conds_string, $has_var) = $match;
                         list($self_var, $conds_string, $has_var) = Text::trim($self_var, $conds_string, $has_var);
                         $conds = array();
                         $ref_table = $ref_table_alias = null;
                         if (!empty($conds_string)) {
                             foreach (explode(",", $conds_string) as $key => $cond) {
                                 $tcc = explode(".", $cond, 3);
                                 if (sizeof($tcc) === 3) {
                                     list($t, $c1, $c2) = $tcc;
                                     $ref_table = $this->set_table_name($t);
                                     $ref_table_alias = "t" . $count++;
                                     $conds[] = Column::cond_instance($c1, "c" . $count++, $ref_table, $ref_table_alias);
                                     $conds[] = Column::cond_instance($c2, "c" . $count++, $ref_table, $ref_table_alias);
                                 } else {
                                     list($t, $c1) = $tcc;
                                     $ref_table = $this->set_table_name($t);
                                     $ref_table_alias = "t" . $count++;
                                     $conds[] = Column::cond_instance($c1, "c" . $count++, $ref_table, $ref_table_alias);
                                 }
                             }
                         }
                         if ($is_has_many) {
                             $this->a($name, "has_many", true);
                             $dao = new $column_type("_class_id_=" . $this->_class_ . "___" . $count++ . ",_class_access_=true");
                             $has_column = $dao->base_column($has_var);
                             $conds[] = Column::cond_instance($has_column->column(), "c" . $count++, $has_column->table(), $has_column->table_alias());
                             array_unshift($conds, Column::cond_instance($self_var, "c" . $count++, $this->_table_, $root_table_alias));
                             $dao->add_conds($conds);
                             $this->_has_many_conds_[$name] = array($dao, $has_var, $self_var);
                         } else {
                             if ($is_has) {
                                 $dao = new $column_type("_class_id_=" . $this->_class_ . "___" . $count++ . ",_hierarchy_=" . $has_hierarchy);
                                 $has_dao[$name] = $dao;
                                 $this->{$name}($dao);
                                 $this->_columns_ = array_merge($this->_columns_, $dao->columns());
                                 $this->_conds_ = array_merge($this->_conds_, $dao->conds());
                                 $this->a($name, "has", true);
                                 foreach ($dao->columns() as $column) {
                                     $this->_alias_[$column->column_alias()] = $name;
                                 }
                                 $has_column = $dao->base_column($has_var);
                                 $conds[] = Column::cond_instance($has_column->column(), "c" . $count++, $has_column->table(), $has_column->table_alias());
                             } else {
                                 $column->table($ref_table);
                                 $column->table_alias($ref_table_alias);
                                 $this->_columns_[] = $column;
                                 $this->_alias_[$column->column_alias()] = $name;
                             }
                             array_unshift($conds, Column::cond_instance($self_var, "c" . $count++, $this->_table_, $root_table_alias));
                             $this->add_conds($conds);
                         }
                     }
                 } else {
                     if ($anon_cond[0] === "@") {
                         $c = $this->base_column(substr($anon_cond, 1));
                         $column->table($c->table());
                         $column->table_alias($c->table_alias());
                         $this->_columns_[] = $column;
                         $this->_alias_[$column->column_alias()] = $name;
                     }
                 }
             }
         }
     }
     self::$DAO[$this->_class_id_] = (object) array("columns" => $this->_columns_, "self_columns" => $this->_self_columns_, "conds" => $this->_conds_, "alias" => $this->_alias_, "has_dao" => $has_dao, "has_many_conds" => $this->_has_many_conds_);
 }
示例#2
0
 protected function column_alias_sql(Column $column, \ebi\Q $q, $alias = true)
 {
     $column_str = $alias ? $column->table_alias() . '.' . $this->quotation($column->column()) : $this->quotation($column->column());
     if ($q->ignore_case()) {
         return 'upper(' . $column_str . ')';
     }
     return $column_str;
 }