public static IEnumerable <T> Query <T>(this IDatabase db, string sql, object param = null) { using (var conn = db.OpenConnection()) { IEnumerable <T> items; try { items = SqlMapper.Query <T>(conn, sql, param); } catch (Exception e) { e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param)); throw; } if (TableMapping.Mapper.LazyLoadList.TryGetValue(typeof(T), out var lazyProperties)) { foreach (var item in items) { ApplyLazyLoad(db, item, lazyProperties); } } return(items); } }
public static IEnumerable <TReturn> Query <TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this IDatabase db, string sql, Func <TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int?commandTimeout = null, CommandType?commandType = null) { TReturn MapWithLazy(TFirst first, TSecond second, TThird third, TFourth fourth, TFifth fifth) { ApplyLazyLoad(db, first); ApplyLazyLoad(db, second); ApplyLazyLoad(db, third); ApplyLazyLoad(db, fourth); ApplyLazyLoad(db, fifth); return(map(first, second, third, fourth, fifth)); } using (var conn = db.OpenConnection()) { try { return(SqlMapper.Query <TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(conn, sql, MapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType)); } catch (Exception e) { e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param)); throw; } } }
private void UpdateFields(IDbConnection connection, IDbTransaction transaction, TModel model, List <PropertyInfo> propertiesToUpdate) { var sql = propertiesToUpdate == _properties ? _updateSql : GetUpdateSql(propertiesToUpdate); SqlBuilderExtensions.LogQuery(sql, model); connection.Execute(sql, model, transaction: transaction); }
private TModel Insert(IDbConnection connection, IDbTransaction transaction, TModel model) { SqlBuilderExtensions.LogQuery(_insertSql, model); var multi = connection.QueryMultiple(_insertSql, model, transaction); var id = (int)multi.Read().First().id; _keyProperty.SetValue(model, id); return(model); }
private void UpdateFields(IDbConnection connection, IDbTransaction transaction, IList <TModel> models, List <PropertyInfo> propertiesToUpdate) { var sql = propertiesToUpdate == _properties ? _updateSql : GetUpdateSql(propertiesToUpdate); foreach (var model in models) { SqlBuilderExtensions.LogQuery(sql, model); } connection.Execute(sql, models, transaction: transaction); }
private TModel Insert(IDbConnection connection, IDbTransaction transaction, TModel model) { SqlBuilderExtensions.LogQuery(_insertSql, model); var multi = connection.QueryMultiple(_insertSql, model, transaction); var multiRead = multi.Read(); var id = (int)(multiRead.First().id ?? multiRead.First().Id); _keyProperty.SetValue(model, id); _database.ApplyLazyLoad(model); return(model); }
private void UpdateFields(IDbConnection connection, IDbTransaction transaction, TModel model, List <PropertyInfo> propertiesToUpdate) { var sql = propertiesToUpdate == _properties ? _updateSql : GetUpdateSql(propertiesToUpdate); SqlBuilderExtensions.LogQuery(sql, model); try { connection.Execute(sql, model, transaction: transaction); } catch (Exception e) { e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, model)); } }
private TModel Insert(IDbConnection connection, IDbTransaction transaction, TModel model) { SqlBuilderExtensions.LogQuery(_insertSql, model); GridReader multi; try { multi = connection.QueryMultiple(_insertSql, model, transaction); } catch (Exception e) { e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(_insertSql, model)); throw; } var id = (int)multi.Read().First().id; _keyProperty.SetValue(model, id); return(model); }