Skip to content

Commit

Permalink
Merge pull request #406 from mk3008/402-add-class-function-and-proper…
Browse files Browse the repository at this point in the history
…ty-comments

Add comment
  • Loading branch information
mk3008 authored May 8, 2024
2 parents f2cb9b7 + fc6cfb4 commit fbab4d8
Show file tree
Hide file tree
Showing 51 changed files with 1,412 additions and 110 deletions.
4 changes: 2 additions & 2 deletions src/Carbunql/AlterTableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public List<AlterTableQuery> Disassemble()

public bool TrySet(TableDefinitionClause clause)
{
if (AlterTableClause.Items.Count != 1) throw new InvalidOperationException();
var cmd = AlterTableClause.Items[0];
if (AlterTableClause.Count != 1) throw new InvalidOperationException();
var cmd = AlterTableClause[0];
return cmd.TrySet(clause);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Carbunql/Building/ReadQueryExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ public static CreateTableQuery ToCreateTableQuery(this IReadQuery source, TableB
/// <returns>The SelectableTable representing the IReadQuery for insertion into the specified table.</returns>
public static SelectableTable ToInsertTable(this IReadQuery source, string table)
{
var s = source.GetSelectClause();
if (s == null)
var clause = source.GetSelectClause();
if (clause == null)
{
return new SelectableTable(new PhysicalTable(table), table);
}

var vals = new ValueCollection();
foreach (var item in s.Items)
foreach (var item in clause)
{
vals.Add(new ColumnValue(item.Alias));
}
Expand Down Expand Up @@ -308,8 +308,8 @@ public static UpdateQuery ToUpdateQuery(this IReadQuery source, SelectableTable
/// <exception cref="NotSupportedException"></exception>
private static SetClause ToSetClause(this IReadQuery source, IEnumerable<string> keys, string queryAlias)
{
var s = source.GetSelectClause() ?? throw new NotSupportedException("select clause is not found.");
var cols = s.Items.Where(x => !keys.Contains(x.Alias)).Select(x => x.Alias).ToList();
var selectclause = source.GetSelectClause() ?? throw new NotSupportedException("select clause is not found.");
var cols = selectclause.Where(x => !keys.Contains(x.Alias)).Select(x => x.Alias).ToList();

var clause = new SetClause();
foreach (var item in cols)
Expand Down Expand Up @@ -407,7 +407,7 @@ private static WhereClause ToWhereClauseAsDelete(this IReadQuery source, IEnumer
private static WhereClause ToWhereClauseAsDelete(this IReadQuery source, string alias)
{
var select = source.GetSelectClause();
var selectColumns = select!.Items!.Select(x => x.Alias);
var selectColumns = select!.Select(x => x.Alias);

if (selectColumns == null || !selectColumns.Any()) throw new InvalidOperationException("Missing select clause.");

Expand Down
50 changes: 48 additions & 2 deletions src/Carbunql/Clauses/AlterTableClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,104 @@

namespace Carbunql.Clauses;

/// <summary>
/// Represents a clause for altering a table structure.
/// </summary>
public class AlterTableClause : QueryCommandCollection<IAlterCommand>, IQueryCommandable, ITable
{
/// <summary>
/// Initializes a new instance of the <see cref="AlterTableClause"/> class with the specified table.
/// </summary>
/// <param name="t">The table to be altered.</param>
public AlterTableClause(ITable t)
{
Schema = t.Schema;
Table = t.Table;
}

/// <summary>
/// Initializes a new instance of the <see cref="AlterTableClause"/> class with the specified table and command.
/// </summary>
/// <param name="t">The table to be altered.</param>
/// <param name="command">The alteration command.</param>
public AlterTableClause(ITable t, IAlterCommand command)
{
Schema = t.Schema;
Table = t.Table;
Items.Add(command);
}

/// <summary>
/// Initializes a new instance of the <see cref="AlterTableClause"/> class with the specified table and constraint.
/// </summary>
/// <param name="t">The table to be altered.</param>
/// <param name="constraint">The constraint to be added.</param>
public AlterTableClause(ITable t, IConstraint constraint)
{
Schema = t.Schema;
Table = t.Table;
Items.Add(new AddConstraintCommand(constraint));
}

/// <summary>
/// Initializes a new instance of the <see cref="AlterTableClause"/> class with the specified schema and table.
/// </summary>
/// <param name="schema">The schema of the table to be altered.</param>
/// <param name="table">The table to be altered.</param>
public AlterTableClause(string schema, string table)
{
Schema = schema;
Table = table;
}

/// <summary>
/// Initializes a new instance of the <see cref="AlterTableClause"/> class with the specified table name.
/// </summary>
/// <param name="table">The table to be altered.</param>
public AlterTableClause(string table)
{
Schema = string.Empty;
Table = table;
}

/// <summary>
/// Gets or sets the schema of the table to be altered.
/// </summary>
public string Schema { get; init; }

/// <summary>
/// Gets or sets the name of the table to be altered.
/// </summary>
public string Table { get; init; }

/// <summary>
/// Gets the full name of the table to be altered.
/// </summary>
public string TableFullName => (string.IsNullOrEmpty(Schema)) ? Table : Schema + "." + Table;

/// <summary>
/// Gets the internal queries associated with this clause.
/// </summary>
public IEnumerable<SelectQuery> GetInternalQueries()
{
yield break;
}

/// <summary>
/// Gets the physical tables associated with this clause.
/// </summary>
public IEnumerable<PhysicalTable> GetPhysicalTables()
{
yield break;
}

/// <summary>
/// Gets the tokens representing this clause.
/// </summary>
public override IEnumerable<Token> GetTokens(Token? parent)
{
if (!Items.Any()) throw new InvalidOperationException();


var altertable = Token.Reserved(this, parent, "alter table");
yield return altertable;
yield return new Token(this, parent, TableFullName);
Expand All @@ -68,8 +111,11 @@ public override IEnumerable<Token> GetTokens(Token? parent)
}
}

/// <summary>
/// Gets the common tables associated with this clause.
/// </summary>
public IEnumerable<CommonTable> GetCommonTables()
{
yield break;
}
}
}
34 changes: 33 additions & 1 deletion src/Carbunql/Clauses/AtTimeZoneClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@

namespace Carbunql.Clauses;

/// <summary>
/// Represents a clause for specifying the time zone for a datetime value.
/// </summary>
[MessagePackObject(keyAsPropertyName: true)]
public class AtTimeZoneClause : ValueBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AtTimeZoneClause"/> class.
/// </summary>
public AtTimeZoneClause()
{
Value = null!;
TimeZone = null!;
}

/// <summary>
/// Initializes a new instance of the <see cref="AtTimeZoneClause"/> class with the specified value and time zone.
/// </summary>
/// <param name="value">The datetime value.</param>
/// <param name="timeZone">The time zone.</param>
public AtTimeZoneClause(ValueBase value, ValueBase timeZone)
{
Value = value;
TimeZone = timeZone;
}

/// <summary>
/// Gets or sets the datetime value.
/// </summary>
public ValueBase Value { get; init; }

/// <summary>
/// Gets or sets the time zone.
/// </summary>
public ValueBase TimeZone { get; init; }

/// <summary>
/// Gets the internal queries associated with this clause.
/// </summary>
protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
{
foreach (var item in Value.GetInternalQueries())
Expand All @@ -34,6 +54,9 @@ protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
}
}

/// <summary>
/// Gets the tokens representing this clause.
/// </summary>
public override IEnumerable<Token> GetCurrentTokens(Token? parent)
{
foreach (var item in Value.GetTokens(parent)) yield return item;
Expand All @@ -42,6 +65,9 @@ public override IEnumerable<Token> GetCurrentTokens(Token? parent)
foreach (var item in TimeZone.GetTokens(parent)) yield return item;
}

/// <summary>
/// Gets the parameters associated with this clause.
/// </summary>
protected override IEnumerable<QueryParameter> GetParametersCore()
{
foreach (var item in Value.GetParameters())
Expand All @@ -54,6 +80,9 @@ protected override IEnumerable<QueryParameter> GetParametersCore()
}
}

/// <summary>
/// Gets the physical tables associated with this clause.
/// </summary>
protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
{
foreach (var item in Value.GetPhysicalTables())
Expand All @@ -66,6 +95,9 @@ protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
}
}

/// <summary>
/// Gets the common tables associated with this clause.
/// </summary>
protected override IEnumerable<CommonTable> GetCommonTablesCore()
{
foreach (var item in Value.GetCommonTables())
Expand All @@ -77,4 +109,4 @@ protected override IEnumerable<CommonTable> GetCommonTablesCore()
yield return item;
}
}
}
}
42 changes: 41 additions & 1 deletion src/Carbunql/Clauses/BetweenClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

