Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions QueryBuilder/Include.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
namespace SqlKata
{
/// <summary>
/// Describes a related query that should be loaded together with the parent query result.
/// </summary>
public class Include
{
/// <summary>
/// Gets or sets the include name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets the query used to load the related records.
/// </summary>
public Query Query { get; set; }

/// <summary>
/// Gets or sets the foreign key used to relate the included query to the parent query.
/// </summary>
public string ForeignKey { get; set; }

/// <summary>
/// Gets or sets the local key used to relate the parent query to the included query.
/// </summary>
public string LocalKey { get; set; }

/// <summary>
/// Gets or sets whether the include represents multiple related records.
/// </summary>
public bool IsMany { get; set; }

/// <summary>
/// Creates a copy of the include definition.
/// </summary>
/// <returns>A cloned include definition.</returns>
public Include Clone()
{
return new Include
Expand Down
76 changes: 74 additions & 2 deletions QueryBuilder/Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace SqlKata
{
/// <summary>
/// Builds a SQL join clause and its join conditions.
/// </summary>
public class Join : BaseQuery<Join>
{
protected string _type = "inner join";

/// <summary>
/// Gets or sets the SQL join type.
/// </summary>
public string Type
{
get
Expand All @@ -18,17 +24,29 @@ public string Type
}
}

/// <summary>
/// Initializes a new inner join clause.
/// </summary>
public Join() : base()
{
}

/// <summary>
/// Creates a copy of the join clause.
/// </summary>
/// <returns>A cloned join clause.</returns>
public override Join Clone()
{
var clone = base.Clone();
clone._type = _type;
return clone;
}

/// <summary>
/// Sets the SQL join type.
/// </summary>
/// <param name="type">The join type, for example <c>left join</c>.</param>
/// <returns>The current join instance.</returns>
public Join AsType(string type)
{
Type = type;
Expand All @@ -39,18 +57,61 @@ public Join AsType(string type)
/// Alias for "from" operator.
/// Since "from" does not sound well with join clauses
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
/// <param name="table">The table to join with.</param>
/// <returns>The current join instance.</returns>
public Join JoinWith(string table) => From(table);

/// <summary>
/// Adds a subquery as the joined source.
/// </summary>
/// <param name="query">The subquery to join with.</param>
/// <returns>The current join instance.</returns>
public Join JoinWith(Query query) => From(query);

/// <summary>
/// Adds a callback-created subquery as the joined source.
/// </summary>
/// <param name="callback">A callback that configures the joined query.</param>
/// <returns>The current join instance.</returns>
public Join JoinWith(Func<Query, Query> callback) => From(callback);

/// <summary>
/// Sets the join type to <c>inner join</c>.
/// </summary>
/// <returns>The current join instance.</returns>
public Join AsInner() => AsType("inner join");

/// <summary>
/// Sets the join type to <c>outer join</c>.
/// </summary>
/// <returns>The current join instance.</returns>
public Join AsOuter() => AsType("outer join");

/// <summary>
/// Sets the join type to <c>left join</c>.
/// </summary>
/// <returns>The current join instance.</returns>
public Join AsLeft() => AsType("left join");

/// <summary>
/// Sets the join type to <c>right join</c>.
/// </summary>
/// <returns>The current join instance.</returns>
public Join AsRight() => AsType("right join");

/// <summary>
/// Sets the join type to <c>cross join</c>.
/// </summary>
/// <returns>The current join instance.</returns>
public Join AsCross() => AsType("cross join");

/// <summary>
/// Adds a join condition comparing two columns.
/// </summary>
/// <param name="first">The first column name.</param>
/// <param name="second">The second column name.</param>
/// <param name="op">The comparison operator.</param>
/// <returns>The current join instance.</returns>
public Join On(string first, string second, string op = "=")
{
return AddComponent("where", new TwoColumnsCondition
Expand All @@ -64,11 +125,22 @@ public Join On(string first, string second, string op = "=")

}

/// <summary>
/// Adds an OR join condition comparing two columns.
/// </summary>
/// <param name="first">The first column name.</param>
/// <param name="second">The second column name.</param>
/// <param name="op">The comparison operator.</param>
/// <returns>The current join instance.</returns>
public Join OrOn(string first, string second, string op = "=")
{
return Or().On(first, second, op);
}

/// <summary>
/// Creates a new join query instance.
/// </summary>
/// <returns>A new join instance.</returns>
public override Join NewQuery()
{
return new Join();
Expand Down
33 changes: 33 additions & 0 deletions QueryBuilder/SqlResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,48 @@

namespace SqlKata
{
/// <summary>
/// Represents the SQL text and bindings produced by compiling a <see cref="Query"/>.
/// </summary>
public class SqlResult
{
private string ParameterPlaceholder { get; set; }
private string EscapeCharacter { get; set; }

/// <summary>
/// Initializes a new SQL result with the placeholder and escape character used by the compiler.
/// </summary>
/// <param name="parameterPlaceholder">The placeholder token used for parameter bindings.</param>
/// <param name="escapeCharacter">The escape character used while replacing placeholders.</param>
public SqlResult(string parameterPlaceholder, string escapeCharacter)
{
ParameterPlaceholder = parameterPlaceholder;
EscapeCharacter = escapeCharacter;
}

/// <summary>
/// Gets or sets the query that produced this result.
/// </summary>
public Query Query { get; set; }

/// <summary>
/// Gets or sets the raw SQL text before bindings are expanded.
/// </summary>
public string RawSql { get; set; } = "";

/// <summary>
/// Gets or sets the ordered binding values for the compiled SQL.
/// </summary>
public List<object> Bindings { get; set; } = new List<object>();

/// <summary>
/// Gets or sets the compiled SQL text.
/// </summary>
public string Sql { get; set; } = "";

/// <summary>
/// Stores bindings by name for compilers that support named parameters.
/// </summary>
public Dictionary<string, object> NamedBindings = new Dictionary<string, object>();

private static readonly Type[] NumberTypes =
Expand All @@ -33,6 +62,10 @@ public SqlResult(string parameterPlaceholder, string escapeCharacter)
typeof(ulong),
};

/// <summary>
/// Returns the raw SQL with binding values inlined for inspection and debugging.
/// </summary>
/// <returns>The SQL text with bindings converted to SQL literals.</returns>
public override string ToString()
{
var deepParameters = Helper.Flatten(Bindings).ToList();
Expand Down
11 changes: 11 additions & 0 deletions QueryBuilder/UnsafeLiteral.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
namespace SqlKata
{
/// <summary>
/// Represents a raw SQL literal that should be inserted without parameter binding.
/// </summary>
public class UnsafeLiteral
{
/// <summary>
/// Gets or sets the literal SQL value.
/// </summary>
public string Value { get; set; }

/// <summary>
/// Initializes a new unsafe SQL literal.
/// </summary>
/// <param name="value">The raw SQL value.</param>
/// <param name="replaceQuotes">Whether single quotes should be escaped in the value.</param>
public UnsafeLiteral(string value, bool replaceQuotes = true)
{
if (value == null)
Expand Down
10 changes: 10 additions & 0 deletions QueryBuilder/Variable.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
namespace SqlKata
{
/// <summary>
/// Represents a named variable placeholder that can be resolved by a query compiler.
/// </summary>
public class Variable
{
/// <summary>
/// Gets or sets the variable name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Initializes a new variable placeholder.
/// </summary>
/// <param name="name">The variable name.</param>
public Variable(string name)
{
this.Name = name;
Expand Down
Loading