protected DeleteableProvider <T> CreateDeleteable <T>() where T : class, new()
        {
            var result     = InstanceFactory.GetDeleteableProvider <T>(this.CurrentConnectionConfig);
            var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig);;

            result.Context                             = this;
            result.SqlBuilder                          = sqlBuilder;
            sqlBuilder.DeleteBuilder                   = result.DeleteBuilder = InstanceFactory.GetDeleteBuilder(this.CurrentConnectionConfig);
            sqlBuilder.DeleteBuilder.Builder           = sqlBuilder;
            sqlBuilder.DeleteBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
            sqlBuilder.Context                         = result.SqlBuilder.DeleteBuilder.Context = this;
            return(result);
        }
        protected IDbLiteQueryable <T> CreateQueryable <T>(IDbLiteQueryable <T> result)
        {
            Check.Exception(typeof(T).IsClass() == false || typeof(T).GetConstructors().Length == 0, "Queryable<{0}> Error ,{0} is invalid , need is a class,and can new().", typeof(T).Name);
            var sqlBuilder = InstanceFactory.GetSqlbuilder(CurrentConnectionConfig);

            result.Context    = this.Context;
            result.SqlBuilder = sqlBuilder;
            result.SqlBuilder.QueryBuilder         = InstanceFactory.GetQueryBuilder(CurrentConnectionConfig);
            result.SqlBuilder.QueryBuilder.Builder = sqlBuilder;
            result.SqlBuilder.Context = result.SqlBuilder.QueryBuilder.Context = this;
            result.SqlBuilder.QueryBuilder.EntityType        = typeof(T);
            result.SqlBuilder.QueryBuilder.EntityName        = typeof(T).Name;
            result.SqlBuilder.QueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(CurrentConnectionConfig);
            return(result);
        }
        protected InsertableProvider <T> CreateInsertable <T>(T[] insertObjs) where T : class, new()
        {
            var result     = InstanceFactory.GetInsertableProvider <T>(this.CurrentConnectionConfig);
            var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig);;

            result.Context                             = this;
            result.EntityInfo                          = this.Context.EntityMaintenance.GetEntityInfo <T>();
            result.SqlBuilder                          = sqlBuilder;
            result.InsertObjs                          = insertObjs;
            sqlBuilder.InsertBuilder                   = result.InsertBuilder = InstanceFactory.GetInsertBuilder(this.CurrentConnectionConfig);
            sqlBuilder.InsertBuilder.Builder           = sqlBuilder;
            sqlBuilder.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
            sqlBuilder.Context                         = result.SqlBuilder.InsertBuilder.Context = this;
            result.Init();
            return(result);
        }
        protected UpdateableProvider <T> CreateUpdateable <T>(T[] UpdateObjs) where T : class, new()
        {
            var result     = InstanceFactory.GetUpdateableProvider <T>(this.CurrentConnectionConfig);
            var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig);;

            result.Context                             = this;
            result.EntityInfo                          = this.Context.EntityMaintenance.GetEntityInfo <T>();
            result.SqlBuilder                          = sqlBuilder;
            result.UpdateObjs                          = UpdateObjs;
            sqlBuilder.UpdateBuilder                   = result.UpdateBuilder = InstanceFactory.GetUpdateBuilder(this.CurrentConnectionConfig);
            sqlBuilder.UpdateBuilder.Builder           = sqlBuilder;
            sqlBuilder.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
            sqlBuilder.Context                         = result.SqlBuilder.UpdateBuilder.Context = this;
            result.Init();
            var ignoreColumns = result.EntityInfo.Columns.Where(it => it.IsOnlyIgnoreUpdate).ToList();

            if (ignoreColumns != null && ignoreColumns.Any())
            {
                result = (UpdateableProvider <T>)result.IgnoreColumns(ignoreColumns.Select(it => it.PropertyName).ToArray());
            }
            return(result);
        }
