Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Data Access » Downloads
 
Add your own
alternative version

Abstract Data Access Layer Design

, 5 Sep 2009
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.
demoproject.zip
DemoProject
Test
(ClassDiagram1).cd
bin
Debug
Test.exe
Test.vshost.exe
Test.vshost.exe.manifest
XPeriment.Persistence.dll
Properties
Settings.settings
Test.csproj.user
XPeriment.Architect
bin
Debug
BizAgi.Architect.vshost.exe
BizAgi.Architect.vshost.exe.manifest
Experiment.Architect.exe
Experiment.Architect.vshost.exe
Experiment.Architect.vshost.exe.manifest
Properties
Settings.settings
XPeriment.Management.suo
XPeriment.Persistence
(ClassDiagram).cd
bin
Debug
XPeriment.Persistence.dll
DataAcessTasks
(ClassDiagram).cd
(ClassDiagram1).cd
SqlServer
PersistentObjects
(ClassDiagram).cd
Attributes
Properties
Settings.settings
XPeriment.Persistence.csproj.user
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)

About the Author

Anwar Ibáñez O'Kamell
Systems Engineer
Colombia Colombia
http://www.construirsoftware.blogspot.com/

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 5 Sep 2009
Article Copyright 2009 by Anwar Ibáñez O'Kamell
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid