-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCreateDDLMySQL.java
148 lines (135 loc) · 6.22 KB
/
CreateDDLMySQL.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
public class CreateDDLMySQL extends EdgeConvertCreateDDL {
protected String databaseName;
//this array is for determining how MySQL refers to datatypes
protected String[] strDataType = {"VARCHAR", "BOOL", "INT", "DOUBLE"};
protected StringBuffer sb;
public CreateDDLMySQL(EdgeTable[] inputTables, EdgeField[] inputFields) {
super(inputTables, inputFields);
sb = new StringBuffer();
} //CreateDDLMySQL(EdgeTable[], EdgeField[])
public CreateDDLMySQL() { //default constructor with empty arg list for to allow output dir to be set before there are table and field objects
}
public void createDDL() {
EdgeConvertGUI.setReadSuccess(true);
databaseName = generateDatabaseName();
sb.append("CREATE DATABASE " + databaseName + ";\r\n");
sb.append("USE " + databaseName + ";\r\n");
for (int boundCount = 0; boundCount <= maxBound; boundCount++) { //process tables in order from least dependent (least number of bound tables) to most dependent
for (int tableCount = 0; tableCount < numBoundTables.length; tableCount++) { //step through list of tables
if (numBoundTables[tableCount] == boundCount) { //
sb.append("CREATE TABLE " + tables[tableCount].getName() + " (\r\n");
int[] nativeFields = tables[tableCount].getNativeFieldsArray();
int[] relatedFields = tables[tableCount].getRelatedFieldsArray();
boolean[] primaryKey = new boolean[nativeFields.length];
int numPrimaryKey = 0;
int numForeignKey = 0;
for (int nativeFieldCount = 0; nativeFieldCount < nativeFields.length; nativeFieldCount++) { //print out the fields
EdgeField currentField = getField(nativeFields[nativeFieldCount]);
sb.append("\t" + currentField.getName() + " " + strDataType[currentField.getDataType()]);
if (currentField.getDataType() == 0) { //varchar
sb.append("(" + currentField.getVarcharValue() + ")"); //append varchar length in () if data type is varchar
}
if (currentField.getDisallowNull()) {
sb.append(" NOT NULL");
}
if (!currentField.getDefaultValue().equals("")) {
if (currentField.getDataType() == 1) { //boolean data type
sb.append(" DEFAULT " + convertStrBooleanToInt(currentField.getDefaultValue()));
} else { //any other data type
sb.append(" DEFAULT " + currentField.getDefaultValue());
}
}
if (currentField.getIsPrimaryKey()) {
primaryKey[nativeFieldCount] = true;
numPrimaryKey++;
} else {
primaryKey[nativeFieldCount] = false;
}
if (currentField.getFieldBound() != 0) {
numForeignKey++;
}
sb.append(",\r\n"); //end of field
}
if (numPrimaryKey > 0) { //table has primary key(s)
sb.append("CONSTRAINT " + tables[tableCount].getName() + "_PK PRIMARY KEY (");
for (int i = 0; i < primaryKey.length; i++) {
if (primaryKey[i]) {
sb.append(getField(nativeFields[i]).getName());
numPrimaryKey--;
if (numPrimaryKey > 0) {
sb.append(", ");
}
}
}
sb.append(")");
if (numForeignKey > 0) {
sb.append(",");
}
sb.append("\r\n");
}
if (numForeignKey > 0) { //table has foreign keys
int currentFK = 1;
for (int i = 0; i < relatedFields.length; i++) {
if (relatedFields[i] != 0) {
sb.append("CONSTRAINT " + tables[tableCount].getName() + "_FK" + currentFK +
" FOREIGN KEY(" + getField(nativeFields[i]).getName() + ") REFERENCES " +
getTable(getField(nativeFields[i]).getTableBound()).getName() + "(" + getField(relatedFields[i]).getName() + ")");
if (currentFK < numForeignKey) {
sb.append(",\r\n");
}
currentFK++;
}
}
sb.append("\r\n");
}
sb.append(");\r\n\r\n"); //end of table
}
}
}
}
protected int convertStrBooleanToInt(String input) { //MySQL uses '1' and '0' for boolean types
if (input.equals("true")) {
return 1;
} else {
return 0;
}
}
public String generateDatabaseName() { //prompts user for database name
String dbNameDefault = "MySQLDB";
//String databaseName = "";
do {
databaseName = (String)JOptionPane.showInputDialog(
null,
"Enter the database name:",
"Database Name",
JOptionPane.PLAIN_MESSAGE,
null,
null,
dbNameDefault);
if (databaseName == null) {
EdgeConvertGUI.setReadSuccess(false);
return "";
}
if (databaseName.equals("")) {
JOptionPane.showMessageDialog(null, "You must select a name for your database.");
}
} while (databaseName.equals(""));
return databaseName;
}
public String getDatabaseName() {
return databaseName;
}
public String getProductName() {
return "MySQL";
}
public String getSQLString() {
createDDL();
return sb.toString();
}
}//EdgeConvertCreateDDL