-
Notifications
You must be signed in to change notification settings - Fork 3
/
geraModelo.php
99 lines (84 loc) · 3.32 KB
/
geraModelo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include "../../config.php";
$sql = "SELECT concat(information_schema.COLUMNS.ORDINAL_POSITION, "
. "information_schema.COLUMNS.TABLE_NAME), "
. "information_schema.COLUMNS.TABLE_NAME AS 'tabela', "
. "information_schema.COLUMNS.COLUMN_NAME AS 'coluna', "
. "information_schema.COLUMNS.DATA_TYPE AS 'type', "
. "information_schema.COLUMNS.CHARACTER_MAXIMUM_LENGTH AS 'length', "
. "information_schema.COLUMNS.COLUMN_KEY AS 'key', "
. "information_schema.COLUMNS.EXTRA AS 'autoincrement', "
. "information_schema.COLUMNS.IS_NULLABLE AS 'isnull' "
. "FROM information_schema.COLUMNS where TABLE_SCHEMA = 'moodle' and TABLE_NAME LIKE 'mdl_tcccafeead%'";
$colunas= $DB->get_records_sql($sql);
$colunas2 = array_values($colunas);
$espaco = ' ';
$tabela = '';
$tabela2 = '';
$listaTabelas = array();
$primaria = '';
$previousT = '';
$nextT = '';
foreach($colunas2 as $key=>$coluna){
if($tabela2 !== $coluna->tabela){
$colunaA = explode("mdl_", $coluna->tabela);
$listaTabelas[] = $colunaA[1];
$tabela2 = $coluna->tabela;
}
}
function encerraTabela($espaco, $primaria){
echo "$espaco </FIELDS><br>";
echo "$espaco <KEYS><br>";
echo $espaco . $espaco. '<KEY NAME="primary" TYPE="primary" FIELDS="' . $primaria->coluna . '" /><br>';
echo "$espaco </KEYS><br>";
echo '</TABLE><br>';
}
ECHO "<TABLES><br>";
$nTabs = 0;
foreach($colunas2 as $key=>$coluna){
if($tabela !== $coluna->tabela){
if($tabela !== ''){
encerraTabela($espaco, $primaria);
$primaria = '';
$nTabs++;
}
$tabelaA = explode("mdl_",$coluna->tabela);
$tabelaN = $tabelaA[1];
$previousT = (isset($listaTabelas[$nTabs - 1]))? 'PREVIOUS="'.$listaTabelas[$nTabs -1] .'"' : '';
$nextT = (isset($listaTabelas[$nTabs + 1]))? 'NEXT="'.$listaTabelas[$nTabs + 1] .'"' : '';
echo '<TABLE NAME="'. $tabelaN .'" ' . $previousT .' ' . $nextT .' COMMENT="" ><br>';
echo $espaco . '<FIELDS><br>';
$tabela = $coluna->tabela;
}
$isnull = ($coluna->isnull === 'YES')? 'false': 'true';
$length = ($coluna->length != 0)? $coluna->length : 10 ;
if($coluna->key === 'PRI'){
$primaria = $coluna;
}
$sequence = ($coluna->autoincrement === 'auto_increment')? 'SEQUENCE="true"' : 'SEQUENCE="false"';
if(isset($colunas2[$key - 1])){
$previousC = $colunas2[$key -1];
if($tabela === $previousC->tabela){
$previous = 'PREVIOUS="'. $previousC->coluna .'"';
}else{
$previous = '';
}
}else{
$previous = '';
}
if(isset($colunas2[$key + 1])){
$nextC = $colunas2[$key + 1];
if($tabela === $nextC->tabela){
$next = 'NEXT="'. $nextC->coluna .'"';
}
else{
$next="";
}
}else{
$next = '';
}
echo"$espaco $espaco";
echo '<FIELD NAME="' . $coluna->coluna . '" TYPE="'. $coluna->type .'" LENGTH="'. $length .'" NOTNULL="' . $isnull . '" ' . $sequence . ' '. $previous .' '. $next .' COMMENT="" /><br>';
}
encerraTabela($espaco, $primaria);
ECHO "</TABLES>";