Example #1
0
        private IWhereClauseBuilder <TKey, TEntity> Render(SqlOperator @operator, Action write, Expression <Func <TEntity, object> > member, object value, object other)
        {
            string GetParameterName() => $"{this._dialectSettings.ParameterPrefix}{AppUtility.GetUniqueStringValue(10)}";
            void InsertParameter(string key, object obj) => this._parameters.Insert(key, obj);

            write?.Invoke(); // ???
            //Func<string> getParameterName = () => $"{this._dialectSettings.ParameterPrefix}{AppUtility.GetUniqueStringValue(10)}";
            //Action<string, object> insertParameter = (arg1, arg2) => this._parameters.Insert(arg1, arg2);

            string pn  = null;
            object pv  = null;
            string spn = null;
            object spv = null;

            string mn = $"{this._storeMapping.GetEscapeTableName(_classMap.TableName, this._dialectSettings)}.{this._storeMapping.GetEscapeColumnName(_classMap.Properties.Get(member.GetMemberName()).ColumnName, this._dialectSettings)}";

            switch (@operator)
            {
            case SqlOperator.Like:
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (value.GetType() != typeof(string))
                {
                    throw new InvalidOperationException();
                }

                pn = GetParameterName();
                pv = value;
                InsertParameter(pn, pv);
                this._builder.Append($"{mn} LIKE {pn}");

                break;

            case SqlOperator.NotLike:
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (value.GetType() != typeof(string))
                {
                    throw new InvalidOperationException();
                }

                pn = GetParameterName();
                pv = value;
                InsertParameter(pn, pv);
                this._builder.Append($"{mn} {this.NotStatement} LIKE {pn}");

                break;

            case SqlOperator.Between:
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (other == null)
                {
                    throw new ArgumentNullException(nameof(other));
                }
                if (!value.GetType().IsSimpleType())
                {
                    throw new InvalidOperationException();
                }
                if (!other.GetType().IsSimpleType())
                {
                    throw new InvalidOperationException();
                }
                if (value.GetType() != other.GetType())
                {
                    throw new InvalidOperationException();
                }

                pn  = GetParameterName();
                pv  = value;
                spn = GetParameterName();
                spv = other;
                InsertParameter(pn, pv);
                InsertParameter(spn, spv);
                this._builder.Append($"{mn} BETWEEN {pn} {this.AndStatement} {spn}");

                break;

            case SqlOperator.NotBetween:
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (other == null)
                {
                    throw new ArgumentNullException(nameof(other));
                }
                if (!value.GetType().IsSimpleType())
                {
                    throw new InvalidOperationException();
                }
                if (!other.GetType().IsSimpleType())
                {
                    throw new InvalidOperationException();
                }
                if (value.GetType() != other.GetType())
                {
                    throw new InvalidOperationException();
                }

                pn  = GetParameterName();
                pv  = value;
                spn = GetParameterName();
                spv = other;
                InsertParameter(pn, pv);
                InsertParameter(spn, spv);
                this._builder.Append($"{mn} {this.NotStatement} BETWEEN {pn} {this.AndStatement} {spn}");

                break;

            case SqlOperator.In:
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (
#if NetCore
                    value.GetType().GetTypeInfo().IsGenericType
#else
                    value.GetType().IsGenericType
#endif
                    )
                {
                    if (!typeof(IEnumerable).IsAssignableFrom(value.GetType().GetGenericTypeDefinition()))
                    {
                        throw new InvalidOperationException();
                    }
                    if (!value.GetType().GetGenericArguments().Single().IsSimpleType())
                    {
                        throw new InvalidOperationException();
                    }
                }
                else if (value.GetType().IsArray)
                {
                    if (!value.GetType().GetElementType().IsSimpleType())
                    {
                        throw new InvalidOperationException();
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }

                pn = GetParameterName();
                pv = value;
                InsertParameter(pn, pv);
                this._builder.Append($"{mn} IN {pn}");

                break;