namespace Carbunql.Clauses;

/// <summary>
/// Represents a BETWEEN clause in SQL.
/// </summary>
[MessagePackObject(keyAsPropertyName: true)]
public class BetweenClause : ValueBase
{
/// <summary>
/// Initializes a new instance of the <see cref="BetweenClause"/> class.
/// </summary>
public BetweenClause()
{
Value = null!;
Expand All @@ -14,6 +20,13 @@ public BetweenClause()
IsNegative = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="BetweenClause"/> class with the specified value, start, end, and negativity.
/// </summary>
/// <param name="value">The value to compare.</param>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <param name="isNegative">Specifies whether the BETWEEN clause is negated.</param>
public BetweenClause(ValueBase value, ValueBase start, ValueBase end, bool isNegative)
{
Value = value;
Expand All @@ -22,14 +35,29 @@ public BetweenClause(ValueBase value, ValueBase start, ValueBase end, bool isNeg
IsNegative = isNegative;
}

/// <summary>
/// Gets or sets the value to compare.
/// </summary>
public ValueBase Value { get; init; }

/// <summary>
/// Gets or sets the start of the range.
/// </summary>
public ValueBase Start { get; init; }

/// <summary>
/// Gets or sets the end of the range.
/// </summary>
public ValueBase End { get; init; }

/// <summary>
/// Gets or sets a value indicating whether the BETWEEN clause is negated.
/// </summary>
public bool IsNegative { get; init; }

/// <summary>
/// Gets the internal queries associated with this clause.
/// </summary>
protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
{
foreach (var item in Value.GetInternalQueries())
Expand All @@ -46,6 +74,9 @@ protected override IEnumerable<SelectQuery> GetInternalQueriesCore()
}
}

/// <summary>
/// Gets the parameters associated with this clause.
/// </summary>
protected override IEnumerable<QueryParameter> GetParametersCore()
{
foreach (var item in Value.GetParameters())
Expand All @@ -62,6 +93,9 @@ protected override IEnumerable<QueryParameter> GetParametersCore()
}
}

/// <summary>
/// Gets the physical tables associated with this clause.
/// </summary>
protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
{
foreach (var item in Value.GetPhysicalTables())
Expand All @@ -78,6 +112,9 @@ protected override IEnumerable<PhysicalTable> GetPhysicalTablesCore()
}
}

/// <summary>
/// Gets the common tables associated with this clause.
/// </summary>
protected override IEnumerable<CommonTable> GetCommonTablesCore()
{
foreach (var item in Value.GetCommonTables())
Expand All @@ -94,6 +131,9 @@ protected override IEnumerable<CommonTable> GetCommonTablesCore()
}
}

/// <summary>
/// Gets the tokens representing this clause.
/// </summary>
public override IEnumerable<Token> GetCurrentTokens(Token? parent)
{
foreach (var item in Value.GetTokens(parent)) yield return item;
Expand All @@ -105,4 +145,4 @@ public override IEnumerable<Token> GetCurrentTokens(Token? parent)
yield return Token.Reserved(this, parent, "and");
foreach (var item in End.GetTokens(parent)) yield return item;
}
}
}
Loading

0 comments on commit fbab4d8

Please sign in to comment.