Exemple #5
0
        public KeyValuePair <string, DbLiteParameter[]> ConditionalModelToSql(List <IConditionalModel> models, int beginIndex = 0)
        {
            if (models.IsNullOrEmpty())
            {
                return(new KeyValuePair <string, DbLiteParameter[]>());
            }
            StringBuilder          builder    = new StringBuilder();
            List <DbLiteParameter> parameters = new List <DbLiteParameter>();
            var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);

            foreach (var model in models)
            {
                if (model is ConditionalModel)
                {
                    var item  = model as ConditionalModel;
                    var index = models.IndexOf(item) + beginIndex;
                    var type  = index == 0 ? "" : "AND";
                    if (beginIndex > 0)
                    {
                        type = null;
                    }
                    string temp          = " {0} {1} {2} {3}  ";
                    string parameterName = string.Format("{0}Conditional{1}{2}", sqlBuilder.SqlParameterKeyWord, item.FieldName, index);
                    if (parameterName.Contains("."))
                    {
                        parameterName = parameterName.Replace(".", "_");
                    }
                    switch (item.ConditionalType)
                    {
                    case ConditionalType.Equal:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, GetFieldValue(item)));
                        break;

                    case ConditionalType.Like:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, "%" + item.FieldValue + "%"));
                        break;

                    case ConditionalType.GreaterThan:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, GetFieldValue(item)));
                        break;

                    case ConditionalType.GreaterThanOrEqual:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), ">=", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, GetFieldValue(item)));
                        break;

                    case ConditionalType.LessThan:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, GetFieldValue(item)));
                        break;

                    case ConditionalType.LessThanOrEqual:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<=", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, GetFieldValue(item)));
                        break;

                    case ConditionalType.In:
                        if (item.FieldValue == null)
                        {
                            item.FieldValue = string.Empty;
                        }
                        var inValue1 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")");
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "IN", inValue1);
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue));
                        break;

                    case ConditionalType.NotIn:
                        if (item.FieldValue == null)
                        {
                            item.FieldValue = string.Empty;
                        }
                        var inValue2 = ("(" + item.FieldValue.Split(',').ToJoinSqlInVals() + ")");
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "NOT IN", inValue2);
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue));
                        break;

                    case ConditionalType.LikeLeft:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue + "%"));
                        break;

                    case ConditionalType.NoLike:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), " NOT LIKE", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue + "%"));
                        break;

                    case ConditionalType.LikeRight:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "LIKE", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, "%" + item.FieldValue));
                        break;

                    case ConditionalType.NoEqual:
                        builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<>", parameterName);
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue));
                        break;

                    case ConditionalType.IsNullOrEmpty:
                        builder.AppendFormat("{0} ({1}) OR ({2}) ", type, item.FieldName.ToSqlFilter() + " IS NULL ", item.FieldName.ToSqlFilter() + " = '' ");
                        parameters.Add(new DbLiteParameter(parameterName, item.FieldValue));
                        break;

                    case ConditionalType.IsNot:
                        if (item.FieldValue == null)
                        {
                            builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), " IS NOT ", "NULL");
                        }
                        else
                        {
                            builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "<>", parameterName);
                            parameters.Add(new DbLiteParameter(parameterName, item.FieldValue));
                        }
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    var item = model as ConditionalCollections;
                    if (item != null && item.ConditionalList.HasValue())
                    {
                        foreach (var con in item.ConditionalList)
                        {
                            var index   = item.ConditionalList.IndexOf(con);
                            var isFirst = index == 0;
                            var isLast  = index == (item.ConditionalList.Count - 1);
                            if (models.IndexOf(item) == 0 && index == 0 && beginIndex == 0)
                            {
                                builder.AppendFormat(" ( ");
                            }
                            else if (isFirst)
                            {
                                builder.AppendFormat(" {0} ( ", con.Key.ToString().ToUpper());
                            }
                            List <IConditionalModel> conModels = new List <IConditionalModel>();
                            conModels.Add(con.Value);
                            var childSqlInfo = ConditionalModelToSql(conModels, 1000 * (1 + index) + models.IndexOf(item));
                            if (!isFirst)
                            {
                                builder.AppendFormat(" {0} ", con.Key.ToString().ToUpper());
                            }
                            builder.Append(childSqlInfo.Key);
                            parameters.AddRange(childSqlInfo.Value);
                            if (isLast)
                            {
                                builder.Append(" ) ");
                            }
                            else
                            {
                            }
                        }
                    }
                }
            }
            return(new KeyValuePair <string, DbLiteParameter[]>(builder.ToString(), parameters.ToArray()));
        }