1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * $Id: DatabaseMap.php 521 2007-01-05 13:29:36Z heltem $ |
---|
5 | * |
---|
6 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
7 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
8 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
9 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
10 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
11 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
12 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
13 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
14 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
15 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
16 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
17 | * |
---|
18 | * This software consists of voluntary contributions made by many individuals |
---|
19 | * and is licensed under the LGPL. For more information please see |
---|
20 | * <http://propel.phpdb.org>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * DatabaseMap is used to model a database. |
---|
25 | * |
---|
26 | * GENERAL NOTE |
---|
27 | * ------------ |
---|
28 | * The propel.map classes are abstract building-block classes for modeling |
---|
29 | * the database at runtime. These classes are similar (a lite version) to the |
---|
30 | * propel.engine.database.model classes, which are build-time modeling classes. |
---|
31 | * These classes in themselves do not do any database metadata lookups, but instead |
---|
32 | * are used by the MapBuilder classes that were generated for your datamodel. The |
---|
33 | * MapBuilder that was created for your datamodel build a representation of your |
---|
34 | * database by creating instances of the DatabaseMap, TableMap, ColumnMap, etc. |
---|
35 | * classes. |
---|
36 | * |
---|
37 | * @author Hans Lellelid <hans@xmpl.org> (Propel) |
---|
38 | * @author John D. McNally <jmcnally@collab.net> (Torque) |
---|
39 | * @author Daniel Rall <dlr@collab.net> (Torque) |
---|
40 | * @version $Revision: 521 $ |
---|
41 | * @package propel.map |
---|
42 | */ |
---|
43 | class DatabaseMap { |
---|
44 | |
---|
45 | /** Name of the database. */ |
---|
46 | private $name; |
---|
47 | |
---|
48 | /** Name of the tables in the database. */ |
---|
49 | protected $tables = array(); |
---|
50 | |
---|
51 | /** |
---|
52 | * The table MapBuilder objects that will initialize tables (on demand). |
---|
53 | * @var array Map of table builders (name => MapBuilder) |
---|
54 | */ |
---|
55 | private $tableBuilders = array(); |
---|
56 | |
---|
57 | /** |
---|
58 | * Constructor. |
---|
59 | * |
---|
60 | * @param string $name Name of the database. |
---|
61 | */ |
---|
62 | public function __construct($name) |
---|
63 | { |
---|
64 | $this->name = $name; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Does this database contain this specific table? |
---|
69 | * |
---|
70 | * @param string $name The String representation of the table. |
---|
71 | * @return boolean True if the database contains the table. |
---|
72 | */ |
---|
73 | public function containsTable($name) |
---|
74 | { |
---|
75 | if ( strpos($name, '.') > 0) { |
---|
76 | $name = substr($name, 0, strpos($name, '.')); |
---|
77 | } |
---|
78 | // table builders are *always* loaded, whereas the tables aren't necessarily |
---|
79 | return isset($this->tableBuilders[$name]); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Get the name of this database. |
---|
84 | * |
---|
85 | * @return string The name of the database. |
---|
86 | */ |
---|
87 | public function getName() |
---|
88 | { |
---|
89 | return $this->name; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Get a TableMap for the table by name. |
---|
94 | * |
---|
95 | * @param string $name Name of the table. |
---|
96 | * @return TableMap A TableMap |
---|
97 | * @throws PropelException if the table is undefined |
---|
98 | */ |
---|
99 | public function getTable($name) |
---|
100 | { |
---|
101 | if (!isset($this->tables[$name])) { |
---|
102 | if (!isset($this->tableBuilders[$name])) { |
---|
103 | throw new PropelException("Cannot fetch TableMap for undefined table: " . $name . ". Make sure you have the static MapBuilder registration code after your peer stub class definition."); |
---|
104 | } |
---|
105 | $this->tableBuilders[$name]->doBuild(); |
---|
106 | } |
---|
107 | return $this->tables[$name]; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Get a TableMap[] of all of the tables in the database. |
---|
112 | * |
---|
113 | * @return array A TableMap[]. |
---|
114 | */ |
---|
115 | public function getTables() |
---|
116 | { |
---|
117 | // if there's a mismatch in the tables and tableBuilders |
---|
118 | if (count($this->tableBuilders) != count($this->tables)) { |
---|
119 | $missingTables = array_diff(array_keys($this->tableBuilders), array_keys($this->tables)); |
---|
120 | foreach ($missingTables as $table) { |
---|
121 | $this->tableBuilders[$table]->doBuild(); |
---|
122 | } |
---|
123 | } |
---|
124 | return $this->tables; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Add a new table to the database by name. |
---|
129 | * |
---|
130 | * This method creates an empty TableMap that must then be populated. This |
---|
131 | * is called indirectly on-demand by the getTable() method, when there is |
---|
132 | * a table builder (MapBuilder) registered, but no TableMap loaded. |
---|
133 | * |
---|
134 | * @param string $tableName The name of the table. |
---|
135 | * @return TableMap The newly created TableMap. |
---|
136 | */ |
---|
137 | public function addTable($tableName) |
---|
138 | { |
---|
139 | $this->tables[$tableName] = new TableMap($tableName, $this); |
---|
140 | return $this->tables[$tableName]; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Add a new table builder (MapBuilder) to the database by name. |
---|
145 | * |
---|
146 | * @param string $tableName The name of the table. |
---|
147 | */ |
---|
148 | public function addTableBuilder($tableName, MapBuilder $builder) |
---|
149 | { |
---|
150 | $this->tableBuilders[$tableName] = $builder; |
---|
151 | } |
---|
152 | } |
---|