/// <summary> /// Adds a table to the FROM statement with "T0" alias and sets it as target for future field selections. /// </summary> /// <param name="query">The query.</param> /// <param name="entity">The entity.</param> /// <returns> /// The query itself. /// </returns> /// <exception cref="ArgumentNullException">row</exception> public static SqlQuery From(this SqlQuery query, IEntity entity) { if (entity == null) { throw new ArgumentNullException("row"); } if (entity as IRow != null) { var fields = (entity as IRow).Fields; query.From(fields); if (!query.IsDialectOverridden) { query.Dialect(fields.Dialect); } } else { if (entity as IAlias != null && ((entity as IAlias).Name == "t0" || (entity as IAlias).Name == "T0") && (entity as IAlias).Table == entity.Table) { query.From(entity as IAlias); } else { query.From(entity.Table, Alias.T0); } } return(query.Into(entity)); }
/// <summary> /// <see cref="SqlQuery"/> nesnesinin içerdiği sorguyu bağlantı üzerinde çalıştırır ve /// sorgunun döndürdüğü her bir kayıt için <see cref="IDataReader"/> parametresi alan bir /// callback fonksiyonunu çağırır.</summary> /// <remarks> /// <p>Bu bir extension metodu olduğundan direk <c>query.ForEach(connection, delegate() {...})</c> /// şeklinde de çalıştırılabilir.</p> /// <p><c>query.GetFromReader(reader)</c> işlemi her satır için callback çağrılmadan /// önce çalıştırılır.</p> /// <p>Eğer <see cref="SqlQuery.CacheTimeOut(int)"/> ile sorgu için saniye cinsinden bir önbellekleme /// süresi belirlenmişse bu değer kullanılır.</p></remarks> /// <param name="connection"> /// Sorgunun çalıştırılacağı bağlantı. Gerekirse otomatik olarak açılır.</param> /// <param name="query"> /// Sorguyu içeren <see cref="SqlQuery"/> nesnesi.</param> /// <param name="callBack"> /// Her kayıt için çağrılacak olan callback fonksiyonu.</param> /// <returns> /// query.CountRecords true ise toplam kayıt sayısı, değilse 0.</returns> public static int ForEach(this SqlQuery query, IDbConnection connection, ReaderCallBack callBack) { int count = 0; if (query.Dialect().MultipleResultsets()) { using (IDataReader reader = SqlHelper.ExecuteReader(connection, query)) { while (reader.Read()) { query.GetFromReader(reader); callBack(reader); } if (query.CountRecords && reader.NextResult() && reader.Read()) { return(Convert.ToInt32(reader.GetValue(0))); } } } else { string[] queries = query.ToString().Split(new string[] { "\n---\n" }, StringSplitOptions.RemoveEmptyEntries); if (queries.Length > 1) { count = Convert.ToInt32(SqlHelper.ExecuteScalar(connection, queries[1], query.Params)); } using (IDataReader reader = SqlHelper.ExecuteReader(connection, queries[0], query.Params)) { while (reader.Read()) { query.GetFromReader(reader); callBack(reader); } } } return(count); }
/// <summary> /// Adds a table to the FROM statement with "T0" alias and sets it as target for future field selections. /// </summary> /// <param name="row">Row object.</param> /// <returns>The query itself.</returns> public static SqlQuery From(this SqlQuery query, IEntity entity) { if (entity == null) { throw new ArgumentNullException("row"); } var row = entity as Row; if (row != null) { var fields = row.GetFields(); query.From(fields); if (!query.IsDialectOverridden && !string.IsNullOrEmpty(fields.connectionKey)) { var cs = SqlConnections.TryGetConnectionString(fields.connectionKey); if (cs != null) { query.Dialect(cs.Dialect); } } } else { var alias = entity as IAlias; if (alias != null && (alias.Name == "t0" || alias.Name == "T0") && alias.Table == entity.Table) { query.From(alias); } else { query.From(entity.Table, string.Empty, Alias.T0); } } return(query.Into(entity)); }