/* @author Comdex * @see com.reflectsky.cozy.Ormer#close() */ @Override public void close() { // TODO 自动生成的方法存根 if (conn != null) { oManager.getConnectionPool().freeConnection(conn); } }
private void ormDebug(OperateSet oSet) { if (oManager.isDebug()) { String sql = oSet.getStrSql(); Vector<Object> param = oSet.getParam(); sql += " parameter:"; for (Object object : param) { if (object != null) { if (object.getClass().getSimpleName().equals("Date")) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = (Date) object; sql += " <" + sdf.format(date) + ">"; } else { sql += " <" + object + ">"; } } else { sql += " <null>"; } } oManager.deBugInfo(sql); } }
private OperateSet generateInsertOperateSet(Object obj) { Class<? extends Object> clazz = obj.getClass(); // 获取tablecache中的对应的info String typeName = clazz.getName(); TableInfo tbinfo = null; String autoKeyName = ""; if (typeName != null) { tbinfo = oManager.getTableCache().getByTN(typeName); } if (tbinfo == null) { // 提示错误 System.out.println( "[generateInsertOperateSet error]-Model<" + typeName + "> has not registered!"); return null; } String strSql = "insert into " + tbinfo.getTableName(); String strField = ""; String strValue = ""; Vector<Object> param = new Vector<Object>(); Vector<FieldInfo> fins = tbinfo.getAllFieldInfos(); for (FieldInfo fin : fins) { // 跳过自动增长的字段 if (fin.isAutoGenerate()) { autoKeyName = fin.getColumnName(); continue; } strField += fin.getColumnName() + ","; strValue += "?,"; // 获取值 Field fie = null; try { fie = clazz.getDeclaredField(fin.getFieldName()); fie.setAccessible(true); } catch (NoSuchFieldException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (SecurityException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } if (fie != null) { try { param.add(fie.get(obj)); } catch (IllegalArgumentException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (IllegalAccessException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } } } // 只有自增字段错误退出 if (strField.equals("")) { System.out.println( "[generateInsertOperateSet error]-Model<" + typeName + "> only have autogenerate field!"); return null; } // 去除尾部的,号 strField = strField.substring(0, strField.length() - 1); strValue = strValue.substring(0, strValue.length() - 1); strSql += " (" + strField + ") values(" + strValue + ")"; OperateSet os = new OperateSet(strSql, param, tbinfo); os.setAutoKeyName(autoKeyName); return os; }
// 生成查询Sql,以主键为查询条件,如果有指定读入的键那只读该键 private OperateSet generateReadOperateSet(Object obj, String... fieldnames) { Class<? extends Object> clazz = obj.getClass(); // 获取tablecache中的对应的info String typeName = clazz.getName(); TableInfo tbinfo = null; String autoKeyName = ""; if (typeName != null) { tbinfo = oManager.getTableCache().getByTN(typeName); } if (tbinfo == null) { // 提示错误 System.out.println( "[generateReadOperateSet error]-Model<" + typeName + "> has not registered!"); return null; } String strSql = "select "; String strField = ""; String strWhere = ""; Vector<Object> param = new Vector<Object>(); if (fieldnames.length != 0) { if (fieldnames.length == 1) { if (fieldnames[0].equals("")) { return null; } } for (String s : fieldnames) { strField += s + ","; } strField = strField.substring(0, strField.length() - 1); strSql += strField + " from " + tbinfo.getTableName(); Vector<FieldInfo> fins = tbinfo.getAllFieldInfos(); for (FieldInfo fin : fins) { // 以主键为查询条件,如果不是主键的继续,暂时不支持联合主键 if (!fin.isPrimaryKey()) { continue; } autoKeyName = fin.getColumnName(); strWhere += fin.getColumnName() + "=?"; Field field = null; try { field = obj.getClass().getDeclaredField(fin.getFieldName()); } catch (NoSuchFieldException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (SecurityException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } field.setAccessible(true); try { param.add(field.get(obj)); } catch (IllegalArgumentException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (IllegalAccessException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } } if (strWhere.equals("")) { // 提示错误 System.out.println( "[generateReadOperateSet error]-Model<" + typeName + "> has not set PrimaryKey to achieve this query!"); return null; } strSql += " where " + strWhere + " limit 1"; OperateSet oSet = new OperateSet(strSql, param, tbinfo); oSet.setAutoKeyName(autoKeyName); return oSet; } else { strSql += "* from " + tbinfo.getTableName(); Vector<FieldInfo> fins = tbinfo.getAllFieldInfos(); for (FieldInfo fin : fins) { // 以主键为查询条件,如果不是主键的继续,暂时不支持联合主键 if (!fin.isPrimaryKey()) { continue; } autoKeyName = fin.getColumnName(); strWhere += fin.getColumnName() + "=?"; Field field = null; try { field = obj.getClass().getDeclaredField(fin.getFieldName()); } catch (NoSuchFieldException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (SecurityException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } field.setAccessible(true); try { param.add(field.get(obj)); } catch (IllegalArgumentException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } catch (IllegalAccessException e) { // TODO 自动生成的 catch 块 this.oManager.deBugInfo(e.getMessage()); return null; } } if (strWhere.equals("")) { // 提示错误 System.out.println( "[generateReadOperateSet error]-Model<" + typeName + "> has not set PrimaryKey to achieve this query!"); return null; } strSql += " where " + strWhere + " limit 1"; OperateSet oSet = new OperateSet(strSql, param, tbinfo); oSet.setAutoKeyName(autoKeyName); return oSet; } }