MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
classmap.class
Ir para a documentação deste ficheiro.
1<?php
2
4{
5 private $name;
6 private $className;
7 private $database;
8 private $superClassName;
9 private $attributeMaps = array();
10 private $hashedAttributeMaps = array();
11 private $keyAttributeMaps = array();
12 private $proxyAttributeMaps = array();
13 private $updateAttributeMaps = array();
14 private $referenceAttributeMaps = array();
15 private $associationMaps = array();
16 private $inverseAssociationMaps = array();
17 private $straightAssociationMaps = array();
18 private $joinAssociationMaps = array();
19 private $tables = array();
20 private $mapObjectClass = NULL;
21 private $isInited = FALSE;
22 private $superClass = NULL;
23 private $selectStatement;
24 private $updateStatement;
25 private $insertStatement;
26 private $deleteStatement;
27 private $broker;
28
29 public function __construct($name, $database, &$broker)
30 {
31 $this->name = $name;
32 $this->database = $database;
33 $this->broker = $broker;
34 }
35
36 public function getName()
37 {
38 return $this->name;
39 }
40
41 public function getDatabase()
42 {
43 return $this->database;
44 }
45
46 public function getTables()
47 {
48 return $this->tables;
49 }
50
51 public function getTable()
52 {
53 reset ($this->tables);
54 return pos($this->tables);
55 }
56
57 public function addTable($tableMap)
58 {
59 $name = $tableMap->getName();
60 $this->tables[$name] = $tableMap;
61 }
62
63 public function getObject()
64 {
66 $className = 'business' . $this->getName();
67 $module = $MIOLO->usesBusiness[$className]['module'];
68 $name = $MIOLO->usesBusiness[$className]['name'];
69 if (($module != '') && ($name != ''))
70 {
71 $object = $MIOLO->GetBusiness($module, $name);
72 }
73 else
74 {
75 $object = new PersistentObject();
76 }
77
78 for ($i = 0; $i < $this->getSize(); $i++)
79 {
80 $this->getAttributeMap($i)->setValue($object, NULL);
81 }
82 return $object;
83 }
84
85 public function setSuperClass($superClassName, $broker)
86 {
88 $sc = 'business' . $superClassName;
89
90 if (($sc != 'business') && ($sc != 'persistentobject'))
91 {
92 $module = $MIOLO->usesBusiness[$sc]['module'];
93 $name = $MIOLO->usesBusiness[$sc]['name'];
94 $superClassMap = $broker->getClassMap($module, $name);
95
96 if ($superClassMap != NULL)
97 {
98 $this->superClass = $superClassMap;
99 }
100 }
101 }
102
103 public function getSuperClass()
104 {
105 return $this->superClass;
106 }
107
108 public function addAttributeMap(&$attributeMap)
109 {
110 $this->hashedAttributeMaps[$attributeMap->getName()] = $attributeMap;
111
112 if (($cm = $attributeMap->getColumnMap()) != NULL)
113 {
114 $this->attributeMaps[] = $attributeMap;
115
116 if ($cm->getKeyType() != 'none')
117 $this->keyAttributeMaps[] = $attributeMap;
118 else
119 $this->updateAttributeMaps[] = $attributeMap;
120
121 if ($attributeMap->getReference() != NULL)
122 $this->referenceAttributeMaps[] = $attributeMap;
123
124 // Add attributeMap table to the table map collection
125 $tableMap = $cm->getTableMap();
126 $this->addTable($tableMap);
127
128 if ($attributeMap->isProxy() || ($cm->getKeyType() != 'none'))
129 {
130 $this->proxyAttributeMaps[] = $attributeMap;
131 }
132 }
133 }
134
135 public function getAttributeMap($name, $areSuperClassesIncluded = FALSE)
136 {
137 $am = NULL;
138 $cm = $this;
139
140 if (gettype($name) == 'string')
141 {
142 do
143 {
144 $am = $cm->hashedAttributeMaps[$name];
145 $cm = $cm->superClass;
146 } while ($areSuperClassesIncluded && ($am == NULL) && ($cm != NULL));
147 }
148 else
149 {
150 $am = $cm->attributeMaps[$name];
151 }
152 return $am;
153 }
154
155 public function getKeyAttributeMap($index)
156 {
157 return $this->keyAttributeMaps[$index];
158 }
159
160 public function getProxyAttributeMap($index)
161 {
162 return $this->proxyAttributeMaps[$index];
163 }
164
165 public function getUpdateAttributeMap($index)
166 {
167 return $this->updateAttributeMaps[$index];
168 }
169
170 public function getReferenceAttributeMap($index)
171 {
172 return $this->referenceAttributeMaps[$index];
173 }
174
175 public function getAssociationMap($name)
176 {
177 return $this->associationMaps[$name];
178 }
179
180 public function getJoinAssociationMap($index)
181 {
182 return $this->joinAssociationMaps[$index];
183 }
184
185 public function putAssociationMap(&$associationMap)
186 {
187 $this->associationMaps[$associationMap->getTargetName()] = $associationMap;
188 if ($associationMap->isInverse())
189 {
190 $this->inverseAssociationMaps[] = $associationMap;
191 }
192 else
193 {
194 $this->straightAssociationMaps[] = $associationMap;
195 }
196 if ($associationMap->isJoinAutomatic())
197 {
198 $this->joinAssociationMaps[] = $associationMap;
199 }
200 }
201
202 public function getAssociationMaps()
203 {
204 return $this->associationMaps;
205 }
206
207 public function getJoinAssociationMaps()
208 {
209 return $this->joinAssociationMaps;
210 }
211
213 {
214 return $this->straightAssociationMaps;
215 }
216
218 {
219 return $this->inverseAssociationMaps;
220 }
221
222 public function getProxySize()
223 {
224 return count($this->proxyAttributeMaps);
225 }
226
227 public function getSize()
228 {
229 return count($this->attributeMaps);
230 }
231
232 public function getReferenceSize()
233 {
234 return count($this->referenceAttributeMaps);
235 }
236
237 public function getAssociationSize()
238 {
239 return count($this->associationMaps);
240 }
241
242 public function getJoinAssociationSize()
243 {
244 return count($this->joinAssociationMaps);
245 }
246
247 public function getKeySize()
248 {
249 return count($this->keyAttributeMaps);
250 }
251
252 public function getUpdateSize()
253 {
254 return count($this->updateAttributeMaps);
255 }
256
257 public function retrieveObject($object, $query)
258 {
259 $index = 0;
260 $classMap = $this;
261
262 do
263 {
264 for ($i = 0; $i < $classMap->getSize(); $i++)
265 {
266 $am = $classMap->getAttributeMap($i);
267 if ($cm = $am->getColumnMap())
268 {
269 $value = $query->getValue($cm->getName());
270 $am->setValue($object, $cm->getValueTo($value));
271 }
272 }
273
274 $classMap = $classMap->superClass;
275 } while ($classMap != NULL);
276
277 $object->setPersistent(TRUE);
278 $object->setProxy(FALSE);
279
280 for ($i = 0; $i < $this->getJoinAssociationSize(); $i++)
281 {
282 $aMap = $this->getJoinAssociationMap($i);
283 $classMap = $aMap->getForClass();
284 $nestedObject = $classMap->getObject();
285 for ($i = 0; $i < $classMap->getSize(); $i++)
286 {
287 $am = $classMap->getAttributeMap($i);
288 if ($cm = $am->getColumnMap())
289 {
290 $value = $query->getValue($cm->getName());
291 $am->setValue($nestedObject, $cm->getValueTo($value));
292 }
293 }
294 $value = $nestedObject;
295 $target = $aMap->getTarget();
296 $target->setValue($object, $value);
297 }
298 }
299
300 public function retrieveProxyObject($object, $query)
301 {
302 $index = 0;
303 $classMap = $this;
304
305 do
306 {
307 for ($i = 0; $i < $classMap->getProxySize(); $i++)
308 {
309 $am = $classMap->getProxyAttributeMap($i);
310 if ($cm = $am->getColumnMap())
311 {
312 $value = $query->getValue($cm->getName());
313 $am->setValue($object, $cm->getValueTo($value));
314 }
315 }
316
317 $classMap = $classMap->superClass;
318 } while ($classMap != NULL);
319
320 $object->setPersistent(TRUE);
321 $object->setProxy(TRUE);
322
323 for ($i = 0; $i < $this->getJoinAssociationSize(); $i++)
324 {
325 $aMap = $this->getJoinAssociationMap($i);
326 $classMap = $aMap->getForClass();
327 $nestedObject = $classMap->getObject();
328 for ($i = 0; $i < $classMap->getSize(); $i++)
329 {
330 $value = $result[$index++];
331 $am = $classMap->getAttributeMap($i);
332 if ($cm = $am->getColumnMap())
333 {
334 $am->setValue($nestedObject, $cm->getValueTo($value));
335 }
336 }
337 $value = $nestedObject;
338 $target = $aMap->getTarget();
339 $target->setValue($object, $value);
340 }
341 }
342
343 public function getSelectSqlFor($object)
344 {
345 $statement = $this->getSelectStatement();
346
347 // Fill statement with values
348 for ($i = 0; $i < $this->getKeySize(); $i++)
349 {
350 $am = $this->getKeyAttributeMap($i);
351 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
352 $statement->addParameter($value);
353 }
354 return $statement;
355 }
356
357 public function getSelectProxySqlFor($object)
358 {
359 $statement = $this->getSelectProxyStatement();
360
361 // Fill statement with values
362 for ($i = 0; $i < $this->getKeySize(); $i++)
363 {
364 $am = $this->getKeyAttributeMap($i);
365 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
366 $statement->addParameter($value);
367 }
368 return $statement;
369 }
370
371 public function getSelectSql($alias = '')
372 {
373 $columns = '';
374 $isFirst = TRUE;
375 $classMap = $this;
376
377 do
378 {
379 for ($i = 0; $i < $classMap->getSize(); $i++)
380 {
381 $am = $classMap->getAttributeMap($i);
382
383 if ($cm = $am->getColumnMap())
384 {
385 $column = $cm->getColumnName($alias);
386 $columns .= ($isFirst ? "" : ", ") . $column;
387 }
388 $isFirst = FALSE;
389 }
390
391 for ($i = 0; $i < $classMap->getJoinAssociationSize(); $i++)
392 {
393 $am = $classMap->getJoinAssociationMap($i);
394
395 $cm = $am->getForClass();
396
397 for ($j = 0; $j < $cm->getSize(); $j++)
398 {
399 $atrm = $cm->getAttributeMap($j);
400 if ($colm = $atrm->getColumnMap())
401 {
402 $column = $colm->getColumnName($alias);
403 $columns .= ($isFirst ? "" : ", ") . $column;
404 }
405 $isFirst = FALSE;
406 }
407 }
408
409 $classMap = $classMap->superClass;
410 } while ($classMap != NULL);
411
412 return $columns;
413 }
414
415 public function getSelectProxySql($alias = '')
416 {
417 $isFirst = TRUE;
418 $classMap = $this;
419
420 do
421 {
422 for ($i = 0; $i < $classMap->getProxySize(); $i++)
423 {
424 $am = $classMap->getProxyAttributeMap($i);
425
426 if ($cm = $am->getColumnMap())
427 {
428 $column = $cm->getColumnName($alias);
429 $columns .= ($isFirst ? "" : ", ") . $column;
430 }
431
432 $isFirst = FALSE;
433 }
434
435 for ($i = 0; $i < $classMap->getJoinAssociationSize(); $i++)
436 {
437 $am = $classMap->getJoinAssociationMap($i);
438
439 $cm = $am->getForClass();
440
441 for ($j = 0; $j < $cm->getSize(); $j++)
442 {
443 $atrm = $cm->getAttributeMap($j);
444 if ($colm = $atrm->getColumnMap())
445 {
446 $column = $colm->getColumnName($alias);
447 $columns .= ($isFirst ? "" : ", ") . $column;
448 }
449 $isFirst = FALSE;
450 }
451 }
452
453 $classMap = $classMap->superClass;
454 } while ($classMap != NULL);
455
456 return $columns;
457 }
458
459 public function getFromSql()
460 {
461 $tables = '';
462 $isFirst = TRUE;
463 $classMap = $this;
464
465 do
466 {
467 $table = $classMap->getAttributeMap(0)->getColumnMap()->getTableMap()->getName();
468 $tables .= ($isFirst ? "" : ", ") . $table;
469 $isFirst = FALSE;
470 $classMap = $classMap->superClass;
471 } while ($classMap != NULL);
472
473 for ($i = 0; $i < $this->getJoinAssociationSize(); $i++)
474 {
475 $classMap = $this->getJoinAssociationMap($i)->getForClass();
476 foreach($classMap->getTables() as $tm)
477 {
478 $table = $tm->getName();
479 $tables .= ($isFirst ? "" : ", ") . $table;
480 $isFirst = FALSE;
481 }
482 }
483
484 return $tables;
485 }
486
487 public function getWhereSql()
488 {
489 $alias = '';
490 $conditions = '';
491 $inheritanceAssociations = $this->getInheritanceAssociations();
492
493 if (($this->getKeySize() > 0) || ($inheritanceAssociations != ''))
494 {
495 $isFirst = TRUE;
496 $classMap = $this;
497
498 for ($i = 0; $i < $classMap->getKeySize(); $i++)
499 {
500 $am = $classMap->getKeyAttributeMap($i);
501
502 if ($cm = $am->getColumnMap())
503 {
504 $column = $cm->getFullyQualifiedName($alias);
505 $conditions .= ($isFirst ? " " : " AND ") . $column . " = ?";
506 }
507
508 $isFirst = FALSE;
509 }
510
511 if ($inheritanceAssociations != '')
512 {
513 $conditions .= (($classMap->getKeySize() > 0) ? " AND " : "") . $inheritanceAssociations;
514 }
515 }
516
517 return $conditions;
518 }
519
521 {
522 $result = '';
523 $isFirst = TRUE;
524 $classMap = $this;
525
526 do
527 {
528 for ($i = 0; $i < $classMap->getReferenceSize(); $i++)
529 {
530 $am = $classMap->getReferenceAttributeMap($i);
531 if ($cm = $am->getColumnMap())
532 {
533 $columnLeft = $cm->getFullyQualifiedName();
534 $columnRight = $am->getReference()->getColumnMap()->getFullyQualifiedName();
535 $result .= ($isFirst ? " " : " AND ") . $columnLeft . " = " . $columnRight;
536 }
537 $isFirst = FALSE;
538 }
539 $classMap = $classMap->superClass;
540 } while ($classMap != NULL);
541
542 return $result;
543 }
544
545 public function getJoinAssociations()
546 {
547 $join = array();
548 for ($i = 0; $i < $this->getJoinAssociationSize(); $i++)
549 {
550 $aMap = $this->getJoinAssociationMap($i);
551 $type = $aMap->getJoinAutomatic();
552 $k = $aMap->getSize();
553 for ($j = 0; $j < $k; $j++)
554 {
555 $entry = $aMap->getEntry($j);
556 $t1 = $entry->getFrom()->getColumnMap()->getTableMap();
557 $t1Name = $t1->getName();
558 $t2 = $entry->getTo()->getColumnMap()->getTableMap();
559 $t2Name = $t2->getName();
560 if ($aMap->IsInverse())
561 {
562 $condition = $entry->getTo()->getColumnMap()->getFullyQualifiedName() . "=" . $entry->getFrom()->getColumnMap()->getFullyQualifiedName();
563 $join[] = array($t2Name,$t1Name,$condition,$type);
564 }
565 else
566 {
567 $condition = $entry->getFrom()->getColumnMap()->getFullyQualifiedName() . "=" . $entry->getTo()->getColumnMap()->getFullyQualifiedName();
568 $join[] = array($t1Name,$t2Name,$condition,$type);
569 }
570 }
571 }
572 return (count($join) ? $join : NULL);
573 }
574
575 public function getUpdateSqlFor($object)
576 {
577 $statement = $this->getUpdateStatement();
578
579 // Fill statement with values
580 for ($i = 0; $i < $this->getUpdateSize(); $i++)
581 {
582 $am = $this->getUpdateAttributeMap($i);
583 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
584 $statement->addParameter($value);
585 }
586
587 for ($i = 0; $i < $this->getKeySize(); $i++)
588 {
589 $am = $this->getKeyAttributeMap($i);
590 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
591 $statement->addParameter($value);
592 }
593 return $statement;
594 }
595
596 public function getUpdateSql()
597 {
598 return $this->getAttributeMap(0)->getColumnMap()->getTableMap()->getName();
599 }
600
601 public function getUpdateSetSql()
602 {
603 $isFirst = TRUE;
604 $classMap = $this;
605
606 for ($i = 0; $i < $classMap->getUpdateSize(); $i++)
607 {
608 $am = $classMap->getUpdateAttributeMap($i);
609
610 if ($cm = $am->getColumnMap())
611 {
612 $column = $cm->getName();
613 $columns .= ($isFirst ? "" : ", ") . $column;
614 }
615
616 $isFirst = FALSE;
617 }
618 return $columns;
619 }
620
621 public function getUpdateWhereSql()
622 {
623 $conditions = '';
624 $isFirst = TRUE;
625 $classMap = $this;
626
627 for ($i = 0; $i < $classMap->getKeySize(); $i++)
628 {
629 $am = $classMap->getKeyAttributeMap($i);
630
631 if ($cm = $am->getColumnMap())
632 {
633 $column = $cm->getFullyQualifiedName($alias);
634 $conditions .= ($isFirst ? " " : " AND ") . $column . " = ?";
635 }
636
637 $isFirst = FALSE;
638 }
639 return $conditions;
640 }
641
642 public function getInsertSqlFor($object)
643 {
644 $statement = $this->getInsertStatement();
645
646 // Fill statement with values
647 for ($i = 0; $i < $this->getSize(); $i++)
648 {
649 $am = $this->getAttributeMap($i);
650 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
651 $statement->addParameter($value);
652 }
653 return $statement;
654 }
655
656 public function getInsertSql()
657 {
658 return $this->getAttributeMap(0)->getColumnMap()->getTableMap()->getName();
659 }
660
661 public function getInsertValuesSql()
662 {
663 $isFirst = TRUE;
664 $classMap = $this;
665
666 for ($i = 0; $i < $classMap->getSize(); $i++)
667 {
668 $am = $classMap->getAttributeMap($i);
669
670 if ($cm = $am->getColumnMap())
671 {
672 $column = $cm->getName();
673 $columns .= ($isFirst ? "" : ", ") . $column;
674 }
675
676 $isFirst = FALSE;
677 }
678
679 return $columns;
680 }
681
682 public function getDeleteSqlFor($object)
683 {
684 $statement = $this->getDeleteStatement();
685
686 // Fill statement with values
687 for ($i = 0; $i < $this->getKeySize(); $i++)
688 {
689 $am = $this->getKeyAttributeMap($i);
690 $value = $am->getColumnMap()->getValueFrom($am->getValue($object));
691 $statement->addParameter($value);
692 }
693 return $statement;
694 }
695
696 public function getDeleteSql()
697 {
698 return $this->getAttributeMap(0)->getColumnMap()->getTableMap()->getName();
699 }
700
701 public function getDeleteWhereSql()
702 {
703 $conditions = '';
704 $isFirst = TRUE;
705 $classMap = $this;
706
707 for ($i = 0; $i < $classMap->getKeySize(); $i++)
708 {
709 $am = $classMap->getKeyAttributeMap($i);
710
711 if ($cm = $am->getColumnMap())
712 {
713 $column = $cm->getName();
714 $conditions .= ($isFirst ? " " : " AND ") . $column . " = ?";
715 }
716
717 $isFirst = FALSE;
718 }
719 return $conditions;
720 }
721
722 public function getSelectStatement()
723 {
724 $this->selectStatement = new MSQL();
725 $this->selectStatement->SetColumns($this->getSelectSql());
726 if ($join = $this->getJoinAssociations())
727 {
728 $this->selectStatement->join = $join;
729 }
730 else
731 {
732 $this->selectStatement->SetTables($this->getFromSql());
733 }
734 $this->selectStatement->SetWhere($this->getWhereSql());
735 return $this->selectStatement;
736 }
737
738 public function getSelectProxyStatement()
739 {
740 $this->selectProxyStatement = new MSQL();
741 $this->selectProxyStatement->setColumns($this->getSelectProxySql());
742 if ($join = $this->getJoinAssociations())
743 {
744 $this->selectProxyStatement->join = $join;
745 }
746 else
747 {
748 $this->selectProxyStatement->SetTables($this->getFromSql());
749 }
750 $this->selectProxyStatement->SetWhere($this->getWhereSql());
751 return $this->selectProxyStatement;
752 }
753
754 public function getUpdateStatement()
755 {
756 $this->updateStatement = new MSQL();
757 $this->updateStatement->SetColumns($this->getUpdateSetSql());
758 $this->updateStatement->SetTables($this->getUpdateSql());
759 $this->updateStatement->SetWhere($this->getUpdateWhereSql());
760 return $this->updateStatement;
761 }
762
763 public function getInsertStatement()
764 {
765 $this->insertStatement = new MSQL();
766 $this->insertStatement->SetColumns($this->getInsertValuesSql());
767 $this->insertStatement->SetTables($this->getInsertSql());
768 return $this->insertStatement;
769 }
770
771 public function getDeleteStatement()
772 {
773 $this->deleteStatement = new MSQL();
774 $this->deleteStatement->SetTables($this->getDeleteSql());
775 $this->deleteStatement->SetWhere($this->getDeleteWhereSql());
776 return $this->deleteStatement;
777 }
778}
779?>
retrieveProxyObject($object, $query)
Definição classmap.class:300
getInverseAssociationMaps()
Definição classmap.class:217
getProxyAttributeMap($index)
Definição classmap.class:160
getUpdateSetSql()
Definição classmap.class:601
getUpdateWhereSql()
Definição classmap.class:621
getJoinAssociations()
Definição classmap.class:545
getUpdateAttributeMap($index)
Definição classmap.class:165
getSuperClass()
Definição classmap.class:103
getJoinAssociationSize()
Definição classmap.class:242
getUpdateStatement()
Definição classmap.class:754
putAssociationMap(&$associationMap)
Definição classmap.class:185
getUpdateSize()
Definição classmap.class:252
getJoinAssociationMaps()
Definição classmap.class:207
getSelectProxySqlFor($object)
Definição classmap.class:357
getReferenceSize()
Definição classmap.class:232
retrieveObject($object, $query)
Definição classmap.class:257
getStraightAssociationMaps()
Definição classmap.class:212
getUpdateSql()
Definição classmap.class:596
addAttributeMap(&$attributeMap)
Definição classmap.class:108
getDatabase()
Definição classmap.class:41
setSuperClass($superClassName, $broker)
Definição classmap.class:85
getAssociationMaps()
Definição classmap.class:202
getDeleteSqlFor($object)
Definição classmap.class:682
getSelectSql($alias='')
Definição classmap.class:371
getDeleteWhereSql()
Definição classmap.class:701
getKeyAttributeMap($index)
Definição classmap.class:155
getSelectProxySql($alias='')
Definição classmap.class:415
getDeleteStatement()
Definição classmap.class:771
getInsertValuesSql()
Definição classmap.class:661
getInsertSqlFor($object)
Definição classmap.class:642
getSelectStatement()
Definição classmap.class:722
getAssociationMap($name)
Definição classmap.class:175
getSelectProxyStatement()
Definição classmap.class:738
getAssociationSize()
Definição classmap.class:237
getSelectSqlFor($object)
Definição classmap.class:343
getDeleteSql()
Definição classmap.class:696
getReferenceAttributeMap($index)
Definição classmap.class:170
getInsertStatement()
Definição classmap.class:763
getProxySize()
Definição classmap.class:222
getJoinAssociationMap($index)
Definição classmap.class:180
__construct($name, $database, &$broker)
Definição classmap.class:29
getAttributeMap($name, $areSuperClassesIncluded=FALSE)
Definição classmap.class:135
addTable($tableMap)
Definição classmap.class:57
getInheritanceAssociations()
Definição classmap.class:520
getInsertSql()
Definição classmap.class:656
getUpdateSqlFor($object)
Definição classmap.class:575
static getInstance()
Definição miolo.class:134
Definição msql.class:8