Search in sources :

Example 1 with POJOAttribute

use of com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute in project cubrid-manager by CUBRID.

the class JavaType method getPOJOAttribute.

/**
	 * Get POJOAttribute by DBAttribute
	 * 
	 * @param dbAttribute DBAttribute
	 * @return attribute POJOAttribute
	 */
private static POJOAttribute getPOJOAttribute(DBAttribute dbAttribute, boolean isJava) {
    String strType = dbAttribute.getType();
    String type = DataType.getTypePart(strType);
    Integer precision = DataType.getSize(strType);
    Integer scale = DataType.getScale(strType);
    /* init data type */
    JavaType model = getJaveType(type, precision, scale);
    if (model == null) {
        LOGGER.debug("Can process complex type:" + dbAttribute.toString());
        return null;
    }
    String elemStrType = DataType.getElemType(strType);
    Integer elemPrecision = -1;
    Integer elemScale = -1;
    String elemType = null;
    JavaType elemModel = null;
    if (elemStrType != null && elemStrType.length() > 0) {
        elemType = DataType.getTypePart(elemStrType);
        elemPrecision = DataType.getSize(elemStrType);
        elemScale = DataType.getScale(elemStrType);
        elemModel = getJaveType(elemType, elemPrecision, elemScale);
        if (elemModel == null) {
            LOGGER.debug("Can process complex element type:" + dbAttribute.toString());
        }
    }
    POJOAttribute attribute = new POJOAttribute();
    attribute.setDbName(dbAttribute.getName());
    attribute.setDbType(type);
    attribute.setDbPrecision(precision);
    attribute.setDbScale(scale);
    attribute.setDbElemType(elemType);
    attribute.setDbElemPrecision(elemPrecision);
    attribute.setDbElemScale(elemScale);
    attribute.setDbDefaultValue(dbAttribute.getDefault());
    attribute.setJavaName(getAttrbuteStyleName(attribute.getDbName()));
    attribute.setJavaType(model.getType());
    attribute.setImportPackage(model.getJavaPackage());
    if (elemModel != null) {
        attribute.setElemType(elemModel.getType());
        attribute.setElemImportPackage(elemModel.getJavaPackage());
    }
    if (isJava)
        initJavaPOJOAttribute(attribute);
    else
        initPhpPOJOAttribute(attribute);
    return attribute;
}
Also used : POJOAttribute(com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute)

Example 2 with POJOAttribute

use of com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute in project cubrid-manager by CUBRID.

the class JavaType method getJavaPOJOString.

/**
	 * Get the Java POJO content string
	 * 
	 * @param template
	 * @return String - The POJO String
	 */
private static String getJavaPOJOString(POJOTemplate template) {
    StringBuffer sb = new StringBuffer();
    sb.append("/*").append(NEW_LINE);
    sb.append(getCRUDQuery(template));
    sb.append("*/").append(NEW_LINE);
    /* Imported package */
    for (POJOAttribute attrbute : template.getAttributes()) {
        if (StringUtil.isNotEmpty(attrbute.getImportPackage())) {
            template.getImportPackages().add(attrbute.getImportPackage());
        }
        if (StringUtil.isNotEmpty(attrbute.getElemImportPackage())) {
            template.getImportPackages().add(attrbute.getElemImportPackage());
        }
    }
    for (String importedPackage : template.getImportPackages()) {
        sb.append("import " + importedPackage + SEMICOLON + NEW_LINE);
    }
    /* Type declare */
    if (sb.length() > 0) {
        sb.append(NEW_LINE);
    }
    if (StringUtil.isNotEmpty(template.getAnnotation())) {
        sb.append(template.getAnnotation() + NEW_LINE);
    }
    sb.append(template.getTypeDeclare() + " {" + NEW_LINE);
    /* Attribute */
    for (POJOAttribute attribute : template.getAttributes()) {
        if (StringUtil.isNotEmpty(attribute.getAnnotation())) {
            sb.append(ONE_INDENTATION + attribute.getAnnotation() + NEW_LINE);
        }
        sb.append(ONE_INDENTATION + "private " + getPOJOType(attribute) + " " + attribute.getJavaName() + " = null");
        sb.append(SEMICOLON + NEW_LINE);
    }
    sb.append(NEW_LINE);
    /* Get and Set method */
    for (POJOAttribute attribute : template.getAttributes()) {
        /* Get method */
        if (StringUtil.isNotEmpty(attribute.getGetAnnotation())) {
            sb.append(attribute.getGetAnnotation() + NEW_LINE);
        }
        sb.append(attribute.getGetMethod() + NEW_LINE);
        /* Set method */
        if (StringUtil.isNotEmpty(attribute.getSetAnnotation())) {
            sb.append(attribute.getSetAnnotation() + NEW_LINE);
        }
        sb.append(attribute.getSetMethod() + NEW_LINE);
    }
    sb.append("}" + NEW_LINE);
    return sb.toString();
}
Also used : POJOAttribute(com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute)

