1: <?php
2:
3: namespace Alchemy\orm;
4: use Alchemy\core\query\Query;
5:
6:
7: 8: 9:
10: class DDL {
11: private $session;
12:
13: 14: 15: 16: 17:
18: public function __construct(Session $session) {
19: $this->session = $session;
20: }
21:
22:
23: 24: 25: 26: 27:
28: public function create($cls) {
29: $create = Query::Create($cls::schema());
30: $this->session->engine()->query($create);
31: }
32:
33:
34: 35: 36: 37:
38: public function createAll() {
39: $mappers = DataMapper::list_mappers();
40: $created = array();
41:
42: while (count($mappers) > 0) {
43: $mapper = array_pop($mappers);
44: $table = $mapper::schema();
45: $dependancies = $table->listDependancies();
46: $dependancies = array_diff($dependancies, $created);
47:
48: if (count($dependancies) > 0) {
49: array_unshift($mappers, $mapper);
50: } else {
51: $this->create($mapper);
52: $created[] = $table->getName();
53: }
54: }
55: }
56:
57:
58: 59: 60:
61: public function drop($cls) {
62: $drop = Query::Drop($cls::schema());
63: $this->session->engine()->query($drop);
64: }
65:
66:
67: 68: 69: 70:
71: public function dropAll() {
72: $mappers = DataMapper::list_mappers();
73: $dropped = array();
74:
75: while (count($mappers) > 0) {
76: $mapper = array_pop($mappers);
77: $table = $mapper::schema();
78: $dependants = $table->listDependants();
79: $dependants = array_diff($dependants, $dropped);
80:
81: if (count($dependants) > 0) {
82: array_unshift($mappers, $mapper);
83: } else {
84: $this->drop($mapper);
85: $dropped[] = $table->getName();
86: }
87: }
88: }
89: }
90: