Skip to content

Commit

Permalink
Refactored SQL serialisation for different database. Closes #64.
Browse files Browse the repository at this point in the history
The process of converting an SQL object into a string has been
completely separated so that it is clear how each database serializer
extends / modifies the default serialisation process. Also reorganised
the Misc.cs static class and moved to appropriate locations (most of it
was related to serialisation).
  • Loading branch information
hisystems committed Jul 24, 2012
1 parent 731b048 commit 98eb906
Show file tree
Hide file tree
Showing 76 changed files with 2,571 additions and 2,150 deletions.
26 changes: 25 additions & 1 deletion Database/Database.cs
Expand Up @@ -1476,6 +1476,30 @@ public ConnectionController Connection
return pobjConnection;
}
}

/// <summary>
/// All keys are returned in lower case.
/// </summary>
/// <exception cref="FormatException">If the connection string is in an invalid format.</exception>
protected static IDictionary<string, string> GetDictionaryFromConnectionString(string strConnectionString)
{
var objDictionary = new Dictionary<string, string>();
string[] strPropertyValueArray;

foreach (string strPropertyValue in strConnectionString.Split(';'))
{
if (!String.IsNullOrEmpty(strPropertyValue))
{
strPropertyValueArray = strPropertyValue.Split('=');
if (strPropertyValueArray.Length == 2)
objDictionary.Add(strPropertyValueArray[0].Trim().ToLower(), strPropertyValueArray[1].Trim());
else
throw new FormatException("Invalid key property definition for '" + strPropertyValue + "' from '" + strConnectionString + "'");
}
}

return objDictionary;
}

/// --------------------------------------------------------------------------------
/// <summary>
Expand Down Expand Up @@ -2044,7 +2068,7 @@ private static IDbConnection CreateConnection(string strConnectionString)
{
//Searches for an occurance of 'Provider='
//If found then it is assumed to be an OLEDB connection otherwise an ODBC connection
if (SQL.Misc.GetDictionaryFromConnectionString(strConnectionString).ContainsKey("provider"))
if (GetDictionaryFromConnectionString(strConnectionString).ContainsKey("provider"))
return new System.Data.OleDb.OleDbConnection(strConnectionString);
else
return new System.Data.Odbc.OdbcConnection(strConnectionString);
Expand Down
2 changes: 1 addition & 1 deletion Database/MicrosoftSQLServerDatabase.cs
Expand Up @@ -69,7 +69,7 @@ public string Name
/// <exception cref="FormatException">If the connection string is in an invalid format.</exception>
public static MicrosoftSQLServerDatabase Parse(string strConnectionString)
{
var objDictionary = SQL.Misc.GetDictionaryFromConnectionString(strConnectionString);
var objDictionary = GetDictionaryFromConnectionString(strConnectionString);

if (objDictionary.ContainsKey("integrated security"))
return new MicrosoftSQLServerDatabase(GetDataSource(objDictionary), GetDatabase(objDictionary));
Expand Down
12 changes: 9 additions & 3 deletions DatabaseObjects.csproj
Expand Up @@ -214,6 +214,15 @@
<Compile Include="SQL\Select\SQLSelectGetDate.cs" />
<Compile Include="SQL\Select\SQLSelectTableBase.cs" />
<Compile Include="SQL\Select\SQLSelectTableFromSelect.cs" />
<Compile Include="SQL\Serializers\MicrosoftSqlServerCompactEditionSerializer.cs" />
<Compile Include="SQL\Serializers\MicrosoftSqlServerSerializer.cs" />
<Compile Include="SQL\Serializers\HyperSqlSerializer.cs" />
<Compile Include="SQL\Serializers\MicrosoftAccessSerializer.cs" />
<Compile Include="SQL\Serializers\Serializer.cs" />
<Compile Include="SQL\Serializers\MicrosoftSerializer.cs" />
<Compile Include="SQL\Serializers\MySqlSerializer.cs" />
<Compile Include="SQL\Serializers\PervasiveSerializer.cs" />
<Compile Include="SQL\Serializers\Serializers.cs" />
<Compile Include="SQL\TableDefinition\SQLTableFields.cs" />
<Compile Include="SQL\TableDefinition\SQLAlterTable.cs" />
<Compile Include="SQL\SQLStatements.cs" />
Expand All @@ -223,9 +232,6 @@
<Compile Include="SQL\ISQLStatement.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="SQL\Misc.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="SQL\SQL.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
16 changes: 3 additions & 13 deletions SQL/Amend/SQLDelete.cs
Expand Up @@ -56,22 +56,12 @@ public SQLConditions Where
pobjConditions = value;
}
}

