Click here to Skip to main content
15,891,748 members
Articles / Programming Languages / C#

Customize an application with XML fragments

Rate me:
Please Sign up or sign in to vote.
1.44/5 (3 votes)
22 May 20073 min read 28.3K   124   10  
How to customize an application using XML fragments
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;

namespace Bulasoft.Common.SchemaGenerators
{
    internal abstract class SchemaGeneratorBase
    {
        private List<TableDescriptor> _tables;
        protected List<TableDescriptor> Tables
        {
            get { return _tables; }
        }

        private DbConnection _connection;
        protected DbConnection Connection
        {
            get { return _connection; }
        }


        protected internal bool generateScript;
        protected internal StringBuilder script;
        protected internal virtual void Generate(List<TableDescriptor> tables, DbConnection connection, string schema)
        {
            generateScript = false;
            _connection = connection;
            _tables = tables;
            GenerateTables();
            GenerateFK();
        }

        protected internal virtual string GenerateScript(List<TableDescriptor> tables, DbConnection connection, string schema)
        {
            generateScript = true;
            script = new StringBuilder();
            _connection = connection;
            _tables = tables;
            GenerateTables();
            GenerateFK();
            return script.ToString();
        }

        protected internal void AppendText(string text)
        {
            script.Append(text);
            script.Append(Environment.NewLine);
            script.Append(Environment.NewLine);
        }

        protected virtual List<string> GetFKList()
        {
            DataTable allConstraints = Connection.GetSchema("ForeignKeys");
            List<string> result = new List<string>();
            foreach (DataRow dr in allConstraints.Rows)
                result.Add(dr["constraint_name"].ToString());

            return result;
        }

        protected abstract string GenerateFKCommand(TableDescriptor table, FieldDescriptor field);

        protected virtual void GenerateFK()
        {
            List<string> fkList = GetFKList();
            foreach (TableDescriptor table in Tables)
                foreach (FieldDescriptor field in table.Fields)
                    if (field.IsFK && (fkList.IndexOf(table.GetFKName(field)) == -1))
                    {
                        string commandText = GenerateFKCommand(table, field);
                        ProcessCommand(commandText);
                        //DbCommand cmd = Connection.CreateCommand();
                        //cmd.CommandText = 
                        //cmd.ExecuteNonQuery();
                    }
        }


        protected abstract FieldDescriptorList GetFieldDescriptorList(string tableName);

        protected virtual List<string> GetIndexList(string tableName)
        {
            List<string> result = new List<string>();
            foreach (DataRow dr in allIndexes.Rows)
            {
                if (dr["table_name"].ToString() == tableName)
                    result.Add(dr["column_name"].ToString());
            }
            return result;
        }

        protected DataTable allColumns;
        protected DataTable allIndexes;
        protected virtual void GenerateTables()
        {
            allColumns = Connection.GetSchema("Columns");
            allIndexes = Connection.GetSchema("IndexColumns");
            foreach (TableDescriptor tableDescriptor in Tables)
                GenerateTable(tableDescriptor);
        }

        protected abstract string GenerateTableCommand(TableDescriptor tableDescriptor, FieldDescriptorList fields);

        protected virtual void GenerateTable(TableDescriptor tableDescriptor)
        {
            FieldDescriptorList fields = GetFieldDescriptorList(tableDescriptor.Name);

            string commandText = GenerateTableCommand(tableDescriptor, fields);

            ProcessCommand(commandText);

            GenerateIndexes(tableDescriptor);
        }

        private void ProcessCommand(string commandText)
        {
            if (!string.IsNullOrEmpty(commandText))
            {
                if (generateScript)
                    AppendText(commandText);
                else
                    ExecuteCommand(commandText);
            }
        }

        private void ExecuteCommand(string commandText)
        {
            DbTransaction t = Connection.BeginTransaction();
            DbCommand cmd = Connection.CreateCommand();
            cmd.Transaction = t;
            cmd.CommandText = commandText.ToString();
            cmd.ExecuteNonQuery();
            t.Commit();
        }

        protected abstract string GenerateIndexCommand(TableDescriptor table, FieldDescriptor field);

        protected virtual void GenerateIndexes(TableDescriptor table)
        {
            List<string> indexes = GetIndexList(table.Name);

            foreach (FieldDescriptor field in table.Fields)
            {
                if ((field.IsIndexed || field.IsFK) && (indexes.IndexOf(field.Name) == -1))
                {
                    string commandText = GenerateIndexCommand(table, field);
                    ProcessCommand(commandText);
                    //DbCommand cmd = Connection.CreateCommand();
                    //cmd.CommandText = 
                    //cmd.ExecuteNonQuery();
                }
            }
        }

        protected abstract string FieldToSQL(FieldDescriptor field);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions