/// <summary>
        /// create the code for validation of a typed table
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="strGroup"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFileName"></param>
        /// <returns></returns>
        public static Boolean WriteValidation(TDataDefinitionStore AStore, string strGroup, string AFilePath, string ANamespaceName, string AFileName)
        {
            Console.WriteLine("processing validation of Typed Tables " + strGroup.Substring(0, 1).ToUpper() + strGroup.Substring(1));

            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "DataTableValidation.cs");

            Template.AddToCodelet("NAMESPACE", ANamespaceName);
            Template.AddToCodelet("DATATABLENAMESPACE", ANamespaceName.Replace("Validation", "Data"));

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            foreach (TTable currentTable in AStore.GetTables())
            {
                if (currentTable.strGroup == strGroup)
                {
                    InsertTableValidation(Template, currentTable, null, "TABLELOOP");
                }
            }

            if (!Directory.Exists(AFilePath))
            {
                Directory.CreateDirectory(AFilePath);
            }

            Template.FinishWriting(AFilePath + AFileName + "-generated.cs", ".cs", true);

            return(true);
        }
        /// <summary>
        /// generate code for cascading deletions etc
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFileName"></param>
        /// <returns></returns>
        public static Boolean WriteTypedDataCascading(TDataDefinitionStore AStore, string AFilePath, string ANamespaceName, string AFileName)
        {
            Console.WriteLine("writing namespace " + ANamespaceName);

            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "DataCascading.cs");

            Template.AddToCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            foreach (TTable currentTable in AStore.GetTables())
            {
                ProcessTemplate snippet = Template.GetSnippet("TABLECASCADING");

                if (InsertMainProcedures(AStore, currentTable, Template, snippet))
                {
                    Template.AddToCodelet("USINGNAMESPACES",
                                          CodeGenerationAccess.GetNamespace(currentTable.strGroup), false);
                    Template.AddToCodelet("USINGNAMESPACES",
                                          CodeGenerationAccess.GetNamespace(currentTable.strGroup).Replace(
                                              ".Data;", ".Data.Access;").
                                          Replace("Ict.Petra.Shared.", "Ict.Petra.Server."), false);

                    Template.InsertSnippet("TABLECASCADINGLOOP", snippet);
                }
            }

            Template.FinishWriting(AFilePath + AFileName + "-generated.cs", ".cs", true);

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// load data from the sql file in Postgresql COPY format
        /// </summary>
        static private bool LoadDataFromReader(TDataDefinitionStore ADataDefinition,
                                               SqliteConnection conn,
                                               StreamReader sr,
                                               string ATablename,
                                               StringCollection AColumnNames)
        {
            using (SqliteTransaction dbTrans = conn.BeginTransaction())
            {
                using (SqliteCommand cmd = conn.CreateCommand())
                {
                    TTable table = ADataDefinition.GetTable(ATablename);

                    // prepare the statement
                    PrepareSqlStatement(cmd, ATablename, table, AColumnNames);

                    string line;

                    while ((line = sr.ReadLine()) != "\\.")
                    {
                        ProcessLine(cmd, line, table, AColumnNames);

                        cmd.ExecuteNonQuery();
                    }
                }

                dbTrans.Commit();
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// create the code for a typed table
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="strGroup"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFileName"></param>
        /// <returns></returns>
        public static Boolean WriteTypedTable(TDataDefinitionStore AStore, string strGroup, string AFilePath, string ANamespaceName, string AFileName)
        {
            Console.WriteLine("processing namespace Typed Tables " + strGroup.Substring(0, 1).ToUpper() + strGroup.Substring(1));

            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "DataTable.cs");

            Template.AddToCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            foreach (TTable currentTable in AStore.GetTables())
            {
                if (currentTable.strGroup == strGroup)
                {
                    if (!currentTable.HasPrimaryKey())
                    {
                        TLogging.Log("Warning: there is no primary key for table " + currentTable.strName);
                    }

                    InsertTableDefinition(Template, currentTable, null, "TABLELOOP");
                    InsertRowDefinition(Template, currentTable, null, "TABLELOOP");
                }
            }

            Template.FinishWriting(AFilePath + AFileName + "-generated.cs", ".cs", true);

            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// load data from a CSV file in Postgresql COPY format
        /// </summary>
        static private bool LoadData(TDataBase DBAccessObj, TDataDefinitionStore ADataDefinition, string APath, string ATablename)
        {
            TTable table = ADataDefinition.GetTable(ATablename);

            // prepare the statement
            string sqlStmt = table.PrepareSQLInsertStatement();

            // load the data from the text file
            string filename = APath + Path.DirectorySeparatorChar + ATablename + ".csv";

            if (File.Exists(filename + ".local"))
            {
                filename += ".local";
            }

            StreamReader reader = new StreamReader(filename);
            string       line;

            while ((line = reader.ReadLine()) != null)
            {
                List <OdbcParameter> Parameters = table.PrepareParametersInsertStatement(line);

                if (DBAccessObj.ExecuteNonQuery(sqlStmt, DBAccessObj.Transaction, Parameters.ToArray()) == 0)
                {
                    throw new Exception("failed to import line for table " + ATablename);
                }
            }

            return(true);
        }
        /// <summary>
        /// load test data written in PostgreSQL syntax into MySQL
        /// </summary>
        private static void LoadTestDataMySQL(string fileName)
        {
            // parse the sql file, and for each table (are there multiple???) load the data
            string sqlfileContent = LoadCSVFileToString(fileName);

            string[] lines        = sqlfileContent.Split(new char[] { '\n' });
            string   currenttable = string.Empty;
            string   sqlStmt      = string.Empty;

            TDataDefinitionParser parser = new TDataDefinitionParser(rootPath + Path.DirectorySeparatorChar + "db" + Path.DirectorySeparatorChar + "petra.xml");
            TDataDefinitionStore  store  = new TDataDefinitionStore();
            TTable table = null;

            if (!parser.ParseDocument(ref store))
            {
                throw new Exception("failed to parse petra.xml");
            }

            TDBTransaction LoadTransaction = new TDBTransaction();
            TDataBase      db           = DBAccess.Connect("LoadTestDataMySQL");
            bool           SubmissionOK = false;

            db.WriteTransaction(ref LoadTransaction,
                                ref SubmissionOK,
                                delegate
            {
                foreach (string line in lines)
                {
                    if (line.StartsWith("--") || (line.Trim() == String.Empty))
                    {
                        continue;
                    }
                    else if (line.StartsWith("COPY "))
                    {
                        currenttable = line.Substring("COPY ".Length, line.IndexOf("(") - "COPY ".Length - 1);

                        table = store.GetTable(currenttable);

                        sqlStmt = table.PrepareSQLInsertStatement();
                    }
                    else if (line == "\\.")
                    {
                        currenttable = String.Empty;
                    }
                    else if (currenttable != String.Empty)
                    {
                        List <OdbcParameter> Parameters = table.PrepareParametersInsertStatement(line);

                        if (db.ExecuteNonQuery(sqlStmt, LoadTransaction, Parameters.ToArray()) == 0)
                        {
                            throw new Exception("failed to import line for table " + currenttable);
                        }
                    }
                }

                SubmissionOK = true;
            });
        }
        private static void InsertMainProcedures(TDataDefinitionStore AStore,
                                                 TTable ACurrentTable,
                                                 ProcessTemplate ATemplate,
                                                 ProcessTemplate ASnippet)
        {
            ASnippet.SetCodelet("TABLENAME", TTable.NiceTableName(ACurrentTable.strName));
            ASnippet.SetCodeletComment("TABLE_DESCRIPTION", ACurrentTable.strDescription);
            ASnippet.SetCodelet("SQLTABLENAME", ACurrentTable.strName);
            ASnippet.SetCodelet("VIAOTHERTABLE", "");
            ASnippet.SetCodelet("VIALINKTABLE", "");

            string formalParametersPrimaryKey;
            string actualParametersPrimaryKey;
            string actualParametersPrimaryKeyToString;
            int    numberPrimaryKeyColumns;

            PrepareCodeletsPrimaryKey(ACurrentTable,
                                      out formalParametersPrimaryKey,
                                      out actualParametersPrimaryKey,
                                      out actualParametersPrimaryKeyToString,
                                      out numberPrimaryKeyColumns);

            ASnippet.SetCodelet("FORMALPARAMETERSPRIMARYKEY", formalParametersPrimaryKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSPRIMARYKEY", actualParametersPrimaryKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSPRIMARYKEYTOSTRING", actualParametersPrimaryKeyToString);
            ASnippet.SetCodelet("PRIMARYKEYNUMBERCOLUMNS", numberPrimaryKeyColumns.ToString());

            string formalParametersUniqueKey;
            string actualParametersUniqueKey;
            int    numberUniqueKeyColumns;

            PrepareCodeletsUniqueKey(ACurrentTable,
                                     out formalParametersUniqueKey,
                                     out actualParametersUniqueKey,
                                     out numberUniqueKeyColumns);

            ASnippet.SetCodelet("FORMALPARAMETERSUNIQUEKEY", formalParametersUniqueKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSUNIQUEKEY", actualParametersUniqueKey);
            ASnippet.SetCodelet("UNIQUEKEYNUMBERCOLUMNS", numberUniqueKeyColumns.ToString());


            ASnippet.SetCodelet("SEQUENCENAMEANDFIELD", "");

            foreach (TTableField tablefield in ACurrentTable.grpTableField)
            {
                // is there a field filled by a sequence?
                // yes: get the next value of that sequence and assign to row
                if (tablefield.strSequence.Length > 0)
                {
                    ASnippet.SetCodelet("SEQUENCENAMEANDFIELD", ", \"" + tablefield.strSequence + "\", \"" + tablefield.strName + "\"");

                    // assume only one sequence per table
                    break;
                }
            }
        }
        /// <summary>
        /// this is for foreign keys, eg load all countries with currency EUR
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaOtherTable(TDataDefinitionStore AStore,
                                                TTable ACurrentTable,
                                                ProcessTemplate ATemplate,
                                                ProcessTemplate ASnippet)
        {
            foreach (TConstraint myConstraint in ACurrentTable.grpConstraint)
            {
                if (ValidForeignKeyConstraintForLoadVia(myConstraint))
                {
                    TTable OtherTable = AStore.GetTable(myConstraint.strOtherTable);

                    if (!LoadViaHasAlreadyBeenImplemented(myConstraint))
                    {
                        InsertViaOtherTableConstraint(AStore,
                                                      myConstraint,
                                                      ACurrentTable,
                                                      OtherTable,
                                                      ATemplate,
                                                      ASnippet);
                    }

                    // AccountHierarchy: there is no constraint that references Ledger directly, but constraint referencing the Account table with a key that contains the ledger reference
                    // but because the key in Ledger is already the primary key, a LoadViaLedger is required.
                    // other way round: p_foundation_proposal_detail has 2 constraints for foundation and foundationproposal
                    foreach (string field in myConstraint.strOtherFields)
                    {
                        // get a constraint that is only based on that field
                        TConstraint OtherLinkConstraint = OtherTable.GetConstraint(StringHelper.StrSplit(field, ","));

                        if ((OtherLinkConstraint != null) && ValidForeignKeyConstraintForLoadVia(OtherLinkConstraint))
                        {
                            TConstraint NewConstraint = new TConstraint();
                            NewConstraint.strName       = OtherLinkConstraint.strName + "forLoadVia";
                            NewConstraint.strType       = "foreignkey";
                            NewConstraint.strThisTable  = myConstraint.strThisTable;
                            NewConstraint.strThisFields =
                                StringHelper.StrSplit(myConstraint.strThisFields[myConstraint.strOtherFields.IndexOf(
                                                                                     field)], ",");
                            NewConstraint.strOtherTable  = OtherLinkConstraint.strOtherTable;
                            NewConstraint.strOtherFields = OtherLinkConstraint.strOtherFields;

                            if (!LoadViaHasAlreadyBeenImplemented(NewConstraint))
                            {
                                InsertViaOtherTableConstraint(AStore,
                                                              NewConstraint,
                                                              ACurrentTable,
                                                              AStore.GetTable(OtherLinkConstraint.strOtherTable),
                                                              ATemplate,
                                                              ASnippet);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// generate code for reading and writing typed data tables from and to the database
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="strGroup"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFilename"></param>
        /// <returns></returns>
        public static Boolean WriteTypedDataAccess(TDataDefinitionStore AStore,
                                                   string strGroup,
                                                   string AFilePath,
                                                   string ANamespaceName,
                                                   string AFilename)
        {
            Console.WriteLine("processing namespace PetraTypedDataAccess." + strGroup.Substring(0, 1).ToUpper() + strGroup.Substring(1));

            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "DataAccess.cs");

            Template.SetCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            Template.AddToCodelet("USINGNAMESPACES", GetNamespace(strGroup), false);

            bool hasTables = false;

            foreach (TTable currentTable in AStore.GetTables())
            {
                if (currentTable.strGroup == strGroup)
                {
                    DirectReferences = new ArrayList();

                    ProcessTemplate snippet = Template.GetSnippet("TABLEACCESS");

                    InsertMainProcedures(AStore, currentTable, Template, snippet);
                    InsertViaOtherTable(AStore, currentTable, Template, snippet);
                    InsertViaLinkTable(AStore, currentTable, Template, snippet);

                    Template.InsertSnippet("TABLEACCESSLOOP", snippet);

                    hasTables = true;
                }
            }

            if (hasTables)
            {
                Template.FinishWriting(AFilePath + AFilename + "-generated.cs", ".cs", true);
            }

            return(true);
        }
Beispiel #10
0
        private static Boolean WriteSequences(StreamWriter ASw,
                                              eDatabaseType ATargetDatabase,
                                              TDataDefinitionStore AStore,
                                              bool AWithSequenceInitialisation)
        {
            if (ATargetDatabase == eDatabaseType.Sqlite)
            {
                // see http://www.mail-archive.com/[email protected]/msg05123.html
                // see http://www.sqlite.org/faq.html#q1
                // no sequences in sqlite
            }
            else if (ATargetDatabase == eDatabaseType.MySQL)
            {
                // also no sequences in Mysql
                // see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html for a workaround
                // look for CREATE TABLE sequence and LAST_INSERT_ID
                List <TSequence> Sequences = AStore.GetSequences();

                foreach (TSequence seq in Sequences)
                {
                    string createStmt = "CREATE TABLE " + seq.strName + " (sequence INTEGER AUTO_INCREMENT, dummy INTEGER, PRIMARY KEY(sequence));";
                    ASw.WriteLine(createStmt);

                    if (AWithSequenceInitialisation)
                    {
                        // the following line would cause trouble later when loading the demo/base data
                        createStmt = "INSERT INTO " + seq.strName + " VALUES(NULL, -1);";
                        ASw.WriteLine(createStmt);
                    }
                }
            }
            else
            {
                List <TSequence> Sequences = AStore.GetSequences();

                foreach (TSequence Sequence in Sequences)
                {
                    if (!WriteSequence(ASw, Sequence))
                    {
                        Environment.Exit(1);
                    }
                }
            }

            return(true);
        }
Beispiel #11
0
        /// <summary>
        /// load data from a CSV file in Postgresql COPY format
        /// </summary>
        static private bool LoadData(TDataDefinitionStore ADataDefinition, SqliteConnection conn, string APath, string ATablename)
        {
            using (SqliteTransaction dbTrans = conn.BeginTransaction())
            {
                using (SqliteCommand cmd = conn.CreateCommand())
                {
                    TTable table = ADataDefinition.GetTable(ATablename);

                    StringCollection ColumnNames = new StringCollection();

                    foreach (TTableField f in table.grpTableField)
                    {
                        ColumnNames.Add(f.strName);
                    }

                    // prepare the statement
                    PrepareSqlStatement(cmd, ATablename, table, ColumnNames);

                    // load the data from the text file
                    string filename = APath + Path.DirectorySeparatorChar + ATablename + ".csv";

                    if (File.Exists(filename + ".local"))
                    {
                        filename += ".local";
                    }

                    StreamReader reader = new StreamReader(filename);
                    string       line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        ProcessLine(cmd, line, table, ColumnNames);

                        if (cmd.ExecuteNonQuery() != 1)
                        {
                            throw new Exception("failed to import line for table " + ATablename);
                        }
                    }
                }

                dbTrans.Commit();
            }

            return(true);
        }
Beispiel #12
0
        /// <summary>
        /// This will parse the xml document and store the contents in our Datadefinition Store
        /// </summary>
        /// <param name="myStore">the destination of all the information</param>
        /// <param name="ADoValidation">do we want the XmlDocument to be validated</param>
        /// <param name="AAddAutoGeneratedFields">should some special fields be generated automatically, eg. createdby/datemodified etc</param>
        /// <returns></returns>
        public Boolean ParseDocument(ref TDataDefinitionStore myStore, Boolean ADoValidation, Boolean AAddAutoGeneratedFields)
        {
            Boolean ReturnValue;
            XmlNode startNode;
            XmlNode cur;
            TTable  table;

            ReturnValue   = false;
            FDoValidation = ADoValidation;
            startNode     = myDoc.DocumentElement;

            if (startNode.Name.ToLower() == "database")
            {
                cur = NextNotBlank(startNode.FirstChild);

                if ((cur == null) || (cur.Name != "table"))
                {
                    return(ReturnValue);
                }

                while (cur != null)
                {
                    if (cur.Name == "table")
                    {
                        table = ParseTable(cur);
                        myStore.AddTable(table);
                        cur = NextNotBlank(cur.NextSibling);
                    }

                    if (cur.Name == "sequence")
                    {
                        myStore.AddSequence(ParseSequence(cur));
                        cur = NextNotBlank(cur.NextSibling);
                    }
                }
            }

            if (AAddAutoGeneratedFields == true)
            {
                myStore.PrepareAutoGeneratedFields();
            }

            myStore.PrepareLinks();
            return(true);
        }
        /// <summary>
        /// write the file clean.sql that removes all data from the database, for easy resetting of the database with clean test data
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        public static void WriteDBClean(TDataDefinitionStore AStore, string AFilename)
        {
            StreamWriter sw = new StreamWriter(AFilename + ".new");

            sw.WriteLine("-- Generated with nant generateORMTables");
            List <TTable>tables = AStore.GetTables();
            tables = TTableSort.TopologicalSort(AStore, tables);
            tables.Reverse();

            foreach (TTable t in tables)
            {
                sw.WriteLine("DELETE FROM " + t.strName + ";");
            }

            sw.Close();

            TTextFile.UpdateFile(AFilename);
        }
Beispiel #14
0
        /// <summary>
        /// write the file clean.sql that removes all data from the database, for easy resetting of the database with clean test data
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        public static void WriteDBClean(TDataDefinitionStore AStore, string AFilename)
        {
            StreamWriter sw = new StreamWriter(AFilename + ".new");

            sw.WriteLine("-- Generated with nant generateORMTables");
            List <TTable> tables = AStore.GetTables();

            tables = TTableSort.TopologicalSort(AStore, tables);
            tables.Reverse();

            foreach (TTable t in tables)
            {
                sw.WriteLine("DELETE FROM " + t.strName + ";");
            }

            sw.Close();

            TTextFile.UpdateFile(AFilename);
        }
Beispiel #15
0
        /// <summary>
        /// get the data structure for the new version
        /// </summary>
        public static TDataDefinitionStore GetStoreNew()
        {
            if (storeNew == null)
            {
                string PetraNewPath = TAppSettingsManager.GetValue("newpetraxml", "petra.xml");

                TLogging.Log(String.Format("Reading OpenPetra xml file {0}...", PetraNewPath));

                TDataDefinitionParser parserNew = new TDataDefinitionParser(PetraNewPath, false);
                storeNew = new TDataDefinitionStore();

                if (!parserNew.ParseDocument(ref storeNew, false, true))
                {
                    return(null);
                }
            }

            return(storeNew);
        }
Beispiel #16
0
        /// <summary>
        /// write an ordered list of tables, ordered by foreign key dependancies
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        public static void WriteTableList(TDataDefinitionStore AStore, string AFilename)
        {
            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "TableList.cs");

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            List <TTable> tables = AStore.GetTables();

            tables = TTableSort.TopologicalSort(AStore, tables);

            string namesCodelet             = string.Empty;
            string namesCodeletCustomReport = string.Empty;

            foreach (TTable t in tables)
            {
                namesCodelet += "list.Add(\"" + t.strName + "\");" + Environment.NewLine;

                if (t.AvailableForCustomReport)
                {
                    namesCodeletCustomReport += "list.Add(\"" + t.strName + "\");" + Environment.NewLine;
                }
            }

            Template.AddToCodelet("DBTableNames", namesCodelet);
            Template.AddToCodelet("DBTableNamesAvailableForCustomReport", namesCodeletCustomReport);

            List <TSequence> Sequences = AStore.GetSequences();

            namesCodelet = string.Empty;

            foreach (TSequence s in Sequences)
            {
                namesCodelet += "list.Add(\"" + s.strName + "\");" + Environment.NewLine;
            }

            Template.AddToCodelet("DBSequenceNames", namesCodelet);

            Template.FinishWriting(AFilename, ".cs", true);
        }
        /// <summary>
        /// create a Progress .p program for dumping the Progress data to CSV files
        /// </summary>
        public void GenerateFulldumpCode()
        {
            string PetraOldPath = TAppSettingsManager.GetValue("oldpetraxml", "petra23.xml");

            TDataDefinitionParser parserOld = new TDataDefinitionParser(PetraOldPath);

            parserOld.SupportPetra2xLegacyStandard = true;

            storeOld = new TDataDefinitionStore();

            System.Console.WriteLine("Reading 2.x xml file {0}...", PetraOldPath);

            if (!parserOld.ParseDocument(ref storeOld, false, true))
            {
                return;
            }

            string OutputFile = "fulldump23.p";

            System.Console.WriteLine("Writing file to {0}...", OutputFile);
            StreamWriter progressWriter = new StreamWriter(OutputFile);

            // print file header
            progressWriter.WriteLine("/* Generated with Ict.Tools.DataDumpPetra2x.exe */");

            System.Resources.ResourceManager RM = new System.Resources.ResourceManager("Ict.Tools.DataDumpPetra2.templateCode",
                                                                                       System.Reflection.Assembly.GetExecutingAssembly());
            progressWriter.WriteLine(RM.GetString("progress_dump_functions"));

            progressWriter.WriteLine();

            List <TTable> oldTables = storeOld.GetTables();

            foreach (TTable oldTable in oldTables)
            {
                DumpTable(ref progressWriter, oldTable);
            }

            DumpSequences(ref progressWriter);
            progressWriter.WriteLine();
            progressWriter.Close();
            System.Console.WriteLine("Success: file written: {0}", Path.GetFullPath(OutputFile));
        }
Beispiel #18
0
        /// <summary>
        /// write the file clean.sql that removes all data from the database, for easy resetting of the database with clean test data
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        private static void WriteDBClean(TDataDefinitionStore AStore, string AFilename)
        {
            StreamWriter sw = new StreamWriter(AFilename + ".new");

            sw.WriteLine("-- Generated with nant createSQLStatements");
            List <TTable> tables = AStore.GetTables();

            tables = TTableSort.TopologicalSort(AStore, tables);
            tables.Reverse();

            foreach (TTable t in tables)
            {
                sw.WriteLine("DELETE FROM " + t.strName + ";");
            }

            sw.Close();

            TTextFile.UpdateFile(AFilename);
            System.Console.WriteLine("Success: file written: {0}", AFilename);
        }
Beispiel #19
0
        /// <summary>
        /// get the data structure for the version that should be upgraded
        /// </summary>
        public static TDataDefinitionStore GetStoreOld()
        {
            if (storeOld == null)
            {
                string PetraOldPath = TAppSettingsManager.GetValue("oldpetraxml", "petra23.xml");

                TLogging.Log(String.Format("Reading 2.x xml file {0}...", PetraOldPath));
                TDataDefinitionParser parserOld = new TDataDefinitionParser(PetraOldPath);
                storeOld = new TDataDefinitionStore();

                parserOld.SupportPetra2xLegacyStandard = true;

                if (!parserOld.ParseDocument(ref storeOld, false, true))
                {
                    return(null);
                }
            }

            return(storeOld);
        }
Beispiel #20
0
        /// <summary>
        /// get the data structure for the version that should be upgraded
        /// </summary>
        public static TDataDefinitionStore GetStoreOld()
        {
            if (storeOld == null)
            {
                string PetraOldPath = TAppSettingsManager.GetValue("oldpetraxml", "petra23.xml");

                TLogging.Log(String.Format("Reading 2.x xml file {0}...", PetraOldPath));
                TDataDefinitionParser parserOld = new TDataDefinitionParser(PetraOldPath);
                storeOld = new TDataDefinitionStore();

                parserOld.SupportPetra2xLegacyStandard = true;

                if (!parserOld.ParseDocument(ref storeOld, false, true))
                {
                    return null;
                }
            }

            return storeOld;
        }
        /// <summary>
        /// also check for .SetStatusBarText([...]Table.Get[...]Help: add text from petra.xml to a separate dummy file so that it will be picked up by gettext
        /// </summary>
        /// <param name="ALine"></param>
        /// <param name="store"></param>
        /// <param name="ADbHelpTranslationWriter">dummy cs file that is used to provide the strings to gettext</param>
        private static void CheckLineAndAddDBHelp(string ALine, TDataDefinitionStore store, StreamWriter ADbHelpTranslationWriter)
        {
            Match m = Regex.Match(ALine, ".*SetStatusBarText\\(.*, (.*)Table.Get(.*)Help\\(\\)");

            if (m.Success)
            {
                // eg FPetraUtilsObject.SetStatusBarText(txtPreferredName, PPersonTable.GetPreferedNameHelp());
                string tablename  = m.Groups[1].Value;
                string columnname = m.Groups[2].Value;

                if ((store == null) || (ADbHelpTranslationWriter == null))
                {
                    Console.WriteLine("please run the usual nant translation for the whole solution so that the help text for {0}.{1} will be added.",
                                      tablename, columnname);
                    return;
                }

                TTable table = store.GetTable(tablename);
                ADbHelpTranslationWriter.WriteLine("Catalog.GetString(\"" + table.GetField(columnname).strHelp + "\");");
            }
        }
Beispiel #22
0
        /// <summary>
        /// write the file clean.sql that removes all data from the database, for easy resetting of the database with clean test data
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        private static void WriteDBClean(TDataDefinitionStore AStore, string AFilename)
        {
            StreamWriter sw = new StreamWriter(AFilename + ".new");

            sw.WriteLine("-- Generated with nant createSQLStatements");
            List <TTable> tables = AStore.GetTables();

            tables = TTableSort.TopologicalSort(AStore, tables);
            tables.Reverse();

            // drop link between s_user and p_partner, for example used for self-service
            sw.WriteLine("UPDATE s_user SET p_partner_key_n = NULL;");

            foreach (TTable t in tables)
            {
                sw.WriteLine("DELETE FROM " + t.strName + ";");
            }

            sw.Close();

            TTextFile.UpdateFile(AFilename);
            System.Console.WriteLine("Success: file written: {0}", AFilename);
        }
        /// <summary>
        /// write an ordered list of tables, ordered by foreign key dependancies
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilename"></param>
        public static void WriteTableList(TDataDefinitionStore AStore, string AFilename)
        {
            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "TableList.cs");

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            List <TTable>tables = AStore.GetTables();

            tables = TTableSort.TopologicalSort(AStore, tables);

            string namesCodelet = string.Empty;

            foreach (TTable t in tables)
            {
                namesCodelet += "list.Add(\"" + t.strName + "\");" + Environment.NewLine;
            }

            Template.AddToCodelet("DBTableNames", namesCodelet);

            List <TSequence>Sequences = AStore.GetSequences();

            namesCodelet = string.Empty;

            foreach (TSequence s in Sequences)
            {
                namesCodelet += "list.Add(\"" + s.strName + "\");" + Environment.NewLine;
            }

            Template.AddToCodelet("DBSequenceNames", namesCodelet);

            Template.FinishWriting(AFilename, ".cs", true);
        }
Beispiel #24
0
        public static void Main(string[] args)
        {
            new TAppSettingsManager(false);

            //enable execution without nant -> debugging easier
            bool independentmode = false;

            if (independentmode)
            {
                TLogging.Log("started in independent-mode");
            }

            try
            {
                if ((TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "removeDoNotTranslate")) || independentmode)
                {
                    string doNotTranslatePath;
                    string poFilePath;

                    if (independentmode)
                    {
                        doNotTranslatePath = "E:\\openpetra\\bzr\\work-690\\i18n\\doNotTranslate.po";
                        poFilePath = "E:\\openpetra\\bzr\\work-690\\i18n\\template.pot";
                    }
                    else
                    {
                        doNotTranslatePath = TAppSettingsManager.GetValue("dntFile");
                        poFilePath = TAppSettingsManager.GetValue("poFile");
                    }

                    // remove all strings from po file that are listed in the "Do Not Translate" file
                    TDropUnwantedStrings.RemoveUnwantedStringsFromTranslation(doNotTranslatePath, poFilePath);
                }
                else if (TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "errorcodedoc"))
                {
                    TGenerateErrorCodeDoc.Execute(TAppSettingsManager.GetValue("csharppath"),
                        TAppSettingsManager.GetValue("templatefilepath"),
                        TAppSettingsManager.GetValue("outfilepath"));
                }
                else if (TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "yamlfiles"))
                {
                    GenerateYamlFiles.WriteYamlFiles(
                        TAppSettingsManager.GetValue("language"),
                        TAppSettingsManager.GetValue("path"),
                        TAppSettingsManager.GetValue("pofile"));
                }
                else if (TAppSettingsManager.HasValue("file"))
                {
                    TGenerateCatalogStrings.Execute(TAppSettingsManager.GetValue("file"), null, null);
                }
                else if (TAppSettingsManager.HasValue("filelist"))
                {
                    TDataDefinitionStore store = new TDataDefinitionStore();
                    Console.WriteLine("parsing " + TAppSettingsManager.GetValue("petraxml", true));
                    TDataDefinitionParser parser = new TDataDefinitionParser(TAppSettingsManager.GetValue("petraxml", true));
                    parser.ParseDocument(ref store, true, true);

                    string CollectedStringsFilename = TAppSettingsManager.GetValue("tmpPath") +
                                                      Path.DirectorySeparatorChar +
                                                      "GenerateI18N.CollectedGettext.cs";
                    StreamWriter writerCollectedStringsFile = new StreamWriter(CollectedStringsFilename);

                    string GettextApp = TAppSettingsManager.GetValue("gettext");

                    string filesToParseWithGettext = string.Empty;
                    StreamReader readerFilelist = new StreamReader(TAppSettingsManager.GetValue("filelist"));

                    while (!readerFilelist.EndOfStream)
                    {
                        string pathCodeFile = readerFilelist.ReadLine().Trim();
                        string ext = Path.GetExtension(pathCodeFile);

                        if (".cs" == ext)
                        {
                            if (TGenerateCatalogStrings.Execute(pathCodeFile, store, writerCollectedStringsFile))
                            {
                                filesToParseWithGettext += "\"" + pathCodeFile + "\" ";

                                if (filesToParseWithGettext.Length > 1500)
                                {
                                    ParseWithGettext(GettextApp, TAppSettingsManager.GetValue("poFile"), filesToParseWithGettext);
                                    filesToParseWithGettext = string.Empty;
                                }
                            }
                        }
                        else if (".yml" == ext)
                        {
                            TGenerateCatalogStrings.AddTranslationUINavigation(pathCodeFile, writerCollectedStringsFile);
                        }
                        else
                        {
                            Console.WriteLine("the file " + pathCodeFile + " has an unknown extension! File ignored!");
                        }
                    }

                    if (filesToParseWithGettext.Length > 0)
                    {
                        ParseWithGettext(GettextApp, TAppSettingsManager.GetValue("poFile"), filesToParseWithGettext);
                    }

                    writerCollectedStringsFile.Close();

                    // delete the file if it is empty
                    if (File.ReadAllText(CollectedStringsFilename).Length == 0)
                    {
                        File.Delete(CollectedStringsFilename);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Environment.Exit(-1);
            }
        }
        /// <summary>
        /// create a Progress .p program for dumping the Progress data to CSV files
        /// </summary>
        public void GenerateFulldumpCode()
        {
            string PetraOldPath = TAppSettingsManager.GetValue("oldpetraxml", "petra23.xml");

            TDataDefinitionParser parserOld = new TDataDefinitionParser(PetraOldPath);

            parserOld.SupportPetra2xLegacyStandard = true;

            storeOld = new TDataDefinitionStore();

            System.Console.WriteLine("Reading 2.x xml file {0}...", PetraOldPath);

            if (!parserOld.ParseDocument(ref storeOld, false, true))
            {
                return;
            }

            string OutputFile = "fulldump23.p";

            System.Console.WriteLine("Writing file to {0}...", OutputFile);
            StreamWriter progressWriter = new StreamWriter(OutputFile);

            // print file header
            progressWriter.WriteLine("/* Generated with Ict.Tools.DataDumpPetra2x.exe */");

            System.Resources.ResourceManager RM = new System.Resources.ResourceManager("Ict.Tools.DataDumpPetra2.templateCode",
                System.Reflection.Assembly.GetExecutingAssembly());
            progressWriter.WriteLine(RM.GetString("progress_dump_functions"));

            progressWriter.WriteLine();

            List <TTable>oldTables = storeOld.GetTables();

            foreach (TTable oldTable in oldTables)
            {
                DumpTable(ref progressWriter, oldTable);
            }

            DumpSequences(ref progressWriter);
            progressWriter.WriteLine();
            progressWriter.Close();
            System.Console.WriteLine("Success: file written: {0}", Path.GetFullPath(OutputFile));
        }
    /// <summary>
    /// also check for .SetStatusBarText([...]Table.Get[...]Help: add text from petra.xml to a separate dummy file so that it will be picked up by gettext
    /// </summary>
    /// <param name="ALine"></param>
    /// <param name="store"></param>
    /// <param name="ADbHelpTranslationWriter">dummy cs file that is used to provide the strings to gettext</param>
    private static void CheckLineAndAddDBHelp(string ALine, TDataDefinitionStore store, StreamWriter ADbHelpTranslationWriter)
    {
        Match m = Regex.Match(ALine, ".*SetStatusBarText\\(.*, (.*)Table.Get(.*)Help\\(\\)");

        if (m.Success)
        {
            // eg FPetraUtilsObject.SetStatusBarText(txtPreferredName, PPersonTable.GetPreferedNameHelp());
            string tablename = m.Groups[1].Value;
            string columnname = m.Groups[2].Value;

            if ((store == null) || (ADbHelpTranslationWriter == null))
            {
                Console.WriteLine("please run the usual nant translation for the whole solution so that the help text for {0}.{1} will be added.",
                    tablename, columnname);
                return;
            }

            TTable table = store.GetTable(tablename);
            ADbHelpTranslationWriter.WriteLine("Catalog.GetString(\"" + table.GetField(columnname).strHelp + "\");");
        }
    }
Beispiel #27
0
        int run()
        {
            new TAppSettingsManager(false);

            cmdLine = new TCmdOpts();

            if (!cmdLine.IsFlagSet("do")
                || ((cmdLine.GetOptValue("do") == "dataset") && (!cmdLine.IsFlagSet("input") || !cmdLine.IsFlagSet("outputNamespace")))
                || ((cmdLine.GetOptValue("do") == "referencecount")
                    && (!cmdLine.IsFlagSet("inputclient") || !cmdLine.IsFlagSet("outputserver") || !cmdLine.IsFlagSet("templatedir"))))
            {
                System.Console.WriteLine("GenerateORM: generate Typed Tables and Datasets");
                System.Console.WriteLine("usage: GenerateORM -do:<operation> -petraxml:<xyz/petra.xml>");
                System.Console.WriteLine("operations available:");
                System.Console.WriteLine("  defaulttables ");
                System.Console.WriteLine("           with parameters: ");
                System.Console.WriteLine("                 -outputshared:<path to ICT/Petra/Shared>");
                System.Console.WriteLine("  dataaccess ");
                System.Console.WriteLine("           with parameters: ");
                System.Console.WriteLine("                 -outputshared:<path to ICT/Petra/Shared>");
                System.Console.WriteLine("  dataset ");
                System.Console.WriteLine("           with parameters: ");
                System.Console.WriteLine("                 -input:<dataset XML file>");
                System.Console.WriteLine("                 -outputNamespace:<Namespace of the file>");
                System.Console.WriteLine("  cachedtables ");
                System.Console.WriteLine("           with parameters: ");
                System.Console.WriteLine("                 -cachedef:<dataset XML file>");
                System.Console.WriteLine("                 -outputshared:<path to ICT/Petra/Shared>");
                System.Console.WriteLine("  referencecount ");
                System.Console.WriteLine("           with parameters: ");
                System.Console.WriteLine("                 -inputclient:<path to ICT/Petra/Client>");
                System.Console.WriteLine("                 -outputserver:<path to ICT/Petra/Server>");
                System.Console.WriteLine("                 -templatedir:<path to inc/template/src>");
                System.Console.WriteLine(
                    "       e.g. GenerateORM -do:dataset -petraxml:U:/sql/datadefinition/petra.xml -input:U:/sql/datadefinition/dataset.xml -outputNamespace:Ict.Petra.Shared.MCommon.Data.Dataset");
                return 100;
            }

            try
            {
                if (cmdLine.GetOptValue("do") == "referencecount")
                {
                    // No need to parse the petra.xml document for this task - so we just run and exit
                    new TLogging();
                    TCreateReferenceCountConnectors createConnectors = new TCreateReferenceCountConnectors();

                    if (!createConnectors.CreateFiles(cmdLine.GetOptValue("outputserver"), cmdLine.GetOptValue("inputclient"),
                            cmdLine.GetOptValue("templatedir")))
                    {
                        return -1;
                    }

                    return 0;
                }

                parser = new TDataDefinitionParser(cmdLine.GetOptValue("petraxml"));
                store = new TDataDefinitionStore();

                if (parser.ParseDocument(ref store))
                {
                    if ((cmdLine.GetOptValue("do") == "defaulttables") || (cmdLine.GetOptValue("do") == "datatables"))
                    {
                        CodeGenerationTable.WriteTypedTable(store, "partner", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MPartner.Partner.Data",
                            "Partner.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "mailroom", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MPartner.Mailroom.Data",
                            "Mailroom.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "personnel", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MPersonnel.Personnel.Data",
                            "Personnel.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "units", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MPersonnel.Units.Data",
                            "Units.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "conference", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MConference.Data",
                            "Conference.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "hospitality", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MHospitality.Data",
                            "Hospitality.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "account", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MFinance.Account.Data",
                            "Account.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "ap", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MFinance.AP.Data",
                            "AP.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "ar", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MFinance.AR.Data",
                            "AR.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "gift", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MFinance.Gift.Data",
                            "Gift.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "sysman", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MSysMan.Data",
                            "SysMan.Tables");
                        CodeGenerationTable.WriteTypedTable(store, "common", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Shared.MCommon.Data",
                            "Common.Tables");

                        CodeGenerationTableValidation.WriteValidation(store, "partner", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MPartner/validation/",
                            "Ict.Petra.Shared.MPartner.Partner.Validation",
                            "Partner.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "mailroom", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MPartner/validation/",
                            "Ict.Petra.Shared.MPartner.Mailroom.Validation",
                            "Mailroom.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "personnel", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MPersonnel/validation/",
                            "Ict.Petra.Shared.MPersonnel.Personnel.Validation",
                            "Personnel.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "units", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MPersonnel/validation/",
                            "Ict.Petra.Shared.MPersonnel.Units.Validation",
                            "Units.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "conference", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MConference/validation/",
                            "Ict.Petra.Shared.MConference.Validation",
                            "Conference.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "hospitality", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MHospitality/validation/",
                            "Ict.Petra.Shared.MHospitality.Validation",
                            "Hospitality.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "account", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MFinance/validation/",
                            "Ict.Petra.Shared.MFinance.Account.Validation",
                            "Account.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "ap", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MFinance/validation/",
                            "Ict.Petra.Shared.MFinance.AP.Validation",
                            "AP.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "ar", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MFinance/validation/",
                            "Ict.Petra.Shared.MFinance.AR.Validation",
                            "AR.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "gift", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MFinance/validation/",
                            "Ict.Petra.Shared.MFinance.Gift.Validation",
                            "Gift.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "sysman", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MSysMan/validation/",
                            "Ict.Petra.Shared.MSysMan.Validation",
                            "SysMan.Validation");
                        CodeGenerationTableValidation.WriteValidation(store, "common", cmdLine.GetOptValue(
                                "outputshared") + "/lib/MCommon/validation/",
                            "Ict.Petra.Shared.MCommon.Validation",
                            "Common.Validation");

                        TGenerateTableList.WriteTableList(store, cmdLine.GetOptValue(
                                "outputshared") + Path.DirectorySeparatorChar + "TableList-generated.cs");
                        TGenerateTableList.WriteDBClean(store, Path.GetDirectoryName(cmdLine.GetOptValue(
                                    "petraxml")) + Path.DirectorySeparatorChar + "basedata" + Path.DirectorySeparatorChar + "clean.sql");
                    }
                    else if (cmdLine.GetOptValue("do") == "dataaccess")
                    {
                        CodeGenerationAccess.WriteTypedDataAccess(store, "partner", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MPartner.Partner.Data.Access",
                            "Partner.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "mailroom", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MPartner.Mailroom.Data.Access",
                            "Mailroom.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "personnel", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MPersonnel.Personnel.Data.Access",
                            "Personnel.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "units", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MPersonnel.Units.Data.Access",
                            "Units.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "conference", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MConference.Data.Access",
                            "Conference.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "hospitality", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MHospitality.Data.Access",
                            "Hospitality.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "account", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MFinance.Account.Data.Access",
                            "Account.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "ap", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MFinance.AP.Data.Access",
                            "AP.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "ar", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MFinance.AR.Data.Access",
                            "AR.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "gift", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MFinance.Gift.Data.Access",
                            "Gift.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "sysman", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MSysMan.Data.Access",
                            "SysMan.Access");
                        CodeGenerationAccess.WriteTypedDataAccess(store, "common", cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MCommon.Data.Access",
                            "Common.Access");
                        CodeGenerationCascading.WriteTypedDataCascading(store, cmdLine.GetOptValue(
                                "outputshared") + "/lib/data/",
                            "Ict.Petra.Server.MCommon.Data.Cascading",
                            "Cascading");
                    }
                    else if (cmdLine.GetOptValue("do") == "dataset")
                    {
                        string[] groups = new string[] {
                            "common", "mailroom", "sysman", "partner",
                            "account", "gift", "ap", "ar", "personnel", "units", "conference", "hospitality"
                        };

                        CodeGenerationDataset.CreateTypedDataSets(cmdLine.GetOptValue("input"),
                            cmdLine.GetOptValue("outputdir"),
                            cmdLine.GetOptValue("outputNamespace"),
                            store, groups,
                            cmdLine.GetOptValue("outputFilename"));
                    }
                    else if (cmdLine.GetOptValue("do") == "datasetaccess")
                    {
                        string[] groups = new string[] {
                            "common", "mailroom", "sysman", "partner",
                            "account", "gift", "ap", "ar", "personnel", "units", "conference", "hospitality"
                        };

                        CodeGenerationDatasetAccess.CreateTypedDataSets(cmdLine.GetOptValue("input"),
                            cmdLine.GetOptValue("outputdir"),
                            cmdLine.GetOptValue("outputNamespace"),
                            store, groups,
                            cmdLine.GetOptValue("outputFilename"));
                    }
                    else if (cmdLine.GetOptValue("do") == "cachedtables")
                    {
                        Ict.Tools.CodeGeneration.CachedTables.TGenerateCachedTables.WriteCachedTables(
                            store,
                            cmdLine.GetOptValue("cachedef"),
                            cmdLine.GetOptValue("outputshared"),
                            cmdLine.GetOptValue("TemplateDir"));
                    }
                    else
                    {
                        Console.WriteLine("could not recognise: " + cmdLine.GetOptValue("do"));
                    }
                }
            }
            catch (Exception E)
            {
                System.Console.WriteLine(E.Message);
                System.Console.WriteLine(E.StackTrace);
                return 100;
                // System.Console.ReadLine();
            }
            return 0;
        }
        /// <summary>
        /// code for cascading functions
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        /// <returns>false if no cascading available</returns>
        private static bool InsertMainProcedures(TDataDefinitionStore AStore,
            TTable ACurrentTable,
            ProcessTemplate ATemplate,
            ProcessTemplate ASnippet)
        {
            string csvListPrimaryKeyFields;
            string formalParametersPrimaryKey;
            string actualParametersPrimaryKey;

            Tuple <string, string, string>[] formalParametersPrimaryKeySeparate;
            string actualParametersPrimaryKeyFromPKArray = String.Empty;

            ASnippet.AddToCodelet("TABLENAME", TTable.NiceTableName(ACurrentTable.strName));
            ASnippet.AddToCodelet("THISTABLELABEL", ACurrentTable.strLabel);

            PrepareCodeletsPrimaryKey(ACurrentTable,
                out csvListPrimaryKeyFields,
                out formalParametersPrimaryKey,
                out actualParametersPrimaryKey,
                out formalParametersPrimaryKeySeparate);

            for (int Counter = 0; Counter < formalParametersPrimaryKeySeparate.Length; Counter++)
            {
                actualParametersPrimaryKeyFromPKArray +=
                    "(" + formalParametersPrimaryKeySeparate[Counter].Item1 + ")" +
                    "APrimaryKeyValues[" + Counter.ToString() + "], ";
            }

            // Strip off trailing ", "
            actualParametersPrimaryKeyFromPKArray = actualParametersPrimaryKeyFromPKArray.Substring(0,
                actualParametersPrimaryKeyFromPKArray.Length - 2);

            ASnippet.AddToCodelet("CSVLISTPRIMARYKEYFIELDS", csvListPrimaryKeyFields);
            ASnippet.AddToCodelet("FORMALPARAMETERSPRIMARYKEY", formalParametersPrimaryKey);
            ASnippet.AddToCodelet("ACTUALPARAMETERSPRIMARYKEY", actualParametersPrimaryKey);
            ASnippet.AddToCodelet("ACTUALPARAMETERSPRIMARYKEYFROMPKARRAY", actualParametersPrimaryKeyFromPKArray);

            for (int Counter = 0; Counter < ACurrentTable.GetPrimaryKey().strThisFields.Count; Counter++)
            {
                ProcessTemplate PKInfoDictBuilding = ASnippet.GetSnippet("PRIMARYKEYINFODICTBUILDING");
                PKInfoDictBuilding.SetCodelet("PKCOLUMNLABEL", formalParametersPrimaryKeySeparate[Counter].Item3);
                PKInfoDictBuilding.SetCodelet("PKCOLUMNCONTENT", formalParametersPrimaryKeySeparate[Counter].Item2);
                ASnippet.InsertSnippet("PRIMARYKEYINFODICTBUILDING", PKInfoDictBuilding);
            }

            ASnippet.AddToCodelet("PRIMARYKEYCOLUMNCOUNT", ACurrentTable.GetPrimaryKey().strThisFields.Count.ToString());

            foreach (TConstraint constraint in ACurrentTable.FReferenced)
            {
                if (AStore.GetTable(constraint.strThisTable).HasPrimaryKey())
                {
                    string csvListOtherPrimaryKeyFields;
                    string notUsed;
                    Tuple <string, string, string>[] formalParametersPrimaryKeySeparate2;

                    TTable OtherTable = AStore.GetTable(constraint.strThisTable);

                    PrepareCodeletsPrimaryKey(OtherTable,
                        out csvListOtherPrimaryKeyFields,
                        out notUsed,
                        out notUsed,
                        out formalParametersPrimaryKeySeparate2);

                    // check if other foreign key exists that references the same table, e.g.
                    // PBankAccess.LoadViaPPartnerPartnerKey
                    // PBankAccess.LoadViaPPartnerContactPartnerKey
                    string DifferentField = CodeGenerationAccess.FindOtherConstraintSameOtherTable(
                        OtherTable.grpConstraint,
                        constraint);
                    string LoadViaProcedureName = TTable.NiceTableName(ACurrentTable.strName);
                    string MyOtherTableName = "My" + TTable.NiceTableName(constraint.strThisTable);

                    if (DifferentField.Length != 0)
                    {
                        LoadViaProcedureName += TTable.NiceFieldName(DifferentField);
                        MyOtherTableName += TTable.NiceFieldName(DifferentField);
                    }

                    // for the moment, don't implement it for too big tables, e.g. s_user)
                    if ((ACurrentTable.HasPrimaryKey() || (ACurrentTable.FReferenced.Count <= CASCADING_DELETE_MAX_REFERENCES))
                        && ((constraint.strThisTable != "a_ledger")
                            && (!LoadViaProcedureName.StartsWith("SUser"))))
                    {
                        ProcessTemplate snippetDelete = ASnippet.GetSnippet("DELETEBYPRIMARYKEYCASCADING");
                        snippetDelete.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(constraint.strThisTable));
                        snippetDelete.SetCodelet("MYOTHERTABLENAME", MyOtherTableName);
                        snippetDelete.SetCodelet("VIAPROCEDURENAME", "Via" + LoadViaProcedureName);
                        snippetDelete.SetCodelet("CSVLISTOTHERPRIMARYKEYFIELDS", csvListOtherPrimaryKeyFields);
                        snippetDelete.SetCodelet("OTHERTABLEALSOCASCADING", "true");


                        ASnippet.InsertSnippet("DELETEBYPRIMARYKEYCASCADING", snippetDelete);

                        snippetDelete = ASnippet.GetSnippet("DELETEBYTEMPLATECASCADING");
                        snippetDelete.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(constraint.strThisTable));
                        snippetDelete.SetCodelet("MYOTHERTABLENAME", MyOtherTableName);
                        snippetDelete.SetCodelet("VIAPROCEDURENAME", "Via" + LoadViaProcedureName);
                        snippetDelete.SetCodelet("CSVLISTOTHERPRIMARYKEYFIELDS", csvListOtherPrimaryKeyFields);
                        snippetDelete.SetCodelet("OTHERTABLEALSOCASCADING", "true");


                        ASnippet.InsertSnippet("DELETEBYTEMPLATECASCADING", snippetDelete);
                    }

                    if ((constraint.strThisTable != "a_ledger")
                        && (!LoadViaProcedureName.StartsWith("SUser")))
                    {
                        ProcessTemplate snippetCount = ASnippet.GetSnippet("COUNTBYPRIMARYKEYCASCADING");
                        snippetCount.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(constraint.strThisTable));
                        snippetCount.SetCodelet("MYOTHERTABLENAME", MyOtherTableName);
                        snippetCount.SetCodelet("VIAPROCEDURENAME", "Via" + LoadViaProcedureName);
                        snippetCount.SetCodelet("CSVLISTOTHERPRIMARYKEYFIELDS", csvListOtherPrimaryKeyFields);
                        snippetCount.SetCodelet("OTHERTABLEALSOCASCADING", "true");

                        for (int Counter = 0; Counter < OtherTable.GetPrimaryKey().strThisFields.Count; Counter++)
                        {
                            ProcessTemplate PKInfoDictBuilding2 = ASnippet.GetSnippet("PRIMARYKEYINFODICTBUILDING");
                            PKInfoDictBuilding2.SetCodelet("PKCOLUMNLABEL", formalParametersPrimaryKeySeparate2[Counter].Item3);
                            PKInfoDictBuilding2.SetCodelet("PKCOLUMNCONTENT", "\"\"");
                            snippetCount.InsertSnippet("PRIMARYKEYINFODICTBUILDING2", PKInfoDictBuilding2);
                        }

                        snippetCount.SetCodelet("PRIMARYKEYCOLUMNCOUNT2", OtherTable.GetPrimaryKey().strThisFields.Count.ToString());

                        ASnippet.InsertSnippet("COUNTBYPRIMARYKEYCASCADING", snippetCount);

                        snippetCount = ASnippet.GetSnippet("COUNTBYTEMPLATECASCADING");
                        snippetCount.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(constraint.strThisTable));
                        snippetCount.SetCodelet("OTHERTABLELABEL", OtherTable.strLabel);
                        snippetCount.SetCodelet("MYOTHERTABLENAME", MyOtherTableName);
                        snippetCount.SetCodelet("VIAPROCEDURENAME", "Via" + LoadViaProcedureName);
                        snippetCount.SetCodelet("CSVLISTOTHERPRIMARYKEYFIELDS", csvListOtherPrimaryKeyFields);
                        snippetCount.SetCodelet("OTHERTABLEALSOCASCADING", "true");

                        for (int Counter = 0; Counter < OtherTable.GetPrimaryKey().strThisFields.Count; Counter++)
                        {
                            ProcessTemplate PKInfoDictBuilding2 = ASnippet.GetSnippet("PRIMARYKEYINFODICTBUILDING");
                            PKInfoDictBuilding2.SetCodelet("PKCOLUMNLABEL", formalParametersPrimaryKeySeparate2[Counter].Item3);
                            PKInfoDictBuilding2.SetCodelet("PKCOLUMNCONTENT", "\"\"");
                            snippetCount.InsertSnippet("PRIMARYKEYINFODICTBUILDING2", PKInfoDictBuilding2);
                        }

                        snippetCount.SetCodelet("PRIMARYKEYCOLUMNCOUNT2", OtherTable.GetPrimaryKey().strThisFields.Count.ToString());

                        ASnippet.InsertSnippet("COUNTBYTEMPLATECASCADING", snippetCount);
                    }
                }
            }

            return true;
        }
        /// <summary>
        /// situation 2: bridging tables
        /// for example p_location and p_partner are connected through p_partner_location
        /// it would be helpful, to be able to do:
        /// location.LoadViaPartner(partnerkey) to get all locations of the partner
        /// partner.loadvialocation(locationkey) to get all partners living at that location
        /// general solution: pick up all foreign keys from other tables (B) to the primary key of the current table (A),
        /// where the other table has a foreign key to another table (C), using other fields in the primary key of (B) than the link to (A).
        /// get all tables that reference the current table
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaLinkTable(TDataDefinitionStore AStore, TTable ACurrentTable, ProcessTemplate ATemplate, ProcessTemplate ASnippet)
        {
            // step 1: collect all the valid constraints of such link tables
            ArrayList OtherLinkConstraints = new ArrayList();
            ArrayList References           = new ArrayList();

            foreach (TConstraint Reference in ACurrentTable.GetReferences())
            {
                TTable LinkTable = AStore.GetTable(Reference.strThisTable);
                try
                {
                    TConstraint LinkPrimaryKey = LinkTable.GetPrimaryKey();

                    if (StringHelper.Contains(LinkPrimaryKey.strThisFields, Reference.strThisFields))
                    {
                        // check how many constraints from the link table are from fields in the primary key
                        // a link table only should have 2
                        // so find the other one
                        TConstraint OtherLinkConstraint = null;
                        bool        IsLinkTable         = false;

                        foreach (TConstraint LinkConstraint in LinkTable.grpConstraint)
                        {
                            // check if there is another constraint for the primary key of the link table.
                            if (ValidForeignKeyConstraintForLoadVia(LinkConstraint) && (LinkConstraint != Reference) &&
                                (StringHelper.Contains(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields)))
                            {
                                if (OtherLinkConstraint == null)
                                {
                                    OtherLinkConstraint = LinkConstraint;
                                    IsLinkTable         = true;
                                }
                                else
                                {
                                    IsLinkTable = false;
                                }
                            }
                            else if (ValidForeignKeyConstraintForLoadVia(LinkConstraint) && (LinkConstraint != Reference) &&
                                     (!StringHelper.Contains(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields)) &&
                                     StringHelper.ContainsSome(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields))
                            {
                                // if there is a key that partly is in the primary key, then this is not a link table.
                                OtherLinkConstraint = LinkConstraint;
                                IsLinkTable         = false;
                            }
                        }

                        if ((IsLinkTable) && (OtherLinkConstraint.strOtherTable != Reference.strOtherTable))
                        {
                            // collect the links. then we are able to name them correctly, once we need them
                            OtherLinkConstraints.Add(OtherLinkConstraint);
                            References.Add(Reference);
                        }
                    }
                }
                catch (Exception)
                {
                    // Console.WriteLine(e.Message);
                }
            }

            // step2: implement the link tables, using correct names for the procedures
            int Count = 0;

            foreach (TConstraint OtherLinkConstraint in OtherLinkConstraints)
            {
                TTable OtherTable    = AStore.GetTable(OtherLinkConstraint.strOtherTable);
                string ProcedureName = "Via" + TTable.NiceTableName(OtherTable.strName);

                // check if other foreign key exists that references the same table, e.g.
                // PPartnerAccess.LoadViaSUserPRecentPartners
                // PPartnerAccess.LoadViaSUserPCustomisedGreeting
                // also PFoundationProposalAccess.LoadViaPFoundationPFoundationProposalDetail and
                // TODO AlreadyExistsProcedureSameName necessary for PPersonAccess.LoadViaPUnit (p_field_key_n) and PPersonAccess.LoadViaPUnitPmGeneralApplication
                // Question: does PPersonAccess.LoadViaPUnitPmGeneralApplication make sense?
                if (FindOtherConstraintSameOtherTable2(OtherLinkConstraints,
                                                       OtherLinkConstraint) ||
                    LoadViaHasAlreadyBeenImplemented(OtherLinkConstraint)

                    // TODO || AlreadyExistsProcedureSameName(ProcedureName)
                    || ((ProcedureName == "ViaPUnit") && (OtherLinkConstraint.strThisTable == "pm_general_application"))
                    )
                {
                    ProcedureName += TTable.NiceTableName(OtherLinkConstraint.strThisTable);
                }

                ATemplate.AddToCodelet("USINGNAMESPACES",
                                       GetNamespace(OtherTable.strGroup), false);

                ProcessTemplate snippetLinkTable = ATemplate.GetSnippet("VIALINKTABLE");

                snippetLinkTable.SetCodelet("VIAPROCEDURENAME", ProcedureName);
                snippetLinkTable.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(OtherTable.strName));
                snippetLinkTable.SetCodelet("SQLOTHERTABLENAME", OtherTable.strName);
                snippetLinkTable.SetCodelet("SQLLINKTABLENAME", OtherLinkConstraint.strThisTable);

                string notUsed;
                int    notUsedInt;
                string formalParametersOtherPrimaryKey;
                string actualParametersOtherPrimaryKey;

                PrepareCodeletsPrimaryKey(OtherTable,
                                          out formalParametersOtherPrimaryKey,
                                          out actualParametersOtherPrimaryKey,
                                          out notUsed,
                                          out notUsedInt);

                PrepareCodeletsPrimaryKey(ACurrentTable,
                                          out notUsed,
                                          out notUsed,
                                          out notUsed,
                                          out notUsedInt);

                string whereClauseForeignKey;
                string whereClauseViaOtherTable;
                string odbcParametersForeignKey;
                PrepareCodeletsForeignKey(
                    OtherTable,
                    OtherLinkConstraint,
                    out whereClauseForeignKey,
                    out whereClauseViaOtherTable,
                    out odbcParametersForeignKey,
                    out notUsedInt,
                    out notUsed);

                string whereClauseViaLinkTable;
                string whereClauseAllViaTables;
                PrepareCodeletsViaLinkTable(
                    (TConstraint)References[Count],
                    OtherLinkConstraint,
                    out whereClauseViaLinkTable,
                    out whereClauseAllViaTables);

                snippetLinkTable.SetCodelet("FORMALPARAMETERSOTHERPRIMARYKEY", formalParametersOtherPrimaryKey);
                snippetLinkTable.SetCodelet("ACTUALPARAMETERSOTHERPRIMARYKEY", actualParametersOtherPrimaryKey);
                snippetLinkTable.SetCodelet("ODBCPARAMETERSFOREIGNKEY", odbcParametersForeignKey);
                snippetLinkTable.SetCodelet("WHERECLAUSEVIALINKTABLE", whereClauseViaLinkTable);
                snippetLinkTable.SetCodelet("WHERECLAUSEALLVIATABLES", whereClauseAllViaTables);

                ASnippet.InsertSnippet("VIALINKTABLE", snippetLinkTable);

                Count = Count + 1;
            }
        }
        /// <summary>
        /// insert the viaothertable functions for one specific constraint
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AConstraint"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="AOtherTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaOtherTableConstraint(TDataDefinitionStore AStore,
                                                          TConstraint AConstraint,
                                                          TTable ACurrentTable,
                                                          TTable AOtherTable,
                                                          ProcessTemplate ATemplate,
                                                          ProcessTemplate ASnippet)
        {
            ATemplate.AddToCodelet("USINGNAMESPACES",
                                   GetNamespace(AOtherTable.strGroup), false);

            ProcessTemplate snippetViaTable = ATemplate.GetSnippet("VIAOTHERTABLE");

            snippetViaTable.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(AOtherTable.strName));
            snippetViaTable.SetCodelet("SQLOTHERTABLENAME", AOtherTable.strName);

            string ProcedureName = "Via" + TTable.NiceTableName(AOtherTable.strName);

            // check if other foreign key exists that references the same table, e.g.
            // PBankAccess.CountViaPPartnerPartnerKey
            // PBankAccess.CountViaPPartnerContactPartnerKey
            string DifferentField = FindOtherConstraintSameOtherTable(ACurrentTable.grpConstraint, AConstraint);

            if (DifferentField.Length != 0)
            {
                ProcedureName += TTable.NiceFieldName(DifferentField);
            }

            int    notUsedInt;
            string formalParametersOtherPrimaryKey;
            string actualParametersOtherPrimaryKey;
            string dummy;

            PrepareCodeletsPrimaryKey(AOtherTable,
                                      out formalParametersOtherPrimaryKey,
                                      out actualParametersOtherPrimaryKey,
                                      out dummy,
                                      out notUsedInt);

            int numberFields;

            string namesOfThisTableFields;
            string notUsed;

            PrepareCodeletsForeignKey(
                AOtherTable,
                AConstraint,
                out notUsed,
                out notUsed,
                out notUsed,
                out numberFields,
                out namesOfThisTableFields);

            snippetViaTable.SetCodelet("VIAPROCEDURENAME", ProcedureName);
            snippetViaTable.SetCodelet("FORMALPARAMETERSOTHERPRIMARYKEY", formalParametersOtherPrimaryKey);
            snippetViaTable.SetCodelet("ACTUALPARAMETERSOTHERPRIMARYKEY", actualParametersOtherPrimaryKey);
            snippetViaTable.SetCodelet("NUMBERFIELDS", numberFields.ToString());
            snippetViaTable.SetCodelet("NUMBERFIELDSOTHER", (actualParametersOtherPrimaryKey.Split(',').Length).ToString());
            snippetViaTable.SetCodelet("THISTABLEFIELDS", namesOfThisTableFields);

            AddDirectReference(AConstraint);

            ASnippet.InsertSnippet("VIAOTHERTABLE", snippetViaTable);
        }
 /// <summary>
 /// overloaded version of ParseDocument, always add special fields, and validate the document
 /// </summary>
 /// <param name="myStore">the destination for all the parsed information</param>
 /// <returns></returns>
 public Boolean ParseDocument(ref TDataDefinitionStore myStore)
 {
     return ParseDocument(ref myStore, true, true);
 }