public override string SQL
{
get
get
{
string strSQL;

if (String.IsNullOrEmpty(TableName))
throw new Exceptions.DatabaseObjectsException("TableName property has not been set.");

strSQL = "DELETE FROM " + Misc.SQLConvertIdentifierName(this.TableName, this.ConnectionType);

if (pobjConditions != null && !pobjConditions.IsEmpty)
strSQL += " WHERE " + pobjConditions.SQL(this.ConnectionType);

return strSQL;
return base.Serializer.SerializeDelete(this);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions SQL/Amend/SQLFieldValues.cs
Expand Up @@ -13,7 +13,7 @@

namespace DatabaseObjects.SQL
{
public class SQLFieldValues : IEnumerable
public class SQLFieldValues : IEnumerable<SQLFieldValue>
{
protected List<SQLFieldValue> pobjFields = new List<SQLFieldValue>();

Expand Down Expand Up @@ -92,7 +92,12 @@ private bool Equals(SQLFieldValue fieldValue, string strFieldName)
return fieldValue.Name.Equals(strFieldName, StringComparison.InvariantCultureIgnoreCase);
}

public System.Collections.IEnumerator GetEnumerator()
IEnumerator<SQLFieldValue> IEnumerable<SQLFieldValue>.GetEnumerator()
{
return pobjFields.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return pobjFields.GetEnumerator();
}
Expand Down
37 changes: 3 additions & 34 deletions SQL/Amend/SQLInsert.cs
Expand Up @@ -48,43 +48,12 @@ public SQLFieldValues Fields
pobjFields = value;
}
}

public override string SQL
{
get
get
{
string strFields = string.Empty;
string strFieldValues = string.Empty;

if (String.IsNullOrEmpty(TableName))
throw new Exceptions.DatabaseObjectsException("TableName property has not been set.");

if (pobjFields.Count == 0)
throw new Exceptions.DatabaseObjectsException("Field values have not been set.");

for (int intIndex = 0; intIndex < pobjFields.Count; intIndex++)
{
strFields += Misc.SQLConvertIdentifierName(pobjFields[intIndex].Name, this.ConnectionType);
if (intIndex != pobjFields.Count - 1)
strFields += ",";
}

SQLFieldValue objField;

for (int intIndex = 0; intIndex < pobjFields.Count; intIndex++)
{
objField = pobjFields[intIndex];

if (objField.Value is SQLExpression)
strFieldValues += ((SQLExpression) objField.Value).SQL(this.ConnectionType);
else
strFieldValues += Misc.SQLConvertValue(pobjFields[intIndex].Value, this.ConnectionType);

if (intIndex != pobjFields.Count - 1)
strFieldValues += ",";
}

return "INSERT INTO " + Misc.SQLConvertIdentifierName(this.TableName, this.ConnectionType) + " " + "(" + strFields + ") VALUES (" + strFieldValues + ")";
return base.Serializer.SerializeInsert(this);
}
}
}
Expand Down
26 changes: 3 additions & 23 deletions SQL/Amend/SQLInsertFromSelect.cs
Expand Up @@ -106,32 +106,12 @@ public SQLSelect Source
pobjSourceSelect = value;
}
}

public override string SQL
{
get
get
{
string strFields = string.Empty;

if (String.IsNullOrEmpty(TableName))
throw new Exceptions.DatabaseObjectsException("TableName property has not been set.");

if (pobjInsertIntoFields.Count > 0)
{
for (int intIndex = 0; intIndex < pobjInsertIntoFields.Count; intIndex++)
{
strFields += Misc.SQLConvertIdentifierName(System.Convert.ToString(pobjInsertIntoFields[intIndex]), this.ConnectionType);
if (intIndex != pobjInsertIntoFields.Count - 1)
{
strFields += ", ";
}
}
strFields = "(" + strFields + ") ";
}

pobjSourceSelect.ConnectionType = base.ConnectionType;

return "INSERT INTO " + Misc.SQLConvertIdentifierName(this.TableName, this.ConnectionType) + " " + strFields + pobjSourceSelect.SQL;
return base.Serializer.SerializeInsertFromSelect(this);
}
}
}
Expand Down
39 changes: 3 additions & 36 deletions SQL/Amend/SQLUpdate.cs
Expand Up @@ -77,45 +77,12 @@ public SQLConditions Where
pobjConditions = value;
}
}

