private static void PrepareCodeletsForeignKey( TTable AOtherTable, TConstraint AConstraint, out string whereClauseForeignKey, out string whereClauseViaOtherTable, out string odbcParametersForeignKey, out int numberFields, out string namesOfThisTableFields) { whereClauseForeignKey = ""; whereClauseViaOtherTable = ""; numberFields = AConstraint.strThisFields.Count; namesOfThisTableFields = ""; odbcParametersForeignKey = "OdbcParameter[] ParametersArray = new OdbcParameter[" + AConstraint.strThisFields.Count.ToString() + "];" + Environment.NewLine; int counterKeyField = 0; foreach (string field in AConstraint.strThisFields) { if (counterKeyField > 0) { whereClauseForeignKey += " AND "; whereClauseViaOtherTable += " AND "; namesOfThisTableFields += ", "; } namesOfThisTableFields += '"' + AConstraint.strThisFields[counterKeyField] + '"'; string otherfield = AConstraint.strOtherFields[counterKeyField]; TTableField otherTypedField = AOtherTable.GetField(otherfield); whereClauseForeignKey += field + " = ?"; whereClauseViaOtherTable += "PUB_" + AConstraint.strThisTable + "." + field + " = PUB_" + AConstraint.strOtherTable + "." + otherfield; odbcParametersForeignKey += "ParametersArray[" + counterKeyField.ToString() + "] = " + "new OdbcParameter(\"\", " + CodeGenerationPetra.ToOdbcTypeString(otherTypedField) + (otherTypedField.iLength != -1 ? ", " + otherTypedField.iLength.ToString() : "") + ");" + Environment.NewLine; odbcParametersForeignKey += "ParametersArray[" + counterKeyField.ToString() + "].Value = " + "((object)(A" + TTable.NiceFieldName(otherfield) + "));" + Environment.NewLine; counterKeyField++; } }
/// <summary> /// create the code for the definition of a typed table /// </summary> /// <param name="Template"></param> /// <param name="currentTable"></param> /// <param name="origTable"></param> /// <param name="WhereToInsert"></param> /// <param name="CalledFromDataSet"></param> public static void InsertTableDefinition(ProcessTemplate Template, TTable currentTable, TTable origTable, string WhereToInsert, Boolean CalledFromDataSet) { ProcessTemplate snippet = Template.GetSnippet("TYPEDTABLE"); string derivedTable = ""; if (origTable != null) { snippet.SetCodelet("BASECLASSTABLE", TTable.NiceTableName(currentTable.strName) + "Table"); derivedTable = "new "; snippet.SetCodelet("TABLEID", origTable.iOrder.ToString()); } else { snippet.SetCodelet("BASECLASSTABLE", "TTypedDataTable"); snippet.SetCodelet("TABLEID", currentTable.iOrder.ToString()); } snippet.SetCodelet("NEW", derivedTable); snippet.SetCodeletComment("TABLE_DESCRIPTION", currentTable.strDescription); snippet.SetCodelet("TABLENAME", currentTable.strDotNetName); if (CalledFromDataSet) { snippet.SetCodelet("TABLEINTDS", "TableInTDS"); } else { snippet.SetCodelet("TABLEINTDS", ""); } if (currentTable.AvailableForCustomReport) { snippet.SetCodelet("AVAILABLEFORCUSTOMREPORT", "true"); } else { snippet.SetCodelet("AVAILABLEFORCUSTOMREPORT", "false"); } snippet.SetCodelet("CUSTOMREPORTPERMISSION", currentTable.CustomReportPermission); if (currentTable.strVariableNameInDataset != null) { snippet.SetCodelet("TABLEVARIABLENAME", currentTable.strVariableNameInDataset); } else { snippet.SetCodelet("TABLEVARIABLENAME", currentTable.strDotNetName); } snippet.SetCodelet("DBTABLENAME", currentTable.strName); snippet.SetCodelet("DBTABLELABEL", currentTable.strLabel); if (currentTable.HasPrimaryKey()) { TConstraint primKey = currentTable.GetPrimaryKey(); bool first = true; string primaryKeyColumns = ""; int prevIndex = -1; // the fields in the primary key should be used in the same order as in the table. // otherwise this is causing confusion. eg. a_processed_fee foreach (TTableField column in currentTable.grpTableField) { int newIndex = -1; if (primKey.strThisFields.Contains(column.strName)) { newIndex = primKey.strThisFields.IndexOf(column.strName); } else if (primKey.strThisFields.Contains(TTable.NiceFieldName(column))) { newIndex = primKey.strThisFields.IndexOf(TTable.NiceFieldName(column)); } if (newIndex != -1) { if (newIndex < prevIndex) { throw new Exception("Please fix the order of the fields in the primary key of table " + currentTable.strName); } prevIndex = newIndex; } } // the fields in the primary key should be used in the same order as in the table. // otherwise this is causing confusion. eg. a_processed_fee foreach (TTableField column in currentTable.grpTableField) { if (primKey.strThisFields.Contains(column.strName) || primKey.strThisFields.Contains(TTable.NiceFieldName(column))) { string columnName = column.strName; string toAdd = currentTable.grpTableField.IndexOf(currentTable.GetField(columnName)).ToString(); if (!first) { toAdd = ", " + toAdd; primaryKeyColumns += ","; } first = false; snippet.AddToCodelet("COLUMNPRIMARYKEYORDER", toAdd); primaryKeyColumns += "Column" + TTable.NiceFieldName(currentTable.GetField(columnName)); } } if (primaryKeyColumns.Length > 0) { snippet.SetCodelet("PRIMARYKEYCOLUMNS", primaryKeyColumns); snippet.SetCodelet("PRIMARYKEYCOLUMNSCOUNT", primKey.strThisFields.Count.ToString()); } } else { snippet.AddToCodelet("COLUMNPRIMARYKEYORDER", ""); } if (currentTable.HasUniqueKey()) { TConstraint primKey = currentTable.GetFirstUniqueKey(); bool first = true; foreach (string columnName in primKey.strThisFields) { string toAdd = currentTable.grpTableField.IndexOf(currentTable.GetField(columnName)).ToString(); if (!first) { toAdd = ", " + toAdd; } first = false; snippet.AddToCodelet("COLUMNUNIQUEKEYORDER", toAdd); } } else { snippet.AddToCodelet("COLUMNUNIQUEKEYORDER", ""); } int colOrder = 0; Boolean CustomReportFieldAdded = false; ProcessTemplate tempTemplate = null; foreach (TTableField col in currentTable.grpTableField) { col.strTableName = currentTable.strName; string columnOverwrite = ""; bool writeColumnProperties = true; if ((origTable != null) && (origTable.GetField(col.strName, false) != null)) { columnOverwrite = "new "; if (origTable.GetField(col.strName).iOrder == colOrder) { // same order number, save some lines of code by not writing them writeColumnProperties = false; } } if (writeColumnProperties && (columnOverwrite.Length == 0)) { tempTemplate = Template.GetSnippet("DATACOLUMN"); tempTemplate.SetCodeletComment("COLUMN_DESCRIPTION", col.strDescription); tempTemplate.SetCodelet("COLUMNNAME", TTable.NiceFieldName(col)); snippet.InsertSnippet("DATACOLUMNS", tempTemplate); } if (writeColumnProperties) { tempTemplate = Template.GetSnippet("COLUMNIDS"); tempTemplate.SetCodelet("COLUMNNAME", TTable.NiceFieldName(col)); tempTemplate.SetCodelet("COLUMNORDERNUMBER", colOrder.ToString()); tempTemplate.SetCodelet("NEW", columnOverwrite); snippet.InsertSnippet("COLUMNIDS", tempTemplate); } if (origTable == null) { tempTemplate = Template.GetSnippet("COLUMNINFO"); tempTemplate.SetCodelet("COLUMNORDERNUMBER", colOrder.ToString()); tempTemplate.SetCodelet("COLUMNNAME", TTable.NiceFieldName(col)); tempTemplate.SetCodelet("COLUMNDBNAME", col.strName); tempTemplate.SetCodelet("COLUMNLABEL", col.strLabel); tempTemplate.SetCodelet("COLUMNODBCTYPE", CodeGenerationPetra.ToOdbcTypeString(col)); tempTemplate.SetCodelet("COLUMNLENGTH", col.iLength.ToString()); tempTemplate.SetCodelet("COLUMNNOTNULL", col.bNotNull.ToString().ToLower()); tempTemplate.SetCodelet("COLUMNCOMMA", colOrder + 1 < currentTable.grpTableField.Count ? "," : ""); snippet.InsertSnippet("COLUMNINFO", tempTemplate); } tempTemplate = Template.GetSnippet("INITCLASSADDCOLUMN"); tempTemplate.SetCodelet("COLUMNDBNAME", col.strName); tempTemplate.SetCodelet("COLUMNDOTNETTYPE", col.GetDotNetType()); tempTemplate.SetCodelet("COLUMNDOTNETTYPENOTNULLABLE", col.GetDotNetType().Replace("?", "")); snippet.InsertSnippet("INITCLASSADDCOLUMN", tempTemplate); tempTemplate = Template.GetSnippet("INITVARSCOLUMN"); tempTemplate.SetCodelet("COLUMNDBNAME", col.strName); tempTemplate.SetCodelet("COLUMNNAME", TTable.NiceFieldName(col)); snippet.InsertSnippet("INITVARSCOLUMN", tempTemplate); if (col.bAvailableForCustomReport) { tempTemplate = Template.GetSnippet("INITVARSCUSTOMREPORTFIELDLIST"); if (CustomReportFieldAdded) { tempTemplate.SetCodelet("LISTDELIMITER", ","); } else { tempTemplate.SetCodelet("LISTDELIMITER", ""); } tempTemplate.SetCodelet("COLUMNDBNAME", col.strName); snippet.InsertSnippet("INITVARSCUSTOMREPORTFIELDLIST", tempTemplate); CustomReportFieldAdded = true; } if (writeColumnProperties) { tempTemplate = Template.GetSnippet("STATICCOLUMNPROPERTIES"); tempTemplate.SetCodelet("COLUMNDBNAME", col.strName); tempTemplate.SetCodelet("COLUMNNAME", TTable.NiceFieldName(col)); tempTemplate.SetCodelet("COLUMNHELP", col.strHelp.Replace("\"", "\\\"")); tempTemplate.SetCodelet("COLUMNLENGTH", col.iLength.ToString()); tempTemplate.SetCodelet("NEW", columnOverwrite); snippet.InsertSnippet("STATICCOLUMNPROPERTIES", tempTemplate); } colOrder++; } if (!CustomReportFieldAdded) { // fill snippet if nothing was added yet tempTemplate = Template.GetSnippet("INITVARSCUSTOMREPORTFIELDLISTEMPTY"); tempTemplate.SetCodelet("EMPTY", ""); snippet.InsertSnippet("INITVARSCUSTOMREPORTFIELDLIST", tempTemplate); } Template.InsertSnippet(WhereToInsert, snippet); }