1 | private String getOneToOneTriggerBody(ForeignKeyDefinition fkDef, | | 1 | /** |
2 | String exceptionName) { | | 2 | * Generates DDL statements for creating a table according to the parameter |
3 | String tableName = fkDef.getTableName(); | | 3 | * <code>tableDefinition</code>. |
4 | | | 4 | * |
5 | StringBuffer sb = new StringBuffer(); | | 5 | * @param tableDefinition |
6 | sb.append(" DECLARE VARIABLE x INTEGER;").append(LINE_SEPARATOR); | | 6 | * A <code>TableDefinition</code> object that holds alls |
7 | | | 7 | * necessary data for generating code. |
8 | sb.append("BEGIN").append(LINE_SEPARATOR); | | 8 | * @return The generated code. |
9 | | | 9 | * @see org.argouml.language.sql.SqlCodeCreator#createTable(org.argouml.language.sql.TableDefinition) |
10 | sb.append(" "); | | 10 | */ |
11 | sb.append("SELECT COUNT(*) FROM ").append(tableName); | | 11 | public String createTable(TableDefinition tableDefinition) { |
12 | sb.append(" WHERE "); | | 12 | StringBuffer sb = new StringBuffer(); |
13 | | | 13 | sb.append("CREATE TABLE "); |
14 | StringBuffer sbWhere = new StringBuffer(); | | 14 | sb.append(tableDefinition.getName()); |
15 | List columnNames = fkDef.getColumnNames(); | | 15 | sb.append(" (").append(LINE_SEPARATOR); |
16 | for (int i = 0; i < columnNames.size(); i++) { | | 16 | |
17 | if (sbWhere.length() > 0) { | | 17 | Iterator it = tableDefinition.getColumnDefinitions().iterator(); |
18 | sbWhere.append(" AND "); | | 18 | while (it.hasNext()) { |
19 | } | | 19 | ColumnDefinition colDef = (ColumnDefinition) it.next(); |
20 | | | 20 | sb.append(colDef.getName()).append(" "); |
21 | String colName = (String) columnNames.get(i); | | 21 | sb.append(colDef.getDatatype()); |
22 | | | 22 | Boolean nullable = colDef.getNullable(); |
23 | sbWhere.append(colName); | | 23 | if (nullable != null) { |
24 | sbWhere.append(" = NEW.").append(colName); | | 24 | if (nullable.equals(Boolean.TRUE)) { |
25 | } | | 25 | sb.append(" ").append("NULL"); |
26 | | | 26 | } else if (nullable.equals(Boolean.FALSE)) { |
27 | List pkFields = fkDef.getTable().getPrimaryKeyFields(); | | 27 | sb.append(" ").append("NOT NULL"); |
28 | for (int i = 0; i < pkFields.size(); i++) { | | 28 | } |
29 | String pkFieldName = (String) pkFields.get(i); | | 29 | } |
30 | sbWhere.append(" AND ").append(pkFieldName); | | 30 | sb.append(",").append(LINE_SEPARATOR); |
31 | sbWhere.append(" <> NEW.").append(pkFieldName); | | 31 | } |
32 | sbWhere.append(" "); | | 32 | |
33 | } | | 33 | StringBuffer sbPk = new StringBuffer(); |
34 | | | 34 | it = tableDefinition.getPrimaryKeyFields().iterator(); |
35 | sb.append(sbWhere).append(" INTO :x;").append(LINE_SEPARATOR); | | 35 | while (it.hasNext()) { |
36 | | | 36 | String primaryKeyField = (String) it.next(); |
37 | sb.append(" IF (:x = 1) THEN").append(LINE_SEPARATOR); | | 37 | if (sbPk.length() > 0) { |
38 | | | 38 | sbPk.append(", "); |
39 | sb.append(" "); | | 39 | } |
40 | sb.append("EXCEPTION ").append(exceptionName); | | 40 | sbPk.append(primaryKeyField); |
41 | sb.append(";").append(LINE_SEPARATOR); | | 41 | } |
42 | | | 42 | |
43 | sb.append("END !!").append(LINE_SEPARATOR); | | 43 | sb.append("PRIMARY KEY ("); |
44 | | | 44 | sb.append(sbPk); |
45 | return sb.toString(); | | 45 | sb.append(")").append(LINE_SEPARATOR); |
46 | } | | 46 | |
| | | 47 | sb.append(");"); |
| | | 48 | |
| | | 49 | primaryKeyCounter++; |
| | | 50 | |
| | | 51 | return sb.toString(); |
| | | 52 | } |