|
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.