Beispiel #1
0
        public SqlQueryBuilder Select(IEnumerable <SelectField> fields)
        {
            fields.AssertNotNull(nameof(fields));

            fields        = fields.Select(x => new SelectField(DAOHelper.EscapeField(x.Field), x.Alias));
            _selectFields = fields;
            return(this);
        }
Beispiel #2
0
        private string WhereCondition(string tableAlias, string fieldName, WhereOperator whereOperator, object value, bool skipIfNull = true)
        {
            tableAlias.AssertHasText(nameof(tableAlias));

            if (skipIfNull && IsNullOrEmptyString(value) && (whereOperator != WhereOperator.EqualTo && whereOperator != WhereOperator.EqualTo))
            {
                return(string.Empty);
            }

            string originalFieldName = fieldName;

            fieldName = DAOHelper.EscapeField(fieldName);

            switch (whereOperator)
            {
            case WhereOperator.EqualTo:
            case WhereOperator.NotEqualTo:

                if (!(IsNullOrEmptyString(value) && skipIfNull))
                {
                    SqlParameter paramNotEqualTo = _parametersManager.BuildSqlParameter(originalFieldName, value);
                    Parameters.Add(paramNotEqualTo);
                    if (IsNullOrEmptyString(value))
                    {
                        return($"{fieldName} IS {(whereOperator == WhereOperator.EqualTo ? string.Empty : "NOT ")}NULL");
                    }

                    return($"{fieldName} {WhereOperatorToSql(whereOperator)} {paramNotEqualTo.Name}");
                }

                break;

            case WhereOperator.GreaterThan:
            case WhereOperator.GreatherEqualThan:
            case WhereOperator.LessEqualThan:
            case WhereOperator.LessThan:

                SqlParameter paramLessThan = _parametersManager.BuildSqlParameter(originalFieldName, value);
                Parameters.Add(paramLessThan);
                return($"{fieldName} {WhereOperatorToSql(whereOperator)} {paramLessThan.Name}");

            case WhereOperator.In:
            case WhereOperator.NotIn:

                IEnumerable <object> valueList = value as IEnumerable <object>;

                if (valueList.IsNull())
                {
                    throw new ArgumentException("Invalid value provided for IN/NOT IN WhereOperator");
                }

                StringBuilder  chunk     = new StringBuilder($"{WhereOperatorToSql(whereOperator)}(");
                IList <string> chunkList = new List <string>();

                foreach (object valueItem in valueList)
                {
                    SqlParameter paramIn = _parametersManager.BuildSqlParameter(originalFieldName, value);
                    Parameters.Add(paramIn);
                    chunkList.Add($"{paramIn.Name}");
                }

                chunk.Append(chunkList.StringJoin(","));
                chunk.Append(")");

                return(chunk.ToString());

            case WhereOperator.Like:
                SqlParameter paramLike = _parametersManager.BuildSqlParameter(originalFieldName, value);
                Parameters.Add(paramLike);
                return($"{fieldName} {WhereOperatorToSql(whereOperator)} %{paramLike.Name}%");
            }

            return(string.Empty);
        }