Example 3 with POJOAttribute

use of com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute in project cubrid-manager by CUBRID.

the class JavaType method getJavaPOJOString.

/**
	 * Get the POJO String, The type of schemaNode should be table or class
	 * 
	 * @param schemaNode
	 * @return the POJO String
	 */
public static String getJavaPOJOString(Connection connection, DefaultSchemaNode schemaNode) {
    CubridDatabase database = schemaNode.getDatabase();
    String tableName = schemaNode.getName();
    SchemaInfo schemaInfo = null;
    if (connection == null) {
        schemaInfo = database.getDatabaseInfo().getSchemaInfo(tableName);
    } else {
        schemaInfo = database.getDatabaseInfo().getSchemaInfo(connection, tableName);
    }
    if (schemaInfo == null) {
        com.cubrid.common.ui.spi.util.CommonUITool.openErrorBox(Messages.bind(Messages.errGetSchemaInfo, tableName));
        LOGGER.debug("Can't get the SchemaInfo:" + tableName);
        return "";
    }
    POJOTemplate template = new POJOTemplate();
    template.setTableName(tableName);
    StringBuffer typeDeclareSB = new StringBuffer();
    typeDeclareSB.append("public class ");
    typeDeclareSB.append(getUpperName(tableName));
    template.setTypeDeclare(typeDeclareSB.toString());
    StringBuffer annotationSB = new StringBuffer();
    annotationSB.append("/**" + NEW_LINE);
    annotationSB.append(" * Table name : " + tableName + NEW_LINE);
    annotationSB.append(" * Generated by CUBRID Tools." + NEW_LINE);
    annotationSB.append(" */");
    template.setAnnotation(annotationSB.toString());
    /* Attributes */
    for (DBAttribute dbAttribute : schemaInfo.getAttributes()) {
        POJOAttribute attribute = getPOJOAttribute(dbAttribute, true);
        if (attribute != null) {
            template.getAttributes().add(attribute);
        }
    }
    /* Class Attribute */
    for (DBAttribute dbAttribute : schemaInfo.getClassAttributes()) {
        POJOAttribute attribute = getPOJOAttribute(dbAttribute, true);
        if (attribute != null) {
            template.getAttributes().add(attribute);
        }
    }
    return getJavaPOJOString(template);
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) POJOTemplate(com.cubrid.cubridmanager.core.cubrid.table.model.POJOTemplate) POJOAttribute(com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 4 with POJOAttribute

use of com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute in project cubrid-manager by CUBRID.

the class JavaType method getPhpPOJOString.

/**
	 * Get the PHP POJO String, The type of schemaNode should be table or class
	 * 
	 * @param schemaNode
	 * @return the POJO String
	 */
public static String getPhpPOJOString(Connection connection, DefaultSchemaNode schemaNode) {
    CubridDatabase database = schemaNode.getDatabase();
    String tableName = schemaNode.getName();
    SchemaInfo schemaInfo = database.getDatabaseInfo().getSchemaInfo(connection, tableName);
    if (schemaInfo == null) {
        com.cubrid.common.ui.spi.util.CommonUITool.openErrorBox(Messages.bind(Messages.errGetSchemaInfo, tableName));
        LOGGER.debug("Can't get the SchemaInfo:" + tableName);
        return "";
    }
    POJOTemplate template = new POJOTemplate();
    template.setTableName(tableName);
    StringBuffer typeDeclareSB = new StringBuffer();
    typeDeclareSB.append("class ");
    typeDeclareSB.append(getUpperName(tableName));
    template.setTypeDeclare(typeDeclareSB.toString());
    StringBuffer annotationSB = new StringBuffer();
    annotationSB.append("/**" + NEW_LINE);
    annotationSB.append(" * Table name : " + tableName + NEW_LINE);
    annotationSB.append(" * Generated by CUBRID Tools." + NEW_LINE);
    annotationSB.append(" */");
    template.setAnnotation(annotationSB.toString());
    /* Attributes */
    for (DBAttribute dbAttribute : schemaInfo.getAttributes()) {
        POJOAttribute attribute = getPOJOAttribute(dbAttribute, false);
        if (attribute != null) {
            template.getAttributes().add(attribute);
        }
    }
    /* Class Attribute */
    for (DBAttribute dbAttribute : schemaInfo.getClassAttributes()) {
        POJOAttribute attribute = getPOJOAttribute(dbAttribute, false);
        if (attribute != null) {
            template.getAttributes().add(attribute);
        }
    }
    return getPhpPOJOString(template);
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) POJOTemplate(com.cubrid.cubridmanager.core.cubrid.table.model.POJOTemplate) POJOAttribute(com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 5 with POJOAttribute

use of com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute in project cubrid-manager by CUBRID.

the class JavaType method getCRUDQuery.

private static String getCRUDQuery(POJOTemplate template) {
    StringBuilder sql = new StringBuilder();
    sql.append(NEW_LINE);
    //
    // SELECT
    //
    int len = template.getAttributes().size();
    int index = 0;
    StringBuilder cols = new StringBuilder();
    StringBuilder cons = new StringBuilder();
    for (POJOAttribute attrbute : template.getAttributes()) {
        index++;
        String col = QuerySyntax.escapeKeyword(attrbute.getDbName());
        cols.append("\t").append(col);
        if (len > index) {
            cols.append(",");
        }
        cols.append(NEW_LINE);
        cons.append("\t");
        if (index > 1) {
            cons.append("AND ");
        }
        cons.append(col).append(" = #").append(getAttrbuteStyleName(col)).append("#");
        cons.append(NEW_LINE);
    }
    sql.append("// SELECT").append(NEW_LINE);
    sql.append("SELECT").append(NEW_LINE);
    sql.append(cols);
    sql.append("FROM").append(NEW_LINE);
    sql.append("\t").append(QuerySyntax.escapeKeyword(template.getTableName())).append(NEW_LINE);
    sql.append("WHERE").append(NEW_LINE);
    sql.append(cons);
    sql.append(NEW_LINE);
    //
    // INSERT
    //
    len = template.getAttributes().size();
    index = 0;
    cols.delete(0, cols.length());
    cons.delete(0, cons.length());
    for (POJOAttribute attrbute : template.getAttributes()) {
        index++;
        String col = QuerySyntax.escapeKeyword(attrbute.getDbName());
        cols.append("\t").append(col);
        if (len > index) {
            cols.append(",");
        }
        cols.append(NEW_LINE);
        cons.append("\t#").append(getAttrbuteStyleName(col)).append("#");
        if (len > index) {
            cons.append(",");
        }
        cons.append(NEW_LINE);
    }
    sql.append("// INSERT").append(NEW_LINE);
    sql.append("INSERT INTO ").append(QuerySyntax.escapeKeyword(template.getTableName())).append(NEW_LINE);
    sql.append("(").append(NEW_LINE);
    sql.append(cols);
    sql.append(")").append(NEW_LINE);
    sql.append("VALUES").append(NEW_LINE);
    sql.append("(").append(NEW_LINE);
    sql.append(cons);
    sql.append(")").append(NEW_LINE);
    sql.append(NEW_LINE);
    //
    // UPDATE
    //
    len = template.getAttributes().size();
    index = 0;
    cols.delete(0, cols.length());
    cons.delete(0, cons.length());
    for (POJOAttribute attrbute : template.getAttributes()) {
        index++;
        String col = QuerySyntax.escapeKeyword(attrbute.getDbName());
        cols.append("\t");
        cols.append(col).append(" = #").append(getAttrbuteStyleName(col)).append("#");
        if (len > index) {
            cols.append(",");
        }
        cols.append(NEW_LINE);
        cons.append("\t");
        if (index > 1) {
            cons.append("AND ");
        }
        cons.append(col).append(" = #").append(getAttrbuteStyleName(col)).append("#");
        cons.append(NEW_LINE);
    }
    sql.append("// UPDATE").append(NEW_LINE);
    sql.append("UPDATE").append(NEW_LINE);
    sql.append("\t").append(QuerySyntax.escapeKeyword(template.getTableName())).append(NEW_LINE);
    sql.append("SET").append(NEW_LINE);
    sql.append(cols);
    sql.append("WHERE").append(NEW_LINE);
    sql.append(cons);
    sql.append(NEW_LINE);
    //
    // DELETE
    //
    len = template.getAttributes().size();
    index = 0;
    cols.delete(0, cols.length());
    cons.delete(0, cons.length());
    for (POJOAttribute attrbute : template.getAttributes()) {
        index++;
        String col = QuerySyntax.escapeKeyword(attrbute.getDbName());
        cons.append("\t");
        if (index > 1) {
            cons.append("AND ");
        }
        cons.append(col).append(" = #").append(getAttrbuteStyleName(col)).append("#");
        cons.append(NEW_LINE);
    }
    sql.append("// DELETE").append(NEW_LINE);
    sql.append("DELETE FROM").append(NEW_LINE);
    sql.append("\t").append(QuerySyntax.escapeKeyword(template.getTableName())).append(NEW_LINE);
    sql.append("WHERE").append(NEW_LINE);
    sql.append(cons);
    sql.append(NEW_LINE);
    return sql.toString();
}
Also used : POJOAttribute(com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute)

Aggregations

POJOAttribute (com.cubrid.cubridmanager.core.cubrid.table.model.POJOAttribute)6 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)2 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)2 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)2 POJOTemplate (com.cubrid.cubridmanager.core.cubrid.table.model.POJOTemplate)2