Beispiel #32
0
    /// <summary>
    /// create a database file or the scripts for creating a database.
    /// for SQLite that is directly the file,
    /// for the other database systems the sql create table scripts etc are written
    /// </summary>
    /// <param name="AStore"></param>
    /// <param name="ATargetDatabase"></param>
    /// <param name="AOutputFile"></param>
    public static void WriteSQL(TDataDefinitionStore AStore, eDatabaseType ATargetDatabase, String AOutputFile)
    {
        System.Console.WriteLine("Writing file to {0}...", AOutputFile);

        FileStream outPutFileStream = new FileStream(AOutputFile, FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(outPutFileStream);
        List <TTable>Tables = AStore.GetTables();

        foreach (TTable Table in Tables)
        {
            if (!WriteTable(ATargetDatabase, sw, Table))
            {
                Environment.Exit(1);
            }
        }

        foreach (TTable Table in Tables)
        {
            if (ATargetDatabase == eDatabaseType.Sqlite)
            {
                // see http://www.sqlite.org/omitted.html:
                // sqlite does not support Alter table add constraint
            }
            else
            {
                DumpConstraints(sw, Table, true, true);
            }
        }

        foreach (TTable Table in Tables)
        {
            // Dump indexes
            DumpIndexes(sw, Table, true);
        }

        WriteSequences(sw, ATargetDatabase, AStore, true);

        sw.Close();
        System.Console.WriteLine("Success: file written: {0}", AOutputFile);

        string createTablesWithoutConstraints =
            Path.GetDirectoryName(AOutputFile) + Path.DirectorySeparatorChar +
            Path.GetFileNameWithoutExtension(AOutputFile) + "_withoutConstraints.sql";
        outPutFileStream = new FileStream(
            createTablesWithoutConstraints,
            FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(outPutFileStream);

        if (ATargetDatabase == eDatabaseType.PostgreSQL)
        {
            sw.WriteLine("SET client_min_messages TO WARNING;");
        }

        foreach (TTable Table in Tables)
        {
            if (!WriteTable(ATargetDatabase, sw, Table))
            {
                Environment.Exit(1);
            }
        }

        WriteSequences(sw, ATargetDatabase, AStore, true);
        sw.Close();
        System.Console.WriteLine("Success: file written: {0}", createTablesWithoutConstraints);

        string removeAllTablesFile =
            Path.GetDirectoryName(AOutputFile) + Path.DirectorySeparatorChar +
            Path.GetFileNameWithoutExtension(AOutputFile) + "_remove.sql";
        outPutFileStream = new FileStream(
            removeAllTablesFile,
            FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(outPutFileStream);

        Tables = TTableSort.TopologicalSort(AStore, Tables);
        Tables.Reverse();

        foreach (TTable Table in Tables)
        {
            sw.WriteLine("DROP TABLE IF EXISTS " + Table.strName + " CASCADE;");
        }

        if (ATargetDatabase == eDatabaseType.Sqlite)
        {
            // see http://www.mail-archive.com/[email protected]/msg05123.html
            // see http://www.sqlite.org/faq.html#q1
            // no sequences in sqlite
        }
        else if (ATargetDatabase == eDatabaseType.MySQL)
        {
            // also no sequences in Mysql
            // see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html for a workaround
            // look for CREATE TABLE sequence and LAST_INSERT_ID
        }
        else
        {
            List <TSequence>Sequences = AStore.GetSequences();

            foreach (TSequence Sequence in Sequences)
            {
                sw.WriteLine("DROP SEQUENCE IF EXISTS " + Sequence.strName + ";");
            }
        }

        sw.Close();
        System.Console.WriteLine("Success: file written: {0}", removeAllTablesFile);

        string LoadDataFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                              System.IO.Path.DirectorySeparatorChar +
                              "loaddata.sh";
        outPutFileStream = new FileStream(LoadDataFile,
            FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(outPutFileStream);

        // todo: sw.WriteLine("echo \"load tables\"; psql petra < petra0203_tables.sql");
        foreach (TTable Table in Tables)
        {
            // Dump indexes
            sw.WriteLine("echo " + Table.strName + "; psql petra < " + Table.strName + ".sql");
        }

        // todo: sw.WriteLine("echo \"load constraints\"; psql petra < petra0203_constraints.sql");
        // todo: sw.WriteLine("echo \"load indexes\"; psql petra < petra0203_indexes.sql");
        sw.Close();
        outPutFileStream.Close();
        System.Console.WriteLine("Success: file written: {0}", LoadDataFile);

        string CreateTablesFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                                  System.IO.Path.DirectorySeparatorChar +
                                  "createtables-" + ATargetDatabase.ToString() + ".sql";
        outPutFileStream = new FileStream(CreateTablesFile,
            FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(outPutFileStream);

        if (ATargetDatabase == eDatabaseType.PostgreSQL)
        {
            sw.WriteLine("SET client_min_messages TO WARNING;");
        }

        foreach (TTable Table in Tables)
        {
            if (!WriteTable(ATargetDatabase, sw, Table))
            {
                Environment.Exit(1);
            }
        }

        WriteSequences(sw, ATargetDatabase, AStore, false);

        sw.Close();
        outPutFileStream.Close();
        System.Console.WriteLine("Success: file written: {0}", CreateTablesFile);

        string CreateConstraintsAndIndexesFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                                                 System.IO.Path.DirectorySeparatorChar +
                                                 "createconstraints-" + ATargetDatabase.ToString() + ".sql";
        outPutFileStream = new FileStream(CreateConstraintsAndIndexesFile,
            FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(outPutFileStream);

        foreach (TTable Table in Tables)
        {
            if (ATargetDatabase == eDatabaseType.Sqlite)
            {
                // see http://www.sqlite.org/omitted.html:
                // sqlite does not support Alter table add constraint
            }
            else
            {
                DumpConstraints(sw, Table, true, true);
            }
        }

        foreach (TTable Table in Tables)
        {
            // Dump indexes
            DumpIndexes(sw, Table, true);
        }

        sw.Close();
        outPutFileStream.Close();
        System.Console.WriteLine("Success: file written: {0}", CreateConstraintsAndIndexesFile);
    }
Beispiel #33
0
    private static Boolean WriteSequences(StreamWriter ASw,
        eDatabaseType ATargetDatabase,
        TDataDefinitionStore AStore,
        bool AWithSequenceInitialisation)
    {
        if (ATargetDatabase == eDatabaseType.Sqlite)
        {
            // see http://www.mail-archive.com/[email protected]/msg05123.html
            // see http://www.sqlite.org/faq.html#q1
            // no sequences in sqlite
        }
        else if (ATargetDatabase == eDatabaseType.MySQL)
        {
            // also no sequences in Mysql
            // see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html for a workaround
            // look for CREATE TABLE sequence and LAST_INSERT_ID
            List <TSequence>Sequences = AStore.GetSequences();

            foreach (TSequence seq in Sequences)
            {
                string createStmt = "CREATE TABLE " + seq.strName + " (sequence INTEGER AUTO_INCREMENT, dummy INTEGER, PRIMARY KEY(sequence));";
                ASw.WriteLine(createStmt);

                if (AWithSequenceInitialisation)
                {
                    // the following line would cause trouble later when loading the demo/base data
                    createStmt = "INSERT INTO " + seq.strName + " VALUES(NULL, -1);";
                    ASw.WriteLine(createStmt);
                }
            }
        }
        else
        {
            List <TSequence>Sequences = AStore.GetSequences();

            foreach (TSequence Sequence in Sequences)
            {
                if (!WriteSequence(ASw, Sequence))
                {
                    Environment.Exit(1);
                }
            }
        }

        return true;
    }
        /// <summary>
        /// main function for generating access methods for typed data sets
        /// </summary>
        /// <param name="AInputXmlfile"></param>
        /// <param name="AOutputPath"></param>
        /// <param name="ANameSpace"></param>
        /// <param name="store"></param>
        /// <param name="groups"></param>
        /// <param name="AFilename"></param>
        public static void CreateTypedDataSets(String AInputXmlfile,
            String AOutputPath,
            String ANameSpace,
            TDataDefinitionStore store,
            string[] groups,
            string AFilename)
        {
            Console.WriteLine("processing dataset " + ANameSpace);

            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataSetAccess.cs");

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            Template.SetCodelet("NAMESPACE", ANameSpace);

            // if no dataset is defined yet in the xml file, the following variables can be empty
            Template.AddToCodelet("USINGNAMESPACES", "");
            Template.AddToCodelet("CONTENTDATASETSANDTABLESANDROWS", "");

            Template.AddToCodelet("USINGNAMESPACES", "using " + ANameSpace.Replace(".Server.", ".Shared.") + ";" + Environment.NewLine, false);

            TXMLParser parserDataSet = new TXMLParser(AInputXmlfile, false);
            XmlDocument myDoc = parserDataSet.GetDocument();
            XmlNode startNode = myDoc.DocumentElement;

            if (startNode.Name.ToLower() == "petradatasets")
            {
                XmlNode cur = TXMLParser.NextNotBlank(startNode.FirstChild);

                while ((cur != null) && (cur.Name.ToLower() == "importunit"))
                {
                    Template.AddToCodelet("USINGNAMESPACES", "using " + TXMLParser.GetAttribute(cur, "name") + ";" + Environment.NewLine, false);
                    Template.AddToCodelet("USINGNAMESPACES", "using " + TXMLParser.GetAttribute(cur, "name").Replace(".Shared.",
                            ".Server.") + ".Access;" + Environment.NewLine, false);
                    cur = TXMLParser.GetNextEntity(cur);
                }

                while ((cur != null) && (cur.Name.ToLower() == "dataset"))
                {
                    ProcessTemplate snippetDataset = Template.GetSnippet("TYPEDDATASET");
                    string datasetname = TXMLParser.GetAttribute(cur, "name");
                    snippetDataset.SetCodelet("DATASETNAME", datasetname);

                    ProcessTemplate snippetSubmitChanges = snippetDataset.GetSnippet("SUBMITCHANGESFUNCTION");
                    snippetSubmitChanges.AddToCodelet("DATASETNAME", datasetname);

                    List <TDataSetTable>tables = new List <TDataSetTable>();

                    XmlNode curChild = cur.FirstChild;

                    // first collect the tables
                    while (curChild != null)
                    {
                        if (curChild.Name.ToLower() == "table")
                        {
                            string tabletype = TTable.NiceTableName(TXMLParser.GetAttribute(curChild, "sqltable"));
                            string variablename = (TXMLParser.HasAttribute(curChild, "name") ?
                                                   TXMLParser.GetAttribute(curChild, "name") :
                                                   tabletype);

                            TDataSetTable table = new TDataSetTable(
                                TXMLParser.GetAttribute(curChild, "sqltable"),
                                tabletype,
                                variablename,
                                store.GetTable(tabletype));

                            tables.Add(table);
                        }

                        curChild = curChild.NextSibling;
                    }

                    foreach (TDataSetTable table in tables)
                    {
                        AddTableToDataset(tables,
                            store.GetTable(table.tableorig),
                            table.tablename,
                            table.tablealias,
                            snippetDataset,
                            snippetSubmitChanges);
                    }

                    // there is one codelet for the dataset name.
                    // only add the full submitchanges function if there are any table to submit
                    if (snippetSubmitChanges.FCodelets.Count > 1)
                    {
                        snippetDataset.InsertSnippet("SUBMITCHANGESFUNCTION", snippetSubmitChanges);
                    }
                    else
                    {
                        snippetDataset.AddToCodelet("SUBMITCHANGESFUNCTION", "");
                    }

                    Template.InsertSnippet("CONTENTDATASETSANDTABLESANDROWS", snippetDataset);

                    cur = TXMLParser.GetNextEntity(cur);
                }
            }

            Template.FinishWriting(AOutputPath + Path.DirectorySeparatorChar + AFilename + "-generated.cs", ".cs", true);
        }
        /// <summary>
        /// generate code for cascading deletions etc
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFileName"></param>
        /// <returns></returns>
        public static Boolean WriteTypedDataCascading(TDataDefinitionStore AStore, string AFilePath, string ANamespaceName, string AFileName)
        {
            Console.WriteLine("writing namespace " + ANamespaceName);

            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataCascading.cs");

            Template.AddToCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            foreach (TTable currentTable in AStore.GetTables())
            {
                ProcessTemplate snippet = Template.GetSnippet("TABLECASCADING");

                if (InsertMainProcedures(AStore, currentTable, Template, snippet))
                {
                    Template.AddToCodelet("USINGNAMESPACES",
                        CodeGenerationAccess.GetNamespace(currentTable.strGroup), false);
                    Template.AddToCodelet("USINGNAMESPACES",
                        CodeGenerationAccess.GetNamespace(currentTable.strGroup).Replace(
                            ".Data;", ".Data.Access;").
                        Replace("Ict.Petra.Shared.", "Ict.Petra.Server."), false);

                    Template.InsertSnippet("TABLECASCADINGLOOP", snippet);
                }
            }

            Template.FinishWriting(AFilePath + AFileName + "-generated.cs", ".cs", true);

            return true;
        }
Beispiel #36
0
        public static void Main(string[] args)
        {
            new TLogging("delivery/bin/Ict.Tools.DataMigrateStatistics.log");
            new TAppSettingsManager(false);

            string row_count_location = TAppSettingsManager.GetValue("fulldumpPath", "delivery/bin/fulldump") +
                                        Path.DirectorySeparatorChar + "_row_count.txt";

            if (File.Exists(row_count_location))
            {
                DBAccess.GDBAccessObj = new TDataBase();

                TCmdOpts cmdLine = new TCmdOpts();
                string xmlfile = cmdLine.GetOptValue("petraxml");

                DBAccess.GDBAccessObj.EstablishDBConnection(CommonTypes.ParseDBType(cmdLine.GetOptValue("type")), cmdLine.GetOptValue("host"),
                    cmdLine.GetOptValue("port"), cmdLine.GetOptValue("database"), cmdLine.GetOptValue("username"), cmdLine.GetOptValue(
                        "password"), "");

                TDataDefinitionParser parserNew = new TDataDefinitionParser(xmlfile, false);
                TDataDefinitionStore storeNew = new TDataDefinitionStore();
                parserNew.ParseDocument(ref storeNew, false, true);
                List <TTable>newTables = storeNew.GetTables();

                // table names and row numbers on alternate lines
                string[] checkRows = File.ReadAllLines(row_count_location);

                Console.WriteLine();
                Console.WriteLine("--- Testing all rows have loaded successfully ---");

                int totalRows = 0; // total number of rows in database
                int totalCheckRows = 0; // total number of rows there should be in the database
                bool rowsMissing = false;

                foreach (TTable newTable in newTables)
                {
                    if (newTable.strName != "s_login") // ignore this table as the row count changes everytime a connection is made to the database
                    {
                        int i = 0;
                        int rowCount; // number of rows actually in table
                        int rowCountCheck = 0; // number of rows there should be in table

                        // count rows in table
                        string sql = "SELECT count(*) from " + newTable.strName + ";";
                        rowCount = Convert.ToInt32(DBAccess.GDBAccessObj.ExecuteScalar(sql, IsolationLevel.ReadUncommitted));

                        // read how many rows there should be in the same table
                        while (i < checkRows.Length)
                        {
                            if (checkRows[i] == newTable.strName)
                            {
                                rowCountCheck = Convert.ToInt32(checkRows[i + 1]);
                                break;
                            }

                            i += 2;
                        }

                        totalRows += rowCount;
                        totalCheckRows += rowCountCheck;

                        // if there are rows missing
                        if (rowCount != rowCountCheck)
                        {
                            Console.Write(newTable.strName + " is incomplete. ");
                            Console.WriteLine((rowCountCheck - rowCount) + " out of " + rowCountCheck + " rows did not load!");
                            rowsMissing = true;
                        }
                    }
                }

                // if all rows are present
                if (!rowsMissing)
                {
                    Console.WriteLine("All rows successfully loaded.");
                }

                // write a summary of the number of rows successfully loaded
                Console.WriteLine();
                Console.WriteLine("--- Total number of rows loaded ---");
                Console.WriteLine(totalRows + " out of " + totalCheckRows + " rows loaded successfully.");
                string percentage = (((double)totalRows / totalCheckRows) * 100).ToString("#.##");
                Console.WriteLine(percentage + "%");
                Console.WriteLine();

                DBAccess.GDBAccessObj.CloseDBConnection();
            }
            else
            {
                TLogging.Log("Warning: unable to perform a row count test. _row_count.txt is missing from the folder .../delivery/bin/fulldump.");
                TLogging.Log("");
            }
        }
        /// <summary>
        /// This will parse the xml document and store the contents in our Datadefinition Store
        /// </summary>
        /// <param name="myStore">the destination of all the information</param>
        /// <param name="ADoValidation">do we want the XmlDocument to be validated</param>
        /// <param name="AAddAutoGeneratedFields">should some special fields be generated automatically, eg. createdby/datemodified etc</param>
        /// <returns></returns>
        public Boolean ParseDocument(ref TDataDefinitionStore myStore, Boolean ADoValidation, Boolean AAddAutoGeneratedFields)
        {
            Boolean ReturnValue;
            XmlNode startNode;
            XmlNode cur;
            TTable table;

            ReturnValue = false;
            FDoValidation = ADoValidation;
            startNode = myDoc.DocumentElement;

            if (startNode.Name.ToLower() == "database")
            {
                cur = NextNotBlank(startNode.FirstChild);

                if ((cur == null) || (cur.Name != "table"))
                {
                    return ReturnValue;
                }

                while (cur != null)
                {
                    if (cur.Name == "table")
                    {
                        table = ParseTable(cur);
                        myStore.AddTable(table);
                        cur = NextNotBlank(cur.NextSibling);
                    }

                    if (cur.Name == "sequence")
                    {
                        myStore.AddSequence(ParseSequence(cur));
                        cur = NextNotBlank(cur.NextSibling);
                    }
                }
            }

            if (AAddAutoGeneratedFields == true)
            {
                myStore.PrepareAutoGeneratedFields();
            }

            myStore.PrepareLinks();
            return true;
        }
Beispiel #38
0
        public static void Main(string[] args)
        {
            new TAppSettingsManager(false);

            //enable execution without nant -> debugging easier
            bool independentmode = false;

            if (independentmode)
            {
                TLogging.Log("started in independent-mode");
            }

            try
            {
                if ((TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "removeDoNotTranslate")) || independentmode)
                {
                    string doNotTranslatePath;
                    string poFilePath;

                    if (independentmode)
                    {
                        doNotTranslatePath = "E:\\openpetra\\bzr\\work-690\\i18n\\doNotTranslate.po";
                        poFilePath         = "E:\\openpetra\\bzr\\work-690\\i18n\\template.pot";
                    }
                    else
                    {
                        doNotTranslatePath = TAppSettingsManager.GetValue("dntFile");
                        poFilePath         = TAppSettingsManager.GetValue("poFile");
                    }

                    // remove all strings from po file that are listed in the "Do Not Translate" file
                    TDropUnwantedStrings.RemoveUnwantedStringsFromTranslation(doNotTranslatePath, poFilePath);
                }
                else if (TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "errorcodedoc"))
                {
                    TGenerateErrorCodeDoc.Execute(TAppSettingsManager.GetValue("csharppath"),
                                                  TAppSettingsManager.GetValue("templatefilepath"),
                                                  TAppSettingsManager.GetValue("outfilepath"));
                }
                else if (TAppSettingsManager.HasValue("do") && (TAppSettingsManager.GetValue("do") == "yamlfiles"))
                {
                    GenerateYamlFiles.WriteYamlFiles(
                        TAppSettingsManager.GetValue("language"),
                        TAppSettingsManager.GetValue("path"),
                        TAppSettingsManager.GetValue("pofile"));
                }
                else if (TAppSettingsManager.HasValue("file"))
                {
                    TGenerateCatalogStrings.Execute(TAppSettingsManager.GetValue("file"), null, null);
                }
                else if (TAppSettingsManager.HasValue("filelist"))
                {
                    TDataDefinitionStore store = new TDataDefinitionStore();
                    Console.WriteLine("parsing " + TAppSettingsManager.GetValue("petraxml", true));
                    TDataDefinitionParser parser = new TDataDefinitionParser(TAppSettingsManager.GetValue("petraxml", true));
                    parser.ParseDocument(ref store, true, true);

                    string CollectedStringsFilename = TAppSettingsManager.GetValue("tmpPath") +
                                                      Path.DirectorySeparatorChar +
                                                      "GenerateI18N.CollectedGettext.cs";
                    StreamWriter writerCollectedStringsFile = new StreamWriter(CollectedStringsFilename);

                    string GettextApp = TAppSettingsManager.GetValue("gettext");

                    string       filesToParseWithGettext = string.Empty;
                    StreamReader readerFilelist          = new StreamReader(TAppSettingsManager.GetValue("filelist"));

                    while (!readerFilelist.EndOfStream)
                    {
                        string pathCodeFile = readerFilelist.ReadLine().Trim();
                        string ext          = Path.GetExtension(pathCodeFile);

                        if (".cs" == ext)
                        {
                            if (TGenerateCatalogStrings.Execute(pathCodeFile, store, writerCollectedStringsFile))
                            {
                                filesToParseWithGettext += "\"" + pathCodeFile + "\" ";

                                if (filesToParseWithGettext.Length > 1500)
                                {
                                    ParseWithGettext(GettextApp, TAppSettingsManager.GetValue("poFile"), filesToParseWithGettext);
                                    filesToParseWithGettext = string.Empty;
                                }
                            }
                        }
                        else if (".yml" == ext)
                        {
                            TGenerateCatalogStrings.AddTranslationUINavigation(pathCodeFile, writerCollectedStringsFile);
                        }
                        else
                        {
                            Console.WriteLine("the file " + pathCodeFile + " has an unknown extension! File ignored!");
                        }
                    }

                    if (filesToParseWithGettext.Length > 0)
                    {
                        ParseWithGettext(GettextApp, TAppSettingsManager.GetValue("poFile"), filesToParseWithGettext);
                    }

                    writerCollectedStringsFile.Close();

                    // delete the file if it is empty
                    if (File.ReadAllText(CollectedStringsFilename).Length == 0)
                    {
                        File.Delete(CollectedStringsFilename);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Environment.Exit(-1);
            }
        }
Beispiel #39
0
        /// <summary>
        /// get the data structure for the new version
        /// </summary>
        public static TDataDefinitionStore GetStoreNew()
        {
            if (storeNew == null)
            {
                string PetraNewPath = TAppSettingsManager.GetValue("newpetraxml", "petra.xml");

                TLogging.Log(String.Format("Reading OpenPetra xml file {0}...", PetraNewPath));

                TDataDefinitionParser parserNew = new TDataDefinitionParser(PetraNewPath, false);
                storeNew = new TDataDefinitionStore();

                if (!parserNew.ParseDocument(ref storeNew, false, true))
                {
                    return null;
                }
            }

            return storeNew;
        }
Beispiel #40
0
        /// <summary>
        /// create an Sqlite database and create all tables and sequences and indexes
        /// </summary>
        /// <param name="ADataDefinition"></param>
        /// <param name="ADBFilename"></param>
        /// <param name="ADBPwd"></param>
        /// <returns></returns>
        static public bool CreateDatabase(TDataDefinitionStore ADataDefinition, string ADBFilename, string ADBPwd)
        {
            if (System.IO.File.Exists(ADBFilename))
            {
                throw new Exception("cannot overwrite existing file " + ADBFilename);
            }

            System.Console.WriteLine("Writing file to {0}...", ADBFilename);

            // see also tutorial http://Sqlite.phxsoftware.com/forums/p/130/452.aspx#452

            // sqlite on Windows does not support encryption with a password
            // System.EntryPointNotFoundException: sqlite3_key
            ADBPwd = string.Empty;

            SqliteConnection conn = new SqliteConnection("Data Source=" + ADBFilename + (ADBPwd.Length > 0 ? ";Password="******""));
            conn.Open();

            foreach (TTable table in ADataDefinition.GetTables())
            {
                // see http://www.Sqlite.org/lang_createtable.html
                string createStmt = "CREATE TABLE " + table.strName + " (";
                bool firstField = true;

                foreach (TTableField field in table.grpTableField)
                {
                    createStmt += TWriteSQL.WriteField(TWriteSQL.eDatabaseType.Sqlite, table, field, firstField, false);
                    firstField = false;
                }

                if (table.HasPrimaryKey() && !createStmt.Contains("PRIMARY KEY AUTOINCREMENT"))
                {
                    createStmt += ", PRIMARY KEY (";
                    bool firstPrimaryKeyColumn = true;

                    foreach (string primaryKeyColumnName in table.GetPrimaryKey().strThisFields)
                    {
                        if (!firstPrimaryKeyColumn)
                        {
                            createStmt += ",";
                        }

                        createStmt += primaryKeyColumnName;
                        firstPrimaryKeyColumn = false;
                    }

                    createStmt += ")";
                }

                createStmt += AddForeignKeys(table);

                createStmt += ");";


                SqliteCommand cmd = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
            }

            // sequence workaround
            // see http://www.Sqlite.org/faq.html#q1 AUTOINCREMENT
            foreach (TSequence seq in ADataDefinition.GetSequences())
            {
                string createStmt = "CREATE TABLE " + seq.strName + " (sequence INTEGER PRIMARY KEY AUTOINCREMENT, dummy INTEGER);";
                SqliteCommand cmd = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
                createStmt = "INSERT INTO " + seq.strName + " VALUES(NULL, -1);";
                cmd = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
            }

            conn.Close();

            return true;
        }
Beispiel #41
0
        /// <summary>
        /// load data from the sql file in Postgresql COPY format
        /// </summary>
        static private bool LoadDataFromReader(TDataDefinitionStore ADataDefinition,
            SqliteConnection conn,
            StreamReader sr,
            string ATablename,
            StringCollection AColumnNames)
        {
            using (SqliteTransaction dbTrans = conn.BeginTransaction())
            {
                using (SqliteCommand cmd = conn.CreateCommand())
                {
                    TTable table = ADataDefinition.GetTable(ATablename);

                    // prepare the statement
                    PrepareSqlStatement(cmd, ATablename, table, AColumnNames);

                    string line;

                    while ((line = sr.ReadLine()) != "\\.")
                    {
                        ProcessLine(cmd, line, table, AColumnNames);

                        cmd.ExecuteNonQuery();
                    }
                }

                dbTrans.Commit();
            }

            return true;
        }
        /// <summary>
        /// create the code for a typed table
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="strGroup"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFileName"></param>
        /// <returns></returns>
        public static Boolean WriteTypedTable(TDataDefinitionStore AStore, string strGroup, string AFilePath, string ANamespaceName, string AFileName)
        {
            Console.WriteLine("processing namespace Typed Tables " + strGroup.Substring(0, 1).ToUpper() + strGroup.Substring(1));

            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataTable.cs");

            Template.AddToCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            foreach (TTable currentTable in AStore.GetTables())
            {
                if (currentTable.strGroup == strGroup)
                {
                    if (!currentTable.HasPrimaryKey())
                    {
                        TLogging.Log("Warning: there is no primary key for table " + currentTable.strName);
                    }

                    InsertTableDefinition(Template, currentTable, null, "TABLELOOP");
                    InsertRowDefinition(Template, currentTable, null, "TABLELOOP");
                }
            }

            Template.FinishWriting(AFilePath + AFileName + "-generated.cs", ".cs", true);

            return true;
        }
        /// <summary>
        /// code for generating typed datasets
        /// </summary>
        /// <param name="AInputXmlfile"></param>
        /// <param name="AOutputPath"></param>
        /// <param name="ANameSpace"></param>
        /// <param name="store"></param>
        /// <param name="groups"></param>
        /// <param name="AFilename"></param>
        public static void CreateTypedDataSets(String AInputXmlfile,
            String AOutputPath,
            String ANameSpace,
            TDataDefinitionStore store,
            string[] groups,
            string AFilename)
        {
            Console.WriteLine("processing dataset " + ANameSpace);

            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataSet.cs");
            Template.AddSnippetsFromOtherFile(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataTable.cs");

            DataSetTableIdCounter = Convert.ToInt16(TAppSettingsManager.GetValue("StartTableId"));

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            Template.SetCodelet("NAMESPACE", ANameSpace);

            // if no dataset is defined yet in the xml file, the following variables can be empty
            Template.AddToCodelet("USINGNAMESPACES", "");
            Template.AddToCodelet("CONTENTDATASETSANDTABLESANDROWS", "");

            TXMLParser parserDataSet = new TXMLParser(AInputXmlfile, false);
            XmlDocument myDoc = parserDataSet.GetDocument();
            XmlNode startNode = myDoc.DocumentElement;

            if (startNode.Name.ToLower() == "petradatasets")
            {
                XmlNode cur = TXMLParser.NextNotBlank(startNode.FirstChild);

                while ((cur != null) && (cur.Name.ToLower() == "importunit"))
                {
                    Template.AddToCodelet("USINGNAMESPACES", "using " + TXMLParser.GetAttribute(cur, "name") + ";" + Environment.NewLine);
                    cur = TXMLParser.GetNextEntity(cur);
                }

                while ((cur != null) && (cur.Name.ToLower() == "dataset"))
                {
                    ProcessTemplate snippetDataset = Template.GetSnippet("TYPEDDATASET");
                    string datasetname = TXMLParser.GetAttribute(cur, "name");
                    snippetDataset.SetCodelet("DATASETNAME", datasetname);

                    // INITCONSTRAINTS and INITRELATIONS can be empty
                    snippetDataset.AddToCodelet("INITCONSTRAINTS", "");
                    snippetDataset.AddToCodelet("INITRELATIONS", "");

                    SortedList <string, TDataSetTable>tables = new SortedList <string, TDataSetTable>();

                    XmlNode curChild = cur.FirstChild;

                    while (curChild != null)
                    {
                        if ((curChild.Name.ToLower() == "table") && TXMLParser.HasAttribute(curChild, "sqltable"))
                        {
                            bool OverloadTable = false;
                            string tabletype = TTable.NiceTableName(TXMLParser.GetAttribute(curChild, "sqltable"));
                            string variablename = (TXMLParser.HasAttribute(curChild, "name") ?
                                                   TXMLParser.GetAttribute(curChild, "name") :
                                                   tabletype);

                            TDataSetTable table = new TDataSetTable(
                                TXMLParser.GetAttribute(curChild, "sqltable"),
                                tabletype,
                                variablename,
                                store.GetTable(tabletype));
                            XmlNode tableNodes = curChild.FirstChild;

                            while (tableNodes != null)
                            {
                                if (tableNodes.Name.ToLower() == "customfield")
                                {
                                    // eg. BestAddress in PartnerEditTDS.PPartnerLocation
                                    TTableField customField = new TTableField();
                                    customField.strName = TXMLParser.GetAttribute(tableNodes, "name");
                                    customField.strTypeDotNet = TXMLParser.GetAttribute(tableNodes, "type");
                                    customField.strDescription = TXMLParser.GetAttribute(tableNodes, "comment");
                                    customField.strDefault = TXMLParser.GetAttribute(tableNodes, "initial");
                                    table.grpTableField.Add(customField);

                                    OverloadTable = true;
                                }

                                if (tableNodes.Name.ToLower() == "field")
                                {
                                    // eg. UnitName in PartnerEditTDS.PPerson
                                    TTableField field = new TTableField(store.GetTable(TXMLParser.GetAttribute(tableNodes, "sqltable")).
                                        GetField(TXMLParser.GetAttribute(tableNodes, "sqlfield")));

                                    if (TXMLParser.HasAttribute(tableNodes, "name"))
                                    {
                                        field.strNameDotNet = TXMLParser.GetAttribute(tableNodes, "name");
                                    }

                                    if (TXMLParser.HasAttribute(tableNodes, "comment"))
                                    {
                                        field.strDescription = TXMLParser.GetAttribute(tableNodes, "comment");
                                    }

                                    table.grpTableField.Add(field);

                                    OverloadTable = true;
                                }

                                if (tableNodes.Name.ToLower() == "primarykey")
                                {
                                    TConstraint primKeyConstraint = table.GetPrimaryKey();
                                    primKeyConstraint.strThisFields = StringHelper.StrSplit(TXMLParser.GetAttribute(tableNodes,
                                            "thisFields"), ",");

                                    OverloadTable = true;
                                }

                                tableNodes = tableNodes.NextSibling;
                            }

                            if (OverloadTable)
                            {
                                tabletype = datasetname + TTable.NiceTableName(table.strName);

                                if (TXMLParser.HasAttribute(curChild, "name"))
                                {
                                    tabletype = datasetname + TXMLParser.GetAttribute(curChild, "name");
                                }

                                table.strDotNetName = tabletype;
                                table.strVariableNameInDataset = variablename;

                                // set tableid
                                table.iOrder = DataSetTableIdCounter++;

                                // TODO: can we derive from the base table, and just overload a few functions?
                                CodeGenerationTable.InsertTableDefinition(snippetDataset, table, store.GetTable(table.tableorig), "TABLELOOP");
                                CodeGenerationTable.InsertRowDefinition(snippetDataset, table, store.GetTable(table.tableorig), "TABLELOOP");
                            }

                            tables.Add(variablename, table);

                            AddTableToDataset(tabletype, variablename,
                                snippetDataset);
                        }
                        else if ((curChild.Name.ToLower() == "table") && TXMLParser.HasAttribute(curChild, "customtable"))
                        {
                            // this refers to a custom table of another dataset, eg. BestAddressTDSLocation
                            // for the moment, such a table cannot have additional fields
                            if (curChild.HasChildNodes)
                            {
                                throw new Exception(
                                    String.Format(
                                        "CreateTypedDataSets(): At the moment, a custom table referenced from another dataset cannot have additional fields. Dataset: {0}, Table: {1}",
                                        datasetname,
                                        TXMLParser.HasAttribute(curChild, "customtable")));
                            }

                            // customtable has to contain the name of the dataset, eg. BestAddressTDSLocation
                            string tabletype = TXMLParser.GetAttribute(curChild, "customtable");
                            string variablename = (TXMLParser.HasAttribute(curChild, "name") ?
                                                   TXMLParser.GetAttribute(curChild, "name") :
                                                   tabletype);

                            AddTableToDataset(tabletype, variablename,
                                snippetDataset);
                        }

                        if (curChild.Name.ToLower() == "customrelation")
                        {
                            ProcessTemplate tempSnippet = Template.GetSnippet("INITRELATIONS");
                            tempSnippet.SetCodelet("RELATIONNAME", TXMLParser.GetAttribute(curChild, "name"));
                            tempSnippet.SetCodelet("TABLEVARIABLENAMEPARENT", TXMLParser.GetAttribute(curChild, "parentTable"));
                            tempSnippet.SetCodelet("TABLEVARIABLENAMECHILD", TXMLParser.GetAttribute(curChild, "childTable"));
                            tempSnippet.SetCodelet("COLUMNNAMESPARENT",
                                StringCollectionToValuesFormattedForArray(tables, TXMLParser.GetAttribute(curChild, "parentTable"),
                                    StringHelper.StrSplit(TXMLParser.GetAttribute(curChild, "parentFields"), ",")));
                            tempSnippet.SetCodelet("COLUMNNAMESCHILD",
                                StringCollectionToValuesFormattedForArray(tables, TXMLParser.GetAttribute(curChild, "childTable"),
                                    StringHelper.StrSplit(TXMLParser.GetAttribute(curChild, "childFields"), ",")));
                            tempSnippet.SetCodelet("CREATECONSTRAINTS", TXMLParser.GetBoolAttribute(curChild, "createConstraints") ? "true" : "false");
                            snippetDataset.InsertSnippet("INITRELATIONS", tempSnippet);
                        }

                        if (curChild.Name.ToLower() == "customtable")
                        {
                            string variablename = TXMLParser.GetAttribute(curChild, "name");
                            string tabletype = datasetname + TXMLParser.GetAttribute(curChild, "name");

                            XmlNode customTableNodes = curChild.FirstChild;
                            TDataSetTable customTable = new TDataSetTable(
                                tabletype,
                                tabletype,
                                variablename,
                                null);

                            // set TableId
                            customTable.iOrder = DataSetTableIdCounter++;
                            customTable.strDescription = TXMLParser.GetAttribute(curChild, "comment");
                            customTable.strName = tabletype;
                            customTable.strDotNetName = tabletype;
                            customTable.strVariableNameInDataset = variablename;

                            while (customTableNodes != null)
                            {
                                if (customTableNodes.Name.ToLower() == "customfield")
                                {
                                    TTableField customField = new TTableField();
                                    customField.strName = TXMLParser.GetAttribute(customTableNodes, "name");
                                    customField.strTypeDotNet = TXMLParser.GetAttribute(customTableNodes, "type");
                                    customField.strDescription = TXMLParser.GetAttribute(customTableNodes, "comment");
                                    customField.strDefault = TXMLParser.GetAttribute(customTableNodes, "initial");
                                    customTable.grpTableField.Add(customField);
                                }

                                if (customTableNodes.Name.ToLower() == "field")
                                {
                                    // eg. SelectedSiteKey in PartnerEditTDS.MiscellaneousData
                                    TTableField field = new TTableField(store.GetTable(TXMLParser.GetAttribute(customTableNodes, "sqltable")).
                                        GetField(TXMLParser.GetAttribute(customTableNodes, "sqlfield")));

                                    if (TXMLParser.HasAttribute(customTableNodes, "name"))
                                    {
                                        field.strNameDotNet = TXMLParser.GetAttribute(customTableNodes, "name");
                                    }

                                    if (TXMLParser.HasAttribute(customTableNodes, "comment"))
                                    {
                                        field.strDescription = TXMLParser.GetAttribute(customTableNodes, "comment");
                                    }

                                    customTable.grpTableField.Add(field);
                                }

                                if (customTableNodes.Name.ToLower() == "primarykey")
                                {
                                    TConstraint primKeyConstraint = new TConstraint();
                                    primKeyConstraint.strName = "PK";
                                    primKeyConstraint.strType = "primarykey";
                                    primKeyConstraint.strThisFields = StringHelper.StrSplit(TXMLParser.GetAttribute(customTableNodes,
                                            "thisFields"), ",");
                                    customTable.grpConstraint.Add(primKeyConstraint);
                                }

                                customTableNodes = customTableNodes.NextSibling;
                            }

                            tables.Add(tabletype, customTable);

                            AddTableToDataset(tabletype, variablename, snippetDataset);

                            CodeGenerationTable.InsertTableDefinition(snippetDataset, customTable, null, "TABLELOOP");
                            CodeGenerationTable.InsertRowDefinition(snippetDataset, customTable, null, "TABLELOOP");
                        }

                        curChild = curChild.NextSibling;
                    }

                    foreach (TDataSetTable table in tables.Values)
                    {
                        // todo? also other constraints, not only from original table?
                        foreach (TConstraint constraint in table.grpConstraint)
                        {
                            if ((constraint.strType == "foreignkey") && tables.ContainsKey(constraint.strOtherTable))
                            {
                                TDataSetTable otherTable = (TDataSetTable)tables[constraint.strOtherTable];

                                ProcessTemplate tempSnippet = Template.GetSnippet("INITCONSTRAINTS");

                                tempSnippet.SetCodelet("TABLEVARIABLENAME1", table.tablealias);
                                tempSnippet.SetCodelet("TABLEVARIABLENAME2", otherTable.tablealias);
                                tempSnippet.SetCodelet("CONSTRAINTNAME", TTable.NiceKeyName(constraint));

                                tempSnippet.SetCodelet("COLUMNNAMES1", StringCollectionToValuesFormattedForArray(constraint.strThisFields));
                                tempSnippet.SetCodelet("COLUMNNAMES2", StringCollectionToValuesFormattedForArray(constraint.strOtherFields));
                                snippetDataset.InsertSnippet("INITCONSTRAINTS", tempSnippet);
                            }
                        }
                    }

                    Template.InsertSnippet("CONTENTDATASETSANDTABLESANDROWS", snippetDataset);

                    cur = TXMLParser.GetNextEntity(cur);
                }
            }

            Template.FinishWriting(AOutputPath + Path.DirectorySeparatorChar + AFilename + "-generated.cs", ".cs", true);
        }
Beispiel #44
0
        public static void Main(string[] args)
        {
            new TLogging("delivery/bin/Ict.Tools.DataMigrateStatistics.log");
            new TAppSettingsManager(false);

            string row_count_location = TAppSettingsManager.GetValue("fulldumpPath", "delivery/bin/fulldump") +
                                        Path.DirectorySeparatorChar + "_row_count.txt";

            if (File.Exists(row_count_location))
            {
                DBAccess.GDBAccessObj = new TDataBase();

                TCmdOpts cmdLine = new TCmdOpts();
                string   xmlfile = cmdLine.GetOptValue("petraxml");

                DBAccess.GDBAccessObj.EstablishDBConnection(CommonTypes.ParseDBType(cmdLine.GetOptValue("type")), cmdLine.GetOptValue("host"),
                                                            cmdLine.GetOptValue("port"), cmdLine.GetOptValue("database"), cmdLine.GetOptValue("username"), cmdLine.GetOptValue(
                                                                "password"), "", "ICT.Tools.DataMigrateStatistics.Program.Main DB Connection");

                TDataDefinitionParser parserNew = new TDataDefinitionParser(xmlfile, false);
                TDataDefinitionStore  storeNew  = new TDataDefinitionStore();
                parserNew.ParseDocument(ref storeNew, false, true);
                List <TTable> newTables = storeNew.GetTables();

                // table names and row numbers on alternate lines
                string[] checkRows = File.ReadAllLines(row_count_location);

                Console.WriteLine();
                Console.WriteLine("--- Testing all rows have loaded successfully ---");

                int  totalRows      = 0; // total number of rows in database
                int  totalCheckRows = 0; // total number of rows there should be in the database
                bool rowsMissing    = false;

                foreach (TTable newTable in newTables)
                {
                    if (newTable.strName != "s_login") // ignore this table as the row count changes everytime a connection is made to the database
                    {
                        int i = 0;
                        int rowCount;          // number of rows actually in table
                        int rowCountCheck = 0; // number of rows there should be in table

                        // count rows in table
                        string sql = "SELECT count(*) from " + newTable.strName + ";";
                        rowCount = Convert.ToInt32(DBAccess.GDBAccessObj.ExecuteScalar(sql, IsolationLevel.ReadUncommitted));

                        // read how many rows there should be in the same table
                        while (i < checkRows.Length)
                        {
                            if (checkRows[i] == newTable.strName)
                            {
                                rowCountCheck = Convert.ToInt32(checkRows[i + 1]);
                                break;
                            }

                            i += 2;
                        }

                        totalRows      += rowCount;
                        totalCheckRows += rowCountCheck;

                        // if there are rows missing
                        if (rowCount != rowCountCheck)
                        {
                            Console.Write(newTable.strName + " is incomplete. ");
                            Console.WriteLine((rowCountCheck - rowCount) + " out of " + rowCountCheck + " rows did not load!");
                            rowsMissing = true;
                        }
                    }
                }

                // if all rows are present
                if (!rowsMissing)
                {
                    Console.WriteLine("All rows successfully loaded.");
                }

                // write a summary of the number of rows successfully loaded
                Console.WriteLine();
                Console.WriteLine("--- Total number of rows loaded ---");
                Console.WriteLine(totalRows + " out of " + totalCheckRows + " rows loaded successfully.");
                string percentage = (decimal.Divide(totalRows * 100, totalCheckRows)).ToString("#.##");
                Console.WriteLine(percentage + "%");
                Console.WriteLine();

                DBAccess.GDBAccessObj.CloseDBConnection();
            }
            else
            {
                TLogging.Log("Warning: unable to perform a row count test. _row_count.txt is missing from the folder .../delivery/bin/fulldump.");
                TLogging.Log("");
            }
        }
Beispiel #45
0
        /// <summary>
        /// load data from a CSV file in Postgresql COPY format
        /// </summary>
        static private bool LoadData(TDataDefinitionStore ADataDefinition, SqliteConnection conn, string APath, string ATablename)
        {
            using (SqliteTransaction dbTrans = conn.BeginTransaction())
            {
                using (SqliteCommand cmd = conn.CreateCommand())
                {
                    TTable table = ADataDefinition.GetTable(ATablename);

                    StringCollection ColumnNames = new StringCollection();

                    foreach (TTableField f in table.grpTableField)
                    {
                        ColumnNames.Add(f.strName);
                    }

                    // prepare the statement
                    PrepareSqlStatement(cmd, ATablename, table, ColumnNames);

                    // load the data from the text file
                    string filename = APath + Path.DirectorySeparatorChar + ATablename + ".csv";

                    if (File.Exists(filename + ".local"))
                    {
                        filename += ".local";
                    }

                    StreamReader reader = new StreamReader(filename);
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        ProcessLine(cmd, line, table, ColumnNames);

                        if (cmd.ExecuteNonQuery() != 1)
                        {
                            throw new Exception("failed to import line for table " + ATablename);
                        }
                    }
                }

                dbTrans.Commit();
            }

            return true;
        }
    /// <summary>
    /// read the designer file and add the strings to the main file
    /// </summary>
    /// <param name="AMainFilename"></param>
    /// <param name="ADataDefinitionStore"></param>
    /// <param name="ADbHelpTranslationWriter">dummy cs file that is used to provide the strings to gettext</param>
    /// <returns>true if the file should be parsed for translatable strings</returns>
    public static bool Execute(string AMainFilename, TDataDefinitionStore ADataDefinitionStore, StreamWriter ADbHelpTranslationWriter)
    {
        string DesignerFileName = GetDesignerFilename(AMainFilename);
        StreamReader readerDesignerFile = null;
        StreamWriter writer = null;

        if (AMainFilename.EndsWith(".Designer.cs") || AMainFilename.EndsWith("AssemblyInfo.cs"))
        {
            return false;
        }

        if (File.Exists(Path.GetDirectoryName(AMainFilename) + Path.DirectorySeparatorChar +
                System.IO.Path.GetFileNameWithoutExtension(AMainFilename.Replace("-generated", string.Empty)) + ".yaml"))
        {
            // do not generate translation code for already generated files;
            // but still let gettext parse this file for Catalog.GetString
            return true;
        }

        if (AMainFilename.Contains("-generated."))
        {
            // do not generate translation code for already generated files
            return false;
        }

        if (File.Exists(DesignerFileName))
        {
            readerDesignerFile = new StreamReader(DesignerFileName);
            writer = new StreamWriter(AMainFilename + ".new");
        }

        StreamReader readerMainFile = new StreamReader(AMainFilename);

        // find the call to InitializeComponent
        string line = "";
        bool ContainsCatalogGetStringCall = false;

        while (!readerMainFile.EndOfStream && !line.Contains("InitializeComponent();"))
        {
            line = readerMainFile.ReadLine();

            CheckLineAndAddDBHelp(line, ADataDefinitionStore, ADbHelpTranslationWriter);

            if (line.Contains("Catalog.GetString"))
            {
                ContainsCatalogGetStringCall = true;
            }

            if (writer != null)
            {
                writer.WriteLine(line);
            }
        }

        if (readerDesignerFile == null)
        {
            // this is just a normal code file without designer code

            if (!readerMainFile.EndOfStream)
            {
                // TODO: be more strict with missing designer file!
                //readerMainFile.Close();
                //throw new Exception("the file " + AMainFilename + " should have a designer file!");
                Console.WriteLine("the file " + AMainFilename + " should have a designer file!");
            }

            readerMainFile.Close();

            return ContainsCatalogGetStringCall;
        }

        if (readerMainFile.EndOfStream)
        {
            readerMainFile.Close();
            readerDesignerFile.Close();
            writer.Close();

            throw new Exception("Problem: cannot find InitializeComponent in " + AMainFilename);
        }

        string identation = "".PadLeft(line.IndexOf("InitializeComponent()"));

        writer.WriteLine(identation + "#region CATALOGI18N");

        // empty line for uncrustify
        writer.WriteLine();
        writer.WriteLine(
            identation + "// this code has been inserted by GenerateI18N, all changes in this region will be overwritten by GenerateI18N");

        // parse the designer files and insert all labels etc into the main file
        while (!readerDesignerFile.EndOfStream)
        {
            string designerLine = readerDesignerFile.ReadLine();

            // catch all .Text = , but also TooltipsText = , but ignore lblSomethingText = new ...
            if (designerLine.Contains("Text = \""))
            {
                bool trailingColon = false;
                string content = designerLine.Substring(
                    designerLine.IndexOf("\"") + 1, designerLine.LastIndexOf("\"") - designerLine.IndexOf("\"") - 1);

                if (content.EndsWith(":"))
                {
                    trailingColon = true;
                    content = content.Substring(0, content.Length - 1);
                }

                // see also FormWriter.cs, SetControlProperty; it also calls ProperI18NCatalogGetString
                try
                {
                    if (TFormWriter.ProperI18NCatalogGetString(content))
                    {
                        writer.WriteLine(identation +
                            designerLine.Substring(0, designerLine.IndexOf(" = ")).Trim() +
                            " = Catalog.GetString(\"" + content + "\")" + (trailingColon ? " + \":\"" : string.Empty) + ";");

                        ADbHelpTranslationWriter.WriteLine("Catalog.GetString(\"" + content + "\");");
                    }
                }
                catch (Exception e)
                {
                    if (e.Message == "Problem with \\r or \\n")
                    {
                        throw new Exception("Problem with \\r or \\n in file " + DesignerFileName + ": " + designerLine);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }

        writer.WriteLine(identation + "#endregion");

        readerDesignerFile.Close();

        bool skip = false;

        while (!readerMainFile.EndOfStream)
        {
            line = readerMainFile.ReadLine();

            if (line.Trim().StartsWith("#region CATALOGI18N"))
            {
                skip = true;
            }

            if (!skip)
            {
                CheckLineAndAddDBHelp(line, ADataDefinitionStore, ADbHelpTranslationWriter);
                writer.WriteLine(line);
            }

            if (skip && line.Trim().StartsWith("#endregion"))
            {
                skip = false;
            }
        }

        writer.Close();
        readerMainFile.Close();

        TTextFile.UpdateFile(AMainFilename, true);

        return true;
    }
Beispiel #47
0
        /// <summary>
        /// create a database file or the scripts for creating a database.
        /// for SQLite that is directly the file,
        /// for the other database systems the sql create table scripts etc are written
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ATargetDatabase"></param>
        /// <param name="AOutputFile"></param>
        public static void WriteSQL(TDataDefinitionStore AStore, eDatabaseType ATargetDatabase, String AOutputFile)
        {
            System.Console.WriteLine("Writing file to {0}...", AOutputFile);

            FileStream   outPutFileStream = new FileStream(AOutputFile, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(outPutFileStream);

            if (ATargetDatabase == eDatabaseType.MySQL)
            {
                sw.WriteLine("SET AUTOCOMMIT=0;");
                sw.WriteLine("SET FOREIGN_KEY_CHECKS=0;");
            }

            List <TTable> Tables = AStore.GetTables();

            Tables = TTableSort.TopologicalSort(AStore, Tables);

            foreach (TTable Table in Tables)
            {
                if (!WriteTable(ATargetDatabase, sw, Table, true))
                {
                    Environment.Exit(1);
                }
            }

            if (ATargetDatabase == eDatabaseType.PostgreSQL)
            {
                foreach (TTable Table in Tables)
                {
                    DumpConstraints(sw, Table, eInclude.eOnlyForeign, true);
                }
            }

            WriteSequences(sw, ATargetDatabase, AStore, true);

            sw.Close();
            System.Console.WriteLine("Success: file written: {0}", AOutputFile);

            string createTablesWithoutConstraints =
                Path.GetDirectoryName(AOutputFile) + Path.DirectorySeparatorChar +
                Path.GetFileNameWithoutExtension(AOutputFile) + "_withoutConstraints.sql";

            outPutFileStream = new FileStream(
                createTablesWithoutConstraints,
                FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(outPutFileStream);

            if (ATargetDatabase == eDatabaseType.PostgreSQL)
            {
                sw.WriteLine("SET client_min_messages TO WARNING;");
            }
            else if (ATargetDatabase == eDatabaseType.MySQL)
            {
                sw.WriteLine("SET AUTOCOMMIT=0;");
                sw.WriteLine("SET FOREIGN_KEY_CHECKS=0;");
            }

            foreach (TTable Table in Tables)
            {
                if (!WriteTable(ATargetDatabase, sw, Table))
                {
                    Environment.Exit(1);
                }
            }

            WriteSequences(sw, ATargetDatabase, AStore, true);
            sw.Close();
            System.Console.WriteLine("Success: file written: {0}", createTablesWithoutConstraints);

            string removeAllTablesFile =
                Path.GetDirectoryName(AOutputFile) + Path.DirectorySeparatorChar +
                Path.GetFileNameWithoutExtension(AOutputFile) + "_remove.sql";

            outPutFileStream = new FileStream(
                removeAllTablesFile,
                FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(outPutFileStream);

            Tables = TTableSort.TopologicalSort(AStore, Tables);
            Tables.Reverse();

            if (ATargetDatabase == eDatabaseType.MySQL)
            {
                sw.WriteLine("SET foreign_key_checks = 0;");
            }

            foreach (TTable Table in Tables)
            {
                sw.WriteLine("DROP TABLE IF EXISTS " + Table.strName + " CASCADE;");
            }

            if (ATargetDatabase == eDatabaseType.Sqlite)
            {
                // see http://www.mail-archive.com/[email protected]/msg05123.html
                // see http://www.sqlite.org/faq.html#q1
                // no sequences in sqlite
            }
            else if (ATargetDatabase == eDatabaseType.MySQL)
            {
                // also no sequences in Mysql
                // see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html for a workaround
                // look for CREATE TABLE sequence and LAST_INSERT_ID
                List <TSequence> Sequences = AStore.GetSequences();

                foreach (TSequence Sequence in Sequences)
                {
                    sw.WriteLine("DROP TABLE IF EXISTS " + Sequence.strName + ";");
                }
            }
            else
            {
                List <TSequence> Sequences = AStore.GetSequences();

                foreach (TSequence Sequence in Sequences)
                {
                    sw.WriteLine("DROP SEQUENCE IF EXISTS " + Sequence.strName + ";");
                }
            }

            sw.Close();
            System.Console.WriteLine("Success: file written: {0}", removeAllTablesFile);

            WriteDBClean(AStore, Path.GetDirectoryName(AOutputFile) + Path.DirectorySeparatorChar +
                         Path.GetFileNameWithoutExtension(AOutputFile) + "_clean.sql");

            string LoadDataFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                                  System.IO.Path.DirectorySeparatorChar +
                                  "loaddata.sh";

            outPutFileStream = new FileStream(LoadDataFile,
                                              FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(outPutFileStream);

            // todo: sw.WriteLine("echo \"load tables\"; psql petra < petra0203_tables.sql");
            foreach (TTable Table in Tables)
            {
                // Dump indexes
                sw.WriteLine("echo " + Table.strName + "; psql petra < " + Table.strName + ".sql");
            }

            // todo: sw.WriteLine("echo \"load constraints\"; psql petra < petra0203_constraints.sql");
            // todo: sw.WriteLine("echo \"load indexes\"; psql petra < petra0203_indexes.sql");
            sw.Close();
            outPutFileStream.Close();
            System.Console.WriteLine("Success: file written: {0}", LoadDataFile);

            string CreateTablesFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                                      System.IO.Path.DirectorySeparatorChar +
                                      "createtables-" + ATargetDatabase.ToString() + ".sql";

            outPutFileStream = new FileStream(CreateTablesFile,
                                              FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(outPutFileStream);

            if (ATargetDatabase == eDatabaseType.PostgreSQL)
            {
                sw.WriteLine("SET client_min_messages TO WARNING;");
            }
            else if (ATargetDatabase == eDatabaseType.MySQL)
            {
                sw.WriteLine("SET AUTOCOMMIT=0;");
                sw.WriteLine("SET FOREIGN_KEY_CHECKS=0;");
            }

            foreach (TTable Table in Tables)
            {
                if (!WriteTable(ATargetDatabase, sw, Table))
                {
                    Environment.Exit(1);
                }
            }

            WriteSequences(sw, ATargetDatabase, AStore, false);

            sw.Close();
            outPutFileStream.Close();
            System.Console.WriteLine("Success: file written: {0}", CreateTablesFile);

            string CreateConstraintsAndIndexesFile = System.IO.Path.GetDirectoryName(AOutputFile) +
                                                     System.IO.Path.DirectorySeparatorChar +
                                                     "createconstraints-" + ATargetDatabase.ToString() + ".sql";

            outPutFileStream = new FileStream(CreateConstraintsAndIndexesFile,
                                              FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(outPutFileStream);

            foreach (TTable Table in Tables)
            {
                if (ATargetDatabase == eDatabaseType.Sqlite)
                {
                    // see http://www.sqlite.org/omitted.html:
                    // sqlite does not support Alter table add constraint
                }
                else
                {
                    DumpConstraints(sw, Table, eInclude.eOnlyForeign, true);
                }
            }

            foreach (TTable Table in Tables)
            {
                // Dump indexes
                DumpIndexes(ATargetDatabase, sw, Table, eInclude.eIncludeSeparate, true);
            }

            sw.Close();
            outPutFileStream.Close();
            System.Console.WriteLine("Success: file written: {0}", CreateConstraintsAndIndexesFile);
        }
        private static void InsertMainProcedures(TDataDefinitionStore AStore,
            TTable ACurrentTable,
            ProcessTemplate ATemplate,
            ProcessTemplate ASnippet)
        {
            ASnippet.SetCodelet("TABLENAME", TTable.NiceTableName(ACurrentTable.strName));
            ASnippet.SetCodeletComment("TABLE_DESCRIPTION", ACurrentTable.strDescription);
            ASnippet.SetCodelet("SQLTABLENAME", ACurrentTable.strName);
            ASnippet.SetCodelet("VIAOTHERTABLE", "");
            ASnippet.SetCodelet("VIALINKTABLE", "");

            string formalParametersPrimaryKey;
            string actualParametersPrimaryKey;
            string actualParametersPrimaryKeyToString;
            int numberPrimaryKeyColumns;

            PrepareCodeletsPrimaryKey(ACurrentTable,
                out formalParametersPrimaryKey,
                out actualParametersPrimaryKey,
                out actualParametersPrimaryKeyToString,
                out numberPrimaryKeyColumns);

            ASnippet.SetCodelet("FORMALPARAMETERSPRIMARYKEY", formalParametersPrimaryKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSPRIMARYKEY", actualParametersPrimaryKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSPRIMARYKEYTOSTRING", actualParametersPrimaryKeyToString);
            ASnippet.SetCodelet("PRIMARYKEYNUMBERCOLUMNS", numberPrimaryKeyColumns.ToString());

            string formalParametersUniqueKey;
            string actualParametersUniqueKey;
            int numberUniqueKeyColumns;

            PrepareCodeletsUniqueKey(ACurrentTable,
                out formalParametersUniqueKey,
                out actualParametersUniqueKey,
                out numberUniqueKeyColumns);

            ASnippet.SetCodelet("FORMALPARAMETERSUNIQUEKEY", formalParametersUniqueKey);
            ASnippet.SetCodelet("ACTUALPARAMETERSUNIQUEKEY", actualParametersUniqueKey);
            ASnippet.SetCodelet("UNIQUEKEYNUMBERCOLUMNS", numberUniqueKeyColumns.ToString());


            ASnippet.SetCodelet("SEQUENCENAMEANDFIELD", "");

            foreach (TTableField tablefield in ACurrentTable.grpTableField)
            {
                // is there a field filled by a sequence?
                // yes: get the next value of that sequence and assign to row
                if (tablefield.strSequence.Length > 0)
                {
                    ASnippet.SetCodelet("SEQUENCENAMEANDFIELD", ", \"" + tablefield.strSequence + "\", \"" + tablefield.strName + "\"");

                    // assume only one sequence per table
                    break;
                }
            }
        }
        /// <summary>
        /// read the designer file and add the strings to the main file
        /// </summary>
        /// <param name="AMainFilename"></param>
        /// <param name="ADataDefinitionStore"></param>
        /// <param name="ADbHelpTranslationWriter">dummy cs file that is used to provide the strings to gettext</param>
        /// <returns>true if the file should be parsed for translatable strings</returns>
        public static bool Execute(string AMainFilename, TDataDefinitionStore ADataDefinitionStore, StreamWriter ADbHelpTranslationWriter)
        {
            string       DesignerFileName   = GetDesignerFilename(AMainFilename);
            StreamReader readerDesignerFile = null;
            StreamWriter writer             = null;

            if (AMainFilename.EndsWith(".Designer.cs") || AMainFilename.EndsWith("AssemblyInfo.cs"))
            {
                return(false);
            }

            if (File.Exists(Path.GetDirectoryName(AMainFilename) + Path.DirectorySeparatorChar +
                            System.IO.Path.GetFileNameWithoutExtension(AMainFilename.Replace("-generated", string.Empty)) + ".yaml"))
            {
                // do not generate translation code for already generated files;
                // but still let gettext parse this file for Catalog.GetString
                return(true);
            }

            if (AMainFilename.Contains("-generated."))
            {
                // do not generate translation code for already generated files
                return(false);
            }

            if (File.Exists(DesignerFileName))
            {
                readerDesignerFile = new StreamReader(DesignerFileName);
                writer             = new StreamWriter(AMainFilename + ".new");
            }

            StreamReader readerMainFile = new StreamReader(AMainFilename);

            // find the call to InitializeComponent
            string line = "";
            bool   ContainsCatalogGetStringCall = false;

            while (!readerMainFile.EndOfStream && !line.Contains("InitializeComponent();"))
            {
                line = readerMainFile.ReadLine();

                CheckLineAndAddDBHelp(line, ADataDefinitionStore, ADbHelpTranslationWriter);

                if (line.Contains("Catalog.GetString"))
                {
                    ContainsCatalogGetStringCall = true;
                }

                if (writer != null)
                {
                    writer.WriteLine(line);
                }
            }

            if (readerDesignerFile == null)
            {
                // this is just a normal code file without designer code

                if (!readerMainFile.EndOfStream)
                {
                    // TODO: be more strict with missing designer file!
                    //readerMainFile.Close();
                    //throw new Exception("the file " + AMainFilename + " should have a designer file!");
                    Console.WriteLine("the file " + AMainFilename + " should have a designer file!");
                }

                readerMainFile.Close();

                return(ContainsCatalogGetStringCall);
            }

            if (readerMainFile.EndOfStream)
            {
                readerMainFile.Close();
                readerDesignerFile.Close();
                writer.Close();

                throw new Exception("Problem: cannot find InitializeComponent in " + AMainFilename);
            }

            string identation = "".PadLeft(line.IndexOf("InitializeComponent()"));

            writer.WriteLine(identation + "#region CATALOGI18N");

            // empty line for uncrustify
            writer.WriteLine();
            writer.WriteLine(
                identation + "// this code has been inserted by GenerateI18N, all changes in this region will be overwritten by GenerateI18N");

            // parse the designer files and insert all labels etc into the main file
            while (!readerDesignerFile.EndOfStream)
            {
                string designerLine = readerDesignerFile.ReadLine();

                // catch all .Text = , but also TooltipsText = , but ignore lblSomethingText = new ...
                if (designerLine.Contains("Text = \""))
                {
                    bool   trailingColon = false;
                    string content       = designerLine.Substring(
                        designerLine.IndexOf("\"") + 1, designerLine.LastIndexOf("\"") - designerLine.IndexOf("\"") - 1);

                    if (content.EndsWith(":"))
                    {
                        trailingColon = true;
                        content       = content.Substring(0, content.Length - 1);
                    }

                    // see also FormWriter.cs, SetControlProperty; it also calls ProperI18NCatalogGetString
                    try
                    {
                        if (TFormWriter.ProperI18NCatalogGetString(content))
                        {
                            writer.WriteLine(identation +
                                             designerLine.Substring(0, designerLine.IndexOf(" = ")).Trim() +
                                             " = Catalog.GetString(\"" + content + "\")" + (trailingColon ? " + \":\"" : string.Empty) + ";");

                            ADbHelpTranslationWriter.WriteLine("Catalog.GetString(\"" + content + "\");");
                        }
                    }
                    catch (Exception e)
                    {
                        if (e.Message == "Problem with \\r or \\n")
                        {
                            throw new Exception("Problem with \\r or \\n in file " + DesignerFileName + ": " + designerLine);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            writer.WriteLine(identation + "#endregion");

            readerDesignerFile.Close();

            bool skip = false;

            while (!readerMainFile.EndOfStream)
            {
                line = readerMainFile.ReadLine();

                if (line.Trim().StartsWith("#region CATALOGI18N"))
                {
                    skip = true;
                }

                if (!skip)
                {
                    CheckLineAndAddDBHelp(line, ADataDefinitionStore, ADbHelpTranslationWriter);
                    writer.WriteLine(line);
                }

                if (skip && line.Trim().StartsWith("#endregion"))
                {
                    skip = false;
                }
            }

            writer.Close();
            readerMainFile.Close();

            TTextFile.UpdateFile(AMainFilename, true);

            return(true);
        }
Beispiel #50
0
        /// <summary>
        /// load statements from an sql file that we use for PostgreSQL
        /// </summary>
        /// <param name="ADataDefinition"></param>
        /// <param name="ADBFilename"></param>
        /// <param name="APath"></param>
        /// <param name="ASqlfile"></param>
        /// <param name="ADBPwd"></param>
        /// <returns></returns>
        static public bool ExecuteLoadScript(TDataDefinitionStore ADataDefinition, string ADBFilename, string APath, string ASqlfile, string ADBPwd)
        {
            // see tutorial for fast bulk loads: http://Sqlite.phxsoftware.com/forums/t/134.aspx

            // sqlite on Windows does not support encryption with a password
            // System.EntryPointNotFoundException: sqlite3_key
            ADBPwd = string.Empty;

            TLogging.Log("Connecting to sqlite database " + ADBFilename);
            TLogging.Log("Loading file " + ASqlfile);

            if (!File.Exists(ADBFilename))
            {
                TLogging.Log("File does not exist: " + ADBFilename);
                return(false);
            }

            SqliteConnection conn;

            try
            {
                conn = new SqliteConnection("Data Source=" + ADBFilename +
                                            (ADBPwd.Length > 0 ? ";Password="******""));

                conn.Open();
            }
            catch (Exception e)
            {
                TLogging.Log("cannot open file " + ADBFilename);
                TLogging.Log(e.ToString());
                return(false);
            }

            StreamReader reader = new StreamReader(ASqlfile);
            string       line   = null;

            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);

                if (line.ToUpper().StartsWith("DELETE FROM "))
                {
                    RunCommand(conn, line);
                }
                else if (line.ToUpper().StartsWith("INSERT INTO ") || line.ToUpper().StartsWith("UPDATE "))
                {
                    line = line.Replace("true", "1");
                    line = line.Replace("false", "0");
                    RunCommand(conn, line);
                }
                else if (line.ToUpper().StartsWith("COPY ") && line.ToUpper().Contains("FROM STDIN"))
                {
                    string tablename = StringHelper.GetCSVValue(line.Replace(" ", ","), 1);

                    string columnnames = line.Substring(line.IndexOf("(") + 1);
                    columnnames = columnnames.Substring(0, columnnames.IndexOf(")"));

                    LoadDataFromReader(ADataDefinition, conn, reader, tablename, StringHelper.GetCSVList(columnnames, ",", true));
                }
                else if (line.ToUpper().StartsWith("COPY "))
                {
                    string tablename = StringHelper.GetCSVValue(line.Replace(" ", ","), 1);
                    LoadData(ADataDefinition, conn, APath, tablename);
                }
                else if (line.ToUpper().StartsWith("SELECT NEXTVAL("))
                {
                    string SequenceName = line.Substring(line.IndexOf("'") + 1);
                    SequenceName = SequenceName.Substring(0, SequenceName.IndexOf("'"));
                    RunCommand(conn, "INSERT INTO " + SequenceName + " VALUES(NULL, -1);");
                }
                else if (!line.StartsWith("--") && (line.Trim().Length > 0))
                {
                    throw new Exception("unknown command " + line);
                }
            }

            conn.Close();

            return(true);
        }
Beispiel #51
0
        /// <summary>
        /// code for generating typed datasets
        /// </summary>
        /// <param name="AInputXmlfile"></param>
        /// <param name="AOutputPath"></param>
        /// <param name="ANameSpace"></param>
        /// <param name="store"></param>
        /// <param name="groups"></param>
        /// <param name="AFilename"></param>
        public static void CreateTypedDataSets(String AInputXmlfile,
                                               String AOutputPath,
                                               String ANameSpace,
                                               TDataDefinitionStore store,
                                               string[] groups,
                                               string AFilename)
        {
            Console.WriteLine("processing dataset " + ANameSpace);

            string          templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template    = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                                                              "ORM" + Path.DirectorySeparatorChar +
                                                              "DataSet.cs");

            Template.AddSnippetsFromOtherFile(templateDir + Path.DirectorySeparatorChar +
                                              "ORM" + Path.DirectorySeparatorChar +
                                              "DataTable.cs");

            DataSetTableIdCounter = Convert.ToInt16(TAppSettingsManager.GetValue("StartTableId"));

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            Template.SetCodelet("NAMESPACE", ANameSpace);

            // if no dataset is defined yet in the xml file, the following variables can be empty
            Template.AddToCodelet("USINGNAMESPACES", "");
            Template.AddToCodelet("CONTENTDATASETSANDTABLESANDROWS", "");

            TXMLParser  parserDataSet = new TXMLParser(AInputXmlfile, false);
            XmlDocument myDoc         = parserDataSet.GetDocument();
            XmlNode     startNode     = myDoc.DocumentElement;

            if (startNode.Name.ToLower() == "petradatasets")
            {
                XmlNode cur = TXMLParser.NextNotBlank(startNode.FirstChild);

                while ((cur != null) && (cur.Name.ToLower() == "importunit"))
                {
                    Template.AddToCodelet("USINGNAMESPACES", "using " + TXMLParser.GetAttribute(cur, "name") + ";" + Environment.NewLine);
                    cur = TXMLParser.GetNextEntity(cur);
                }

                while ((cur != null) && (cur.Name.ToLower() == "dataset"))
                {
                    ProcessTemplate snippetDataset = Template.GetSnippet("TYPEDDATASET");
                    string          datasetname    = TXMLParser.GetAttribute(cur, "name");
                    snippetDataset.SetCodelet("DATASETNAME", datasetname);

                    // INITCONSTRAINTS and INITRELATIONS can be empty
                    snippetDataset.AddToCodelet("INITCONSTRAINTS", "");
                    snippetDataset.AddToCodelet("INITRELATIONS", "");

                    SortedList <string, TDataSetTable> tables = new SortedList <string, TDataSetTable>();

                    XmlNode curChild = cur.FirstChild;

                    while (curChild != null)
                    {
                        if ((curChild.Name.ToLower() == "table") && TXMLParser.HasAttribute(curChild, "sqltable"))
                        {
                            bool   OverloadTable = false;
                            string tabletype     = TTable.NiceTableName(TXMLParser.GetAttribute(curChild, "sqltable"));
                            string variablename  = (TXMLParser.HasAttribute(curChild, "name") ?
                                                    TXMLParser.GetAttribute(curChild, "name") :
                                                    tabletype);

                            TDataSetTable table = new TDataSetTable(
                                TXMLParser.GetAttribute(curChild, "sqltable"),
                                tabletype,
                                variablename,
                                store.GetTable(tabletype));
                            XmlNode tableNodes = curChild.FirstChild;

                            while (tableNodes != null)
                            {
                                if (tableNodes.Name.ToLower() == "customfield")
                                {
                                    // eg. BestAddress in PartnerEditTDS.PPartnerLocation
                                    TTableField customField = new TTableField();
                                    customField.strName        = TXMLParser.GetAttribute(tableNodes, "name");
                                    customField.strTypeDotNet  = TXMLParser.GetAttribute(tableNodes, "type");
                                    customField.strDescription = TXMLParser.GetAttribute(tableNodes, "comment");
                                    customField.strDefault     = TXMLParser.GetAttribute(tableNodes, "initial");
                                    table.grpTableField.Add(customField);

                                    OverloadTable = true;
                                }

                                if (tableNodes.Name.ToLower() == "field")
                                {
                                    // eg. UnitName in PartnerEditTDS.PPerson
                                    TTableField field = new TTableField(store.GetTable(TXMLParser.GetAttribute(tableNodes, "sqltable")).
                                                                        GetField(TXMLParser.GetAttribute(tableNodes, "sqlfield")));

                                    if (TXMLParser.HasAttribute(tableNodes, "name"))
                                    {
                                        field.strNameDotNet = TXMLParser.GetAttribute(tableNodes, "name");
                                    }

                                    if (TXMLParser.HasAttribute(tableNodes, "comment"))
                                    {
                                        field.strDescription = TXMLParser.GetAttribute(tableNodes, "comment");
                                    }

                                    table.grpTableField.Add(field);

                                    OverloadTable = true;
                                }

                                if (tableNodes.Name.ToLower() == "primarykey")
                                {
                                    TConstraint primKeyConstraint = table.GetPrimaryKey();
                                    primKeyConstraint.strThisFields = StringHelper.StrSplit(TXMLParser.GetAttribute(tableNodes,
                                                                                                                    "thisFields"), ",");

                                    OverloadTable = true;
                                }

                                tableNodes = tableNodes.NextSibling;
                            }

                            if (OverloadTable)
                            {
                                tabletype = datasetname + TTable.NiceTableName(table.strName);

                                if (TXMLParser.HasAttribute(curChild, "name"))
                                {
                                    tabletype = datasetname + TXMLParser.GetAttribute(curChild, "name");
                                }

                                table.strDotNetName            = tabletype;
                                table.strVariableNameInDataset = variablename;

                                // set tableid
                                table.iOrder = DataSetTableIdCounter++;

                                // TODO: can we derive from the base table, and just overload a few functions?
                                CodeGenerationTable.InsertTableDefinition(snippetDataset, table, store.GetTable(table.tableorig), "TABLELOOP", true);
                                CodeGenerationTable.InsertRowDefinition(snippetDataset, table, store.GetTable(table.tableorig), "TABLELOOP");
                            }

                            tables.Add(variablename, table);

                            AddTableToDataset(tabletype, variablename,
                                              snippetDataset);
                        }
                        else if ((curChild.Name.ToLower() == "table") && TXMLParser.HasAttribute(curChild, "customtable"))
                        {
                            // this refers to a custom table of another dataset, eg. BestAddressTDSLocation
                            // for the moment, such a table cannot have additional fields
                            if (curChild.HasChildNodes)
                            {
                                throw new Exception(
                                          String.Format(
                                              "CreateTypedDataSets(): At the moment, a custom table referenced from another dataset cannot have additional fields. Dataset: {0}, Table: {1}",
                                              datasetname,
                                              TXMLParser.HasAttribute(curChild, "customtable")));
                            }

                            // customtable has to contain the name of the dataset, eg. BestAddressTDSLocation
                            string tabletype    = TXMLParser.GetAttribute(curChild, "customtable");
                            string variablename = (TXMLParser.HasAttribute(curChild, "name") ?
                                                   TXMLParser.GetAttribute(curChild, "name") :
                                                   tabletype);

                            AddTableToDataset(tabletype, variablename,
                                              snippetDataset);
                        }

                        if (curChild.Name.ToLower() == "customrelation")
                        {
                            ProcessTemplate tempSnippet = Template.GetSnippet("INITRELATIONS");
                            tempSnippet.SetCodelet("RELATIONNAME", TXMLParser.GetAttribute(curChild, "name"));
                            tempSnippet.SetCodelet("TABLEVARIABLENAMEPARENT", TXMLParser.GetAttribute(curChild, "parentTable"));
                            tempSnippet.SetCodelet("TABLEVARIABLENAMECHILD", TXMLParser.GetAttribute(curChild, "childTable"));
                            tempSnippet.SetCodelet("COLUMNNAMESPARENT",
                                                   StringCollectionToValuesFormattedForArray(tables, TXMLParser.GetAttribute(curChild, "parentTable"),
                                                                                             StringHelper.StrSplit(TXMLParser.GetAttribute(curChild, "parentFields"), ",")));
                            tempSnippet.SetCodelet("COLUMNNAMESCHILD",
                                                   StringCollectionToValuesFormattedForArray(tables, TXMLParser.GetAttribute(curChild, "childTable"),
                                                                                             StringHelper.StrSplit(TXMLParser.GetAttribute(curChild, "childFields"), ",")));
                            tempSnippet.SetCodelet("CREATECONSTRAINTS", TXMLParser.GetBoolAttribute(curChild, "createConstraints") ? "true" : "false");
                            snippetDataset.InsertSnippet("INITRELATIONS", tempSnippet);
                        }

                        if (curChild.Name.ToLower() == "customtable")
                        {
                            string variablename = TXMLParser.GetAttribute(curChild, "name");
                            string tabletype    = datasetname + TXMLParser.GetAttribute(curChild, "name");

                            XmlNode       customTableNodes = curChild.FirstChild;
                            TDataSetTable customTable      = new TDataSetTable(
                                tabletype,
                                tabletype,
                                variablename,
                                null);

                            // set TableId
                            customTable.iOrder                   = DataSetTableIdCounter++;
                            customTable.strDescription           = TXMLParser.GetAttribute(curChild, "comment");
                            customTable.strName                  = tabletype;
                            customTable.strDotNetName            = tabletype;
                            customTable.strVariableNameInDataset = variablename;

                            while (customTableNodes != null)
                            {
                                if (customTableNodes.Name.ToLower() == "customfield")
                                {
                                    TTableField customField = new TTableField();
                                    customField.strName        = TXMLParser.GetAttribute(customTableNodes, "name");
                                    customField.strTypeDotNet  = TXMLParser.GetAttribute(customTableNodes, "type");
                                    customField.strDescription = TXMLParser.GetAttribute(customTableNodes, "comment");
                                    customField.strDefault     = TXMLParser.GetAttribute(customTableNodes, "initial");
                                    customTable.grpTableField.Add(customField);
                                }

                                if (customTableNodes.Name.ToLower() == "field")
                                {
                                    // eg. SelectedSiteKey in PartnerEditTDS.MiscellaneousData
                                    TTableField field = new TTableField(store.GetTable(TXMLParser.GetAttribute(customTableNodes, "sqltable")).
                                                                        GetField(TXMLParser.GetAttribute(customTableNodes, "sqlfield")));

                                    if (TXMLParser.HasAttribute(customTableNodes, "name"))
                                    {
                                        field.strNameDotNet = TXMLParser.GetAttribute(customTableNodes, "name");
                                    }

                                    if (TXMLParser.HasAttribute(customTableNodes, "comment"))
                                    {
                                        field.strDescription = TXMLParser.GetAttribute(customTableNodes, "comment");
                                    }

                                    customTable.grpTableField.Add(field);
                                }

                                if (customTableNodes.Name.ToLower() == "primarykey")
                                {
                                    TConstraint primKeyConstraint = new TConstraint();
                                    primKeyConstraint.strName       = "PK";
                                    primKeyConstraint.strType       = "primarykey";
                                    primKeyConstraint.strThisFields = StringHelper.StrSplit(TXMLParser.GetAttribute(customTableNodes,
                                                                                                                    "thisFields"), ",");
                                    customTable.grpConstraint.Add(primKeyConstraint);
                                }

                                customTableNodes = customTableNodes.NextSibling;
                            }

                            tables.Add(tabletype, customTable);

                            AddTableToDataset(tabletype, variablename, snippetDataset);

                            CodeGenerationTable.InsertTableDefinition(snippetDataset, customTable, null, "TABLELOOP", true);
                            CodeGenerationTable.InsertRowDefinition(snippetDataset, customTable, null, "TABLELOOP");
                        }

                        curChild = curChild.NextSibling;
                    }

                    foreach (TDataSetTable table in tables.Values)
                    {
                        // todo? also other constraints, not only from original table?
                        foreach (TConstraint constraint in table.grpConstraint)
                        {
                            if ((constraint.strType == "foreignkey") && tables.ContainsKey(constraint.strOtherTable))
                            {
                                TDataSetTable otherTable = (TDataSetTable)tables[constraint.strOtherTable];

                                ProcessTemplate tempSnippet = Template.GetSnippet("INITCONSTRAINTS");

                                tempSnippet.SetCodelet("TABLEVARIABLENAME1", table.tablealias);
                                tempSnippet.SetCodelet("TABLEVARIABLENAME2", otherTable.tablealias);
                                tempSnippet.SetCodelet("CONSTRAINTNAME", TTable.NiceKeyName(constraint));

                                tempSnippet.SetCodelet("COLUMNNAMES1", StringCollectionToValuesFormattedForArray(constraint.strThisFields));
                                tempSnippet.SetCodelet("COLUMNNAMES2", StringCollectionToValuesFormattedForArray(constraint.strOtherFields));
                                snippetDataset.InsertSnippet("INITCONSTRAINTS", tempSnippet);
                            }
                        }
                    }

                    Template.InsertSnippet("CONTENTDATASETSANDTABLESANDROWS", snippetDataset);

                    cur = TXMLParser.GetNextEntity(cur);
                }
            }

            Template.FinishWriting(AOutputPath + Path.DirectorySeparatorChar + AFilename + "-generated.cs", ".cs", true);
        }
Beispiel #52
0
        public static void Main(string[] args)
        {
            TCmdOpts cmdLine = new TCmdOpts();
            string operation, xmlfile, outputfile;

            try
            {
                operation = cmdLine.GetOptValue("do");
                xmlfile = cmdLine.GetOptValue("petraxml");
                outputfile = cmdLine.GetOptValue("outputFile");
                TWriteSQL.eDatabaseType dbms = TWriteSQL.StringToDBMS(cmdLine.GetOptValue("dbms"));

                if (operation == "sql")
                {
                    System.Console.WriteLine("Reading xml file {0}...", xmlfile);
                    TDataDefinitionParser parser = new TDataDefinitionParser(xmlfile);
                    TDataDefinitionStore store = new TDataDefinitionStore();

                    if (parser.ParseDocument(ref store))
                    {
                        if (dbms == TWriteSQL.eDatabaseType.Sqlite)
                        {
                            // we want to write directly to the database
                            string password = "";

                            if (cmdLine.IsFlagSet("password"))
                            {
                                password = cmdLine.GetOptValue("password");
                            }

                            if (!TSQLiteWriter.CreateDatabase(store, outputfile, password))
                            {
                                Environment.Exit(-1);
                            }
                        }
                        else
                        {
                            // create an sql script that will be loaded into the database later
                            TWriteSQL.WriteSQL(store, dbms, outputfile);
                        }
                    }
                }

                if (operation == "load")
                {
                    if (dbms == TWriteSQL.eDatabaseType.MySQL)
                    {
                        TLoadMysql.LoadData(cmdLine.GetOptValue("database"), cmdLine.GetOptValue("username"),
                            cmdLine.GetOptValue("password"), cmdLine.GetOptValue("sqlfile"));
                    }
                    else if (dbms == TWriteSQL.eDatabaseType.Sqlite)
                    {
                        System.Console.WriteLine("Reading xml file {0}...", xmlfile);
                        TDataDefinitionParser parser = new TDataDefinitionParser(xmlfile);
                        TDataDefinitionStore store = new TDataDefinitionStore();

                        if (parser.ParseDocument(ref store))
                        {
                            if (dbms == TWriteSQL.eDatabaseType.Sqlite)
                            {
                                // we want to write directly to the database
                                string password = "";

                                if (cmdLine.IsFlagSet("password"))
                                {
                                    password = cmdLine.GetOptValue("password");
                                }

                                TSQLiteWriter.ExecuteLoadScript(store, outputfile,
                                    cmdLine.GetOptValue("datapath"),
                                    cmdLine.GetOptValue("sqlfile"),
                                    password);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error " + e.GetType().ToString() + ": " + e.Message);
                System.Console.WriteLine(e.StackTrace);
                System.Console.WriteLine();
                System.Console.WriteLine("generate the SQL files to create the database in SQL");
                System.Console.WriteLine(
                    "usage: GenerateSQL -do:<operation> -dbms:<type of database system> -petraxml:<path and filename of petra.xml> -outputFile:<path and filename of the output file>");
                System.Console.WriteLine("operations available:");
                System.Console.WriteLine("  sql ");
                System.Console.WriteLine("    sample call: ");
                System.Console.WriteLine(
                    "        GenerateSQL -do:sql -dbms:postgresql -petraxml:u:/sql/datadefinition/petra.xml -outputFile:U:/setup/petra0300/petra.sql");
                System.Console.WriteLine("Available database managment systems and their code:");
                System.Console.WriteLine("  postgresql (Recommended)");
                System.Console.WriteLine("  mysql (experimental)");
                System.Console.WriteLine("  sqlite (for the light version)");
                System.Environment.Exit(-1);
            }
        }
        /// <summary>
        /// generate code for reading and writing typed data tables from and to the database
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="strGroup"></param>
        /// <param name="AFilePath"></param>
        /// <param name="ANamespaceName"></param>
        /// <param name="AFilename"></param>
        /// <returns></returns>
        public static Boolean WriteTypedDataAccess(TDataDefinitionStore AStore,
            string strGroup,
            string AFilePath,
            string ANamespaceName,
            string AFilename)
        {
            Console.WriteLine("processing namespace PetraTypedDataAccess." + strGroup.Substring(0, 1).ToUpper() + strGroup.Substring(1));

            string templateDir = TAppSettingsManager.GetValue("TemplateDir", true);
            ProcessTemplate Template = new ProcessTemplate(templateDir + Path.DirectorySeparatorChar +
                "ORM" + Path.DirectorySeparatorChar +
                "DataAccess.cs");

            Template.SetCodelet("NAMESPACE", ANamespaceName);

            // load default header with license and copyright
            Template.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(templateDir));

            Template.AddToCodelet("USINGNAMESPACES", GetNamespace(strGroup), false);

            foreach (TTable currentTable in AStore.GetTables())
            {
                if (currentTable.strGroup == strGroup)
                {
                    DirectReferences = new ArrayList();

                    ProcessTemplate snippet = Template.GetSnippet("TABLEACCESS");

                    InsertMainProcedures(AStore, currentTable, Template, snippet);
                    InsertViaOtherTable(AStore, currentTable, Template, snippet);
                    InsertViaLinkTable(AStore, currentTable, Template, snippet);

                    Template.InsertSnippet("TABLEACCESSLOOP", snippet);
                }
            }

            Template.FinishWriting(AFilePath + AFilename + "-generated.cs", ".cs", true);

            return true;
        }
        /// creates a file with enums in Shared and one file per submodule in Server for cached tables
        public static void WriteCachedTables(TDataDefinitionStore AStore,
            string ACacheYamlFilename,
            string ASharedPath,
            string ATemplateDir)
        {
            // Load yaml file with list of tables that should be cached
            TYml2Xml ymlParser = new TYml2Xml(ACacheYamlFilename);
            XmlDocument xmlDoc = ymlParser.ParseYML2XML();

            XmlNode module = xmlDoc.DocumentElement.FirstChild.FirstChild;

            while (module != null)
            {
                XmlNode subModule = module.FirstChild;
                bool severalSubModules = (subModule != null && subModule.NextSibling != null);

                // write the shared file with the enum definitions
                ProcessTemplate SharedTemplate = new ProcessTemplate(ATemplateDir + Path.DirectorySeparatorChar +
                    "ORM" + Path.DirectorySeparatorChar +
                    "Cacheable.Shared.cs");

                SharedTemplate.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(ATemplateDir));
//                SharedTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Shared.M" + module.Name);
                SharedTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Shared");

                while (subModule != null)
                {
                    List <string>UsingNamespaces = new List <string>();

                    // write the server file for each submodule
                    ProcessTemplate ServerTemplate = new ProcessTemplate(ATemplateDir + Path.DirectorySeparatorChar +
                        "ORM" + Path.DirectorySeparatorChar +
                        "Cacheable.Server.cs");

                    ServerTemplate.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(ATemplateDir));
                    ServerTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Server.M" + module.Name + "." + subModule.Name + ".Cacheable");
                    ServerTemplate.SetCodelet("SUBNAMESPACE", "M" + module.Name + "." + subModule.Name);
                    ServerTemplate.SetCodelet("CACHEABLECLASS", "T" + module.Name + "Cacheable");
                    ServerTemplate.SetCodelet("SUBMODULE", subModule.Name);
                    ServerTemplate.SetCodelet("GETCALCULATEDLISTFROMDB", "");
                    ServerTemplate.SetCodelet("LEDGERGETCACHEABLE", "");
                    ServerTemplate.SetCodelet("LEDGERSAVECACHEABLE", "");

                    if (!severalSubModules)
                    {
                        // for MCommon
                        ServerTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Server.M" + module.Name + ".Cacheable");
                        ServerTemplate.SetCodelet("SUBNAMESPACE", "M" + module.Name);
                        ServerTemplate.SetCodelet("CACHEABLECLASS", "TCacheable");
                    }

                    ProcessTemplate snippetSubmodule = SharedTemplate.GetSnippet("SUBMODULEENUM");
                    snippetSubmodule.SetCodelet("SUBMODULE", subModule.Name);
                    snippetSubmodule.SetCodelet("MODULE", module.Name);

                    ProcessTemplate snippetLedgerGetTable = null;
                    ProcessTemplate snippetLedgerSaveTable = null;

                    XmlNode TableOrListElement = subModule.FirstChild;

                    while (TableOrListElement != null)
                    {
                        XmlNode enumElement = TableOrListElement.FirstChild;

                        while (enumElement != null)
                        {
                            bool DependsOnLedger = false;

                            if (TYml2Xml.GetAttributeRecursive(enumElement, "DependsOnLedger") == "true")
                            {
                                if (snippetLedgerGetTable == null)
                                {
                                    snippetLedgerGetTable = ServerTemplate.GetSnippet("LEDGERGETCACHEABLE");
                                    snippetLedgerGetTable.SetCodelet("SUBMODULE", subModule.Name);
                                }

                                if ((snippetLedgerSaveTable == null) && (TableOrListElement.Name == "DatabaseTables"))
                                {
                                    snippetLedgerSaveTable = ServerTemplate.GetSnippet("LEDGERSAVECACHEABLE");
                                    snippetLedgerSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                }

                                DependsOnLedger = true;

                                ServerTemplate.SetCodelet("WITHLEDGER", "true");
                            }

                            ProcessTemplate snippetElement = SharedTemplate.GetSnippet("ENUMELEMENT");

                            if ((enumElement.NextSibling == null)
                                && ((TableOrListElement.NextSibling == null) || (TableOrListElement.NextSibling.FirstChild == null)))
                            {
                                snippetElement = SharedTemplate.GetSnippet("ENUMELEMENTLAST");
                            }

                            string Comment = TXMLParser.GetAttribute(enumElement, "Comment");

                            if (TableOrListElement.Name == "DatabaseTables")
                            {
                                TTable Table = AStore.GetTable(enumElement.Name);

                                string Namespace = "Ict.Petra.Shared." + TTable.GetNamespace(Table.strGroup) + ".Data";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                Namespace = "Ict.Petra.Shared." + TTable.GetNamespace(Table.strGroup) + ".Validation";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                Namespace = "Ict.Petra.Server." + TTable.GetNamespace(Table.strGroup) + ".Data.Access";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                if (Table == null)
                                {
                                    throw new Exception("Error: cannot find table " + enumElement.Name + " for caching in module " + module.Name);
                                }

                                if (Comment.Length == 0)
                                {
                                    Comment = Table.strDescription;
                                }
                            }

                            if (Comment.Length == 0)
                            {
                                Comment = "todoComment";
                            }

                            snippetElement.SetCodelet("ENUMCOMMENT", Comment);

                            string enumName = enumElement.Name;

                            if (TXMLParser.HasAttribute(enumElement, "Enum"))
                            {
                                enumName = TXMLParser.GetAttribute(enumElement, "Enum");
                            }
                            else if (TableOrListElement.Name == "DatabaseTables")
                            {
                                string character2 = enumElement.Name.Substring(1, 1);

                                if (character2.ToLower() == character2)
                                {
                                    // this is a table name that has a 2 digit prefix
                                    enumName = enumElement.Name.Substring(2) + "List";
                                }
                                else
                                {
                                    enumName = enumElement.Name.Substring(1) + "List";
                                }
                            }

                            snippetElement.SetCodelet("ENUMNAME", enumName);
                            snippetElement.SetCodelet("DATATABLENAME", enumElement.Name);

                            snippetSubmodule.InsertSnippet("ENUMELEMENTS", snippetElement);

                            if (TableOrListElement.Name == "DatabaseTables")
                            {
                                ProcessTemplate snippetLoadTable = ServerTemplate.GetSnippet("LOADTABLE");

                                if (DependsOnLedger)
                                {
                                    snippetLoadTable = ServerTemplate.GetSnippet("LOADTABLEVIALEDGER");
                                }

                                snippetLoadTable.SetCodelet("ENUMNAME", enumName);
                                snippetLoadTable.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerGetTable.InsertSnippet("LOADTABLESANDLISTS", snippetLoadTable);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("LOADTABLESANDLISTS", snippetLoadTable);
                                }

                                ProcessTemplate snippetSaveTable = ServerTemplate.GetSnippet("SAVETABLE");
                                snippetSaveTable.SetCodelet("ENUMNAME", enumName);
                                snippetSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                snippetSaveTable.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerSaveTable.InsertSnippet("SAVETABLE", snippetSaveTable);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("SAVETABLE", snippetSaveTable);
                                }

                                ProcessTemplate snippetDataValidation = ServerTemplate.GetSnippet("DATAVALIDATION");
                                snippetDataValidation.SetCodelet("ENUMNAME", enumName);
                                snippetDataValidation.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerSaveTable.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                }
                            }
                            else
                            {
                                ProcessTemplate snippetLoadList = ServerTemplate.GetSnippet("LOADCALCULATEDLIST");

                                if (DependsOnLedger)
                                {
                                    snippetLoadList = ServerTemplate.GetSnippet("LOADCALCULATEDLISTFORLEDGER");
                                }

                                snippetLoadList.SetCodelet("ENUMNAME", enumName);
                                snippetLoadList.SetCodelet("CALCULATEDLISTNAME", enumName);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerGetTable.InsertSnippet("LOADTABLESANDLISTS", snippetLoadList);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("LOADTABLESANDLISTS", snippetLoadList);
                                }

                                if (TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable") != String.Empty)
                                {
                                    ProcessTemplate snippetSaveTable = ServerTemplate.GetSnippet("SAVETABLE");
                                    snippetSaveTable.SetCodelet("ENUMNAME", enumName);
                                    snippetSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                    snippetSaveTable.SetCodelet("DATATABLENAME", TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable"));

                                    if (DependsOnLedger)
                                    {
                                        snippetLedgerSaveTable.InsertSnippet("SAVETABLE", snippetSaveTable);
                                    }
                                    else
                                    {
                                        ServerTemplate.InsertSnippet("SAVETABLE", snippetSaveTable);
                                    }

                                    ProcessTemplate snippetDataValidation = ServerTemplate.GetSnippet("DATAVALIDATION");
                                    snippetDataValidation.SetCodelet("ENUMNAME", enumName);
                                    snippetDataValidation.SetCodelet("DATATABLENAME",
                                        TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable"));

                                    if (DependsOnLedger)
                                    {
                                        snippetLedgerSaveTable.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                    }
                                    else
                                    {
                                        ServerTemplate.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                    }
                                }
                            }

                            enumElement = enumElement.NextSibling;

                            if (enumElement != null)
                            {
                                snippetSubmodule.AddToCodelet("ENUMELEMENTS", Environment.NewLine);
                            }
                        }

                        TableOrListElement = TableOrListElement.NextSibling;
                    }

                    SharedTemplate.InsertSnippet("ENUMS", snippetSubmodule);

                    if (snippetLedgerGetTable != null)
                    {
                        ServerTemplate.InsertSnippet("LEDGERGETCACHEABLE", snippetLedgerGetTable);
                    }

                    if (snippetLedgerSaveTable != null)
                    {
                        ServerTemplate.InsertSnippet("LEDGERSAVECACHEABLE", snippetLedgerSaveTable);
                    }

                    ServerTemplate.SetCodelet("USINGNAMESPACES", string.Empty);

                    foreach (string UsingNamespace in UsingNamespaces)
                    {
                        ServerTemplate.AddToCodelet("USINGNAMESPACES", "using " + UsingNamespace + ";" + Environment.NewLine);
                    }

                    string path = ASharedPath +
                                  Path.DirectorySeparatorChar + ".." +
                                  Path.DirectorySeparatorChar + "Server" +
                                  Path.DirectorySeparatorChar + "lib" +
                                  Path.DirectorySeparatorChar + "M" + module.Name +
                                  Path.DirectorySeparatorChar;

                    if (File.Exists(path + "Cacheable.ManualCode.cs"))
                    {
                        path += "Cacheable-generated.cs";
                    }
                    else
                    {
                        if (File.Exists(path + "data" + Path.DirectorySeparatorChar + subModule.Name + "." + "Cacheable.ManualCode.cs"))
                        {
                            path += "data" + Path.DirectorySeparatorChar + subModule.Name + "." + "Cacheable-generated.cs";
                        }
                        else if (File.Exists(path + "data" + Path.DirectorySeparatorChar + "Cacheable.ManualCode.cs"))
                        {
                            path += "data" + Path.DirectorySeparatorChar + "Cacheable-generated.cs";
                        }
                        else
                        {
                            path += subModule.Name + "." + "Cacheable-generated.cs";
                        }
                    }

                    ServerTemplate.FinishWriting(path, ".cs", true);

                    subModule = subModule.NextSibling;
                }

                SharedTemplate.FinishWriting(ASharedPath +
                    Path.DirectorySeparatorChar + "M" + module.Name + ".Cacheable-generated.cs",
                    ".cs", true);

                module = module.NextSibling;
            }
        }
        /// <summary>
        /// insert the viaothertable functions for one specific constraint
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="AConstraint"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="AOtherTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaOtherTableConstraint(TDataDefinitionStore AStore,
            TConstraint AConstraint,
            TTable ACurrentTable,
            TTable AOtherTable,
            ProcessTemplate ATemplate,
            ProcessTemplate ASnippet)
        {
            ATemplate.AddToCodelet("USINGNAMESPACES",
                GetNamespace(AOtherTable.strGroup), false);

            ProcessTemplate snippetViaTable = ATemplate.GetSnippet("VIAOTHERTABLE");
            snippetViaTable.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(AOtherTable.strName));
            snippetViaTable.SetCodelet("SQLOTHERTABLENAME", AOtherTable.strName);

            string ProcedureName = "Via" + TTable.NiceTableName(AOtherTable.strName);

            // check if other foreign key exists that references the same table, e.g.
            // PBankAccess.CountViaPPartnerPartnerKey
            // PBankAccess.CountViaPPartnerContactPartnerKey
            string DifferentField = FindOtherConstraintSameOtherTable(ACurrentTable.grpConstraint, AConstraint);

            if (DifferentField.Length != 0)
            {
                ProcedureName += TTable.NiceFieldName(DifferentField);
            }

            int notUsedInt;
            string formalParametersOtherPrimaryKey;
            string actualParametersOtherPrimaryKey;
            string dummy;

            PrepareCodeletsPrimaryKey(AOtherTable,
                out formalParametersOtherPrimaryKey,
                out actualParametersOtherPrimaryKey,
                out dummy,
                out notUsedInt);

            int numberFields;

            string namesOfThisTableFields;
            string notUsed;
            PrepareCodeletsForeignKey(
                AOtherTable,
                AConstraint,
                out notUsed,
                out notUsed,
                out notUsed,
                out numberFields,
                out namesOfThisTableFields);

            snippetViaTable.SetCodelet("VIAPROCEDURENAME", ProcedureName);
            snippetViaTable.SetCodelet("FORMALPARAMETERSOTHERPRIMARYKEY", formalParametersOtherPrimaryKey);
            snippetViaTable.SetCodelet("ACTUALPARAMETERSOTHERPRIMARYKEY", actualParametersOtherPrimaryKey);
            snippetViaTable.SetCodelet("NUMBERFIELDS", numberFields.ToString());
            snippetViaTable.SetCodelet("NUMBERFIELDSOTHER", (actualParametersOtherPrimaryKey.Split(',').Length).ToString());
            snippetViaTable.SetCodelet("THISTABLEFIELDS", namesOfThisTableFields);

            AddDirectReference(AConstraint);

            ASnippet.InsertSnippet("VIAOTHERTABLE", snippetViaTable);
        }
 /// <summary>
 /// overloaded method of ParseDocument, always with adding special fields (createdby etc)
 /// </summary>
 /// <param name="myStore">the destination for all the parsed information</param>
 /// <param name="ADoValidation">should the document be validated</param>
 /// <returns></returns>
 public Boolean ParseDocument(ref TDataDefinitionStore myStore, Boolean ADoValidation)
 {
     return ParseDocument(ref myStore, ADoValidation, true);
 }
Beispiel #57
0
        /// <summary>
        /// create an Sqlite database and create all tables and sequences and indexes
        /// </summary>
        /// <param name="ADataDefinition"></param>
        /// <param name="ADBFilename"></param>
        /// <param name="ADBPwd"></param>
        /// <returns></returns>
        static public bool CreateDatabase(TDataDefinitionStore ADataDefinition, string ADBFilename, string ADBPwd)
        {
            if (System.IO.File.Exists(ADBFilename))
            {
                throw new Exception("cannot overwrite existing file " + ADBFilename);
            }

            System.Console.WriteLine("Writing file to {0}...", ADBFilename);

            // see also tutorial http://Sqlite.phxsoftware.com/forums/p/130/452.aspx#452

            // sqlite on Windows does not support encryption with a password
            // System.EntryPointNotFoundException: sqlite3_key
            ADBPwd = string.Empty;

            SqliteConnection conn = new SqliteConnection("Data Source=" + ADBFilename + (ADBPwd.Length > 0 ? ";Password="******""));

            conn.Open();

            foreach (TTable table in ADataDefinition.GetTables())
            {
                // see http://www.Sqlite.org/lang_createtable.html
                string createStmt = "CREATE TABLE " + table.strName + " (";
                bool   firstField = true;

                foreach (TTableField field in table.grpTableField)
                {
                    createStmt += TWriteSQL.WriteField(TWriteSQL.eDatabaseType.Sqlite, table, field, firstField, false);
                    firstField  = false;
                }

                if (table.HasPrimaryKey() && !createStmt.Contains("PRIMARY KEY AUTOINCREMENT"))
                {
                    createStmt += ", PRIMARY KEY (";
                    bool firstPrimaryKeyColumn = true;

                    foreach (string primaryKeyColumnName in table.GetPrimaryKey().strThisFields)
                    {
                        if (!firstPrimaryKeyColumn)
                        {
                            createStmt += ",";
                        }

                        createStmt           += primaryKeyColumnName;
                        firstPrimaryKeyColumn = false;
                    }

                    createStmt += ")";
                }

                createStmt += AddForeignKeys(table);

                createStmt += ");";


                SqliteCommand cmd = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
            }

            // sequence workaround
            // see http://www.Sqlite.org/faq.html#q1 AUTOINCREMENT
            foreach (TSequence seq in ADataDefinition.GetSequences())
            {
                string        createStmt = "CREATE TABLE " + seq.strName + " (sequence INTEGER PRIMARY KEY AUTOINCREMENT, dummy INTEGER);";
                SqliteCommand cmd        = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
                createStmt = "INSERT INTO " + seq.strName + " VALUES(NULL, -1);";
                cmd        = new SqliteCommand(createStmt, conn);
                cmd.ExecuteNonQuery();
            }

            conn.Close();

            return(true);
        }
        /// <summary>
        /// this is for foreign keys, eg load all countries with currency EUR
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaOtherTable(TDataDefinitionStore AStore,
            TTable ACurrentTable,
            ProcessTemplate ATemplate,
            ProcessTemplate ASnippet)
        {
            foreach (TConstraint myConstraint in ACurrentTable.grpConstraint)
            {
                if (ValidForeignKeyConstraintForLoadVia(myConstraint))
                {
                    TTable OtherTable = AStore.GetTable(myConstraint.strOtherTable);

                    if (!LoadViaHasAlreadyBeenImplemented(myConstraint))
                    {
                        InsertViaOtherTableConstraint(AStore,
                            myConstraint,
                            ACurrentTable,
                            OtherTable,
                            ATemplate,
                            ASnippet);
                    }

                    // AccountHierarchy: there is no constraint that references Ledger directly, but constraint referencing the Account table with a key that contains the ledger reference
                    // but because the key in Ledger is already the primary key, a LoadViaLedger is required.
                    // other way round: p_foundation_proposal_detail has 2 constraints for foundation and foundationproposal
                    foreach (string field in myConstraint.strOtherFields)
                    {
                        // get a constraint that is only based on that field
                        TConstraint OtherLinkConstraint = OtherTable.GetConstraint(StringHelper.StrSplit(field, ","));

                        if ((OtherLinkConstraint != null) && ValidForeignKeyConstraintForLoadVia(OtherLinkConstraint))
                        {
                            TConstraint NewConstraint = new TConstraint();
                            NewConstraint.strName = OtherLinkConstraint.strName + "forLoadVia";
                            NewConstraint.strType = "foreignkey";
                            NewConstraint.strThisTable = myConstraint.strThisTable;
                            NewConstraint.strThisFields =
                                StringHelper.StrSplit(myConstraint.strThisFields[myConstraint.strOtherFields.IndexOf(
                                                                                     field)], ",");
                            NewConstraint.strOtherTable = OtherLinkConstraint.strOtherTable;
                            NewConstraint.strOtherFields = OtherLinkConstraint.strOtherFields;

                            if (!LoadViaHasAlreadyBeenImplemented(NewConstraint))
                            {
                                InsertViaOtherTableConstraint(AStore,
                                    NewConstraint,
                                    ACurrentTable,
                                    AStore.GetTable(OtherLinkConstraint.strOtherTable),
                                    ATemplate,
                                    ASnippet);
                            }
                        }
                    }
                }
            }
        }
        /// creates a file with enums in Shared and one file per submodule in Server for cached tables
        public static void WriteCachedTables(TDataDefinitionStore AStore,
                                             string ACacheYamlFilename,
                                             string ASharedPath,
                                             string ATemplateDir)
        {
            // Load yaml file with list of tables that should be cached
            TYml2Xml    ymlParser = new TYml2Xml(ACacheYamlFilename);
            XmlDocument xmlDoc    = ymlParser.ParseYML2XML();

            XmlNode module = xmlDoc.DocumentElement.FirstChild.FirstChild;

            while (module != null)
            {
                XmlNode subModule         = module.FirstChild;
                bool    severalSubModules = (subModule != null && subModule.NextSibling != null);

                // write the shared file with the enum definitions
                ProcessTemplate SharedTemplate = new ProcessTemplate(ATemplateDir + Path.DirectorySeparatorChar +
                                                                     "ORM" + Path.DirectorySeparatorChar +
                                                                     "Cacheable.Shared.cs");

                SharedTemplate.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(ATemplateDir));
//                SharedTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Shared.M" + module.Name);
                SharedTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Shared");

                while (subModule != null)
                {
                    List <string> UsingNamespaces = new List <string>();

                    // write the server file for each submodule
                    ProcessTemplate ServerTemplate = new ProcessTemplate(ATemplateDir + Path.DirectorySeparatorChar +
                                                                         "ORM" + Path.DirectorySeparatorChar +
                                                                         "Cacheable.Server.cs");

                    ServerTemplate.SetCodelet("GPLFILEHEADER", ProcessTemplate.LoadEmptyFileComment(ATemplateDir));
                    ServerTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Server.M" + module.Name + "." + subModule.Name + ".Cacheable");
                    ServerTemplate.SetCodelet("SUBNAMESPACE", "M" + module.Name + "." + subModule.Name);
                    ServerTemplate.SetCodelet("CACHEABLECLASS", "T" + module.Name + "Cacheable");
                    ServerTemplate.SetCodelet("SUBMODULE", subModule.Name);
                    ServerTemplate.SetCodelet("GETCALCULATEDLISTFROMDB", "");
                    ServerTemplate.SetCodelet("LEDGERGETCACHEABLE", "");
                    ServerTemplate.SetCodelet("LEDGERSAVECACHEABLE", "");

                    if (!severalSubModules)
                    {
                        // for MCommon
                        ServerTemplate.SetCodelet("NAMESPACE", "Ict.Petra.Server.M" + module.Name + ".Cacheable");
                        ServerTemplate.SetCodelet("SUBNAMESPACE", "M" + module.Name);
                        ServerTemplate.SetCodelet("CACHEABLECLASS", "TCacheable");
                    }

                    ProcessTemplate snippetSubmodule = SharedTemplate.GetSnippet("SUBMODULEENUM");
                    snippetSubmodule.SetCodelet("SUBMODULE", subModule.Name);
                    snippetSubmodule.SetCodelet("MODULE", module.Name);

                    ProcessTemplate snippetLedgerGetTable  = null;
                    ProcessTemplate snippetLedgerSaveTable = null;

                    XmlNode TableOrListElement = subModule.FirstChild;

                    while (TableOrListElement != null)
                    {
                        XmlNode enumElement = TableOrListElement.FirstChild;

                        while (enumElement != null)
                        {
                            bool DependsOnLedger = false;

                            if (TYml2Xml.GetAttributeRecursive(enumElement, "DependsOnLedger") == "true")
                            {
                                if (snippetLedgerGetTable == null)
                                {
                                    snippetLedgerGetTable = ServerTemplate.GetSnippet("LEDGERGETCACHEABLE");
                                    snippetLedgerGetTable.SetCodelet("SUBMODULE", subModule.Name);
                                }

                                if ((snippetLedgerSaveTable == null) && (TableOrListElement.Name == "DatabaseTables"))
                                {
                                    snippetLedgerSaveTable = ServerTemplate.GetSnippet("LEDGERSAVECACHEABLE");
                                    snippetLedgerSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                }

                                DependsOnLedger = true;

                                ServerTemplate.SetCodelet("WITHLEDGER", "true");
                            }

                            ProcessTemplate snippetElement = SharedTemplate.GetSnippet("ENUMELEMENT");

                            if ((enumElement.NextSibling == null) &&
                                ((TableOrListElement.NextSibling == null) || (TableOrListElement.NextSibling.FirstChild == null)))
                            {
                                snippetElement = SharedTemplate.GetSnippet("ENUMELEMENTLAST");
                            }

                            string Comment = TXMLParser.GetAttribute(enumElement, "Comment");

                            if (TableOrListElement.Name == "DatabaseTables")
                            {
                                TTable Table = AStore.GetTable(enumElement.Name);

                                string Namespace = "Ict.Petra.Shared." + TTable.GetNamespace(Table.strGroup) + ".Data";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                Namespace = "Ict.Petra.Server." + TTable.GetNamespace(Table.strGroup) + ".Validation";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                Namespace = "Ict.Petra.Server." + TTable.GetNamespace(Table.strGroup) + ".Data.Access";

                                if (!UsingNamespaces.Contains(Namespace))
                                {
                                    UsingNamespaces.Add(Namespace);
                                }

                                if (Table == null)
                                {
                                    throw new Exception("Error: cannot find table " + enumElement.Name + " for caching in module " + module.Name);
                                }

                                if (Comment.Length == 0)
                                {
                                    Comment = Table.strDescription;
                                }
                            }

                            if (Comment.Length == 0)
                            {
                                Comment = "todoComment";
                            }

                            snippetElement.SetCodelet("ENUMCOMMENT", Comment);

                            string enumName = enumElement.Name;

                            if (TXMLParser.HasAttribute(enumElement, "Enum"))
                            {
                                enumName = TXMLParser.GetAttribute(enumElement, "Enum");
                            }
                            else if (TableOrListElement.Name == "DatabaseTables")
                            {
                                string character2 = enumElement.Name.Substring(1, 1);

                                if (character2.ToLower() == character2)
                                {
                                    // this is a table name that has a 2 digit prefix
                                    enumName = enumElement.Name.Substring(2) + "List";
                                }
                                else
                                {
                                    enumName = enumElement.Name.Substring(1) + "List";
                                }
                            }

                            snippetElement.SetCodelet("ENUMNAME", enumName);
                            snippetElement.SetCodelet("DATATABLENAME", enumElement.Name);

                            snippetSubmodule.InsertSnippet("ENUMELEMENTS", snippetElement);

                            if (TableOrListElement.Name == "DatabaseTables")
                            {
                                ProcessTemplate snippetLoadTable = ServerTemplate.GetSnippet("LOADTABLE");

                                if (DependsOnLedger)
                                {
                                    snippetLoadTable = ServerTemplate.GetSnippet("LOADTABLEVIALEDGER");
                                }

                                snippetLoadTable.SetCodelet("ENUMNAME", enumName);
                                snippetLoadTable.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerGetTable.InsertSnippet("LOADTABLESANDLISTS", snippetLoadTable);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("LOADTABLESANDLISTS", snippetLoadTable);
                                }

                                ProcessTemplate snippetSaveTable = ServerTemplate.GetSnippet("SAVETABLE");
                                snippetSaveTable.SetCodelet("ENUMNAME", enumName);
                                snippetSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                snippetSaveTable.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerSaveTable.InsertSnippet("SAVETABLE", snippetSaveTable);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("SAVETABLE", snippetSaveTable);
                                }

                                ProcessTemplate snippetDataValidation = ServerTemplate.GetSnippet("DATAVALIDATION");
                                snippetDataValidation.SetCodelet("ENUMNAME", enumName);
                                snippetDataValidation.SetCodelet("DATATABLENAME", enumElement.Name);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerSaveTable.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                }
                            }
                            else
                            {
                                ProcessTemplate snippetLoadList = ServerTemplate.GetSnippet("LOADCALCULATEDLIST");

                                if (DependsOnLedger)
                                {
                                    snippetLoadList = ServerTemplate.GetSnippet("LOADCALCULATEDLISTFORLEDGER");
                                }

                                snippetLoadList.SetCodelet("ENUMNAME", enumName);
                                snippetLoadList.SetCodelet("CALCULATEDLISTNAME", enumName);

                                if (DependsOnLedger)
                                {
                                    snippetLedgerGetTable.InsertSnippet("LOADTABLESANDLISTS", snippetLoadList);
                                }
                                else
                                {
                                    ServerTemplate.InsertSnippet("LOADTABLESANDLISTS", snippetLoadList);
                                }

                                if (TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable") != String.Empty)
                                {
                                    ProcessTemplate snippetSaveTable = ServerTemplate.GetSnippet("SAVETABLE");
                                    snippetSaveTable.SetCodelet("ENUMNAME", enumName);
                                    snippetSaveTable.SetCodelet("SUBMODULE", subModule.Name);
                                    snippetSaveTable.SetCodelet("DATATABLENAME", TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable"));

                                    if (DependsOnLedger)
                                    {
                                        snippetLedgerSaveTable.InsertSnippet("SAVETABLE", snippetSaveTable);
                                    }
                                    else
                                    {
                                        ServerTemplate.InsertSnippet("SAVETABLE", snippetSaveTable);
                                    }

                                    ProcessTemplate snippetDataValidation = ServerTemplate.GetSnippet("DATAVALIDATION");
                                    snippetDataValidation.SetCodelet("ENUMNAME", enumName);
                                    snippetDataValidation.SetCodelet("DATATABLENAME",
                                                                     TYml2Xml.GetAttributeRecursive(enumElement, "IsStorableToDBTable"));

                                    if (DependsOnLedger)
                                    {
                                        snippetLedgerSaveTable.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                    }
                                    else
                                    {
                                        ServerTemplate.InsertSnippet("DATAVALIDATION", snippetDataValidation);
                                    }
                                }
                            }

                            enumElement = enumElement.NextSibling;

                            if (enumElement != null)
                            {
                                snippetSubmodule.AddToCodelet("ENUMELEMENTS", Environment.NewLine);
                            }
                        }

                        TableOrListElement = TableOrListElement.NextSibling;
                    }

                    SharedTemplate.InsertSnippet("ENUMS", snippetSubmodule);

                    if (snippetLedgerGetTable != null)
                    {
                        ServerTemplate.InsertSnippet("LEDGERGETCACHEABLE", snippetLedgerGetTable);
                    }

                    if (snippetLedgerSaveTable != null)
                    {
                        ServerTemplate.InsertSnippet("LEDGERSAVECACHEABLE", snippetLedgerSaveTable);
                    }

                    ServerTemplate.SetCodelet("USINGNAMESPACES", string.Empty);

                    foreach (string UsingNamespace in UsingNamespaces)
                    {
                        ServerTemplate.AddToCodelet("USINGNAMESPACES", "using " + UsingNamespace + ";" + Environment.NewLine);
                    }

                    string path = ASharedPath +
                                  Path.DirectorySeparatorChar + ".." +
                                  Path.DirectorySeparatorChar + "Server" +
                                  Path.DirectorySeparatorChar + "lib" +
                                  Path.DirectorySeparatorChar + "M" + module.Name +
                                  Path.DirectorySeparatorChar;

                    if (File.Exists(path + "Cacheable.ManualCode.cs"))
                    {
                        path += "Cacheable-generated.cs";
                    }
                    else
                    {
                        if (File.Exists(path + "data" + Path.DirectorySeparatorChar + subModule.Name + "." + "Cacheable.ManualCode.cs"))
                        {
                            path += "data" + Path.DirectorySeparatorChar + subModule.Name + "." + "Cacheable-generated.cs";
                        }
                        else if (File.Exists(path + "data" + Path.DirectorySeparatorChar + "Cacheable.ManualCode.cs"))
                        {
                            path += "data" + Path.DirectorySeparatorChar + "Cacheable-generated.cs";
                        }
                        else
                        {
                            path += subModule.Name + "." + "Cacheable-generated.cs";
                        }
                    }

                    ServerTemplate.FinishWriting(path, ".cs", true);

                    subModule = subModule.NextSibling;
                }

                SharedTemplate.FinishWriting(ASharedPath +
                                             Path.DirectorySeparatorChar + "M" + module.Name + ".Cacheable-generated.cs",
                                             ".cs", true);

                module = module.NextSibling;
            }
        }
        /// <summary>
        /// situation 2: bridging tables
        /// for example p_location and p_partner are connected through p_partner_location
        /// it would be helpful, to be able to do:
        /// location.LoadViaPartner(partnerkey) to get all locations of the partner
        /// partner.loadvialocation(locationkey) to get all partners living at that location
        /// general solution: pick up all foreign keys from other tables (B) to the primary key of the current table (A),
        /// where the other table has a foreign key to another table (C), using other fields in the primary key of (B) than the link to (A).
        /// get all tables that reference the current table
        /// </summary>
        /// <param name="AStore"></param>
        /// <param name="ACurrentTable"></param>
        /// <param name="ATemplate"></param>
        /// <param name="ASnippet"></param>
        private static void InsertViaLinkTable(TDataDefinitionStore AStore, TTable ACurrentTable, ProcessTemplate ATemplate, ProcessTemplate ASnippet)
        {
            // step 1: collect all the valid constraints of such link tables
            ArrayList OtherLinkConstraints = new ArrayList();
            ArrayList References = new ArrayList();

            foreach (TConstraint Reference in ACurrentTable.GetReferences())
            {
                TTable LinkTable = AStore.GetTable(Reference.strThisTable);
                try
                {
                    TConstraint LinkPrimaryKey = LinkTable.GetPrimaryKey();

                    if (StringHelper.Contains(LinkPrimaryKey.strThisFields, Reference.strThisFields))
                    {
                        // check how many constraints from the link table are from fields in the primary key
                        // a link table only should have 2
                        // so find the other one
                        TConstraint OtherLinkConstraint = null;
                        bool IsLinkTable = false;

                        foreach (TConstraint LinkConstraint in LinkTable.grpConstraint)
                        {
                            // check if there is another constraint for the primary key of the link table.
                            if (ValidForeignKeyConstraintForLoadVia(LinkConstraint) && (LinkConstraint != Reference)
                                && (StringHelper.Contains(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields)))
                            {
                                if (OtherLinkConstraint == null)
                                {
                                    OtherLinkConstraint = LinkConstraint;
                                    IsLinkTable = true;
                                }
                                else
                                {
                                    IsLinkTable = false;
                                }
                            }
                            else if (ValidForeignKeyConstraintForLoadVia(LinkConstraint) && (LinkConstraint != Reference)
                                     && (!StringHelper.Contains(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields))
                                     && StringHelper.ContainsSome(LinkPrimaryKey.strThisFields, LinkConstraint.strThisFields))
                            {
                                // if there is a key that partly is in the primary key, then this is not a link table.
                                OtherLinkConstraint = LinkConstraint;
                                IsLinkTable = false;
                            }
                        }

                        if ((IsLinkTable) && (OtherLinkConstraint.strOtherTable != Reference.strOtherTable))
                        {
                            // collect the links. then we are able to name them correctly, once we need them
                            OtherLinkConstraints.Add(OtherLinkConstraint);
                            References.Add(Reference);
                        }
                    }
                }
                catch (Exception)
                {
                    // Console.WriteLine(e.Message);
                }
            }

            // step2: implement the link tables, using correct names for the procedures
            int Count = 0;

            foreach (TConstraint OtherLinkConstraint in OtherLinkConstraints)
            {
                TTable OtherTable = AStore.GetTable(OtherLinkConstraint.strOtherTable);
                string ProcedureName = "Via" + TTable.NiceTableName(OtherTable.strName);

                // check if other foreign key exists that references the same table, e.g.
                // PPartnerAccess.LoadViaSUserPRecentPartners
                // PPartnerAccess.LoadViaSUserPCustomisedGreeting
                // also PFoundationProposalAccess.LoadViaPFoundationPFoundationProposalDetail and
                // TODO AlreadyExistsProcedureSameName necessary for PPersonAccess.LoadViaPUnit (p_field_key_n) and PPersonAccess.LoadViaPUnitPmGeneralApplication
                // Question: does PPersonAccess.LoadViaPUnitPmGeneralApplication make sense?
                if (FindOtherConstraintSameOtherTable2(OtherLinkConstraints,
                        OtherLinkConstraint)
                    || LoadViaHasAlreadyBeenImplemented(OtherLinkConstraint)

                    // TODO || AlreadyExistsProcedureSameName(ProcedureName)
                    || ((ProcedureName == "ViaPUnit") && (OtherLinkConstraint.strThisTable == "pm_general_application"))
                    )
                {
                    ProcedureName += TTable.NiceTableName(OtherLinkConstraint.strThisTable);
                }

                ATemplate.AddToCodelet("USINGNAMESPACES",
                    GetNamespace(OtherTable.strGroup), false);

                ProcessTemplate snippetLinkTable = ATemplate.GetSnippet("VIALINKTABLE");

                snippetLinkTable.SetCodelet("VIAPROCEDURENAME", ProcedureName);
                snippetLinkTable.SetCodelet("OTHERTABLENAME", TTable.NiceTableName(OtherTable.strName));
                snippetLinkTable.SetCodelet("SQLOTHERTABLENAME", OtherTable.strName);
                snippetLinkTable.SetCodelet("SQLLINKTABLENAME", OtherLinkConstraint.strThisTable);

                string notUsed;
                int notUsedInt;
                string formalParametersOtherPrimaryKey;
                string actualParametersOtherPrimaryKey;

                PrepareCodeletsPrimaryKey(OtherTable,
                    out formalParametersOtherPrimaryKey,
                    out actualParametersOtherPrimaryKey,
                    out notUsed,
                    out notUsedInt);

                PrepareCodeletsPrimaryKey(ACurrentTable,
                    out notUsed,
                    out notUsed,
                    out notUsed,
                    out notUsedInt);

                string whereClauseForeignKey;
                string whereClauseViaOtherTable;
                string odbcParametersForeignKey;
                PrepareCodeletsForeignKey(
                    OtherTable,
                    OtherLinkConstraint,
                    out whereClauseForeignKey,
                    out whereClauseViaOtherTable,
                    out odbcParametersForeignKey,
                    out notUsedInt,
                    out notUsed);

                string whereClauseViaLinkTable;
                string whereClauseAllViaTables;
                PrepareCodeletsViaLinkTable(
                    (TConstraint)References[Count],
                    OtherLinkConstraint,
                    out whereClauseViaLinkTable,
                    out whereClauseAllViaTables);

                snippetLinkTable.SetCodelet("FORMALPARAMETERSOTHERPRIMARYKEY", formalParametersOtherPrimaryKey);
                snippetLinkTable.SetCodelet("ACTUALPARAMETERSOTHERPRIMARYKEY", actualParametersOtherPrimaryKey);
                snippetLinkTable.SetCodelet("ODBCPARAMETERSFOREIGNKEY", odbcParametersForeignKey);
                snippetLinkTable.SetCodelet("WHERECLAUSEVIALINKTABLE", whereClauseViaLinkTable);
                snippetLinkTable.SetCodelet("WHERECLAUSEALLVIATABLES", whereClauseAllViaTables);

                ASnippet.InsertSnippet("VIALINKTABLE", snippetLinkTable);

                Count = Count + 1;
            }
        }