import java.io.*;
import java.util.*;
public class RowGenerator implements Serializable
{
protected PrintWriter file;
protected String filename;
protected Database theSchema;
protected CodeGenerator theGenerator;
public RowGenerator(CodeGenerator generator)
{
theGenerator = generator;
theSchema = theGenerator.getTheSchema();
}
protected void OpenFile(String filename)
{
try {
file = new PrintWriter( new FileWriter(filename) );
theGenerator.RegisterFile(filename);
}
catch(IOException e) {
System.err.println("GENJAVA-F-BADOUTPUT: " +
filename + " - " +
e.getMessage());
System.exit(-1);
}
}
public Database getTheSchema()
{
return theSchema;
}
public void setTheSchema(Database newSchema)
{
theSchema = newSchema;
}
public void Generate()
{
GenerateRows();
}
protected void GenerateConstructors(TT TTcurr) throws IOException
{
RC TAcurr;
String javaType;
String linebuffer = new String();
file.println();
file.println();
file.println(" " + TTcurr.getTTabb() + "(String rowbuffer, Database mySchema)");
file.println(" {");
file.println(" String buffer;");
file.println(" StringTokenizer tokens =");
file.println(" new StringTokenizer( rowbuffer );");
file.println();
file.println(" buffer = tokens.nextToken();");
file.println(" pkid = new Key(buffer, mySchema);");
file.println();
// LOOPING TEMPLATE CODE
// Loop over each TA child of this TT
TAcurr = (TA)TTcurr.TAid_fcp;
// Skip first one, we know it's the primary key
if( TAcurr != null ) {
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// Don't go all the way to the end -
// Different retrieval is necessary for the last attribute
// to allow embedded whitespace
while( TAcurr != null && TAcurr != TTcurr.TAid_bcp ) {
file.println(" buffer = tokens.nextToken();");
linebuffer = " ";
linebuffer += ((TA)TAcurr).getFname() + " = ";
javaType = JavaTypeFromTA((TA)TAcurr);
if( javaType.compareTo("String") == 0 ) {
linebuffer += "buffer;";
}
else if( javaType.compareTo("int") == 0 ) {
linebuffer += "Integer.parseInt(buffer);";
}
else if( javaType.compareTo("double") == 0 ) {
linebuffer += "Double.valueOf(buffer).doubleValue();";
}
else if( javaType.compareTo("Key") == 0 ) {
linebuffer += "new Key(buffer, mySchema);";
}
else if( javaType.compareTo("boolean") == 0 ) {
linebuffer += "Boolean.getBoolean(buffer);";
}
else {
System.err.println("GENJAVA-F-BADTYPE: unsupported data-type " + ((TA)TAcurr).getFtype() +
" for field " + ((TA)TAcurr).getFname() +
" in table " + ((TA)TAcurr).TTid_pp.getTTname());
System.exit(-1);
}
file.println(linebuffer);
file.println();
// Move to next attribute
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// END LOOPING TEMPLATE CODE
// Last attribute - can contain whitespace
file.println(" buffer = tokens.nextToken(\"\\r\\n\");");
linebuffer = " ";
linebuffer += ((TA)TAcurr).getFname() + " = ";
javaType = JavaTypeFromTA((TA)TAcurr);
if( javaType.compareTo("String") == 0 ) {
linebuffer += "buffer.trim();";
}
else if( javaType.compareTo("int") == 0 ) {
linebuffer += "Integer.parseInt(buffer.trim());";
}
else if( javaType.compareTo("double") == 0 ) {
linebuffer += "Double.valueOf(buffer.trim()).doubleValue();";
}
else if( javaType.compareTo("Key") == 0 ) {
linebuffer += "new Key(buffer.trim(), mySchema);";
}
else if( javaType.compareTo("boolean") == 0 ) {
linebuffer += "Boolean.getBoolean(buffer.trim());";
}
else {
System.err.println("GENJAVA-F-BADTYPE: unsupported data-type " + ((TA)TAcurr).getFtype() +
" for field " + ((TA)TAcurr).getFname() +
" in table " + ((TA)TAcurr).TTid_pp.getTTname());
System.exit(-1);
}
file.println(linebuffer);
file.println();
file.println(" Schema = mySchema;");
file.println(" Table = mySchema." + TTcurr.getTTabb() + "tab;");
file.println(" }");
file.println();
file.println();
file.println(" " + TTcurr.getTTabb() + "()");
file.println(" {");
file.println(" }");
}
protected void GenerateRowHeader(TT TTcurr) throws IOException
{
int startpos = 0;
int endpos = 0;
// Remove the C-style comment from the description
String description = TTcurr.getDescr();
endpos = description.length() - 1;
while( description.charAt(startpos) != '/' &&
description.charAt(startpos + 1) != '*' ) {
startpos++;
}
startpos += 2;
while( description.charAt(endpos) != '/' &&
description.charAt(endpos - 1) != '*' ) {
endpos--;
}
endpos -= 2;
description = description.substring(startpos, endpos - startpos).trim();
file.println("import java.util.*;");
file.println("import java.text.DecimalFormat;");
file.println("import java.io.*;");
file.println();
file.println("/**");
file.println(" * " + description );
file.println(" * @author Genjava v1.0");
file.println(" */");
file.println();
file.println("public class " + TTcurr.getTTabb() + " extends RC implements Serializable");
file.println("{");
}
protected void GenerateAddRow(String currTable) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Adds this " + currTable + " to its Table" +
" * and links it to its parents and siblings");
file.println(" */");
file.println(" public void addRow()");
file.println(" {");
file.println(" if( Table.insertRow(this) )");
file.println(" linkRow();");
file.println(" }");
}
protected void GenerateDeleteRow(String currTable) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Removes this " + currTable + " from its Table" +
" * and unlinks it from its parents and siblings");
file.println(" */");
file.println(" public void deleteRow()");
file.println(" {");
file.println(" if( Table.removeRow(this) )");
file.println(" unlinkRow();");
file.println(" }");
}
protected void GenerateDumpRow(String currTable) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Writes this " + currTable + " to a given BufferedWriter");
file.println(" */");
file.println(" public void dumpRow(BufferedWriter output) throws IOException");
file.println(" {");
file.println(" output.write(this.toString());");
file.println(" output.newLine();");
file.println(" output.flush();");
file.println(" }");
}
protected void GenerateAttribute(TA TAcurr) throws IOException
{
String line = " protected ";
line += JavaTypeFromTA(TAcurr);
line += " ";
line += TAcurr.getFname();
line += ";";
file.println(line);
}
protected void GenerateAccessor(TA TAcurr) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Retrieves the value of " + TAcurr.getFname() + "");
file.println(" */");
file.println();
file.println(" public " + JavaTypeFromTA(TAcurr) + " get"
+ TAcurr.getFname() + "()");
file.println(" {");
file.println(" return " + TAcurr.getFname() + ";");
file.println(" }");
}
protected void GenerateModifier(TA TAcurr) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Sets the value of " + TAcurr.getFname() + "");
file.println(" */");
file.println();
file.println(" public void set" + TAcurr.getFname() + "(" +
JavaTypeFromTA(TAcurr) + " value)");
file.println("{");
file.println(" /* added by AV-NT for logging */");
if (JavaTypeFromTA(TAcurr).equals("int"))
{
file.println("if (GlobalVarsBean.getHcg_log()==1)");
file.println("Pr_log.logstr(\"PR_SET_INT \" +pkid+\" "+TAcurr.getFname()+" \"+ value);");
}else if (JavaTypeFromTA(TAcurr).equals("double"))
{
file.println("if (GlobalVarsBean.getHcg_log()==1)");
file.println("Pr_log.logstr(\"PR_SET_FLT \" +pkid+\" "+TAcurr.getFname()+" \"+ value);");
}else if (JavaTypeFromTA(TAcurr).equals("String"))
{
file.println("if (GlobalVarsBean.getHcg_log()==1)");
file.println("Pr_log.logstr(\"PR_SET_STR \" +pkid+\" "+TAcurr.getFname()+" \"+ value);");
}
file.println(" " + TAcurr.getFname() + " = value;");
file.println(" }");
}
protected void GenerateChildPointers(String currTable) throws IOException
{
RR RRcurr = theSchema.RRtab.getFirstRow();
String testTable = new String();
while( RRcurr != null ) {
testTable = RRcurr.getFirstid();
testTable = testTable.substring(0, currTable.length());
if( testTable.compareTo(currTable) == 0 ) {
file.println(" public " + RRcurr.getSecondid().substring(0, currTable.length()) +
" " + RRcurr.getSecondid() + "_fcp;");
if( !Genjava.cli_nobp && RRcurr.getHasbp() && !RRcurr.getSingleton() ) {
file.println(" public " + RRcurr.getSecondid().substring(0, currTable.length()) +
" " + RRcurr.getSecondid() + "_bcp;");
}
}
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
}
}
protected void GenerateParentPointers(String currTable) throws IOException
{
RR RRcurr = theSchema.RRtab.getFirstRow();
String testTable = new String();
while( RRcurr != null ) {
testTable = RRcurr.getSecondid();
testTable = testTable.substring(0, currTable.length());
if( testTable.compareTo(currTable) == 0 ) {
file.println(" public " + RRcurr.getFirstid().substring(0, currTable.length()) +
" " + RRcurr.getFirstid() + "_pp;");
if( ! RRcurr.getSingleton() ) {
file.println(" public RC " + RRcurr.getFirstid() + "_fpp;");
if( !Genjava.cli_nobp && RRcurr.getHasbp() ) {
file.println(" public RC " + RRcurr.getFirstid() + "_bpp;");
}
}
}
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
}
}
protected void GenerateSchemaRefs(String TTabb) throws IOException
{
file.println(" protected " + TTabb + "table Table;");
file.println(" protected Database Schema;");
}
protected void GenerateSchemaMethods(String TTabb) throws IOException
{
file.println();
file.println();
file.println(" /**");
file.println(" * Retrieves a reference to the " + TTabb + "table");
file.println(" */");
file.println();
file.println(" public " + TTabb + "table getTable()");
file.println(" {");
file.println(" return Table;");
file.println(" }");
file.println();
file.println();
file.println(" /**");
file.println(" * Sets the reference to the " + TTabb + "table");
file.println(" */");
file.println();
file.println(" public void setTable(" + TTabb + "table value)");
file.println(" {");
file.println(" Table = value;");
file.println(" }");
file.println();
file.println();
file.println(" /**");
file.println(" * Retrieves a reference to the global schema");
file.println(" */");
file.println();
file.println(" public Database getSchema()");
file.println(" {");
file.println(" return Schema;");
file.println(" }");
file.println();
file.println();
file.println(" /**");
file.println(" * Modifies the reference to the global schema");
file.println(" */");
file.println();
file.println(" public void setSchema(Database value)");
file.println(" {");
file.println(" Schema = value;");
file.println(" }");
}
protected String JavaTypeFromTA(TA TAcurr)
{
String line = new String();
switch(TAcurr.getFtype().charAt(0)) {
case 'i':
line = "int";
break;
case 'f':
line = "double";
break;
case 'd':
case 't':
line = "String";
break;
case 'c':
if( TAcurr.getIsKey().charAt(0) != '0' ) {
line = "Key";
}
else {
line = "String";
}
break;
case 'b':
line = "boolean";
break;
default:
System.err.println("GENJAVA-F-BADTYPE: unsupported data-type " + TAcurr.getFtype() +
" for field " + TAcurr.getFname() +
" in table " + TAcurr.TTid_pp.getTTname());
System.exit(-1);
}
return line;
}
protected void GenerateRC() throws IOException
{
String filename = new String();
filename = new String(Genjava.directory);
filename += Genjava.filesep;
filename += "RC.java";
OpenFile(filename);
file.println("/**");
file.println(" * All row attribute classes are descended from this class");
file.println(" * @author Genjava v1.0");
file.println(" */");
file.println("public abstract class RC");
file.println("{");
file.println(" protected Key pkid;");
file.println(" protected int rflag;");
file.println();
file.println(" public void setPkid(Key id)");
file.println(" {");
file.println(" pkid = id;");
file.println(" }");
file.println();
file.println(" public void setRflag(int flag)");
file.println(" {");
file.println(" rflag = flag;");
file.println(" }");
file.println();
file.println(" Key getPkid()");
file.println(" {");
file.println(" return pkid;");
file.println(" }");
file.println();
file.println(" int getRflag()");
file.println(" {");
file.println(" return rflag;");
file.println(" }");
file.println("}");
file.println();
file.close();
}
protected void GenerateRows()
{
String filename = new String();
RC TAcurr;
TT TTcurr;
try{
GenerateRC();
for( TTcurr = theSchema.TTtab.getFirstRow();
TTcurr != null;
TTcurr = theSchema.TTtab.getNextRow(TTcurr)) {
filename = new String(Genjava.directory);
filename += Genjava.filesep;
filename += TTcurr.getTTabb();
filename += ".java";
OpenFile(filename);
GenerateRowHeader(TTcurr);
// First for the fields themselves
// Loop over each TA child of this TT
TAcurr = (TA)TTcurr.TAid_fcp;
// Skip the primary key
if( TAcurr != null ) {
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// Generate the attributes themselves
while( TAcurr != null && TAcurr != TTcurr ) {
GenerateAttribute((TA)TAcurr);
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// Add the table and the schema
GenerateSchemaRefs(TTcurr.getTTabb());
GenerateParentPointers(TTcurr.getTTabb());
GenerateChildPointers(TTcurr.getTTabb());
GenerateConstructors(TTcurr);
// Now for accessors and modifiers
// Loop over each TA child of this TT
TAcurr = (TA)TTcurr.TAid_fcp;
// Skip the primary key
if( TAcurr != null ) {
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// Generate the attributes themselves
while( TAcurr != null && TAcurr != TTcurr ) {
GenerateAccessor((TA)TAcurr);
GenerateModifier((TA)TAcurr);
TAcurr = ((TA)TAcurr).TTid_fpp;
}
GenerateSchemaMethods(TTcurr.getTTabb());
GenerateAddRow(TTcurr.getTTabb());
GenerateDeleteRow(TTcurr.getTTabb());
GenerateLinkRow(TTcurr.getTTabb());
GenerateUnlinkRow(TTcurr.getTTabb());
GenerateDumpRow(TTcurr.getTTabb());
GenerateToString(TTcurr);
// Close out the class
file.println("}");
file.close();
} // end for
}
catch(IOException e) {
System.err.println("GENJAVA-F-BADOUTPUT: " +
filename + " - " +
e.getMessage());
System.exit(-1);
}
}
protected void GenerateLinkRow(String currTable) throws IOException
{
RR RRcurr;
RR RRcurr2;
String testTable = new String();
String relatedTable = new String();
String vecTable = new String();
Vector parentTypes = new Vector(1);
Vector childTypes = new Vector(1);
int varlength = currTable.length();
int i;
// Add a javadoc comment
file.println();
file.println();
file.println(" /**");
file.print(" * Links this " + currTable + " ");
file.println(" to all of it's related tables");
file.println(" */");
file.println();
file.println(" public void linkRow()");
file.println(" {");
// First generate locals
// First get list of tables which contain children of this row
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getFirstid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getSecondid().substring(0, varlength);
RRcurr2 = theSchema.RRtab.getFirstRow();
// Seems redundant, but we don't want
// to declare variables repeatedly in the case where
// more than one parent or child comes from the same table
for( i = 0; i < childTypes.size(); ++i ) {
vecTable = (String)childTypes.elementAt(i);
vecTable = vecTable.substring(0, varlength);
if( vecTable.compareTo(relatedTable) == 0 ) {
break;
} // end if
} // end for
if( i == childTypes.size() ) {
childTypes.addElement(relatedTable);
}
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// Next get list of tables which contain parents of this row
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getSecondid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getFirstid().substring(0, varlength);
// Seems redundant, but we don't want
// to declare variables repeatedly in the case where
// more than one parent or child comes from the same table
for( i = 0; i < parentTypes.size(); ++i ) {
vecTable = (String)parentTypes.elementAt(i);
vecTable = vecTable.substring(0, varlength);
if( vecTable.compareTo(relatedTable) == 0 ) {
break;
} // end if
} // end for
if( i == parentTypes.size() ) {
parentTypes.addElement(relatedTable);
}
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// Now, iterate over these two vectors and print local variables
file.println();
if( parentTypes.size() != 0 ) {
file.println(" " + currTable + " " + currTable + "ptemp;");
}
file.println(" // Locals for parent tables");
for( i = 0; i < parentTypes.size(); ++i ) {
file.println(" " + (String)parentTypes.elementAt(i) +
" " + (String)parentTypes.elementAt(i) + "ptemp;");
}
file.println();
file.println(" // Locals for child tables");
for( i = 0; i < childTypes.size(); ++i ) {
file.println(" " + (String)childTypes.elementAt(i) +
" " + (String)childTypes.elementAt(i) + "ctemp;");
}
for( i = 0; i < childTypes.size(); ++i ) {
file.println(" " + (String)childTypes.elementAt(i) +
" " + (String)childTypes.elementAt(i) + "ccurr;");
}
// For each RR containing this table as a child, write out
// a version of link_child_bp_m
file.println(" // This loop basically implements link_child_bp_m");
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getSecondid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getFirstid().substring(0, varlength);
file.println();
file.print(" for( " + relatedTable + "ptemp");
file.println(" = Schema." + relatedTable + "tab.getFirstRow();");
file.println(" " + relatedTable + "ptemp != null;");
file.print(" " + relatedTable + "ptemp ");
file.print(" = Schema." + relatedTable);
file.println("tab.getNextRow(" + relatedTable + "ptemp) ) {");
file.print(" if( ");
file.print(RRcurr.getFirstid() + ".compare(");
file.println(relatedTable + "ptemp.getPkid()) == 0 ) {");
file.print(" ");
file.println(RRcurr.getFirstid() + "_pp = " + relatedTable
+ "ptemp;");
file.println(" " +
currTable + "ptemp = " +
relatedTable + "ptemp." +
RRcurr.getSecondid() + "_bcp;");
file.println(" " +
relatedTable + "ptemp." +
RRcurr.getSecondid() + "_bcp = this;" );
file.println(" " +
RRcurr.getFirstid() + "_fpp = " +
relatedTable + "ptemp;");
file.println(" " +
"if( " + relatedTable + "ptemp." +
RRcurr.getSecondid() + "_fcp == null ) {" );
file.println(" " +
relatedTable + "ptemp." +
RRcurr.getSecondid() + "_fcp = this;");
file.println(" " +
RRcurr.getFirstid() + "_bpp = " +
relatedTable + "ptemp;");
file.println(" }");
file.println(" else {");
file.println(" " +
RRcurr.getFirstid() + "_bpp = " + currTable + "ptemp;");
file.println(" " +
currTable + "ptemp." + RRcurr.getFirstid() + "_fpp = this;" );
file.println(" }");
file.println(" } // End if ==");
file.println(" }// End for");
// end generated code
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// For each RR containing this table as a parent, write out
// a version of link_child_bp_m
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getFirstid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getSecondid().substring(0, varlength);
file.println();
file.print(" for( " + relatedTable + "ccurr");
file.println(" = Schema." + relatedTable + "tab.getFirstRow();");
file.println(" " + relatedTable + "ccurr != null;");
file.print(" " + relatedTable + "ccurr ");
file.print(" = Schema." + relatedTable);
file.println("tab.getNextRow(" + relatedTable + "ccurr) ) {");
file.print(" if( ");
file.print(relatedTable + "ccurr.get" + RRcurr.getFirstid());
file.println("().compare(pkid) == 0 ) {");
file.println(" " +
relatedTable + "ccurr." + RRcurr.getFirstid() + "_pp = this;");
file.println(" " +
relatedTable + "ctemp = " + RRcurr.getSecondid() + "_bcp;");
file.println(" " +
RRcurr.getSecondid() + "_bcp = " + relatedTable + "ccurr;");
file.println(" " +
relatedTable + "ccurr." + RRcurr.getFirstid() + "_fpp = this;");
file.println(" if ( " +
RRcurr.getSecondid() + "_fcp == null ) {");
file.println(" " +
RRcurr.getSecondid() + "_fcp = " + relatedTable + "ccurr;");
file.println(" " +
relatedTable + "ccurr." + RRcurr.getFirstid() + "_bpp = this;");
file.println(" }");
file.println(" else {");
file.println(" " +
relatedTable + "ccurr." + RRcurr.getFirstid() +
"_bpp = " + relatedTable + "ctemp;");
file.println(" " +
relatedTable + "ctemp." + RRcurr.getFirstid() + "_fpp = " +
relatedTable + "ccurr;");
file.println(" } // End else");
file.println(" } // End if ==");
file.println(" } // End for");
// end generated code
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// Close the method
file.println(" }");
}
protected void GenerateUnlinkRow(String currTable) throws IOException
{
RR RRcurr;
RR RRcurr2;
String testTable = new String();
String relatedTable = new String();
String vecTable = new String();
Vector parentTypes = new Vector(1);
Vector childTypes = new Vector(1);
int varlength = currTable.length();
int i;
// Add a javadoc comment
file.println();
file.println();
file.println(" /**");
file.print(" * Unlinks this " + currTable + " ");
file.println(" from all of it's related tables");
file.println(" */");
file.println();
file.println(" public void unlinkRow()");
file.println(" {");
// First generate locals
// First get list of tables which contain children of this row
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getFirstid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getSecondid().substring(0, varlength);
RRcurr2 = theSchema.RRtab.getFirstRow();
// Seems redundant, but we don't want
// to declare variables repeatedly in the case where
// more than one parent or child comes from the same table
for( i = 0; i < childTypes.size(); ++i ) {
vecTable = (String)childTypes.elementAt(i);
vecTable = vecTable.substring(0, varlength);
if( vecTable.compareTo(relatedTable) == 0 ) {
break;
} // end if
} // end for
if( i == childTypes.size() ) {
childTypes.addElement(relatedTable);
}
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// Next get list of tables which contain parents of this row
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getSecondid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getFirstid().substring(0, varlength);
// Seems redundant, but we don't want
// to declare variables repeatedly in the case where
// more than one parent or child comes from the same table
for( i = 0; i < parentTypes.size(); ++i ) {
vecTable = (String)parentTypes.elementAt(i);
vecTable = vecTable.substring(0, varlength);
if( vecTable.compareTo(relatedTable) == 0 ) {
break;
} // end if
} // end for
if( i == parentTypes.size() ) {
parentTypes.addElement(relatedTable);
}
} // if( testTable.compareTo(currTable) == 0 )
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
} // while( RRcurr != null )
// Now, iterate over these two vectors and print local variables
file.println();
file.println(" // Locals for parent tables");
if( parentTypes.size() != 0 ) {
file.println(" " + currTable + " " + currTable + "temp;");
}
for( i = 0; i < parentTypes.size(); ++i ) {
file.println(" " + (String)parentTypes.elementAt(i) +
" " + (String)parentTypes.elementAt(i) + "temp;");
}
file.println();
file.println(" // Locals for child tables");
if( childTypes.size() != 0 ) {
file.println(" RC RCtemp;");
file.println(" RC RCtemp2;");
}
// For each RR containing this table as a child, write out
// a version of unlink_child_bp_m
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getSecondid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getFirstid().substring(0, varlength);
file.println();
// Begin generated code
file.println(" " + relatedTable +
"temp = " + RRcurr.getFirstid() + "_pp;" );
file.println(" " + "if( " +
relatedTable + "temp != null ) {");
file.println(" " + "if( " +
relatedTable + "temp." + RRcurr.getSecondid() +
"_fcp == this && ");
file.println(" " + RRcurr.getFirstid() +
"_fpp == " + relatedTable + "temp ) {");
file.println(" " + relatedTable + "temp." +
RRcurr.getSecondid() + "_fcp = null;");
file.println(" " + relatedTable + "temp." +
RRcurr.getSecondid() + "_bcp = null;");
file.println(" " + "}");
file.println(" " + "else if ( " + relatedTable +
"temp." + RRcurr.getSecondid() + "_fcp == this ) {");
file.println(" " + "// First child case");
file.println(" " + relatedTable + "temp." +
RRcurr.getSecondid() + "_fcp = (" + currTable + ")" +
RRcurr.getFirstid() + "_fpp;");
file.println(" " + currTable + "temp = (" +
currTable + ")" + RRcurr.getFirstid() + "_fpp;");
file.println(" " + currTable + "temp." +
RRcurr.getFirstid() + "_bpp = " + relatedTable + "temp;");
file.println(" " + "}");
file.println(" " + "else if ( " +
RRcurr.getFirstid() + "_fpp == " + relatedTable + "temp ) {");
file.println(" " + "// Last child case");
file.println(" " + relatedTable + "temp." +
RRcurr.getSecondid() + "_bcp = (" + currTable + ")" +
RRcurr.getFirstid() + "_bpp;");
file.println(" " + currTable + "temp = (" +
currTable + ")" + RRcurr.getFirstid() + "_bpp;");
file.println(" " + currTable + "temp." +
RRcurr.getFirstid() + "_fpp = " + relatedTable + "temp;");
file.println(" " + "}");
file.println(" " + "else {");
file.println(" " + currTable + "temp = (" +
currTable + ")" + RRcurr.getFirstid() + "_bpp;");
file.println(" " + currTable + "temp." +
RRcurr.getFirstid() + "_fpp = (" + currTable + ")" +
RRcurr.getFirstid() + "_fpp;");
file.println(" " + currTable + "temp = (" +
currTable + ")" + RRcurr.getFirstid() + "_fpp;");
file.println(" " + currTable + "temp." +
RRcurr.getFirstid() + "_bpp = (" + currTable + ")" +
RRcurr.getFirstid() + "_bpp;");
file.println(" " + "}");
file.println(" " + "}");
// End generated code
}
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
}
// Now we unlink a given row from its children
// For each RR containing this table as a child, write out
// a version of unlink_child_bp_m
RRcurr = theSchema.RRtab.getFirstRow();
while( RRcurr != null ) {
testTable = RRcurr.getFirstid().substring(0, varlength);
if( testTable.compareTo(currTable) == 0 ) {
relatedTable = RRcurr.getSecondid().substring(0, varlength);
file.println();
file.println(" RCtemp = " + RRcurr.getSecondid() + "_fcp;");
file.println(" while( ( RCtemp != null ) && " +
"( RCtemp != this ) ) {");
file.println(" RCtemp2 = ((" + relatedTable + ")" +
"RCtemp)." + RRcurr.getFirstid() + "_fpp;");
file.println(" ((" + relatedTable + ")RCtemp)." +
RRcurr.getFirstid() + "_bpp = ((" + relatedTable +
")RCtemp)." + RRcurr.getFirstid() + "_pp =");
file.println(" ((" + relatedTable + ")RCtemp)." +
RRcurr.getFirstid() + "_pp = null;");
file.println(" RCtemp = RCtemp2;");
file.println(" }");
}
RRcurr = theSchema.RRtab.getNextRow(RRcurr);
}
// Close the method
file.println(" }");
}
protected void GenerateToString(TT TTcurr) throws IOException
{
RC TAcurr;
String linebuffer = new String(" return pkid");
// First for the fields themselves
// Loop over each TA child of this TT
TAcurr = (TA)TTcurr.TAid_fcp;
file.println();
file.println();
file.println(" public String toString()");
file.println(" {");
// return pkid + " " + TTid + " " + Fname + " " + AltFname + " " + Ftype + " " + IsKey + " " + Descr;
// Skip the primary key
if( TAcurr != null ) {
TAcurr = ((TA)TAcurr).TTid_fpp;
}
// Generate the attributes themselves
while( TAcurr != null && TAcurr != TTcurr ) {
linebuffer += " + \" \" + " + ((TA)TAcurr).getFname();
TAcurr = ((TA)TAcurr).TTid_fpp;
}
linebuffer += ";";
file.println(linebuffer);
file.println(" }");
}
}