Skip to content

Commit

Permalink
Refactored collection classes. Closes #63.
Browse files Browse the repository at this point in the history
Maintenance work to update some of the "collection" classes to utilise
LINQ and simplify the code.
  • Loading branch information
hisystems committed Jul 23, 2012
1 parent af1a55f commit 731b048
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 58 deletions.
23 changes: 8 additions & 15 deletions SQL/Amend/SQLFieldValues.cs
Expand Up @@ -8,12 +8,14 @@
using System.Collections;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;

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

public SQLFieldValues()
{
Expand Down Expand Up @@ -50,8 +52,8 @@ public virtual void Add(SQLFieldValues objFieldValues)
{
if (!this.Exists(strFieldName))
throw new ArgumentException("Field '" + strFieldName + "' does not exist.");
return (SQLFieldValue)pobjFields[FieldNameIndex(strFieldName)];

return pobjFields.First(field => Equals(field, strFieldName));
}
}

Expand All @@ -73,7 +75,7 @@ public int Count

public bool Exists(string strFieldName)
{
return FieldNameIndex(strFieldName) >= 0;
return pobjFields.FirstOrDefault(field => Equals(field, strFieldName)) != null;
}

public void Delete(ref SQLFieldValue objFieldValue)
Expand All @@ -85,18 +87,9 @@ public void Delete(ref SQLFieldValue objFieldValue)
objFieldValue = null;
}

internal int FieldNameIndex(string strFieldName)
private bool Equals(SQLFieldValue fieldValue, string strFieldName)
{
SQLFieldValue objSQLFieldValue;

for (int intIndex = 0; intIndex < this.Count; intIndex++)
{
objSQLFieldValue = (SQLFieldValue) (pobjFields[intIndex]);
if (string.Compare(strFieldName, objSQLFieldValue.Name, true) == 0)
return intIndex;
}

return -1;
return fieldValue.Name.Equals(strFieldName, StringComparison.InvariantCultureIgnoreCase);
}

public System.Collections.IEnumerator GetEnumerator()
Expand Down
21 changes: 8 additions & 13 deletions SQL/Select/SQLSelectOrderByFields.cs
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;

namespace DatabaseObjects.SQL
{
Expand Down Expand Up @@ -76,7 +77,10 @@ public SQLSelectOrderByField Add(string strFieldName, SQL.AggregateFunction eAgg
{
get
{
return this[FieldNameIndex(strFieldName)];
if (!Exists(strFieldName))
throw new ArgumentException(strFieldName + " does not exist");

return this.Single(field => Equals(field, strFieldName));
}
}

Expand Down Expand Up @@ -112,7 +116,7 @@ internal string SQL(Database.ConnectionType eConnectionType)

public bool Exists(string strFieldName)
{
return FieldNameIndex(strFieldName) >= 0;
return this.SingleOrDefault(field => Equals(field, strFieldName)) != null;
}

public void Delete(ref SQLSelectOrderByField objOrderByField)
Expand All @@ -130,18 +134,9 @@ public void OrderingReverseAll()
objOrderBy.OrderingReverse();
}

private int FieldNameIndex(string strFieldName)
private bool Equals(SQLSelectOrderByField field, string strFieldName)
{
SQLSelectOrderByField objOrderByField;

for (int intIndex = 0; intIndex < this.Count; intIndex++)
{
objOrderByField = (SQLSelectOrderByField) (pobjOrderByFields[intIndex]);
if (string.Compare(strFieldName, objOrderByField.Name, true) == 0)
return intIndex;
}

return -1;
return field.Name.Equals(strFieldName, StringComparison.InvariantCultureIgnoreCase);
}

public System.Collections.IEnumerator GetEnumerator()
Expand Down
21 changes: 8 additions & 13 deletions SQL/Select/SQLSelectTables.cs
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;

namespace DatabaseObjects.SQL
{
Expand Down Expand Up @@ -84,7 +85,10 @@ public void Add(SQL.SQLSelectTableFromSelect objTable)
{
get
{
return this[TableNameIndex(strTableName)];
if (!Exists(strTableName))
throw new ArgumentException(strTableName + " does not exist");

return pobjTables.Single(table => Equals(table, strTableName));
}
}

Expand Down Expand Up @@ -119,7 +123,7 @@ public SQLSelectTableJoins Joins

public bool Exists(string strTableName)
{
return TableNameIndex(strTableName) >= 0;
return pobjTables.SingleOrDefault(table => Equals(table, strTableName)) != null;
}

public void Delete(ref SQLSelectTable objTable)
Expand Down Expand Up @@ -167,18 +171,9 @@ internal string SQL(Database.ConnectionType eConnectionType)
return strSQL;
}

private int TableNameIndex(string strTableName)
private bool Equals(SQLSelectTable table, string strTableName)
{
SQLSelectTable objTable;

for (int intIndex = 0; intIndex < this.Count; intIndex++)
{
objTable = (SQLSelectTable)pobjTables[intIndex];
if (string.Compare(strTableName, objTable.Name, true) == 0)
return intIndex;
}

return -1;
return table.Name.Equals(strTableName, StringComparison.InvariantCultureIgnoreCase);
}

public System.Collections.IEnumerator GetEnumerator()
Expand Down
30 changes: 13 additions & 17 deletions SQL/TableDefinition/SQLTableFields.cs
Expand Up @@ -101,17 +101,14 @@ public void Add(SQLTableField objField)
{
EnsureAlterModeValid(AlterModeType.Modify);

SQLTableField tableField;
var intIndex = FieldNameIndex(strFieldName);
var tableField = GetTableFieldOrDefault(strFieldName);

if (intIndex == -1)
if (tableField == null)
{
tableField = new SQLTableField();
tableField.Name = strFieldName;
pobjFields.Add(tableField);
}
else
tableField = (SQLTableField)(pobjFields[intIndex]);

return tableField;
}
Expand All @@ -125,21 +122,20 @@ public void Drop(string strFieldName)

objField.Name = strFieldName;

if (FieldNameIndex(strFieldName) == -1)
pobjFields.Add(objField);
else
throw new ArgumentException("Field '" + strFieldName + "' already exists");
if (GetTableFieldOrDefault(strFieldName) != null)
throw new ArgumentException("Field '" + strFieldName + "' already exists");
pobjFields.Add(objField);
}

private SQLTableField GetTableFieldOrDefault(string strFieldName)
{
return pobjFields.Where(field => field is SQLTableField).Cast<SQLTableField>().SingleOrDefault(field => Equals(field, strFieldName));
}

private int FieldNameIndex(string strFieldName)
private bool Equals(SQLTableField tableField, string strFieldName)
{
for (int intIndex = 0; intIndex < pobjFields.Count; intIndex++)
{
if (string.Compare(((SQLTableField)(pobjFields[intIndex])).Name, strFieldName, true) == 0)
return intIndex;
}

return -1;
return tableField.Name.Equals(strFieldName, StringComparison.InvariantCultureIgnoreCase);
}

/// <summary>
Expand Down

0 comments on commit 731b048

Please sign in to comment.