Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Metadata / Source / AbstractSource.php
blob9e6c614b7f02ee45a1344f19a4d5b10420bf5da3
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Db\Metadata\Source;
12 use Zend\Db\Adapter\Adapter;
13 use Zend\Db\Metadata\MetadataInterface;
14 use Zend\Db\Metadata\Object;
16 abstract class AbstractSource implements MetadataInterface
18 const DEFAULT_SCHEMA = '__DEFAULT_SCHEMA__';
20 /**
22 * @var Adapter
24 protected $adapter = null;
26 /**
28 * @var string
30 protected $defaultSchema = null;
32 /**
34 * @var array
36 protected $data = array();
38 /**
39 * Constructor
41 * @param Adapter $adapter
43 public function __construct(Adapter $adapter)
45 $this->adapter = $adapter;
46 $this->defaultSchema = ($adapter->getCurrentSchema()) ?: self::DEFAULT_SCHEMA;
49 /**
50 * Get schemas
53 public function getSchemas()
55 $this->loadSchemaData();
57 return $this->data['schemas'];
60 /**
61 * Get table names
63 * @param string $schema
64 * @param bool $includeViews
65 * @return string[]
67 public function getTableNames($schema = null, $includeViews = false)
69 if ($schema === null) {
70 $schema = $this->defaultSchema;
73 $this->loadTableNameData($schema);
75 if ($includeViews) {
76 return array_keys($this->data['table_names'][$schema]);
79 $tableNames = array();
80 foreach ($this->data['table_names'][$schema] as $tableName => $data) {
81 if ('BASE TABLE' == $data['table_type']) {
82 $tableNames[] = $tableName;
85 return $tableNames;
89 /**
90 * Get tables
92 * @param string $schema
93 * @param bool $includeViews
94 * @return Object\TableObject[]
96 public function getTables($schema = null, $includeViews = false)
98 if ($schema === null) {
99 $schema = $this->defaultSchema;
102 $tables = array();
103 foreach ($this->getTableNames($schema, $includeViews) as $tableName) {
104 $tables[] = $this->getTable($tableName, $schema);
106 return $tables;
110 * Get table
112 * @param string $tableName
113 * @param string $schema
114 * @return Object\TableObject
116 public function getTable($tableName, $schema = null)
118 if ($schema === null) {
119 $schema = $this->defaultSchema;
122 $this->loadTableNameData($schema);
124 if (!isset($this->data['table_names'][$schema][$tableName])) {
125 throw new \Exception('Table "' . $tableName . '" does not exist');
128 $data = $this->data['table_names'][$schema][$tableName];
129 switch ($data['table_type']) {
130 case 'BASE TABLE':
131 $table = new Object\TableObject($tableName);
132 break;
133 case 'VIEW':
134 $table = new Object\ViewObject($tableName);
135 $table->setViewDefinition($data['view_definition']);
136 $table->setCheckOption($data['check_option']);
137 $table->setIsUpdatable($data['is_updatable']);
138 break;
139 default:
140 throw new \Exception('Table "' . $tableName . '" is of an unsupported type "' . $data['table_type'] . '"');
142 $table->setColumns($this->getColumns($tableName, $schema));
143 $table->setConstraints($this->getConstraints($tableName, $schema));
144 return $table;
148 * Get view names
150 * @param string $schema
151 * @return array
153 public function getViewNames($schema = null)
155 if ($schema === null) {
156 $schema = $this->defaultSchema;
159 $this->loadTableNameData($schema);
161 $viewNames = array();
162 foreach ($this->data['table_names'][$schema] as $tableName => $data) {
163 if ('VIEW' == $data['table_type']) {
164 $viewNames[] = $tableName;
167 return $viewNames;
171 * Get views
173 * @param string $schema
174 * @return array
176 public function getViews($schema = null)
178 if ($schema === null) {
179 $schema = $this->defaultSchema;
182 $views = array();
183 foreach ($this->getViewNames($schema) as $tableName) {
184 $views[] = $this->getTable($tableName, $schema);
186 return $views;
190 * Get view
192 * @param string $viewName
193 * @param string $schema
194 * @return \Zend\Db\Metadata\Object\TableObject
196 public function getView($viewName, $schema = null)
198 if ($schema === null) {
199 $schema = $this->defaultSchema;
202 $this->loadTableNameData($schema);
204 $tableNames = $this->data['table_names'][$schema];
205 if (isset($tableNames[$viewName]) && 'VIEW' == $tableNames[$viewName]['table_type']) {
206 return $this->getTable($viewName, $schema);
208 throw new \Exception('View "' . $viewName . '" does not exist');
212 * Gt column names
214 * @param string $table
215 * @param string $schema
216 * @return array
218 public function getColumnNames($table, $schema = null)
220 if ($schema === null) {
221 $schema = $this->defaultSchema;
224 $this->loadColumnData($table, $schema);
226 if (!isset($this->data['columns'][$schema][$table])) {
227 throw new \Exception('"' . $table . '" does not exist');
230 return array_keys($this->data['columns'][$schema][$table]);
234 * Get columns
236 * @param string $table
237 * @param string $schema
238 * @return array
240 public function getColumns($table, $schema = null)
242 if ($schema === null) {
243 $schema = $this->defaultSchema;
246 $this->loadColumnData($table, $schema);
248 $columns = array();
249 foreach ($this->getColumnNames($table, $schema) as $columnName) {
250 $columns[] = $this->getColumn($columnName, $table, $schema);
252 return $columns;
256 * Get column
258 * @param string $columnName
259 * @param string $table
260 * @param string $schema
261 * @return Object\ColumnObject
263 public function getColumn($columnName, $table, $schema = null)
265 if ($schema === null) {
266 $schema = $this->defaultSchema;
269 $this->loadColumnData($table, $schema);
271 if (!isset($this->data['columns'][$schema][$table][$columnName])) {
272 throw new \Exception('A column by that name was not found.');
275 $info = $this->data['columns'][$schema][$table][$columnName];
277 $column = new Object\ColumnObject($columnName, $table, $schema);
278 $props = array(
279 'ordinal_position', 'column_default', 'is_nullable',
280 'data_type', 'character_maximum_length', 'character_octet_length',
281 'numeric_precision', 'numeric_scale', 'numeric_unsigned',
282 'erratas'
284 foreach ($props as $prop) {
285 if (isset($info[$prop])) {
286 $column->{'set' . str_replace('_', '', $prop)}($info[$prop]);
290 $column->setOrdinalPosition($info['ordinal_position']);
291 $column->setColumnDefault($info['column_default']);
292 $column->setIsNullable($info['is_nullable']);
293 $column->setDataType($info['data_type']);
294 $column->setCharacterMaximumLength($info['character_maximum_length']);
295 $column->setCharacterOctetLength($info['character_octet_length']);
296 $column->setNumericPrecision($info['numeric_precision']);
297 $column->setNumericScale($info['numeric_scale']);
298 $column->setNumericUnsigned($info['numeric_unsigned']);
299 $column->setErratas($info['erratas']);
301 return $column;
305 * Get constraints
307 * @param string $table
308 * @param string $schema
309 * @return array
311 public function getConstraints($table, $schema = null)
313 if ($schema === null) {
314 $schema = $this->defaultSchema;
317 $this->loadConstraintData($table, $schema);
319 $constraints = array();
320 foreach (array_keys($this->data['constraints'][$schema][$table]) as $constraintName) {
321 $constraints[] = $this->getConstraint($constraintName, $table, $schema);
324 return $constraints;
328 * Get constraint
330 * @param string $constraintName
331 * @param string $table
332 * @param string $schema
333 * @return Object\ConstraintObject
335 public function getConstraint($constraintName, $table, $schema = null)
337 if ($schema === null) {
338 $schema = $this->defaultSchema;
341 $this->loadConstraintData($table, $schema);
343 if (!isset($this->data['constraints'][$schema][$table][$constraintName])) {
344 throw new \Exception('Cannot find a constraint by that name in this table');
347 $info = $this->data['constraints'][$schema][$table][$constraintName];
348 $constraint = new Object\ConstraintObject($constraintName, $table, $schema);
350 foreach (array(
351 'constraint_type' => 'setType',
352 'match_option' => 'setMatchOption',
353 'update_rule' => 'setUpdateRule',
354 'delete_rule' => 'setDeleteRule',
355 'columns' => 'setColumns',
356 'referenced_table_schema' => 'setReferencedTableSchema',
357 'referenced_table_name' => 'setReferencedTableName',
358 'referenced_columns' => 'setReferencedColumns',
359 'check_clause' => 'setCheckClause',
360 ) as $key => $setMethod) {
361 if (isset($info[$key])) {
362 $constraint->{$setMethod}($info[$key]);
366 return $constraint;
370 * Get constraint keys
372 * @param string $constraint
373 * @param string $table
374 * @param string $schema
375 * @return array
377 public function getConstraintKeys($constraint, $table, $schema = null)
379 if ($schema === null) {
380 $schema = $this->defaultSchema;
383 $this->loadConstraintReferences($table, $schema);
385 // organize references first
386 $references = array();
387 foreach ($this->data['constraint_references'][$schema] as $refKeyInfo) {
388 if ($refKeyInfo['constraint_name'] == $constraint) {
389 $references[$refKeyInfo['constraint_name']] = $refKeyInfo;
393 $this->loadConstraintDataKeys($schema);
395 $keys = array();
396 foreach ($this->data['constraint_keys'][$schema] as $constraintKeyInfo) {
397 if ($constraintKeyInfo['table_name'] == $table && $constraintKeyInfo['constraint_name'] === $constraint) {
398 $keys[] = $key = new Object\ConstraintKeyObject($constraintKeyInfo['column_name']);
399 $key->setOrdinalPosition($constraintKeyInfo['ordinal_position']);
400 if (isset($references[$constraint])) {
401 //$key->setReferencedTableSchema($constraintKeyInfo['referenced_table_schema']);
402 $key->setForeignKeyUpdateRule($references[$constraint]['update_rule']);
403 $key->setForeignKeyDeleteRule($references[$constraint]['delete_rule']);
404 //$key->setReferencedTableSchema($references[$constraint]['referenced_table_schema']);
405 $key->setReferencedTableName($references[$constraint]['referenced_table_name']);
406 $key->setReferencedColumnName($references[$constraint]['referenced_column_name']);
411 return $keys;
415 * Get trigger names
417 * @param string $schema
418 * @return array
420 public function getTriggerNames($schema = null)
422 if ($schema === null) {
423 $schema = $this->defaultSchema;
426 $this->loadTriggerData($schema);
428 return array_keys($this->data['triggers'][$schema]);
432 * Get triggers
434 * @param string $schema
435 * @return array
437 public function getTriggers($schema = null)
439 if ($schema === null) {
440 $schema = $this->defaultSchema;
443 $triggers = array();
444 foreach ($this->getTriggerNames($schema) as $triggerName) {
445 $triggers[] = $this->getTrigger($triggerName, $schema);
447 return $triggers;
451 * Get trigger
453 * @param string $triggerName
454 * @param string $schema
455 * @return Object\TriggerObject
457 public function getTrigger($triggerName, $schema = null)
459 if ($schema === null) {
460 $schema = $this->defaultSchema;
463 $this->loadTriggerData($schema);
465 if (!isset($this->data['triggers'][$schema][$triggerName])) {
466 throw new \Exception('Trigger "' . $triggerName . '" does not exist');
469 $info = $this->data['triggers'][$schema][$triggerName];
471 $trigger = new Object\TriggerObject();
473 $trigger->setName($triggerName);
474 $trigger->setEventManipulation($info['event_manipulation']);
475 $trigger->setEventObjectCatalog($info['event_object_catalog']);
476 $trigger->setEventObjectSchema($info['event_object_schema']);
477 $trigger->setEventObjectTable($info['event_object_table']);
478 $trigger->setActionOrder($info['action_order']);
479 $trigger->setActionCondition($info['action_condition']);
480 $trigger->setActionStatement($info['action_statement']);
481 $trigger->setActionOrientation($info['action_orientation']);
482 $trigger->setActionTiming($info['action_timing']);
483 $trigger->setActionReferenceOldTable($info['action_reference_old_table']);
484 $trigger->setActionReferenceNewTable($info['action_reference_new_table']);
485 $trigger->setActionReferenceOldRow($info['action_reference_old_row']);
486 $trigger->setActionReferenceNewRow($info['action_reference_new_row']);
487 $trigger->setCreated($info['created']);
489 return $trigger;
493 * Prepare data hierarchy
495 * @param string $type
496 * @param string $key ...
498 protected function prepareDataHierarchy($type)
500 $data = &$this->data;
501 foreach (func_get_args() as $key) {
502 if (!isset($data[$key])) {
503 $data[$key] = array();
505 $data = &$data[$key];
510 * Load schema data
512 protected function loadSchemaData()
517 * Load table name data
519 * @param string $schema
521 protected function loadTableNameData($schema)
523 if (isset($this->data['table_names'][$schema])) {
524 return;
527 $this->prepareDataHierarchy('table_names', $schema);
531 * Load column data
533 * @param string $table
534 * @param string $schema
536 protected function loadColumnData($table, $schema)
538 if (isset($this->data['columns'][$schema][$table])) {
539 return;
542 $this->prepareDataHierarchy('columns', $schema, $table);
546 * Load constraint data
548 * @param string $table
549 * @param string $schema
551 protected function loadConstraintData($table, $schema)
553 if (isset($this->data['constraints'][$schema])) {
554 return;
557 $this->prepareDataHierarchy('constraints', $schema);
561 * Load constraint data keys
563 * @param string $schema
565 protected function loadConstraintDataKeys($schema)
567 if (isset($this->data['constraint_keys'][$schema])) {
568 return;
571 $this->prepareDataHierarchy('constraint_keys', $schema);
575 * Load constraint references
577 * @param string $table
578 * @param string $schema
580 protected function loadConstraintReferences($table, $schema)
582 if (isset($this->data['constraint_references'][$schema])) {
583 return;
586 $this->prepareDataHierarchy('constraint_references', $schema);
590 * Load trigger data
592 * @param string $schema
594 protected function loadTriggerData($schema)
596 if (isset($this->data['triggers'][$schema])) {
597 return;
600 $this->prepareDataHierarchy('triggers', $schema);