Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / SQL

Abstract Data Access Layer Design

Rate me:
Please Sign up or sign in to vote.
4.85/5 (16 votes)
5 Sep 2009CPOL4 min read 72.9K   1.2K   68  
The present document tries to describe the architecture of a specific layer of access to data for relational databases. This document tries to present/display a form to automate tasks of access to data.
using System;
using System.Collections.Generic;
using System.Text;

namespace XPeriment.DataAccessTasks
{
    public class ScriptBuilder
    {
        public virtual string FormatValue(object obj)
        {
            if (obj is string)
                return "'" + obj.ToString() + "'";
            else if (obj is DateTime)
                return "'" + obj.ToString() + "'";
            else if (obj is Guid)
                return "'" + obj.ToString() + "'";
            else if (obj is bool)
                return (((bool)obj) == true)?"1":"0";
            else if (obj == null)
                return " NULL ";
            else if (obj == DBNull.Value)
                return " NULL ";
            else
                return obj.ToString();
        }
        
        public string GetScript(QueryTask query)
        {
            string retval = "SELECT ";
            if (query.Fields.Count == 0)
                retval += " * ";

            int count = 0;
            foreach(FieldData fd in query.Fields.Values)
            {
                retval += " " + fd.Name;
                if (count < query.Fields.Count - 1)
                    retval += " , ";
                count++;
            }

            retval += " FROM " + query.BaseTable;

            foreach (JoinData jd in query.Joins)
                retval += " " + jd.JoinType + " JOIN " + jd.TargetTable + " ON " + jd.Condition;

            if (query.Condition != string.Empty)
                retval += " WHERE " + query.Condition;

            return retval;
        }

        public string GetScript(InsertTask task)
        {
            string retval = "INSERT INTO " + task.BaseTable;
            retval += " (";
            int fieldcount = 0;
            foreach(string key in task.Fields.Keys)
            {
                retval += key;
                if (fieldcount < task.Fields.Count - 1)
                    retval += ",";
                fieldcount++;
            }
            retval += ") VALUES (";

            fieldcount = 0;
            foreach (string key in task.Fields.Keys)
            {
                retval += FormatValue((task.Fields[key] as FieldValueData).Value);
                if (fieldcount < task.Fields.Count - 1)
                    retval += ",";
                fieldcount++;
            }

            retval += ")";
            return retval;

        }

        public string GetScript(DeleteTask delete)
        {
            string retval = "DELETE " + delete.BaseTable + " WHERE " + delete.Condition ;
            return retval;
        }

        public string GetScript(UpdateTask update)
        {
            string retval = "UPDATE " + update.BaseTable;
            retval += " SET ";
            int count = 0;
            foreach (FieldData fd in update.Fields.Values)
            {
                FieldValueData fvd = fd as FieldValueData;
                retval += " " + fvd.Name + " = " + FormatValue(fvd.Value);
                if (count < update.Fields.Count - 1)
                    retval += ",";
                count++;
            }
            retval += " WHERE " + update.Condition;
            return retval;
        }
    }
}

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
Systems Engineer
Colombia Colombia
http://www.construirsoftware.blogspot.com/

Comments and Discussions