private String getOneToOneTriggerBody(ForeignKeyDefinition fkDef, String exceptionName) { String tableName = fkDef.getTableName(); StringBuffer sb = new StringBuffer(); sb.append(" DECLARE VARIABLE x INTEGER;").append(LINE_SEPARATOR); sb.append("BEGIN").append(LINE_SEPARATOR); sb.append(" "); sb.append("SELECT COUNT(*) FROM ").append(tableName); sb.append(" WHERE "); StringBuffer sbWhere = new StringBuffer(); List columnNames = fkDef.getColumnNames(); for (int i = 0; i < columnNames.size(); i++) { if (sbWhere.length() > 0) { sbWhere.append(" AND "); } String colName = (String) columnNames.get(i); sbWhere.append(colName); sbWhere.append(" = NEW.").append(colName); } List pkFields = fkDef.getTable().getPrimaryKeyFields(); for (int i = 0; i < pkFields.size(); i++) { String pkFieldName = (String) pkFields.get(i); sbWhere.append(" AND ").append(pkFieldName); sbWhere.append(" <> NEW.").append(pkFieldName); sbWhere.append(" "); } sb.append(sbWhere).append(" INTO :x;").append(LINE_SEPARATOR); sb.append(" IF (:x = 1) THEN").append(LINE_SEPARATOR); sb.append(" "); sb.append("EXCEPTION ").append(exceptionName); sb.append(";").append(LINE_SEPARATOR); sb.append("END !!").append(LINE_SEPARATOR); return sb.toString(); }
/** * Generates DDL statements for creating a table according to the parameter * <code>tableDefinition</code>. * * @param tableDefinition * A <code>TableDefinition</code> object that holds alls * necessary data for generating code. * @return The generated code. * @see org.argouml.language.sql.SqlCodeCreator#createTable(org.argouml.language.sql.TableDefinition) */ public String createTable(TableDefinition tableDefinition) { StringBuffer sb = new StringBuffer(); sb.append("CREATE TABLE "); sb.append(tableDefinition.getName()); sb.append(" (").append(LINE_SEPARATOR); Iterator it = tableDefinition.getColumnDefinitions().iterator(); while (it.hasNext()) { ColumnDefinition colDef = (ColumnDefinition) it.next(); sb.append(colDef.getName()).append(" "); sb.append(colDef.getDatatype()); Boolean nullable = colDef.getNullable(); if (nullable != null) { if (nullable.equals(Boolean.TRUE)) { sb.append(" ").append("NULL"); } else if (nullable.equals(Boolean.FALSE)) { sb.append(" ").append("NOT NULL"); } } sb.append(",").append(LINE_SEPARATOR); } StringBuffer sbPk = new StringBuffer(); it = tableDefinition.getPrimaryKeyFields().iterator(); while (it.hasNext()) { String primaryKeyField = (String) it.next(); if (sbPk.length() > 0) { sbPk.append(", "); } sbPk.append(primaryKeyField); } sb.append("PRIMARY KEY ("); sb.append(sbPk); sb.append(")").append(LINE_SEPARATOR); sb.append(");"); primaryKeyCounter++; return sb.toString(); }
Clone fragments detected by clone detection tool
File path: /ArgoUML-0.34-src/argouml-sql/src/org/argouml/language/sql/FirebirdSqlCodeCreator.java File path: /ArgoUML-0.34-src/argouml-sql/src/org/argouml/language/sql/MySqlCodeCreator.java
Method name: String getOneToOneTriggerBody(ForeignKeyDefinition, String) Method name: String createTable(TableDefinition)
Number of AST nodes: 6 Number of AST nodes: 5
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
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements6
    Number of unmapped statements in the first code fragment2
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.833
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    10
    for (int i = 0; i < columnNames.size(); i++)
    10
    for (int i = 0; i < columnNames.size(); i++)
    19
    while (it.hasNext())
    Differences
    Expression1Expression2Difference
    i < columnNames.size()it.hasNext()TYPE_COMPATIBLE_REPLACEMENT
    columnNamestableDefinition.getPrimaryKeyFields()TYPE_COMPATIBLE_REPLACEMENT
    columnNamestableDefinition.getPrimaryKeyFields()TYPE_COMPATIBLE_REPLACEMENT
    19
    while (it.hasNext())
    11
    if (sbWhere.length() > 0)
    11
    if (sbWhere.length() > 0)
    21
    if (sbPk.length() > 0)
    Differences
    Expression1Expression2Difference
    sbWheresbPkVARIABLE_NAME_MISMATCH
    21
    if (sbPk.length() > 0)
    12
    sbWhere.append(" AND ");
    12
    sbWhere.append(" AND ");
    22
    sbPk.append(", ");
    Differences
    Expression1Expression2Difference
    " AND "", "LITERAL_VALUE_MISMATCH
    sbWheresbPkVARIABLE_NAME_MISMATCH
    22
    sbPk.append(", ");
    13
    String colName = (String)columnNames.get(i);
    13
    String colName = (String)columnNames.get(i);
    Preondition Violations
    Unmatched statement String colName=(String)columnNames.get(i); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                                                                
                                                                                              
    20
    String primaryKeyField = (String)it.next();
    14
    sbWhere.append(colName);
    14
    sbWhere.append(colName);
    23
    sbPk.append(primaryKeyField);
    Differences
    Expression1Expression2Difference
    colNameprimaryKeyFieldVARIABLE_NAME_MISMATCH
    sbWheresbPkVARIABLE_NAME_MISMATCH
    23
    sbPk.append(primaryKeyField);
    15
    sbWhere.append(" = NEW.").append(colName);
                                                                                                
    Precondition Violations (2)
    Row Violation
    1Unmatched statement String colName=(String)columnNames.get(i); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    2Clone fragment #1 returns variables i , while Clone fragment #2 returns variables