Skip to content

Commit

Permalink
Merge pull request #403 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 6, 2024
2 parents c21aa83 + c783b57 commit 9ba70ba
Show file tree
Hide file tree
Showing 55 changed files with 890 additions and 152 deletions.
63 changes: 47 additions & 16 deletions src/Carbunql/Analysis/Parser/AlterCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses alter commands based on provided table and token reader.
/// </summary>
public class AlterCommandParser
{
//public static IAlterCommand Parse(ITable t, string text)
//{
// var r = new SqlTokenReader(text);
// var q = Parse(t, r);
// return q;
//}

/// <summary>
/// Parses the alter command.
/// </summary>
/// <param name="t">The table to alter.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed alter command.</returns>
/// <exception cref="NotSupportedException">Thrown when the token is not supported.</exception>
public static IAlterCommand Parse(ITable t, ITokenReader r)
{
var token = r.Peek();
Expand All @@ -22,7 +25,7 @@ public static IAlterCommand Parse(ITable t, ITokenReader r)

if (token.IsEqualNoCase("drop"))
{
return ParseAdDropCommand(t, r);
return ParseAsDropCommand(t, r);
}

if (token.IsEqualNoCase("alter column"))
Expand All @@ -34,9 +37,16 @@ public static IAlterCommand Parse(ITable t, ITokenReader r)
{
return ParseAsRenameCommand(t, r);
}
throw new NotSupportedException($"Token:{token}");
throw new NotSupportedException($"Unsupported token:{token}");
}

/// <summary>
/// Parses the "add" command.
/// </summary>
/// <param name="t">The table to alter.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed alter command.</returns>
/// <exception cref="NotSupportedException">Thrown when the token is not supported.</exception>
private static IAlterCommand ParseAsAddCommand(ITable t, ITokenReader r)
{
r.Read("add");
Expand All @@ -54,15 +64,22 @@ private static IAlterCommand ParseAsAddCommand(ITable t, ITokenReader r)
}
else
{
//Consider "add column" with "column" omitted.
// Consider "add column" with "column" omitted.
var definition = ColumnDefinitionParser.Parse(t, r);
return new AddColumnCommand(definition);
}

//throw new NotSupportedException($"Token:{token}");
throw new NotSupportedException($"Unsupported token:{token}");
}

private static IAlterCommand ParseAdDropCommand(ITable t, ITokenReader r)
/// <summary>
/// Parses the "drop" command.
/// </summary>
/// <param name="t">The table to alter.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed alter command.</returns>
/// <exception cref="NotSupportedException">Thrown when the token is not supported.</exception>
private static IAlterCommand ParseAsDropCommand(ITable t, ITokenReader r)
{
r.Read("drop");
var target = r.Read();
Expand All @@ -76,9 +93,16 @@ private static IAlterCommand ParseAdDropCommand(ITable t, ITokenReader r)
var name = r.Read();
return new DropConstraintCommand(t, name);
}
throw new NotSupportedException();
throw new NotSupportedException($"Unsupported token:{target}");
}

/// <summary>
/// Parses the "alter column" command.
/// </summary>
/// <param name="t">The table to alter.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed alter command.</returns>
/// <exception cref="NotSupportedException">Thrown when the token is not supported.</exception>
private static IAlterCommand ParseAsAlterColumnCommand(ITable t, ITokenReader r)
{
r.Read("alter column");
Expand Down Expand Up @@ -116,24 +140,31 @@ private static IAlterCommand ParseAsAlterColumnCommand(ITable t, ITokenReader r)
var columnType = ValueParser.Parse(r);
return new ChangeColumnTypeCommand(t, column, columnType);
}
throw new NotSupportedException();

throw new NotSupportedException($"Unsupported token:{token}");
}

