Menu

[r11]: / trunk / src / MyTable.java  Maximize  Restore  History

Download this file

268 lines (196 with data), 8.0 kB

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.util.*;
/**
*
*/
/**
* @author Uisleandro
*
*/
public class MyTable {
private String tableName;
//HashTable replaced by Map
private Map<String, MyColumn> columns;
private Map<String, MyColumn> pk_columns;
//The name of the foreign key columns, used after as indexes
private ArrayList<String> fkNames;
private float size;
private float weight;
public MyTable(String table){
this.tableName = table;
this.columns = new HashMap<String, MyColumn>();
this.pk_columns = new HashMap<String, MyColumn>();
this.fkNames = new ArrayList<String>();
this.weight = 1F;
}
public MyTable(String table, float weight){
this.tableName = table;
this.columns = new HashMap<String, MyColumn>();
this.pk_columns = new HashMap<String, MyColumn>();
this.fkNames = new ArrayList<String>();
this.weight = weight;
}
/**
* @return the size of this table in Bytes
*/
public float getSize() {
return this.size;
}
/**
* @return the weight, in this case the number of insertions inside the database;
*/
public float getWeight() {
return weight;
}
/**
* @param weight the weight to set
*/
public void setWeight(int weight) {
this.weight = weight;
}
/*
* @param column
* @param size
* */
public void addColumn(String columnName, MyColumn columnType){
if(columnType == null){
System.out.println("Erro: MyTableAdapter: columnType == null");
return;
}
if(columnType.isFK()){
this.fkNames.add(columnName);
}
//Just count the size, no need to put it on insert.
if(columnType.getAutoIncrement()){
this.size += columnType.getSize();
if(!this.pk_columns.containsKey(columnName))
{
this.pk_columns.put(columnName, columnType);
}
return;
}
if(!this.columns.containsKey(columnName))
{
this.size += columnType.getSize();
this.columns.put(columnName, columnType);
}
}
/**
* @return Collection of columns from this MyTable instance
*/
public Map<String, MyColumn> getColumns(){
return this.columns;
}
/**
* @return The name of the table represented by this MyTable instance
*/
public String getTable(){
return this.tableName;
}
/**
* @return ArrayList containing the foreign key names
*/
public ArrayList<String> getFkNames(){
return this.fkNames;
}
/**
* @param o
* @return
*/
public Boolean pointsTo(MyTable o){
Iterator<String> it = this.getFkNames().iterator();
while(it.hasNext()){
if(this.getColumns().get(it.next()).getRefTable().equals(o.getTable())){
return true;
}
}
return false;
}
@Override
public String toString(){
return this.tableName;
}
/**
* @param args
*/
public static void main(String[] args) {
MyTable tabela = new MyTable("cargo");
tabela.addColumn("Cargo_id", new MyPKColumn() );
tabela.addColumn("fk_Empresa_id", new MyFKColumn("empresa", "Empresa_id") );
tabela.addColumn("Cargo_Nome", new MyColumn("varchar", 45) );
tabela = new MyTable("cargo_possui_risco_ocupacional");
tabela.addColumn("fk_Cargo_id", new MyPKColumn() );
tabela.addColumn("fk_Risco_id", new MyPKColumn() );
tabela = new MyTable("categoria_de_exames");
tabela.addColumn("Categoria_De_Exames_id", new MyPKColumn() );
tabela.addColumn("Categoria_De_Exames_Nome", new MyColumn("varchar", 45) );
tabela = new MyTable("empresa");
tabela.addColumn("Empresa_id", new MyPKColumn() );
tabela.addColumn("fk_User_id", new MyFKColumn("user", "User_id") );
tabela.addColumn("Empresa_Nome", new MyColumn("varchar", 45) );
tabela.addColumn("Empresa_CNPJ", new MyColumn("varchar", 21) );
tabela.addColumn("Empresa_IE", new MyColumn("varchar", 21) );
tabela = new MyTable("especialidade");
tabela.addColumn("Especialidade_id", new MyPKColumn() );
tabela.addColumn("Especialidade_Nome", new MyColumn("varchar", 45) );
tabela = new MyTable("exame");
tabela.addColumn("Exame_id", new MyPKColumn() );
tabela.addColumn("Exame_Nome", new MyColumn("varchar", 60) );
tabela.addColumn("Exame_Descricao", new MyColumn("varchar", 9000) );
tabela = new MyTable("funcionario");
tabela.addColumn("Funcionario_id", new MyPKColumn() );
tabela.addColumn("fk_User_id", new MyPKColumn() );
tabela.addColumn("Funcionario_Nome", new MyColumn("varchar", 45) );
tabela.addColumn("Funcionario_Matricula", new MyColumn("varchar", 11) );
tabela = new MyTable("funcionario_possui_cargo");
tabela.addColumn("fk_Funcionario_id", new MyPKColumn() );
tabela.addColumn("fk_Cargo_id", new MyPKColumn() );
tabela = new MyTable("funcionario_realiza_pacote_de_exames");
tabela.addColumn("fk_Funcionario_id", new MyPKColumn() );
tabela.addColumn("fk_Pacote_De_exames_id", new MyPKColumn() );
tabela.addColumn("Data_De_Entrega", new MyPKColumn() );
tabela.addColumn("Pacote_Realizado_id", new MyColumn("int", null) );
tabela.addColumn("fk_Medico_id", new MyColumn("int", null) );
tabela.addColumn("Laldo_Medico", new MyColumn("text", 65535) );
tabela = new MyTable("medico");
tabela.addColumn("fk_User_id", new MyPKColumn() );
tabela.addColumn("Nome", new MyColumn("varchar", 45) );
tabela.addColumn("CRM", new MyColumn("varchar", 21) );
tabela = new MyTable("medico_possui_especialidade");
tabela.addColumn("fk_Medico_id", new MyPKColumn() );
tabela.addColumn("fk_Especialidade_id", new MyPKColumn() );
tabela = new MyTable("pacote_de_exames");
tabela.addColumn("Pacote_De_Exames_id", new MyPKColumn() );
tabela.addColumn("fk_Categoria_De_Exames_id", new MyFKColumn("categoria_de_exames", "Categoria_De_Exames_id") );
tabela.addColumn("fk_Medico_id", new MyFKColumn("medico", "fk_User_id") );
tabela.addColumn("Pacote_De_Exames_Descricao", new MyColumn("varchar", 9000) );
tabela = new MyTable("pacote_de_exames_possui_exame");
tabela.addColumn("fk_Pacote_De_exames_id", new MyPKColumn() );
tabela.addColumn("fk_Exame_id", new MyPKColumn() );
tabela = new MyTable("resultado_do_exame");
tabela.addColumn("Resultado_Do_Exame_id", new MyPKColumn() );
tabela.addColumn("fk_Exame_id", new MyPKColumn() );
tabela.addColumn("fk_Pacote_Realizado", new MyFKColumn("funcionario_realiza_pacote_de_exames", "Pacote_Realizado_id") );
tabela.addColumn("Data_Do_Exame", new MyColumn("date", null) );
tabela.addColumn("Resultado_Descricao", new MyColumn("text", 65535) );
tabela = new MyTable("risco_ocupacional");
tabela.addColumn("Risco_id", new MyPKColumn() );
tabela.addColumn("fk_Medico_id", new MyFKColumn("medico", "fk_User_id") );
tabela.addColumn("Risco_Nome", new MyColumn("varchar", 45) );
tabela.addColumn("Risco_Descricao", new MyColumn("text", 65535) );
tabela = new MyTable("role");
tabela.addColumn("Role_id", new MyPKColumn() );
tabela.addColumn("RoleName", new MyColumn("varchar", 45) );
tabela = new MyTable("user");
tabela.addColumn("User_id", new MyPKColumn() );
tabela.addColumn("User_Name", new MyColumn("varchar", 45) );
tabela.addColumn("Password", new MyColumn("varchar", 45) );
tabela.addColumn("fk_Role_id", new MyFKColumn("role", "Role_id") );
System.out.println("End");
/*
SELECT * FROM information_schema.KEY_COLUMN_USAGE K WHERE CONSTRAINT_SCHEMA = 'pcmso';
#CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, POSITION_IN_UNIQUE_CONSTRAINT, REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
SELECT * FROM information_schema.`COLUMNS` C, information_schema.KEY_COLUMN_USAGE K;
UIS: idéia: atribuir pesos para as tabelas..
*/
}
}