/* * /// <summary> * /// Returns a reader for the specified amount of rows for the specified entity type. * /// </summary> * /// <typeparam name="T">The type of entity to be queried.</typeparam> * /// <param name="condition">An optional condition to allow querying for specific rows. Leave at null to query all rows.</param> * /// <param name="amountOfRows">The amount of rows to be returned (maximum). Leave at -1 to query all rows.</param> * /// <returns>A DbDataReader that has tried to query rows for entities of type T.</returns> * internal DbDataReader GetReader<T>(BuiltSqlCondition condition = null, int amountOfRows = -1) * { * return GetReader(typeof(T), condition, amountOfRows); * } * * /// <summary> * /// Returns a reader for the specified amount of rows for the specified entity type. * /// </summary> * /// <param name="entityType">The type of entity to be queried.</param> * /// <param name="condition">An optional condition to allow querying for specific rows. Leave at null to query all rows.</param> * /// <param name="amountOfRows">The amount of rows to be returned (maximum). Leave at -1 to query all rows.</param> * /// <returns>A DbDataReader that has tried to query rows for entities of type T.</returns> * internal DbDataReader GetReader(Type entityType, BuiltSqlCondition condition = null, int amountOfRows = -1) * { * var select = SqlBuild.Select(entityType); * * if (condition != null) * select.Condition = condition; * * if (amountOfRows > 0) * select.Limit(amountOfRows); * * return select.GenerateCommand(_connection).ExecuteReader(); * } * * /// <summary> * /// Returns a reader for the specified amount of rows for the specified entity type. * /// </summary> * /// <param name="entityType">The type of entity to be queried.</param> * /// <param name="commandString">The command to execute to create the reader.</param> * /// <returns>A DbDataReader that has tried to query rows for entities of type entityType.</returns> * internal DbDataReader GetReader(Type entityType, string commandString) * { * using (var command = _connection.CreateCommand()) * { * command.CommandText = commandString; * return command.ExecuteReader(); * } * } */ /// <summary> /// Fetches and initializes all available rows for entities of type T. /// </summary> /// <typeparam name="T">The type of entity to be queried.</typeparam> /// <param name="condition">An optional condition to allow querying for specific rows. Leave at null to query all rows.</param> /// <returns>An enumerable of constructed entities of type T.</returns> public IEnumerable <T> All <T>(BuiltSqlCondition condition = null) where T : new() { var command = _connection.CreateCommand(); command.CommandText = SqlBuild.Select <T>().GenerateStatement(); condition?.GenerateCommand(command); var reader = command.ExecuteReader(); List <T> resultSet = new List <T>(); while (reader.Read()) { resultSet.Add(BuildSingle <T>(reader)); } command.Dispose(); return(resultSet); }
/// <summary> /// Fetches and initializes the specified number of rows for entities of type T. /// </summary> /// <typeparam name="T">The type of entity to be queried.</typeparam> /// <param name="condition">An optional condition to allow querying for specific rows. Leave at null to query all rows.</param> /// <param name="amountOfRows">The amount of rows to be queried (maximum).</param> /// <returns>An enumerable of constructed entities of type T.</returns> public IEnumerable <T> Fetch <T>(BuiltSqlCondition condition, int amountOfRows = -1) where T : new() { var select = SqlBuild.Select <T>(); select.Condition = condition; if (amountOfRows != -1) { if (ProviderSpecific.SupportsLimit) { select.Limit(amountOfRows); } } var command = select.GenerateCommand(_connection); var reader = command.ExecuteReader(); List <T> resultSet = new List <T>(); if (!ProviderSpecific.SupportsLimit && amountOfRows != -1) { int rows = 0; while (reader.Read() && rows < amountOfRows) { resultSet.Add(BuildSingle <T>(reader)); rows++; } } else { while (reader.Read()) { resultSet.Add(BuildSingle <T>(reader)); } } command.Dispose(); return(resultSet); //var reader = GetReader<T>(condition, amountOfRows); //List<T> resultSet = new List<T>(); while (reader.Read()) { resultSet.Add(BuildSingle <T>(reader)); } return(resultSet); }
/// <summary> /// Adds a condition to apply to this DELETE command. /// </summary> public BuiltDeleteCommand Where(BuiltSqlCondition condition) { _condition = condition; return(this); }
/// <summary> /// Creates a WHERE clause for this BuiltSqlCommand. /// Calling this twice on a BuiltSelectCommand will overwrite the first call. /// </summary> public BuiltSelectCommand Where(BuiltSqlCondition condition) { Condition = condition; return(this); }
/// <summary> /// Fetches and initializes a single row for entities of type T. /// </summary> /// <typeparam name="T">The type of entity to be queried.</typeparam> /// <param name="condition">An optional condition to allow querying for specific rows. Leave at null to query all rows.</param> /// <returns>The constructed entity of type T.</returns> public T Single <T>(BuiltSqlCondition condition = null) where T : new() { return(Fetch <T>(condition, 1).FirstOrDefault()); }
/// <summary> /// Sets a condition to limit which datasets are updated. /// </summary> /// <param name="condition">The condition to be used.</param> public BuiltUpdateCommand <T> Where(BuiltSqlCondition condition) { _condition = condition; return(this); }