1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Database\Schema;
13:
14: use Doctrine\DBAL\Schema\Table;
15: use Doctrine\DBAL\Schema\Schema;
16: use Doctrine\DBAL\Schema\Column;
17: use Doctrine\DBAL\Schema\ForeignKeyConstraint;
18: use Doctrine\DBAL\Schema\Sequence;
19: use Doctrine\DBAL\Schema\Index;
20:
21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34: class ImportSchema
35: {
36:
37: private $xPrefix = '';
38: private $xDbName = '';
39: private $schemaArray = array();
40:
41: 42: 43:
44: public function __construct()
45: {
46: $this->xPrefix = strtolower(\XoopsBaseConfig::get('db-prefix') . '_');
47: $this->xDbName = strtolower(\XoopsBaseConfig::get('db-name'));
48: }
49:
50: 51: 52: 53: 54: 55: 56:
57: public function importSchemaArray(array $schemaArray)
58: {
59: $tables = array();
60: $sequences = array();
61: $this->schemaArray = $schemaArray;
62: foreach ($schemaArray as $type => $entity) {
63: switch ($type) {
64: case 'tables':
65: $tables = $this->importTables($entity);
66: break;
67: case 'sequence':
68: $sequences = $this->importSequences($entity);
69: break;
70: }
71: }
72: return new Schema($tables, $sequences);
73: }
74:
75: 76: 77: 78: 79: 80: 81:
82: public function importTables(array $tableArray)
83: {
84: $tables=array();
85: foreach ($tableArray as $name => $tabledef) {
86:
87:
88: $tableName = $this->xPrefix . $name;
89: $columns = array();
90: $indexes = array();
91: $fkConstraints = array();
92: $options = array();
93: $idGeneratorType = 0;
94: foreach ($tabledef['columns'] as $colName => $colOptions) {
95: $colType = \Doctrine\DBAL\Types\Type::getType($colOptions['type']);
96: unset($colOptions['type']);
97: $columns[] = new Column($colName, $colType, $colOptions);
98: }
99:
100: if (isset($tabledef['indexes'])) {
101: foreach ($tabledef['indexes'] as $indexName => $indexDef) {
102: $indexes[] = new Index(
103: $indexName,
104: $indexDef['columns'],
105: $indexDef['unique'],
106: $indexDef['primary']
107: );
108: }
109: }
110:
111: if (isset($tabledef['constraint'])) {
112: foreach ($tabledef['constraint'] as $constraintDef) {
113: $fkConstraints[] = new ForeignKeyConstraint(
114: $constraintDef['localcolumns'],
115: $constraintDef['foreigntable'],
116: $constraintDef['foreigncolumns'],
117: $constraintDef['name'] = null,
118: $constraintDef['options']
119: );
120: }
121: }
122:
123: if (isset($tabledef['options']))
124: $options = $tabledef['options'];
125: $tables[] = new Table(
126: $tableName,
127: $columns,
128: $indexes,
129: $fkConstraints,
130: $idGeneratorType,
131: $options
132: );
133: }
134: return $tables;
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function importSequences(array $sequenceArray)
145: {
146: $sequences = array();
147:
148: foreach ($sequenceArray as $name => $sequenceDef) {
149:
150:
151: $sequences[] = new Sequence(
152: $sequenceDef['name'],
153: $sequenceDef['allocationsize'],
154: $sequenceDef['initialvalue']
155: );
156: }
157: return $sequences;
158: }
159: }
160: