internal static string BuildDeleteByFieldsQuery(string tableName, FieldDefinition[] fieldDefinitions)
        {
            StringBuilder deleteQuery = new StringBuilder();

            deleteQuery.AppendLine(BuildDeleteQuery(tableName));

            // add the where condition
            if (fieldDefinitions != null && fieldDefinitions.Length > 0) {
                deleteQuery.AppendLine(BuildWhereClause(fieldDefinitions));
            }

            return deleteQuery.ToString();
        }
 public FieldValue(FieldDefinition fieldDefinition, object value)
     : this(fieldDefinition)
 {
     m_Value = value;
 }
        public FieldValue(FieldDefinition fieldDefinition)
        {
            m_Dirty = false;
            m_Value = null;

            m_FieldDefinition = fieldDefinition;
        }
        internal static FieldDefinition[] CreatePrimaryKeysList(FieldDefinition[] fieldDefinitions)
        {
            List<FieldDefinition> pkList = new List<FieldDefinition>();

            // iterate through list of fieldDefainitions
            for (int index = 0; index < fieldDefinitions.Length; index++)
            {

                // Is this field a PrimaryKey
                if (fieldDefinitions[index].IsPrimaryKey)
                {

                    // incrment primaryKeyCount
                    pkList.Add(fieldDefinitions[index]);
                }

            }

            // return value
            return pkList.ToArray();
        }
        internal static string BuildWhereClause(FieldDefinition[] fieldDefinitions)
        {
            string whereClause = "WHERE ";

            for (int index = 0; index < fieldDefinitions.Length; index++)
            {

                // add this fieldDefinition to where clause.
                whereClause += m_FieldPrefix + fieldDefinitions[index].SourceColumnName + m_FieldSuffix
                    + " = @" + fieldDefinitions[index].Name + " And ";

            }

            if (whereClause.Length > 7)
            {
                whereClause = whereClause.Substring(0, whereClause.Length - 5);
            }
            else
            {
                whereClause = "";
            }

            return whereClause;
        }
 internal static string BuildSelectByFieldsQuery(string tableName, FieldDefinition[] selectFieldDefinitions, List<FieldValue> whereFieldValueList)
 {
     return BuildSelectAllQuery(tableName, selectFieldDefinitions)
             + BuildWhereClause(whereFieldValueList);
 }
        internal static string BuildSelectByFieldsQuery(string tableName, FieldDefinition[] fieldDefinitions)
        {
            // create PrimaryKeyDefinitions
            FieldDefinition[] pkFieldDefinitions = CreatePrimaryKeysList(fieldDefinitions);

            return BuildSelectAllQuery(tableName, fieldDefinitions)
                    + BuildWhereClause(pkFieldDefinitions);
        }
        internal static string BuildSelectAllWithOrderByFieldQuery(string tableName, FieldDefinition[] fieldDefinitions, FieldDefinition orderByField, bool ascending)
        {
            // Get root of BuildSelectAllQuery
            string query = BuildSelectAllQuery(tableName, fieldDefinitions);

            // create StringBuilder
            StringBuilder sb = new StringBuilder(query);

            // now append Order By Field
            sb.Append(" Order By ");

            // append FieldDefintion.Name
            sb.Append(orderByField.Name);

            // get direction
            string direction = " asc";

            // if descending
            if (!ascending)
            {
                direction = " desc";
            }

            // append direction
            sb.AppendLine(direction);

            // return value
            return sb.ToString();
        }
 internal static string BuildSelectAllQuery(string tableName, FieldDefinition[] fieldDefinitions)
 {
     return BuildPrefixedSelectAllQuery(tableName, null, fieldDefinitions);
 }
        internal static string BuildPrefixedSelectAllQuery(string tableName, string prefix, FieldDefinition[] fieldDefinitions)
        {
            StringBuilder query = new StringBuilder();

            query.Append("SELECT ");

            if (String.IsNullOrEmpty(prefix))
            {
                prefix = "";
            }
            else
            {
                prefix += ".";
            }

            for (int index = 0; index < fieldDefinitions.Length; index++)
            {

                query.Append(prefix + m_FieldPrefix
                    + fieldDefinitions[index].SourceColumnName
                    + m_FieldSuffix + ", ");

            }

            query.Remove(query.Length - 2, 2);
            query.AppendLine();

            query.AppendLine("FROM "
                + tableName);

            return query.ToString();
        }
        internal static string BuildOrderByClause(FieldDefinition[] fieldDefinitions)
        {
            string orderByClause = "";

            for (int index = 0; index < fieldDefinitions.Length; index++)
            {

                // add this fieldDefinition to where clause.
                orderByClause +=
                    m_FieldPrefix + fieldDefinitions[index].SourceColumnName + m_FieldSuffix + ", ";

            }

            if (orderByClause.Length > 0)
            {
                orderByClause =
                    "ORDER BY " + orderByClause.Substring(0, orderByClause.Length - 2);
            }

            return orderByClause;
        }
        internal static string BuildDeleteByPrimaryKeyQuery(string tableName, FieldDefinition[] fieldDefinitions)
        {
            FieldDefinition[] pkFieldDefinitionList = CreatePrimaryKeysList(fieldDefinitions);

            return BuildDeleteByFieldsQuery(tableName, pkFieldDefinitionList);
        }