Click here to Skip to main content
15,881,769 members
Articles / Web Development / HTML

Database Object Model

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
21 Feb 2012CPOL2 min read 25.9K   693   11  
A Sample ORM project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;

namespace DBOMSqlSharp
{
    public class SqlArithmetic : SqlExpression
    {
        SqlExpression left;
        SqlExpression right;

        protected SqlArithmetic()
            : base()
        {
        }
        protected SqlArithmetic(SqlExpression left, SqlExpression right)
            : this()
        {
            this.left = left;
            this.right = right;
        }

        public override object Clone()
        {
            SqlArithmetic arithmetic = (SqlArithmetic)this.MemberwiseClone();
            return arithmetic;
        }
        public override bool Equals(BaseSqlObject other)
        {
            bool b = base.Equals(other);
            SqlArithmetic o = (SqlArithmetic)other;
            return b
                && SqlComparer.EqualObjects(this.Left, o.Left)
                && SqlComparer.EqualObjects(this.Right, o.Right);
        }

        public SqlExpression Left
        {
            get
            {
                return this.left;
            }
        }
        public SqlExpression Right
        {
            get
            {
                return this.right;
            }
        }

        public virtual string Operator
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public override List<SqlParameter> SqlParameters
        {
        	get
        	{
                return BaseSqlObject.SqlParametersOf(this.Left, this.Right);
            }
        }
        public override SqlTreeNode SqlNode
        {
            get
            {
                SqlTreeNode n = new SqlTreeNode(null, this);
                this.Nodes.Add(n);
                n.AddChild(this.Left.SqlNode);
                n.AddChild(this.Right.SqlNode);
                return n;
            }
        }
        public override string SqlString
        {
            get
            {
                this.OnSqlStringIn(this, EventArgs.Empty);
                string s1 = this.Left.SqlString;
                if (this.TypeIn(typeof(SqlMultiply), typeof(SqlDivide)) && this.Left.TypeIn(typeof(SqlAdd), typeof(SqlSubtract)))
                    s1 = s1.InRoundBrackets();

                string s2 = this.Right.SqlString;
                if (
                    (this.GetType() == typeof(SqlSubtract) && this.Left.GetType() == typeof(SqlSubtract))
                    || (this.GetType() == typeof(SqlMultiply) && this.Right.TypeIn(typeof(SqlAdd), typeof(SqlSubtract)))
                    || (this.GetType() == typeof(SqlDivide) && this.Right.TypeIn(typeof(SqlAdd), typeof(SqlSubtract), typeof(SqlMultiply))))
                    s2 = s2.InRoundBrackets();

                string s = s1 + " " + this.Operator + " " + s2;
                this.OnSqlStringOut(this, EventArgs.Empty);
                return s;
            }
        }
        public override string HtmlString
        {
            get
            {
                return "";
            }
        }
    }

    public class SqlAdd : SqlArithmetic
    {
        public SqlAdd(SqlExpression left, SqlExpression right)
            : base(left, right)
        {
        }

        public override string Operator
        {
            get
            {
                return "+";
            }
        }
    }

    public class SqlSubtract : SqlArithmetic
    {
        public SqlSubtract(SqlExpression left, SqlExpression right)
            : base(left, right)
        {
        }

        public override string Operator
        {
            get
            {
                return "-";
            }
        }
    }

    public class SqlMultiply : SqlArithmetic
    {
        public SqlMultiply(SqlExpression left, SqlExpression right)
            : base(left, right)
        {
        }

        public override string Operator
        {
            get
            {
                return "*";
            }
        }
    }

    public class SqlDivide : SqlArithmetic
    {
        public SqlDivide(SqlExpression left, SqlExpression right)
            : base(left, right)
        {
        }

        public override string Operator
        {
            get
            {
                return "/";
            }
        }
    }

    public class SqlModulo : SqlArithmetic
    {
        public SqlModulo(SqlExpression left, SqlExpression right)
            : base(left, right)
        {
        }

        public override string Operator
        {
            get
            {
                return "%";
            }
        }
    }

}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions