public long GetCountByConditions(ITableSpec table, Web.Core.DB.conditions.AbstractCondition conditions, params JoinSpec[] joins) { using (var logger = this.CreateCommandExecutionLogger()) { using (DbConnection connection = this.createConnection()) { using (DbCommand command = connection.CreateCommand()) { command.CommandType = System.Data.CommandType.Text; var conditionsCompiled = ConditionCompiler.Compile(conditions, this.traits); string queryConditions = ""; if (conditionsCompiled.Key != "") { queryConditions = "WHERE " + conditionsCompiled.Key; } ParamsHolder paramsHolder = conditionsCompiled.Value; string queryJoins = ""; { if (joins.Length > 0) { throw new NotImplementedException(); } } command.CommandText = logger.commandText = "SELECT COUNT(*) " + "FROM " + table.compile(this.traits) + " " + queryJoins + " " + queryConditions; foreach (KeyValuePair <string, string> kvp in paramsHolder.data) { command.AddParameter(kvp.Key, kvp.Value); } object rawCount = command.ExecuteScalar(); long count = (long)rawCount; return(count); } } } }
private List <string> _LoadIdsByConditions(DbCommand command, ITableSpec table, Web.Core.DB.conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts, ColumnSpec idSpec, bool allowHugeLists) { using (var logger = this.CreateCommandExecutionLogger()) { command.CommandType = System.Data.CommandType.Text; var conditionsCompiled = ConditionCompiler.Compile(conditions, this.traits); string queryConditions = ""; if (conditionsCompiled.Key != "") { queryConditions = "WHERE " + conditionsCompiled.Key; } ParamsHolder paramsHolder = conditionsCompiled.Value; StringBuilder queryJoins = new StringBuilder(); { foreach (var join in joins) { queryJoins.Append(" JOIN "); queryJoins.Append(join.additionalTableRaw.compile(this.traits)); queryJoins.Append(" "); queryJoins.Append(join.additionalTable.compile(this.traits)); queryJoins.Append(" ON "); queryJoins.Append(join.mainColumn.compile(this.traits)); queryJoins.Append(" = "); queryJoins.Append(join.additionalTable.getIdSpec().compile(this.traits)); } } string querySorts = ""; if (sorts.Length > 0) { List <string> sortParts = new List <string>(); foreach (SortSpec sortSpec in sorts) { if (sortSpec.ascending) { sortParts.Add(sortSpec.column.compile(this.traits) + " ASC"); } else { sortParts.Add(sortSpec.column.compile(this.traits) + " DESC"); } } querySorts = "ORDER BY " + string.Join(", ", sortParts.ToArray()); } string queryMain = "FROM " + table.compile(this.traits) + " " + queryJoins + " " + queryConditions; foreach (KeyValuePair <string, string> kvp in paramsHolder.data) { command.AddParameter(kvp.Key, kvp.Value); } if (!diapasone.total.HasValue) { command.CommandText = logger.commandText = "SELECT COUNT(*) " + queryMain; object rawCount; //try { rawCount = command.ExecuteScalar(); //} catch(Npgsql.NpgsqlException e) { //throw new FLocalException("Error while trying to execute " + command.CommandText + ": " + e.Message); //} long count = (long)rawCount; if (count < 1) { diapasone.total = 0; } else { diapasone.total = count; } } if (diapasone.total.Value < 1) { return(new List <string>()); } else { if (diapasone.total.Value > 1000 && diapasone.count < 0 && !allowHugeLists) { throw new CriticalException("huge list"); } string queryLimits = ""; if (diapasone.count >= 0) { queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; } command.CommandText = logger.commandText = "SELECT " + idSpec.compile(this.traits) + " " + queryMain + " " + querySorts + " " + queryLimits; List <string> result = new List <string>(); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add(reader.GetValue(0).ToString()); } } return(result); } } }