/// <summary> /// Get the record count of the clause. If the clause is empty, the table count is returned. /// </summary> /// <param name="tableName">The table name for the count. </param> /// <param name="clause">The clause to use to produce count.</param> /// <returns><see cref="IAliasedCommandTypeDataOrder"/> containing the query, parents, and aliases.</returns> public static IAliasedCommandTypeDataOrder GetRecordCount(string tableName, Clause clause) { var clauseOrder = clause.Build(); string sql = $"SELECT\r\n COUNT(*)\r\nFROM {tableName}{(clauseOrder.Query.IsMeaningful() ? $"\r\nWHERE {clauseOrder.Query}" : "") }"; return(new AliasedCommandTypeDataOrder(sql, CommandType.Text, clauseOrder.Parameters)); }
/// <summary> /// Conditionally add previous built clause to this clause. If condition returns true, clause is added. Otherwise, it is not. /// </summary> /// <param name="clause">Previously built clause.</param> /// <param name="condition">Clause condition which determines if clause will be inserted. The parameter passed into this is the value parameter of this method. Returning true adds clause, false skips it.</param> /// <returns>Modified clause if condition was true. Otherwise, returns input clause.</returns> public Clause AddClause(Clause clause, Func <bool> condition) { bool conditionFailed = condition() == false; if (conditionFailed) { return(this); } SimpleDataOrder dataOrder = clause.Build(); if (string.IsNullOrWhiteSpace(dataOrder.Query)) { return(this); } return(GetClone(string.Join($"{Environment.NewLine} ", dataOrder.Query.Split(new[] { Environment.NewLine, "\r\n", "\r", "\n" }, StringSplitOptions.None)), dataOrder.Parameters)); }