protected bool AddCondition(SqlStringBuilder buffer, SqlString on) { if (SqlStringHelper.IsNotEmpty(on)) { if (!on.StartsWithCaseInsensitive(" and")) { buffer.Add(" and "); } buffer.Add(on); return(true); } else { return(false); } }
/// <summary> /// Attempts to discover what type of object this is and calls the appropriate /// method. /// </summary> /// <param name="part">The part to add when it is not known if it is a Parameter, String, or SqlString.</param> /// <returns>This SqlStringBuilder.</returns> /// <exception cref="ArgumentException">Thrown when the part is not a Parameter, String, or SqlString.</exception> public SqlStringBuilder AddObject(object part) { if (part == null) { return(this); } Parameter paramPart = part as Parameter; if (paramPart != null) { return(Add(paramPart)); // EARLY EXIT } string stringPart = part as string; if (StringHelper.IsNotEmpty(stringPart)) { return(Add(stringPart)); } SqlString sqlPart = part as SqlString; if (SqlStringHelper.IsNotEmpty(sqlPart)) { return(Add(sqlPart)); } // remarks - we should not get to here - this is a problem with the // SQL being generated. if (paramPart == null && stringPart == null && sqlPart == null) { throw new ArgumentException("Part was not a Parameter, String, or SqlString."); } return(this); }
/// <summary></summary> public SqlString ToSqlString() { // 4 = the "SELECT", selectClause, "FROM", fromClause are straight strings // plus the number of parts in outerJoinsAfterFrom SqlString. // 1 = the "WHERE" // plus the number of parts in outerJoinsAfterWhere SqlString. // 1 = the whereClause // 2 = the "ORDER BY" and orderByClause var joinAfterFrom = outerJoinsAfterFrom != null ? outerJoinsAfterFrom.Count : 0; var joinAfterWhere = outerJoinsAfterWhere != null ? outerJoinsAfterWhere.Count : 0; int initialCapacity = 4 + joinAfterFrom + 1 + joinAfterWhere + 1 + 2; if (!string.IsNullOrEmpty(comment)) { initialCapacity++; } SqlStringBuilder sqlBuilder = new SqlStringBuilder(initialCapacity + 2); if (!string.IsNullOrEmpty(comment)) { sqlBuilder.Add("/* " + comment + " */ "); } sqlBuilder.Add("SELECT ") .Add(selectClause) .Add(" FROM ") .Add(fromClause); if (SqlStringHelper.IsNotEmpty(outerJoinsAfterFrom)) { sqlBuilder.Add(outerJoinsAfterFrom); } if (SqlStringHelper.IsNotEmpty(whereClause) || SqlStringHelper.IsNotEmpty(outerJoinsAfterWhere)) { sqlBuilder.Add(" WHERE "); // the outerJoinsAfterWhere needs to come before where clause to properly // handle dynamic filters if (SqlStringHelper.IsNotEmpty(outerJoinsAfterWhere)) { sqlBuilder.Add(outerJoinsAfterWhere); if (SqlStringHelper.IsNotEmpty(whereClause)) { sqlBuilder.Add(" AND "); } } if (SqlStringHelper.IsNotEmpty(whereClause)) { sqlBuilder.Add(whereClause); } } if (SqlStringHelper.IsNotEmpty(groupByClause)) { sqlBuilder.Add(" GROUP BY ") .Add(groupByClause); } if (SqlStringHelper.IsNotEmpty(havingClause)) { sqlBuilder.Add(" HAVING ") .Add(havingClause); } if (SqlStringHelper.IsNotEmpty(orderByClause)) { sqlBuilder.Add(" ORDER BY ") .Add(orderByClause); } if (lockMode != null) { sqlBuilder.Add(GetForUpdateString()); } if (log.IsDebugEnabled()) { if (initialCapacity < sqlBuilder.Count) { log.Debug("The initial capacity was set too low at: {0} for the SelectSqlBuilder that needed a capacity of: {1} for the table {2}", initialCapacity, sqlBuilder.Count, fromClause); } else if (initialCapacity > 16 && ((float)initialCapacity / sqlBuilder.Count) > 1.2) { log.Debug("The initial capacity was set too high at: {0} for the SelectSqlBuilder that needed a capacity of: {1} for the table {2}", initialCapacity, sqlBuilder.Count, fromClause); } } return(sqlBuilder.ToSqlString()); }