Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Import.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * class Doctrine_Import
24
 * Main responsible of performing import operation. Delegates database schema
25
 * reading to a reader object and passes the result to a builder object which
26
 * builds a Doctrine data model.
27
 *
28
 * @package     Doctrine
29
 * @subpackage  Import
30
 * @link        www.doctrine-project.org
31
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
32
 * @since       1.0
33
 * @version     $Revision: 7490 $
34
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
35
 * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
36
 */
37
class Doctrine_Import extends Doctrine_Connection_Module
38
{
39
    protected $sql = array();
40
 
41
    /**
42
     * lists all databases
43
     *
44
     * @return array
45
     */
46
    public function listDatabases()
47
    {
48
        if ( ! isset($this->sql['listDatabases'])) {
49
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
50
        }
51
 
52
        return $this->conn->fetchColumn($this->sql['listDatabases']);
53
    }
54
 
55
    /**
56
     * lists all availible database functions
57
     *
58
     * @return array
59
     */
60
    public function listFunctions()
61
    {
62
        if ( ! isset($this->sql['listFunctions'])) {
63
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
64
        }
65
 
66
        return $this->conn->fetchColumn($this->sql['listFunctions']);
67
    }
68
 
69
    /**
70
     * lists all database triggers
71
     *
72
     * @param string|null $database
73
     * @return array
74
     */
75
    public function listTriggers($database = null)
76
    {
77
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
78
    }
79
 
80
    /**
81
     * lists all database sequences
82
     *
83
     * @param string|null $database
84
     * @return array
85
     */
86
    public function listSequences($database = null)
87
    {
88
        if ( ! isset($this->sql['listSequences'])) {
89
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
90
        }
91
 
92
        return $this->conn->fetchColumn($this->sql['listSequences']);
93
    }
94
 
95
    /**
96
     * lists table constraints
97
     *
98
     * @param string $table     database table name
99
     * @return array
100
     */
101
    public function listTableConstraints($table)
102
    {
103
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
104
    }
105
 
106
    /**
107
     * lists table relations
108
     *
109
     * Expects an array of this format to be returned with all the relationships in it where the key is
110
     * the name of the foreign table, and the value is an array containing the local and foreign column
111
     * name
112
     *
113
     * Array
114
     * (
115
     *   [groups] => Array
116
     *     (
117
     *        [local] => group_id
118
     *        [foreign] => id
119
     *     )
120
     * )
121
     *
122
     * @param string $table     database table name
123
     * @return array
124
     */
125
    public function listTableRelations($table)
126
    {
127
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
128
    }
129
 
130
    /**
131
     * lists table constraints
132
     *
133
     * @param string $table     database table name
134
     * @return array
135
     */
136
    public function listTableColumns($table)
137
    {
138
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
139
    }
140
 
141
    /**
142
     * lists table constraints
143
     *
144
     * @param string $table     database table name
145
     * @return array
146
     */
147
    public function listTableIndexes($table)
148
    {
149
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
150
    }
151
 
152
    /**
153
     * lists tables
154
     *
155
     * @param string|null $database
156
     * @return array
157
     */
158
    public function listTables($database = null)
159
    {
160
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
161
    }
162
 
163
    /**
164
     * lists table triggers
165
     *
166
     * @param string $table     database table name
167
     * @return array
168
     */
169
    public function listTableTriggers($table)
170
    {
171
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
172
    }
173
 
174
    /**
175
     * lists table views
176
     *
177
     * @param string $table     database table name
178
     * @return array
179
     */
180
    public function listTableViews($table)
181
    {
182
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
183
    }
184
 
185
    /**
186
     * lists database users
187
     *
188
     * @return array
189
     */
190
    public function listUsers()
191
    {
192
        if ( ! isset($this->sql['listUsers'])) {
193
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
194
        }
195
 
196
        return $this->conn->fetchColumn($this->sql['listUsers']);
197
    }
198
 
199
    /**
200
     * lists database views
201
     *
202
     * @param string|null $database
203
     * @return array
204
     */
205
    public function listViews($database = null)
206
    {
207
        if ( ! isset($this->sql['listViews'])) {
208
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
209
        }
210
 
211
        return $this->conn->fetchColumn($this->sql['listViews']);
212
    }
213
 
214
    /**
215
     * checks if a database exists
216
     *
217
     * @param string $database
218
     * @return boolean
219
     */
220
    public function databaseExists($database)
221
    {
222
        return in_array($database, $this->listDatabases());
223
    }
224
 
225
    /**
226
     * checks if a function exists
227
     *
228
     * @param string $function
229
     * @return boolean
230
     */
231
    public function functionExists($function)
232
    {
233
        return in_array($function, $this->listFunctions());
234
    }
235
 
236
    /**
237
     * checks if a trigger exists
238
     *
239
     * @param string $trigger
240
     * @param string|null $database
241
     * @return boolean
242
     */
243
    public function triggerExists($trigger, $database = null)
244
    {
245
        return in_array($trigger, $this->listTriggers($database));
246
    }
247
 
248
    /**
249
     * checks if a sequence exists
250
     *
251
     * @param string $sequence
252
     * @param string|null $database
253
     * @return boolean
254
     */
255
    public function sequenceExists($sequence, $database = null)
256
    {
257
        return in_array($sequence, $this->listSequences($database));
258
    }
259
 
260
    /**
261
     * checks if a table constraint exists
262
     *
263
     * @param string $constraint
264
     * @param string $table     database table name
265
     * @return boolean
266
     */
267
    public function tableConstraintExists($constraint, $table)
268
    {
269
        return in_array($constraint, $this->listTableConstraints($table));
270
    }
271
 
272
    /**
273
     * checks if a table column exists
274
     *
275
     * @param string $column
276
     * @param string $table     database table name
277
     * @return boolean
278
     */
279
    public function tableColumnExists($column, $table)
280
    {
281
        return in_array($column, $this->listTableColumns($table));
282
    }
283
 
284
    /**
285
     * checks if a table index exists
286
     *
287
     * @param string $index
288
     * @param string $table     database table name
289
     * @return boolean
290
     */
291
    public function tableIndexExists($index, $table)
292
    {
293
        return in_array($index, $this->listTableIndexes($table));
294
    }
295
 
296
    /**
297
     * checks if a table exists
298
     *
299
     * @param string $table
300
     * @param string|null $database
301
     * @return boolean
302
     */
303
    public function tableExists($table, $database = null)
304
    {
305
        return in_array($table, $this->listTables($database));
306
    }
307
 
308
    /**
309
     * checks if a table trigger exists
310
     *
311
     * @param string $trigger
312
     * @param string $table     database table name
313
     * @return boolean
314
     */
315
    public function tableTriggerExists($trigger, $table)
316
    {
317
        return in_array($trigger, $this->listTableTriggers($table));
318
    }
319
 
320
    /**
321
     * checks if a table view exists
322
     *
323
     * @param string $view
324
     * @param string $table     database table name
325
     * @return boolean
326
     */
327
    public function tableViewExists($view, $table)
328
    {
329
        return in_array($view, $this->listTableViews($table));
330
    }
331
 
332
    /**
333
     * checks if a user exists
334
     *
335
     * @param string $user
336
     * @return boolean
337
     */
338
    public function userExists($user)
339
    {
340
        return in_array($user, $this->listUsers());
341
    }
342
 
343
    /**
344
     * checks if a view exists
345
     *
346
     * @param string $view
347
     * @param string|null $database
348
     * @return boolean
349
     */
350
    public function viewExists($view, $database = null)
351
    {
352
         return in_array($view, $this->listViews($database));
353
    }
354
 
355
    /**
356
     * importSchema
357
     *
358
     * method for importing existing schema to Doctrine_Record classes
359
     *
360
     * @param string $directory
361
     * @param array $connections Array of connection names to generate models for
362
     * @return array                the names of the imported classes
363
     */
364
    public function importSchema($directory, array $connections = array(), array $options = array())
365
    {
366
        $classes = array();
367
 
368
        $manager = Doctrine_Manager::getInstance();
369
        foreach ($manager as $name => $connection) {
370
          // Limit the databases to the ones specified by $connections.
371
          // Check only happens if array is not empty
372
          if ( ! empty($connections) && ! in_array($name, $connections)) {
373
            continue;
374
          }
375
 
376
          $builder = new Doctrine_Import_Builder();
377
          $builder->setTargetPath($directory);
378
          $builder->setOptions($options);
379
 
380
          $definitions = array();
381
 
382
          foreach ($connection->import->listTables() as $table) {
383
              $definition = array();
384
              $definition['tableName'] = $table;
385
              $definition['className'] = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table));
386
              $definition['columns'] = $connection->import->listTableColumns($table);
387
              $definition['connection'] = $connection->getName();
388
              $definition['connectionClassName'] = $definition['className'];
389
 
390
              try {
391
                  $definition['relations'] = array();
392
                  $relations = $connection->import->listTableRelations($table);
393
                  $relClasses = array();
394
                  foreach ($relations as $relation) {
395
                      $table = $relation['table'];
396
                      $class = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table));
397
                      if (in_array($class, $relClasses)) {
398
                          $alias = $class . '_' . (count($relClasses) + 1);
399
                      } else {
400
                          $alias = $class;
401
                      }
402
                      $relClasses[] = $class;
403
                      $definition['relations'][$alias] = array(
404
                          'alias'   => $alias,
405
                          'class'   => $class,
406
                          'local'   => $relation['local'],
407
                          'foreign' => $relation['foreign']
408
                      );
409
                  }
410
              } catch (Exception $e) {}
411
 
412
              $definitions[strtolower($definition['className'])] = $definition;
413
              $classes[] = $definition['className'];
414
          }
415
 
416
          // Build opposite end of relationships
417
          foreach ($definitions as $definition) {
418
              $className = $definition['className'];
419
              $relClasses = array();
420
              foreach ($definition['relations'] as $alias => $relation) {
421
                  if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) {
422
                      $alias = $className . '_' . (count($relClasses) + 1);
423
                  } else {
424
                      $alias = $className;
425
                  }
426
                  $relClasses[] = $relation['class'];
427
                  $definitions[strtolower($relation['class'])]['relations'][$alias] = array(
428
                    'type' => Doctrine_Relation::MANY,
429
                    'alias' => $alias,
430
                    'class' => $className,
431
                    'local' => $relation['foreign'],
432
                    'foreign' => $relation['local']
433
                  );
434
              }
435
          }
436
 
437
          // Build records
438
          foreach ($definitions as $definition) {
439
              $builder->buildRecord($definition);
440
          }
441
        }
442
 
443
        return $classes;
444
    }
445
}