📄 genericfactorygenerator.java
字号:
String sqlName = subField.getSqlName (); String preName = varName +".get" + jf.getGetSet () + " ().get"; if (sqlName.equals (subPrimaryKey)) { // do nothing .. } else if (subField instanceof JCompositeField) { throw new SchemaException ("ephman.abra.tools.nocompinline", currentClass.getSchemaFileName ()); } else { String set_type = findSetType (subField); String endMod = ""; if (subField.isDate ()) set_type = "Timestamp"; else if (subField.getObjectType ().equals ("boolean")) { set_type = "String"; endMod = "? \"T\" : \"F\""; } if (subField.getObjectType().equals ("Boolean")) { factoryOutFile.write ("\t\tstmt.setString (i++, "+ "getBooleanAsString("+ preName + subField.getGetSet () + " ())" + endMod + ");\n"); } else factoryOutFile.write ("\t\tstmt.set" + set_type + " (i++, "+ preName + subField.getGetSet () + " ()" + endMod + ");\n"); } } */ } else { String set_type = findSetType (jf); String tmp = ""; String get_set_name = ""; boolean is_foreign = false; if (set_type == null) { is_foreign = true; set_type = "Int"; tmp = jf.getJavaName() + "Oid"; } else { tmp = jf.getJavaName (); } get_set_name += tmp.substring (0,1).toUpperCase () + tmp.substring (1); if (i == key_loc) { prim_str = "\t\tif (update)\n\t"; prim_str += "\t\tstmt.set" + set_type + " (i++, "+ varName +".get" + get_set_name + " ());\n"; writeSetArgForPK (factoryOutFile, varName, get_set_name); // factoryOutFile.write ("\t\tif (!update)\n\t\t\tstmt.set" + set_type // + " (i++, item.get" + get_set_name + " ());\n"); } else if (jf.isDate ()) {// is date - hack factoryOutFile.write ("\t\tstmt.setTimestamp (i++, " +varName+".get" + get_set_name + " ());\n"); } else if (jf.getObjectType().equals ("boolean")) {// is boolean - hack factoryOutFile.write ("\t\tstmt.setString (i++, " +varName+".get" + get_set_name + " ()? \"T\" : \"F\");\n"); } else if (jf.getObjectType().equals ("Boolean")) { factoryOutFile.write ("\t\tstmt.setString (i++, "+ "getBooleanAsString("+ varName + ".get" + get_set_name + " ()));\n"); } else if (jf.getObjectType().startsWith ("java.math")) { factoryOutFile.write ("\t\tif (" + varName + ".get" + jf.getGetSet ()+ "() == null) \n"); factoryOutFile.write ("\t\t\tstmt.setNull (i++, java.sql.Types."); if (jf.getObjectType().equals ("java.math.BigDecimal")) factoryOutFile.write ("DECIMAL"); else factoryOutFile.write ("INTEGER"); factoryOutFile.write (");\n\t\telse\n\t\t\tstmt.setObject (i++, "+ varName + ".get" + get_set_name + " ());\n"); } else { if (set_type.equals ("Object") || is_foreign) { factoryOutFile.write ("\t\tif (" + varName + ".get" + jf.getGetSet ()+ "() == null) {\n"); if (is_foreign) factoryOutFile.write ("\t\t\tif (" + varName + ".get" + jf.getGetSet ()+ "Oid () == 0)\n\t"); factoryOutFile.write ("\t\t\tstmt.setNull (i++, java.sql.Types."); } if (set_type.equals ("Object")) { //#AMHERE NativeObjectMapping nom = (NativeObjectMapping)nativeSetTypes.get (jf.getObjectType()); factoryOutFile.write (nom.sqlTypes + ");\n\t\t} else"); factoryOutFile.write ("\t\t\tstmt.set" + nom.setName + " (i++, "+ varName +".get" + get_set_name + " ()."+nom.nativeGetter+");\n"); } else if (is_foreign) { factoryOutFile.write ("INTEGER);\n"); factoryOutFile.write ("\t\t\telse\n"); factoryOutFile.write ("\t\t\t\tstmt.set" + set_type + " (i++, "+ varName +".get" + get_set_name + " ());\n"); factoryOutFile.write ("\t\t} else\n\t"); factoryOutFile.write ("\t\tstmt.set" + set_type + " (i++, "+ varName +".get" + jf.getGetSet () + " ().getOid ());\n"); } else factoryOutFile.write ("\t\tstmt.set" + set_type + " (i++, "+ varName +".get" + get_set_name + " ());\n"); } } } else key_loc++; } factoryOutFile.write (prim_str); factoryOutFile.write ("\t}\n"); writeClobMethods (factoryOutFile, currentClass, clobList); } /** methods which should be implemented by descendant generator */ public abstract String getPrimaryKeyString (JClass currentClass); protected void writeSetArgForPK (FileWriter factoryOutFile, String varName, String get_set_name) throws IOException { // nothing for most dbs (db2 and any others that don't support sequences can call a KeyService.. } //// protected void writeFactoryMethods (FileWriter factoryOutFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { if (currentClass.getPrimaryKeyJava ().equals ("")) { if (!currentClass.isManyToMany ()) { Debugger.trace ("!!No primary key found for class " + currentClass.getClassName (), Debugger.ERROR); } else { Debugger.trace (currentClass.getClassName () + " is many-to-many.", Debugger.VERBOSE); // many to many writeManyToManyMethods (factoryOutFile, currentClass, allFields); } } else { // If this a query based factory - no updates are allowed if (!currentClass.isQuery()) { // All these methods are for updates writeUpdateMethod (factoryOutFile, currentClass, allFields); // now gen the insert string int key_loc = writeInsertMethod (factoryOutFile, currentClass, allFields); writeSetArgMethod (factoryOutFile, currentClass, allFields, key_loc); writeProcs (factoryOutFile, currentClass, allFields); writeStoreAndDeleteMethods (factoryOutFile, currentClass, allFields); } else { // write methods needed for Query based factory writeQueryBasedFactoryMethods (factoryOutFile, currentClass); writeHasClobsMethod(factoryOutFile, hasClobs (allFields)); } writeMakeFromRs (factoryOutFile, currentClass, allFields); writeViewCode (factoryOutFile, currentClass, allFields); writeBasicQuery (factoryOutFile, currentClass); // writeDeepRetrieval (factoryOutFile, currentClass, allFields); } } protected void writeQueryBasedFactoryMethods (FileWriter outFile, JClass currentClass) throws IOException { // first write constant that is the query string - append outFile.write ("\tprivate String queryString =\n"); outFile.write ("\t\t\"" + currentClass.getQuery() + "\";\n\n"); // routine to return the sql to methods that do queries outFile.write ("\tprotected String getSelectSql () {return queryString;}\n\n"); // rotuine to get select sql with one extra predicate on one column outFile.write ("\tprotected String getSelectSql (String column) {\n"); outFile.write ("\t\treturn queryString + \" and (\" + column + \" = ?)\";\n"); outFile.write ("\t}\n\n"); // For cursor queries do the same kind of sql as normal query, since // we do not know what the primary key may be etc... The user can override // in the child class if necessary outFile.write ("\tprotected String getCursorQuerySql () {return queryString;}\n\n"); // Query already has where clause no need to add it in outFile.write ("\tprotected boolean defaultNeedsWhereLogic () {return false;};\n\n"); outFile.write ("\tprotected boolean needsAndBeforeFilter () {return true;};\n\n"); // the routines below are not yet supported - they throw exceptions String exceptionCode = "\t\tthrow new RuntimeException (\"Not supported for query based factories\");\n"; outFile.write ("\tprotected String getSelectCountSql () {\n" ); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected boolean useStoredProcs () {\n"); outFile.write ("\t\treturn false;\n"); outFile.write ("\t}\n\n"); // TODO: consider whether it makes sense to split GenricFactoryBase into two. Parent with // only queries and descendant with updates. Then the query based factories do not have to do this outFile.write ("\tprotected String getDeleteSql () {\n" ); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected void setArguments (PreparedStatement ps, Object o, boolean update) throws SQLException {\n"); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected String makeUpdateCall () {\n"); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected String makeInsertCall () {\n"); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected String makeUpdateString () {\n"); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); outFile.write ("\tprotected String makeInsertString () {\n"); outFile.write (exceptionCode); outFile.write ("\t}\n\n"); } protected void writeDeepRetrieval (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { outFile.write ("\t// method to retrieve composite objects\n"); outFile.write ("\tpublic void deepRetrieval (DatabaseSession dbSess, Identified item) throws SQLException {\n"); outFile.write ("\t\t" + currentClass.getClassName () + " foo = (" + currentClass.getClassName () + ")item;\n"); for (int i=0; i < allFields.size (); i++) { JField jf = (JField)allFields.elementAt (i); if (jf instanceof JCompositeField) { JCompositeField jcf = (JCompositeField)jf; if (jcf.getJClass () != null) { // is composite and has class(i.e. table) JClass jfClass = jcf.getJClass (); String m_name = jcf.getGetSet (); // so assume <class>Factory exists and we have generated outFile.write ("\t\tif (foo.get" + m_name + "Oid() != 0)\n"); outFile.write ("\t\t\tfoo.set" + m_name +" (" +jfClass.getClassName() + "Factory.getInstance ().getByOid (dbSess, foo.get" + m_name + "Oid()))"); } } } outFile.write ("\t\tsuper.deleteObject (dbSess, foo);\n\t}\n"); } /** routine which writes store, getByOid and delete */ protected void writeStoreAndDeleteMethods (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { outFile.write ("\t// methods to store and delete\n"); outFile.write ("\tpublic void delete (DatabaseSession dbSess, " + currentClass.getClassName () + " foo) throws SQLException {\n"); outFile.write ("\t\tsuper.deleteObject (dbSess, foo);\n\t}\n"); outFile.write ("\tpublic void store (DatabaseSession dbSess, " + currentClass.getClassName () + " foo) throws SQLException {\n"); if (currentClass.hasDescendant ()) outFile.write ("\t\tfoo.prepareToStore ();\n"); outFile.write ("\t\tsuper.storeObject (dbSess, foo);\n\t}\n"); } private void writeBasicQuery(FileWriter outFile, JClass currentClass) throws IOException { String className = currentClass.getDescendantName (); outFile.write ("\tpublic " + className + " getByOid (DatabaseSession dbSess, " + "int object_id) throws SQLException {\n"); outFile.write ("\t\treturn (" +className + ")super.getObject (dbSess, " + "this.oid, object_id);\n\t}\n"); } // defaults to true can override if false.. protected boolean usePackages () { return true; } /** write procs */ protected void writeProcs (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { String pkgName = usePackages () ? SchemaGenerator.dbPackageName + "." : ""; String insertProc = "{? = call " + pkgName + "FUNC_INS_" + currentClass.getStoredProcedureName () + "("; String updateProc = "{ call " + pkgName + "PROC_UPD_" + currentClass.getStoredProcedureName () + "("; String commonProc = ""; for (int i=0; i < allFields.size (); i++) { JField jf = (JField)allFields.elementAt (i); if (jf.isInline ()) { JCompositeField jcf = (JCompositeField)jf; JClass fieldClass = jcf.getJClass (); Vector fields = new Vector (fieldClass.getAllFields ().values ()); for (int j = 0; j < fields.size (); j++) { JField subField = (JField)fields.elementAt (j); if (subField.isDbColumn()) commonProc += "?,"; } // take away one for pk commonProc = commonProc.substring (0, commonProc.length()-2); } else if (jf.isDbColumn () && !isClob (jf)) { commonProc += "?,"; } } commonProc = commonProc.substring (0, commonProc.length()-1); insertProc += commonProc; updateProc += commonProc; if (!currentClass.isVersioned ()) { // remove one ? insertProc = insertProc.substring (0, insertProc.length()-2); } else updateProc += ",?"; insertProc += ") }"; updateProc += ") }"; outFile.write ("\n\tprotected String makeInsertCall () {\n\t\treturn insertCallString;\n\t}\n"); outFile.write ("\n\tpublic static final String insertCallString = \"" + insertProc + "\";\n"); outFile.write ("\n\tpublic static final String updateCallString = \"" + updateProc + "\";\n"); outFile.write ("\n\tprotected String makeUpdateCall () {\n\t\treturn updateCallString;\n\t}\n"); outFile.write ("\n\tprotected boolean useStoredProcs () { return "+ useProcs +"; }\n"); } protected void writeMakeFromRs (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { String class_name = currentClass.getClassName (); String desc_name = currentClass.getDescendantName (); outFile.write ("\n\n\t/* a routine to build an object of this type from an sql ResultSet */\n"); outFile.write ("\n\tprotected Identified makeFromResultSet (ResultSet rs) throws SQLException{\n"); outFile.write ("\t\t" + class_name + " foo = new " + desc_name + " ();\n"); outFile.write ("\t\trefreshFromResultSet (foo, rs);\n\t\treturn foo;\n\t}\n\n"); outFile.write ("\t/* a routine to overwrite an object of this type from an sql ResultSet */\n"); outFile.write ("\tprotected void refreshFromResultSet (Identified item, ResultSet rs) throws SQLException{\n"); outFile.write ("\t\t" + class_name + " foo = (" + class_name +")item;\n"); if (currentClass.isVersioned ()) outFile.write ("\t\tfoo.setVersion (rs.getInt(" + VERSION_NUMBER + "));\n"); for (int i = 0; i < allFields.size (); i++) { JField jf = (JField)allFields.elementAt (i); if (jf instanceof JCompositeField && jf.isDbColumn ()) { //System.out.println ("composite lookup??"); if (jf.isInline ()) { JCompositeField jcf = (JCompositeField)jf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -