00001 <?php
00002 require_once dirname(__FILE__) . '/dbquery.select.cls.php';
00003
00004
00005
00006
00007
00008
00009
00010 class DBQueryJoined extends DBQuerySelect {
00011
00012
00013
00014 const INNER = 0;
00015
00016
00017
00018 const LEFT = 1;
00019
00020
00021
00022 const RIGHT = 2;
00023
00024
00025
00026 const AUTODETECT_CONDITIONS = 128;
00027
00028
00029
00030
00031
00032
00033 protected $join_type;
00034
00035
00036
00037
00038
00039
00040 protected $parent_query;
00041
00042
00043
00044
00045
00046
00047 protected $join_conditions;
00048
00049
00050
00051
00052
00053
00054 protected $relations = false;
00055
00056
00057 public function __construct(IDBTable $table, DBQuery $parent_query, $join_type = self::INNER, $policy = self::AUTODETECT_CONDITIONS) {
00058 parent::__construct($table, $policy);
00059 $this->parent_query = $parent_query;
00060 $this->join_type = $join_type;
00061 $this->join_conditions = new DBWhereGroup($table);
00062 }
00063
00064
00065
00066
00067
00068
00069 public function set_join_type($join_type) {
00070 $this->join_type = $join_type;
00071 }
00072
00073
00074
00075
00076
00077
00078 public function get_join_type() {
00079 return $this->join_type;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 public function add_join_condition($this_field, $parent_field, $mode = IDBWhere::LOGIC_AND) {
00091 $condition = new DBJoinCondition($this->get_table(), $this_field, $this->parent_query->get_table(), $parent_field, $mode);
00092 $this->add_join_condition_object($condition);
00093 return $condition;
00094 }
00095
00096
00097
00098
00099
00100
00101 public function add_join_condition_object(IDBWhere $condition) {
00102 $this->join_conditions->add_where_object($condition);
00103 }
00104
00105
00106
00107
00108
00109
00110 public function get_join_conditions() {
00111 $ret = $this->join_conditions;
00112 if ($this->has_policy(self::AUTODETECT_CONDITIONS)) {
00113 $ret->add_where_object($this->compute_join_conditions());
00114 }
00115 return $ret;
00116 }
00117
00118 protected function compute_join_conditions() {
00119 $parent = $this->get_table();
00120 $child = $this->parent_query->get_table();
00121
00122 $ret = new DBWhereGroup($parent);
00123
00124 $relations = $this->get_relations();
00125 foreach($relations as $relation) {
00126 foreach($relation->get_fields() as $fieldrelation) {
00127 $ret->add_where_object(
00128 new DBJoinCondition(
00129 $parent,
00130 $fieldrelation->get_source_field_name(),
00131 $child,
00132 $fieldrelation->get_target_field_name()
00133 )
00134 );
00135 }
00136 }
00137 return $ret;
00138 }
00139
00140 protected function get_relations() {
00141 if (!is_array($this->relations)) {
00142 $this->relations = $this->get_table()->get_matching_relations($this->parent_query->get_table());
00143 }
00144 return $this->relations;
00145 }
00146
00147
00148
00149
00150 public function get_relation_type() {
00151 $types = array();
00152 $relations = $this->get_relations();
00153 foreach($relations as $relation) {
00154
00155 $types[$relation->get_type()] = $relation->get_type();
00156 }
00157 return count($types) ? max($types) : DBRelation::MANY_TO_MANY;
00158 }
00159 }