public override string SQL
{
get
get
{
string strSQL;
string strFieldValues = string.Empty;

if (String.IsNullOrEmpty(TableName))
throw new Exceptions.DatabaseObjectsException("TableName property has not been set.");
else if (pobjFields.Count == 0)
throw new Exceptions.DatabaseObjectsException("Field values have not been set.");

SQLFieldValue objField;

for (int intIndex = 0; intIndex < pobjFields.Count; intIndex++)
{
objField = pobjFields[intIndex];
//Check the field name has been set. Can't really check whether the value has been set or not.
if (String.IsNullOrEmpty(objField.Name))
throw new Exceptions.DatabaseObjectsException("Field Name has not been set.");

if (objField is SQLUpdateField)
strFieldValues += Misc.SQLConvertIdentifierName(objField.Name, this.ConnectionType) + " = " + ((SQLUpdateField) objField).Value.SQL(this.ConnectionType);
else if (objField is SQLFieldValue)
strFieldValues += Misc.SQLConvertIdentifierName(objField.Name, this.ConnectionType) + " = " + Misc.SQLConvertValue(objField.Value, this.ConnectionType);
else
throw new NotSupportedException(objField.GetType().Name);

if (intIndex != pobjFields.Count - 1)
strFieldValues += ", ";
}

strSQL = "UPDATE " + Misc.SQLConvertIdentifierName(this.TableName, this.ConnectionType) + " " + "SET " + strFieldValues;

if (pobjConditions != null && !pobjConditions.IsEmpty)
strSQL += " WHERE " + pobjConditions.SQL(this.ConnectionType);

return strSQL;
return base.Serializer.SerializeUpdate(this);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion SQL/Expressions/SQLAggregateExpression.cs
Expand Up @@ -13,14 +13,30 @@ namespace DatabaseObjects.SQL
{
public class SQLAggregateExpression : SQLFunctionExpression
{
private AggregateFunction aggregate;

public SQLAggregateExpression(AggregateFunction aggregate, string fieldName)
: this(aggregate, new SQLFieldExpression(fieldName))
{
}

public SQLAggregateExpression(AggregateFunction aggregate, SQLExpression expression)
: base(Misc.SQLConvertAggregate(aggregate), expression)
: base(expression)
{
this.aggregate = aggregate;
}

public AggregateFunction Aggregate
{
get
{
return aggregate;
}
}

internal override string SQL(Serializers.Serializer serializer)
{
return serializer.SerializeAggregateExpression(this);
}
}
}
4 changes: 2 additions & 2 deletions SQL/Expressions/SQLAllFieldsExpression.cs
Expand Up @@ -41,12 +41,12 @@ public SQLSelectTable Table
}
}

internal override string SQL(Database.ConnectionType eConnectionType)
internal override string SQL(Serializers.Serializer serializer)
{
string strSQL = string.Empty;

if (pobjTable != null)
strSQL += Misc.SQLTablePrefix(pobjTable, eConnectionType) + ".";
strSQL += serializer.SerializeTablePrefix(pobjTable) + ".";

return strSQL + "*";
}
Expand Down
25 changes: 2 additions & 23 deletions SQL/Expressions/SQLArithmeticExpression.cs
Expand Up @@ -92,30 +92,9 @@ public ArithmeticOperator Operator
}
}

internal override string SQL(Database.ConnectionType eConnectionType)
internal override string SQL(Serializers.Serializer serializer)
{
if (pobjLeft == null)
throw new ArgumentNullException(this.GetType().Name + ".LeftExpression");
else if (pobjRight == null)
throw new ArgumentNullException(this.GetType().Name + ".RightExpression");

return "(" + pobjLeft.SQL(eConnectionType) + " " + OperatorString(peOperator) + " " + pobjRight.SQL(eConnectionType) + ")";
}

private string OperatorString(SQL.ArithmeticOperator eOperator)
{
if (eOperator == ArithmeticOperator.Add)
return "+";
else if (eOperator == ArithmeticOperator.Subtract)
return "-";
else if (eOperator == ArithmeticOperator.Divide)
return "/";
else if (eOperator == ArithmeticOperator.Multiply)
return "*";
else if (eOperator == ArithmeticOperator.Modulus)
return "%";
else
throw new NotSupportedException(typeof(SQL.ArithmeticOperator).Name);
return serializer.SerializeArithmeticExpression(this);
}
}
}
19 changes: 2 additions & 17 deletions SQL/Expressions/SQLBitwiseExpression.cs
Expand Up @@ -89,24 +89,9 @@ public BitwiseOperator Operator
}
}

internal override string SQL(Database.ConnectionType eConnectionType)
internal override string SQL(Serializers.Serializer serializer)
{
if (pobjLeft == null)
throw new ArgumentNullException(this.GetType().Name + ".LeftExpression");
else if (pobjRight == null)
throw new ArgumentNullException(this.GetType().Name + ".RightExpression");

return "(" + pobjLeft.SQL(eConnectionType) + " " + OperatorString(peOperator) + " " + pobjRight.SQL(eConnectionType) + ")";
}

private string OperatorString(SQL.BitwiseOperator eOperator)
{
if (eOperator == BitwiseOperator.And)
return "&";
else if (eOperator == BitwiseOperator.Or)
return "|";
else
throw new NotSupportedException(typeof(SQL.BitwiseOperator).Name);
return serializer.SerializeBitwiseExpression(this);
}
}
}

0 comments on commit 98eb906

Please sign in to comment.