Esempio n. 1
0
        /// <summary>
        ///   <para>Convert a database structure into various enumerations.</para>
        /// </summary>
        /// <param name="databaseName">The database name for which to generate the code.</param>
        /// <remarks>This method overwrites the contents of the &lt;databaseName&gt;.cs file.
        ///          The goal of this approach is to easily handle schema changes without having
        ///          to hunt through code in order to locate strings that now point to obsolete
        ///          names.  Instead those will now generate compile-time errors.</remarks>
        public void makeDatabaseVariables()
        {
            String query;
            String tableName;
            String tableEnum;
            String columnAlignedPosition;

            DataTable dataTable;

            dataTable = executeQuery("SELECT DISTINCT table_name "
                                     + "FROM information_schema.columns "
                                     + "WHERE table_schema = '"
                                     + DATABASE_NAME
                                     + "'", null, null);

            System.IO.FileInfo     fileInfo     = new System.IO.FileInfo("..\\..\\..\\..\\CommonTools\\Database\\" + DATABASE_NAME + ".cs");
            System.IO.StreamWriter streamWriter = fileInfo.CreateText();

            streamWriter.WriteLine("// This file was automatically generated by Database.makeDatabaseVariables() on "
                                   + DateTime.Now.Year.ToString() + "/"
                                   + DateTime.Now.Month.ToString() + "/"
                                   + DateTime.Now.Day.ToString() + " "
                                   + DateTime.Now.Hour.ToString() + ":"
                                   + DateTime.Now.Minute.ToString() + ":"
                                   + DateTime.Now.Second.ToString());

            streamWriter.WriteLine("namespace CommonTools.Database");
            streamWriter.WriteLine("{");
            streamWriter.WriteLine("    public static class " + DATABASE_NAME);
            streamWriter.WriteLine("    {");

            tableEnum             = "        public enum Tables { ";
            columnAlignedPosition = new String(' ', tableEnum.Length);
            streamWriter.Write(tableEnum);
            DataRow dataRow;

            for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; ++rowIndex)
            {
                dataRow   = dataTable.Rows[rowIndex];
                tableName = dataRow["table_name"].ToString();
                if (rowIndex != dataTable.Rows.Count - 1)
                {
                    tableName += ", ";
                }
                streamWriter.Write(tableName);
                if (rowIndex != 0 && (rowIndex % 5) == 0)
                {
                    streamWriter.WriteLine("");
                    streamWriter.Write(columnAlignedPosition);
                }
            }
            streamWriter.WriteLine(" };");
            streamWriter.WriteLine("");

            query = "SELECT table_name, column_name, data_type "
                    + "FROM information_schema.columns "
                    + "WHERE table_schema = '"
                    + DATABASE_NAME
                    + "' "
                    + "ORDER BY table_name, ordinal_position";
            makeDatabaseEnum(streamWriter, query, columnAlignedPosition, false, true);

            query = "SELECT "
                    + "'item_type' as 'table_name', "
                    + "name as 'column_name', "
                    + "id as 'data_type' "
                    + "FROM "
                    + Net7.Tables.item_type.ToString()
                    + " ORDER BY "
                    + ColumnData.GetName(Net7.Table_item_type._id);
            makeDatabaseEnum(streamWriter, query, columnAlignedPosition, true, true);

            streamWriter.WriteLine("    }");
            streamWriter.WriteLine("}");
            streamWriter.Close();
        }