/// <summary>
/// Parses the "rename" command.
/// </summary>
/// <param name="t">The table to alter.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed alter command.</returns>
private static IAlterCommand ParseAsRenameCommand(ITable t, ITokenReader r)
{
r.Read("rename");
var token = r.Read();
if (token.IsEqualNoCase("column"))
{
//rename column
// Rename column.
var oldName = r.Read();
r.Read("to");
var newName = r.Read();
return new RenameColumnCommand(t, oldName, newName);
}
else
{
//rename table
// Rename table.
return new RenameTableCommand(t, token);
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/Carbunql/Analysis/Parser/AlterTableClauseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses ALTER TABLE clauses from SQL text or token streams.
/// </summary>
public static class AlterTableClauseParser
{
/// <summary>
/// Parses the ALTER TABLE clause from SQL text.
/// </summary>
/// <param name="text">The SQL text containing the ALTER TABLE clause.</param>
/// <returns>The parsed ALTER TABLE clause.</returns>
/// <exception cref="NotSupportedException">Thrown when parsing is terminated due to unparsed tokens.</exception>
public static AlterTableClause Parse(string text)
{
var r = new SqlTokenReader(text);
var q = Parse(r);

if (!r.Peek().IsEndToken())
{
throw new NotSupportedException($"Parsing terminated despite the presence of unparsed tokens.(token:'{r.Peek()}')");
throw new NotSupportedException($"Parsing terminated despite the presence of unparsed tokens. (Token:'{r.Peek()}')");
}

return q;
}

/// <summary>
/// Parses the ALTER TABLE clause from the token stream.
/// </summary>
/// <param name="r">The token reader.</param>
/// <returns>The parsed ALTER TABLE clause.</returns>
public static AlterTableClause Parse(ITokenReader r)
{
var t = ParseAsAlterTableCommand(r);
Expand All @@ -28,6 +42,11 @@ public static AlterTableClause Parse(ITokenReader r)
return t;
}

/// <summary>
/// Parses the "alter table" command.
/// </summary>
/// <param name="r">The token reader.</param>
/// <returns>The parsed ALTER TABLE clause.</returns>
private static AlterTableClause ParseAsAlterTableCommand(ITokenReader r)
{
r.Read("alter table");
Expand All @@ -49,4 +68,4 @@ private static AlterTableClause ParseAsAlterTableCommand(ITokenReader r)

return new AlterTableClause(schema, table);
}
}
}
18 changes: 15 additions & 3 deletions src/Carbunql/Analysis/Parser/ArrayParser.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses an array from SQL text or token streams.
/// </summary>
public class ArrayParser
{
/// <summary>
/// Parses an array from SQL text.
/// </summary>
/// <param name="text">The SQL text containing the array.</param>
/// <returns>The parsed array.</returns>
public static List<string> Parse(string text)
{
var r = new SqlTokenReader(text);
var q = Parse(r);
return q;
return Parse(r);
}

/// <summary>
/// Parses an array from the token stream.
/// </summary>
/// <param name="r">The token reader.</param>
/// <returns>The parsed array.</returns>
public static List<string> Parse(ITokenReader r)
{
var lst = new List<string>();
Expand All @@ -24,4 +36,4 @@ public static List<string> Parse(ITokenReader r)

return lst;
}
}
}
17 changes: 15 additions & 2 deletions src/Carbunql/Analysis/Parser/ArrayValueParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses an array value from SQL text or token streams.
/// </summary>
public static class ArrayValueParser
{
/// <summary>
/// Parses an array value from SQL text.
/// </summary>
/// <param name="argument">The SQL text containing the array value.</param>
/// <returns>The parsed array value.</returns>
public static ValueBase Parse(string argument)
{
var r = new SqlTokenReader(argument);
return Parse(r);
}

/// <summary>
/// Parses an array value from the token stream.
/// </summary>
/// <param name="r">The token reader.</param>
/// <returns>The parsed array value.</returns>
public static ValueBase Parse(ITokenReader r)
{
var token = r.Read("array");
Expand All @@ -28,6 +41,6 @@ public static ValueBase Parse(ITokenReader r)
var value = BracketValueParser.Parse(r);
return new ArrayValue(value);
}
throw new NotSupportedException();
throw new NotSupportedException($"Unsupported token:{next}");
}
}
}
15 changes: 15 additions & 0 deletions src/Carbunql/Analysis/Parser/AsArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses an AS argument from SQL text or token streams.
/// </summary>
public static class AsArgumentParser
{
/// <summary>
/// Parses an AS argument from SQL text.
/// </summary>
/// <param name="value">The value to be parsed as an argument.</param>
/// <param name="argument">The SQL text containing the AS argument.</param>
/// <returns>The parsed AS argument.</returns>
public static AsArgument Parse(ValueBase value, string argument)
{
var r = new SqlTokenReader(argument);
return Parse(value, r);
}

/// <summary>
/// Parses an AS argument from the token stream.
/// </summary>
/// <param name="value">The value to be parsed as an argument.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed AS argument.</returns>
public static AsArgument Parse(ValueBase value, ITokenReader r)
{
var type = ValueParser.Parse(r);
Expand Down
11 changes: 10 additions & 1 deletion src/Carbunql/Analysis/Parser/AtTimeZoneClauseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses an AT TIME ZONE clause from the token stream.
/// </summary>
public static class AtTimeZoneClauseParser
{
/// <summary>
/// Parses an AT TIME ZONE clause from the token stream.
/// </summary>
/// <param name="value">The value to be associated with the time zone.</param>
/// <param name="r">The token reader.</param>
/// <returns>The parsed AT TIME ZONE clause.</returns>
public static AtTimeZoneClause Parse(ValueBase value, ITokenReader r)
{
r.Read("at time zone");
var timezone = ValueParser.Parse(r);
return new AtTimeZoneClause(value, timezone);
}
}
}
18 changes: 17 additions & 1 deletion src/Carbunql/Analysis/Parser/BetweenClauseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses a BETWEEN clause from SQL text or token streams.
/// </summary>
public static class BetweenClauseParser
{
/// <summary>
/// Parses a BETWEEN clause from SQL text.
/// </summary>
/// <param name="value">The value to be associated with the BETWEEN clause.</param>
/// <param name="argument">The SQL text containing the BETWEEN clause.</param>
/// <returns>The parsed BETWEEN clause.</returns>
public static BetweenClause Parse(ValueBase value, string argument)
{
var r = new SqlTokenReader(argument);
return Parse(value, r, false);
}

/// <summary>
/// Parses a BETWEEN clause from the token stream.
/// </summary>
/// <param name="value">The value to be associated with the BETWEEN clause.</param>
/// <param name="r">The token reader.</param>
/// <param name="isNegative">Indicates whether the BETWEEN clause is negative.</param>
/// <returns>The parsed BETWEEN clause.</returns>
public static BetweenClause Parse(ValueBase value, ITokenReader r, bool isNegative)
{
r.Read("between");
Expand All @@ -19,4 +35,4 @@ public static BetweenClause Parse(ValueBase value, ITokenReader r, bool isNegati
var end = ValueParser.ParseCore(r);
return new BetweenClause(value, start, end, isNegative);
}
}
}
13 changes: 13 additions & 0 deletions src/Carbunql/Analysis/Parser/BracketValueParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@

namespace Carbunql.Analysis.Parser;

/// <summary>
/// Parses a bracket value from token streams.
/// </summary>
public static class BracketValueParser
{
/// <summary>
/// Checks if the text represents a bracket value.
/// </summary>
/// <param name="text">The text to check.</param>
/// <returns>True if the text represents a bracket value; otherwise, false.</returns>
public static bool IsBracketValue(string text)
{
return text == "(";
}

/// <summary>
/// Parses a bracket value from the token stream.
/// </summary>
/// <param name="r">The token reader.</param>
/// <returns>The parsed bracket value.</returns>
public static ValueBase Parse(ITokenReader r)
{
using var ir = new BracketInnerTokenReader(r);
Expand Down
Loading

0 comments on commit 9ba70ba

Please sign in to comment.