Esempio n. 1
0
        public Clause SelectAsync <TModel>(IQuery <TModel> query)
        {
            var clause = GetSqlClause(query);

            try
            {
                if (query.Skip.HasValue || query.Take.HasValue)
                {
                    var sql = BuildPageSql(query, clause);
                    clause.Sql = sql;
                    return(clause);
                }

                if (string.IsNullOrEmpty(clause.Where))
                {
                    return(clause);
                }

                var descriptor = TableCache.GetTableDescriptor(typeof(TModel));
                var sqlBuilder = StringBuilderCache.Acquire();
                sqlBuilder.Append(this.BuildSelectSql(descriptor));
                sqlBuilder.Append(clause.Where);
                sqlBuilder.Append(clause.OrderBy);
                clause.Sql = StringBuilderCache.GetStringAndRelease(sqlBuilder);
                return(clause);
            }
            finally
            {
                clause.Where   = null;
                clause.OrderBy = null;
            }
        }
Esempio n. 2
0
        private string BuildPageSql <TModel>(IQuery <TModel> query, Clause clause)
        {
            var descriptor = TableCache.GetTableDescriptor(typeof(TModel));

            clause.Paging = true;

            var options = descriptor.Options;

            if (query.OrderBys == null || query.OrderBys.Count == 0)
            {
                BuildDefaultOrderBy(clause, descriptor);
            }

            var sqlBuilder = StringBuilderCache.Acquire();

            sqlBuilder.Append(BuildSelectSql(descriptor));
            sqlBuilder.Append(clause.Where);
            sqlBuilder.Append(clause.OrderBy);
            sqlBuilder.Append(options.GetPageSql(query.Skip ?? 0, query.Take ?? 1));
            sqlBuilder.Append(" ;");

            sqlBuilder.Append("SELECT COUNT(1) from ");
            sqlBuilder.Append(options.StartDelimiter);
            sqlBuilder.Append(descriptor.TableName);
            sqlBuilder.Append(options.EndDelimiter);
            sqlBuilder.Append(clause.Where);
            sqlBuilder.Append(" ;");
            return(StringBuilderCache.GetStringAndRelease(sqlBuilder));
        }
Esempio n. 3
0
        public static async Task <int> InsertAsync <T>(this IDbConnection connection, T entity,
                                                       IDbTransaction transaction = null)
            where T : class
        {
            var type = typeof(T);
            var sql  = LazySqlBuilder.Value.Insert(type);

            if (string.IsNullOrEmpty(sql))
            {
                throw new ArgumentException("SqlBuilder Return Sql Empty");
            }

            if (Debugger.IsAttached)
            {
                Trace.WriteLine(sql);
            }

            if (type.IsArray)
            {
                return(await connection.ExecuteAsync(sql, entity, transaction));
            }

            var multi = await connection.QueryMultipleAsync(sql, entity).ConfigureAwait(false);

            var first = multi.Read <IdInfo>().FirstOrDefault();

            if (first == null)
            {
                throw new Exception("InsertAsync Error");
            }

            var columnDescriptors = TableCache.GetColumnDescriptors(typeof(T));
            var primaryKey        = columnDescriptors.FirstOrDefault(n => n.IsPrimaryKey);

            primaryKey?.SetPrimaryKey(entity, first.Id);
            return(1);
        }