private static void generateSingle(Table table, Proc proc, PrintWriter outData) { String dataStruct; if (proc.isStd || proc.isStdExtended()) dataStruct = table.useName(); else dataStruct = table.useName() + proc.upperFirst(); String parameters = ""; boolean hasInput = (proc.inputs.size() > 0 || proc.dynamics.size() > 0); outData.println(" def " + proc.name + "(self):"); outData.println(" ''' Single returns boolean and record"); if (hasInput == true) { outData.println(" Input:"); for (int f = 0; f < proc.inputs.size(); f++) { Field field = (Field) proc.inputs.elementAt(f); outData.println(" " + field.useName()); parameters += ", " + field.useName() + "=" + defValue(field); } for (int f = 0; f < proc.dynamics.size(); f++) { String field = (String) proc.dynamics.elementAt(f); outData.println(" " + field); parameters += ", " + field + "=''"; } } outData.println(" Output:"); for (int f = 0; f < proc.outputs.size(); f++) { Field field = (Field) proc.outputs.elementAt(f); outData.println(" " + field.useName()); } outData.println(" '''"); outData.println( " return " + pymodFront + table.useName() + proc.upperFirst() + "(self)"); if (hasInput == true) generateInput(table.useName(), dataStruct, proc.name, parameters, outData); else generateCover(table.useName(), dataStruct, proc.name, outData); }
/** Build of standard and user defined procedures */ static void generate(Table table, String output, PrintWriter outLog) { try { outLog.println("Code: " + output + table.useName() + ".cls"); OutputStream outFile = new FileOutputStream(output + table.useName() + ".cls"); try { PrintWriter outData = new PrintWriter(outFile); generateStd(table, outData); generateOther(table, output, outLog); outData.flush(); } finally { outFile.close(); } } catch (IOException e1) { outLog.println("Generate Procs IO Error"); } }
private static void generateStructs(Table table, String output, PrintWriter outLog) { try { outLog.println("Code: " + output + table.useName() + ".h"); OutputStream outFile = new FileOutputStream(output + table.useName() + ".h"); try { PrintWriter outData = new PrintWriter(outFile); try { outData.println( "// This code was generated, do not modify it, modify it at source and regenerate it."); outData.println("// 1: Mutilation, Spindlization and Bending will result in goto 1"); outData.println(); outData.println("#import \"cracklerdc.h\""); outData.println(); generateInterfaceStructs(table, outData); } finally { outData.flush(); } } finally { outFile.close(); } outLog.println("Code: " + output + table.useName() + ".m"); outFile = new FileOutputStream(output + table.useName() + ".m"); try { PrintWriter outData = new PrintWriter(outFile); try { outData.println( "// This code was generated, do not modify it, modify it at source and regenerate it."); outData.println("// 1: Mutilation, Spindlization and Bending will result in goto 1"); outData.println(); outData.println("#import \"" + table.useName() + ".h\""); outData.println(); generateImplementationStructs(table, outData); } finally { outData.flush(); } } finally { outFile.close(); } } catch (IOException e1) { outLog.println("Generate Procs IO Error"); } }
private static void generateEnumOrdinals(Table table, PrintWriter outData) { for (int i = 0; i < table.fields.size(); i++) { Field field = (Field) table.fields.elementAt(i); if (field.enums.size() > 0) { outData.println("enum e" + table.useName() + field.useName()); String start = "{"; for (int j = 0; j < field.enums.size(); j++) { Enum element = (Enum) field.enums.elementAt(j); String evalue = "" + element.value; if (field.type == Field.ANSICHAR && field.length == 1) evalue = "'" + (char) element.value + "'"; outData.println( start + " " + table.useName() + field.useName() + element.name + " = " + evalue); start = ","; } outData.println("};"); outData.println(); outData.println("inline NSString *" + table.useName() + field.useName() + "Lookup(int no)"); outData.println("{"); outData.println(" switch(no)"); outData.println(" {"); for (int j = 0; j < field.enums.size(); j++) { Enum element = (Enum) field.enums.elementAt(j); String evalue = "" + element.value; if (field.type == Field.ANSICHAR && field.length == 1) evalue = "'" + (char) element.value + "'"; outData.println(" case " + evalue + ": return @\"" + element.name + "\";"); } outData.println(" default: return @\"<unknown value>\";"); outData.println(" }"); outData.println("}"); outData.println(); } if (field.valueList.size() > 0) { outData.println("enum e" + table.useName() + field.useName()); String start = "{"; for (int j = 0; j < field.valueList.size(); j++) { String evalue = (String) field.valueList.elementAt(j); outData.println(start + " " + table.useName() + field.useName() + evalue); start = ","; } outData.println("};"); outData.println(); outData.println("inline NSString *" + table.useName() + field.useName() + "Lookup(int no)"); outData.println("{"); outData.println(" switch(no)"); outData.println(" {"); for (int j = 0; j < field.valueList.size(); j++) { String evalue = (String) field.valueList.elementAt(j); outData.println(" case " + j + ": return @\"" + evalue + "\";"); } outData.println(" default: return @\"<unknown value>\";"); outData.println(" }"); outData.println("}"); outData.println(); } } }
private static void generateAction(Table table, Proc proc, PrintWriter outData) { String dataStruct; if (proc.isStd || proc.isStdExtended()) dataStruct = table.useName(); else dataStruct = table.useName() + proc.upperFirst(); String parameters = ""; outData.println(" def " + proc.name + "(self):"); outData.println(" ''' Action"); outData.println(" Input:"); for (int f = 0; f < proc.inputs.size(); f++) { Field field = (Field) proc.inputs.elementAt(f); outData.println(" " + field.useName()); parameters += ", " + field.useName() + "=" + defValue(field); } for (int f = 0; f < proc.dynamics.size(); f++) { String field = (String) proc.dynamics.elementAt(f); outData.println(" " + field); parameters += ", " + field + "=''"; } outData.println(" '''"); outData.println( " return " + pymodFront + table.useName() + proc.upperFirst() + "(self)"); generateInput(table.useName(), dataStruct, proc.name, parameters, outData); }
private static void generateInterfaceStructs(Table table, PrintWriter outData) { if (table.fields.size() > 0) { if (table.comments.size() > 0) { for (int i = 0; i < table.comments.size(); i++) { String s = (String) table.comments.elementAt(i); outData.println("// " + s); } } generateInterfaceTableStructs(table.fields, table.useName(), outData); generateEnumOrdinals(table, outData); } for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData || proc.isStd || proc.isStdExtended() || proc.hasNoData()) continue; if (proc.comments.size() > 0) { for (int j = 0; j < proc.comments.size(); j++) { String s = (String) proc.comments.elementAt(j); outData.println("// " + s); } } generateInterfaceStructPairs(proc, table.useName() + proc.upperFirst(), outData); } }
private static void generateBulkAction(Table table, Proc proc, PrintWriter outData) { outData.println(" def " + proc.name + "(self, recs):"); outData.println(" ''' Bulk Action"); outData.println(" Input:"); for (int f = 0; f < proc.inputs.size(); f++) { Field field = (Field) proc.inputs.elementAt(f); outData.println(" " + field.useName()); } for (int f = 0; f < proc.dynamics.size(); f++) { String field = (String) proc.dynamics.elementAt(f); outData.println(" " + field); } outData.println(" '''"); outData.println( " " + pymodFront + table.useName() + proc.upperFirst() + "(len(recs), recs)"); }
/** Build of all required standard procedures */ static void generateOther(Table table, String output, PrintWriter outLog) { for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData || proc.isStd || proc.hasNoData()) continue; try { outLog.println("Code: " + output + table.useName() + proc.upperFirst() + ".cls"); OutputStream outFile = new FileOutputStream(output + table.useName() + proc.upperFirst() + ".cls"); try { PrintWriter outData = new PrintWriter(outFile); outData.println("VERSION 1.0 CLASS"); outData.println("BEGIN"); outData.println(" MultiUse = -1"); outData.println("END"); outData.println("ATTRIBUTE VB_NAME=\"t" + table.useName() + proc.upperFirst() + "\""); outData.println("Option explicit"); outData.println( "' This code was generated, do not modify it, modify it at source and regenerate it."); for (int j = 0; j < proc.comments.size(); j++) { String comment = (String) proc.comments.elementAt(j); outData.println("'" + comment); } boolean hasMulti = true; if (proc.inputs.size() > 0) {} if (proc.outputs.size() == 0 || proc.isSingle == true) hasMulti = false; outData.println(); outData.println("Private Type TRec"); for (int j = 0; j < proc.inputs.size(); j++) { Field field = (Field) proc.inputs.elementAt(j); String io; if (!proc.hasOutput(field.name)) io = "input"; else io = "input-output"; outData.println(" " + varType(field) + " '" + io); for (int c = 0; c < field.comments.size(); c++) { String s = (String) field.comments.elementAt(c); outData.println("'" + s); } if (field.isNull && notString(field)) outData.println(" " + field.useName() + "IsNull as Boolean"); } for (int j = 0; j < proc.outputs.size(); j++) { Field field = (Field) proc.outputs.elementAt(j); if (!proc.hasInput(field.name)) { outData.println(" " + varType(field) + " 'output"); for (int c = 0; c < field.comments.size(); c++) { String s = (String) field.comments.elementAt(c); outData.println("'" + s); } if (field.isNull && notString(field)) outData.println(" " + field.useName() + "IsNull as Boolean"); } } for (int j = 0; j < proc.dynamics.size(); j++) { String s = (String) proc.dynamics.elementAt(j); outData.println(" " + s + " As String 'dynamic"); } outData.println("End Type"); outData.println(); if (hasMulti) { outData.println("Private Rec() as TRec"); outData.println("Public RecCount as Long"); outData.println("Private " + proc.name + "Cursor As TCursor"); } else outData.println("Private Rec as TRec"); outData.println(); generateCopy(table.useName() + proc.upperFirst(), outData, hasMulti); String optZero = hasMulti ? "(0)" : ""; String optIndex = hasMulti ? "Optional aIndex as Long" : ""; String optIndex1 = hasMulti ? ", Optional aIndex as Long" : ""; String optIndex2 = hasMulti ? "Optional aIndex as Long, " : ""; String optUseIndex = hasMulti ? "(aIndex)" : ""; outData.println("Private Sub Class_Initialize()"); if (hasMulti) outData.println(" RecClear"); for (int j = 0; j < proc.inputs.size(); j++) { Field field = (Field) proc.inputs.elementAt(j); outData.println(" Rec" + optZero + "." + initVBVar(field)); if (field.isNull && notString(field)) outData.println(" Rec" + optZero + "." + field.useName() + "IsNull = false"); } for (int j = 0; j < proc.outputs.size(); j++) { Field field = (Field) proc.outputs.elementAt(j); if (proc.hasInput(field.name)) continue; outData.println(" Rec" + optZero + "." + initVBVar(field)); if (field.isNull && notString(field)) outData.println(" Rec" + optZero + "." + field.useName() + "IsNull = false"); } for (int j = 0; j < proc.dynamics.size(); j++) { String s = (String) proc.dynamics.elementAt(j); outData.println(" Rec" + optZero + "." + s + " = \"\""); } outData.println("End Sub"); outData.println(); if (ioRoutines) { outData.println("Public Sub Class_PutFile(OutFile as Long" + optIndex1 + ")"); outData.println(" Put OutFile,, Rec" + optUseIndex); outData.println("End Sub"); outData.println(); outData.println("Public Sub Class_GetFile(InFile as Long" + optIndex1 + ")"); outData.println(" Get InFile,, Rec" + optUseIndex); outData.println("End Sub"); outData.println(); } for (int j = 0; j < proc.inputs.size(); j++) { Field field = (Field) proc.inputs.elementAt(j); generateProperties(field, outData, optIndex, optIndex2, optUseIndex); } for (int j = 0; j < proc.outputs.size(); j++) { Field field = (Field) proc.outputs.elementAt(j); if (proc.hasInput(field.name)) continue; generateProperties(field, outData, optIndex, optIndex2, optUseIndex); } for (int j = 0; j < proc.dynamics.size(); j++) { String s = (String) proc.dynamics.elementAt(j); outData.println("Public Property Get " + s + "(" + optIndex + ") as String"); outData.println(" " + s + " = Rec" + optUseIndex + "." + s); outData.println("End Property"); outData.println(); outData.println("Public Property Let " + s + "(" + optIndex2 + "aValue as String)"); outData.println(" Rec" + optUseIndex + "." + s + " = aValue"); outData.println("End Property"); outData.println(); } emitCode(proc, outData, table.useName(), optIndex1, optUseIndex); outData.flush(); } finally { outFile.close(); } } catch (IOException e1) { outLog.println("Generate Procs IO Error"); } } }
/** Build of all required standard procedures */ public static void generateStd(Table table, PrintWriter outData) { outData.println("VERSION 1.0 CLASS"); outData.println("BEGIN"); outData.println(" MultiUse = -1"); outData.println("END"); outData.println("ATTRIBUTE VB_NAME=\"T" + table.useName() + "\""); outData.println("Option explicit"); outData.println( "' This code was generated, do not modify it, modify it at source and regenerate it."); for (int i = 0; i < table.comments.size(); i++) { String s = (String) table.comments.elementAt(i); outData.println("'" + s); } boolean hasMulti = false; for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData || proc.hasNoData() || proc.isStd == false) continue; if (proc.inputs.size() > 0) {} if (proc.outputs.size() == 0 || proc.isSingle == true) continue; hasMulti = true; outData.println("Private " + proc.name + "Cursor As TCursor"); } outData.println(); outData.println("Private Type TRec"); generateRec(table.fields, outData); outData.println("End Type"); outData.println(); if (hasMulti) { outData.println("Private Rec() as TRec"); outData.println("Public RecCount as Long"); } else outData.println("Private Rec as TRec"); outData.println(); generateCopy(table.useName(), outData, hasMulti); String optZero = hasMulti ? "(0)" : ""; String optIndex = hasMulti ? "Optional aIndex as Long" : ""; String optIndex1 = hasMulti ? ", Optional aIndex as Long" : ""; String optIndex2 = hasMulti ? "Optional aIndex as Long, " : ""; String optUseIndex = hasMulti ? "(aIndex)" : ""; outData.println("Private Sub Class_Initialize()"); outData.println(" RecClear"); for (int i = 0; i < table.fields.size(); i++) { Field field = (Field) table.fields.elementAt(i); outData.println(" Rec" + optZero + "." + initVBVar(field)); if (field.isNull && notString(field)) outData.println(" Rec" + optZero + "." + field.useName() + "IsNull = false"); } outData.println("End Sub"); outData.println(); if (ioRoutines) { outData.println("Public Sub Class_PutFile(OutFile as Long" + optIndex1 + ")"); outData.println(" Put OutFile,, Rec" + optUseIndex); outData.println("End Sub"); outData.println(); outData.println("Public Sub Class_GetFile(InFile as Long" + optIndex1 + ")"); outData.println(" Get InFile,, Rec" + optUseIndex); outData.println("End Sub"); outData.println(); } for (int i = 0; i < table.fields.size(); i++) { Field field = (Field) table.fields.elementAt(i); generateProperties(field, outData, optIndex, optIndex2, optUseIndex); } for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData) continue; if (proc.hasNoData()) emitCode(proc, outData, table.useName(), "", ""); else if (proc.isStd) emitCode(proc, outData, table.useName(), optIndex1, optUseIndex); } }
/** Build of enums */ static void generateEnums(Table table, PrintWriter outData) { for (int i = 0; i < table.fields.size(); i++) { Field field = (Field) table.fields.elementAt(i); generateEnums(table.useName() + field.useName(), field, outData); } }
/** Build of output data rec for user procedures */ static void generateUserOutputRecs(Table table, PrintWriter outData) { for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData || proc.isStd || proc.hasNoData()) continue; if (proc.isStdExtended()) continue; String work = "(object)"; String superName = ""; if (proc.outputs.size() > 0) { for (int j = 0; j < proc.comments.size(); j++) outData.println("##" + (String) proc.comments.elementAt(j)); String typeChar = "D"; if (proc.hasDiscreteInput()) typeChar = "O"; work = "(" + typeChar + table.useName() + proc.upperFirst() + ")"; superName = typeChar + table.useName() + proc.upperFirst(); outData.println("## \\class " + typeChar + table.useName() + proc.upperFirst()); outData.println("class " + typeChar + table.useName() + proc.upperFirst() + "(object):"); outData.println( " def _make(self): return " + typeChar + table.useName() + proc.upperFirst() + "()"); if (proc.hasDiscreteInput()) outData.println( " def _name(self): return ('" + typeChar + table.useName() + proc.upperFirst() + "')"); else outData.println( " def _name(self): return ('D" + table.useName() + proc.upperFirst() + "', 'O" + table.useName() + proc.upperFirst() + "')"); generateDataFields( proc.outputs, "", typeChar + table.useName() + proc.upperFirst(), table.useName(), outData); outData.println(); } if (proc.hasDiscreteInput()) { outData.println("## \\class D" + table.useName() + proc.upperFirst()); outData.println("class D" + table.useName() + proc.upperFirst() + work + ":"); outData.println( " def _make(self): return D" + table.useName() + proc.upperFirst() + "()"); outData.println( " def _name(self): return ('D" + table.useName() + proc.upperFirst() + "')"); Vector<Field> discreteInputs = new Vector<Field>(); for (int j = 0; j < proc.inputs.size(); j++) { Field field = (Field) proc.inputs.elementAt(j); if (!proc.hasOutput(field.name)) discreteInputs.addElement(field); } for (int j = 0; j < proc.dynamics.size(); j++) { Field f = new Field(); Integer length = (Integer) proc.dynamicSizes.elementAt(j); f.name = (String) proc.dynamics.elementAt(j); f.type = Field.CHAR; f.length = length.intValue(); discreteInputs.addElement(f); } generateDataFields( discreteInputs, superName, "D" + table.useName() + proc.upperFirst(), table.useName(), outData); outData.println(); } else if (proc.outputs.size() > 0) { outData.println("## \\class O" + table.useName() + proc.upperFirst()); outData.println("## \\field see:O" + table.useName() + proc.upperFirst()); outData.println( "O" + table.useName() + proc.upperFirst() + " = D" + table.useName() + proc.upperFirst()); outData.println(); } if (pymodName.length() > 0) { coverFunctions = new Vector<String>(); outData.println( "class " + table.useName() + proc.upperFirst() + "(D" + table.useName() + proc.upperFirst() + "):"); outData.println( " def __init__(self): D" + table.useName() + proc.upperFirst() + ".__init__(self)"); if (proc.isMultipleInput) generateBulkAction(table, proc, outData); else if (proc.isInsert && proc.hasReturning) generateAction(table, proc, outData); else if (proc.outputs.size() > 0) if (proc.isSingle) generateSingle(table, proc, outData); else generateMultiple(table, proc, outData); else generateAction(table, proc, outData); outData.println(); for (int j = 0; j < coverFunctions.size(); j++) outData.println((String) coverFunctions.elementAt(j)); } } }
/** Build of output data rec for standard procedures */ static void generateStdOutputRec(Table table, PrintWriter outData) { for (int i = 0; i < table.comments.size(); i++) { String s = (String) table.comments.elementAt(i); outData.println("## " + s); } outData.println("## \\class D" + table.useName()); outData.println("class D" + table.useName() + "(object):"); outData.println(" def _make(self): return D" + table.useName() + "()"); outData.println( " def _name(self): return ('D" + table.useName() + "','O" + table.useName() + "')"); generateDataFields(table.fields, "", "D" + table.useName(), table.useName(), outData); outData.println(); outData.println("## \\class O" + table.useName()); outData.println("## \\field see:D" + table.useName()); outData.println("O" + table.useName() + " = D" + table.useName()); outData.println(); if (pymodName.length() > 0) { outData.println("class " + table.useName() + "(D" + table.useName() + "):"); outData.println(" def __init__(self): D" + table.useName() + ".__init__(self)"); coverFunctions = new Vector<String>(); for (int i = 0; i < table.procs.size(); i++) { Proc proc = (Proc) table.procs.elementAt(i); if (proc.isData == true || (proc.isStd == false && proc.isStdExtended() == false)) continue; if (proc.isMultipleInput) generateBulkAction(table, proc, outData); else if (proc.isInsert && proc.hasReturning) generateAction(table, proc, outData); else if (proc.outputs.size() > 0) if (proc.isSingle) generateSingle(table, proc, outData); else generateMultiple(table, proc, outData); else generateAction(table, proc, outData); } outData.println(); for (int i = 0; i < coverFunctions.size(); i++) outData.println((String) coverFunctions.elementAt(i)); } }
/** Build of standard and user defined procedures */ static void generateTable(Table table, String output, PrintWriter outLog) { try { outLog.println("Code: " + output + "DB_" + table.useName().toUpperCase() + ".py"); OutputStream outFile = new FileOutputStream(output + "DB_" + table.useName().toUpperCase() + ".py"); try { PrintWriter outData = new PrintWriter(outFile); try { if (useUTF8 == true) outData.println("# -*- coding: utf-8 -*-"); else if (useLatin1 == true) outData.println("# -*- coding: iso-8859-1 -*-"); outData.println( "# This code was generated, do not modify it, modify it at source and regenerate it."); outData.println("# " + table.useName().toUpperCase() + ".py"); outData.println(); if (pymodName.length() > 0) outData.println("import " + pymodName); outData.println(); outData.println("_BLOB = 1;_BOOLEAN = 2;_BYTE = 3;_CHAR = 4;_DATE = 5;_DATETIME = 6"); outData.println( "_DOUBLE = 7;_DYNAMIC = 8;_FLOAT = 9;_IDENTITY = 10;_INT = 11;_LONG = 12"); outData.println("_MONEY = 13;_SEQUENCE = 14;_SHORT = 15;_STATUS = 16;_TIME = 17"); outData.println( "_TIMESTAMP = 18;_TLOB = 19;_USERSTAMP = 20;_ANSICHAR = 21;_UID = 22;_XML = 23"); outData.println("_BIGSEQUENCE = 24;_BIGIDENTITY = 25"); outData.println(); outData.println("# =i=n=d=e=n=t=a=t=i=o=n===b=y===f=o=u=r======"); outData.println("# s : value as a string"); outData.println("# attr : (type, length, scale, precision)"); outData.println("# name : name of field for reporting"); outData.println("# =i=s===a===p=a=i=n=========================="); outData.println(); outData.println("def _validate(s, attr, name):"); outData.println( " if attr[0] in (_CHAR, _ANSICHAR, _DATE, _DATETIME, _TIME, _TIMESTAMP, _USERSTAMP, _XML):"); outData.println( " if len(s) > attr[1]: raise AssertionError, '%s:Length exceeds %d' % (name, attr[1])"); outData.println(" elif attr[0] in (_DOUBLE, _FLOAT) and attr[2] > 15:"); outData.println( " if len(s) > attr[2]+2: raise AssertionError, '%s:Length exceeds %d' % (name, attr[2]+2)"); outData.println(" elif attr[0] == _MONEY:"); outData.println( " if len(s) > 20: raise AssertionError, '%s:Length exceeds %d' % (name, 20)"); outData.println(" return s"); outData.println(); outData.println("def _str(s, attr, name):"); outData.println(" if s == None:"); outData.println(" return None"); if (useUTF8 == true) { outData.println(" elif isinstance(s, unicode):"); outData.println(" fix = s.encode('utf-8')"); outData.println(" return _validate(str(fix), attr, name)"); } else if (useLatin1 == true) { outData.println(" elif isinstance(s, unicode):"); outData.println(" fix = s.encode('iso-8859-1')"); outData.println(" return _validate(str(fix), attr, name)"); } else { outData.println(" elif isinstance(s, unicode):"); outData.println(" fix = ''"); outData.println(" for c in s: fix += chr(ord(c)%256)"); outData.println(" return _validate(str(fix), attr, name)"); } outData.println(" elif isinstance(s, float):"); outData.println(" return '%0.15g' % (s)"); outData.println(" return _validate(str(s), attr, name)"); outData.println(); generateEnums(table, outData); if (table.hasStdProcs) generateStdOutputRec(table, outData); generateUserOutputRecs(table, outData); outData.flush(); } finally { outData.flush(); } } finally { outFile.close(); } } catch (IOException e1) { outLog.println("Generate Procs IO Error"); } }