00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBTable implements IDBTable {
00009
00010
00011
00012
00013
00014 protected $name = '';
00015
00016
00017
00018
00019
00020 protected $alias = '';
00021
00022
00023
00024
00025
00026 protected $fields = array();
00027
00028
00029
00030
00031
00032 protected $keys = array();
00033
00034
00035
00036
00037
00038 protected $relations = array();
00039
00040
00041
00042
00043
00044 protected $constraints = array();
00045
00046
00047
00048
00049
00050 protected $driver;
00051
00052 public function __construct($name, $fields = null, $keys = null, $relations = null, $constraints = null, $driver = null) {
00053 $this->driver = empty($driver) ? DB::get_connection(DB::DEFAULT_CONNECTION) : DB::get_connection($driver);
00054 $this->name = $name;
00055 $this->alias = $name;
00056 if (is_array($fields)) {
00057 foreach($fields as $field) {
00058 $this->add_field($field);
00059 }
00060 }
00061 if (!empty($keys)) {
00062 foreach(Arr::force($keys) as $key) {
00063 $this->set_as_key($key);
00064 }
00065 }
00066 if (!empty($relations)) {
00067 foreach(Arr::force($relations) as $relation) {
00068 $this->add_relation($relation);
00069 }
00070 }
00071 if (!empty($constraints)) {
00072 foreach(Arr::force($constraints) as $constraint) {
00073 $this->add_constraint($constraint);
00074 }
00075 }
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 public function get_table_name() {
00086 return $this->name;
00087 }
00088
00089
00090
00091
00092
00093
00094 public function get_table_alias() {
00095 return $this->alias;
00096 }
00097
00098
00099
00100
00101
00102
00103 public function get_table_name_escaped() {
00104 return $this->driver->escape_database_entity($this->name, IDBDriver::TABLE);
00105 }
00106
00107
00108
00109
00110
00111
00112 public function get_table_alias_escaped() {
00113 return $this->driver->escape_database_entity($this->alias, IDBDriver::ALIAS);
00114 }
00115
00116
00117
00118
00119
00120
00121 public function get_table_fields() {
00122 return $this->fields;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 public function get_table_field($column) {
00132 $ret = Arr::get_item($this->fields, $column, false);
00133 return $ret;
00134 }
00135
00136
00137
00138
00139
00140
00141 public function get_table_keys() {
00142 return $this->keys;
00143 }
00144
00145
00146
00147
00148
00149
00150 public function get_table_driver() {
00151 return $this->driver;
00152 }
00153
00154
00155
00156
00157
00158
00159 public function get_table_relations() {
00160 return $this->relations;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function get_matching_relations(IDBTable $other) {
00170 $relations_this2other = $this->find_relations($this, $other);
00171 $relations_other2this = $this->find_relations($other, $this);
00172
00173 $relations_other2this = $this->remove_duplicated_relations($relations_other2this, $relations_this2other);
00174
00175 foreach($relations_other2this as $relation) {
00176 $relations_this2other[] = new DBRelation($other->get_table_name(), $relation->get_reversed_fields(), $relation->get_policy(), $relation->get_type());
00177 }
00178
00179 return $relations_this2other;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189 protected function find_relations(IDBTable $source, IDBTable $target) {
00190 $ret = array();
00191 $table_name_to_check = $target->get_table_name();
00192 foreach($source->get_table_relations() as $relation) {
00193 if ($relation->get_target_table_name() == $table_name_to_check) {
00194 $ret[] = $relation;
00195 }
00196 }
00197 return $ret;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207 protected function remove_duplicated_relations($arr1, $arr2) {
00208 $ret = array();
00209 foreach($arr1 as $parent_relation) {
00210 if (!$this->relation_is_in_array($parent_relation, $arr2)) {
00211 $ret[] = $parent_relation;
00212 }
00213 }
00214 return $ret;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224 protected function relation_is_in_array(IDBRelation $relation, $arr_relations) {
00225 $duplicate = false;
00226
00227 $fields_to_check = array();
00228 foreach($relation->get_fields() as $fieldrelation) {
00229 $fields_to_check[$fieldrelation->get_source_field_name()] = $fieldrelation->get_target_field_name();
00230 }
00231 $c_check = count($fields_to_check);
00232 foreach($arr_relations as $relation_to_test_against) {
00233 $fields = $relation_to_test_against->get_fields();
00234 if (count($fields) != $c_check) {
00235 continue;
00236 }
00237 $duplicate = true;
00238 foreach($fields as $fieldrelation) {
00239 $fieldname_to_check = Arr::get_item($fields_to_check, $fieldrelation->get_target_field_name(), '');
00240 if ($fieldname_to_check != $fieldrelation->get_source_field_name()) {
00241 $duplicate = false;
00242 break;
00243 }
00244 }
00245 if ($duplicate) {
00246 break;
00247 }
00248 }
00249 return $duplicate;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function get_table_constraints() {
00258 return $this->constraints;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 public function add_field(IDBField $field) {
00271 $field->set_connection($this->driver);
00272 $field->set_table($this);
00273 $this->fields[$field->get_field_name()] = $field;
00274 }
00275
00276
00277
00278
00279
00280
00281 public function add_relation(IDBRelation $relation) {
00282 $this->relations[] = $relation;
00283 }
00284
00285
00286
00287
00288
00289
00290 public function add_constraint(IDBConstraint $constraint) {
00291 $this->constraints[] = $constraint;
00292 }
00293
00294
00295
00296
00297
00298
00299 public function set_as_key($column) {
00300 $key_field = $this->get_table_field($column);
00301 if ($key_field) {
00302 $this->keys[$column] = $key_field;
00303 }
00304 else {
00305 throw new Exception(tr('Can not set key - no field %col', 'core', array('%col' => $column)));
00306 }
00307 }
00308 }