Skip to content

Commit 669da31

Browse files
committed
refactor UpdateSetOperator finally!
1 parent 5e1fa32 commit 669da31

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

JankSQL/Contexts/UpdateContext.cs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,13 @@
44
using JankSQL.Expressions;
55
using JankSQL.Operators;
66

7-
internal enum SetOperator
8-
{
9-
ASSIGN,
10-
ADD_ASSIGN,
11-
SUB_ASSIGN,
12-
MUL_ASSIGN,
13-
DIV_ASSIGN,
14-
MOD_ASSIGN,
15-
}
16-
17-
//TODO: move to Operators (or Expressions?)
18-
internal class SetOperation
19-
{
20-
private readonly FullColumnName fcn;
21-
private readonly Expression expression;
22-
private readonly SetOperator op;
23-
24-
internal SetOperation(FullColumnName fcn, SetOperator op, Expression expression)
25-
{
26-
this.fcn = fcn;
27-
this.op = op;
28-
this.expression = expression;
29-
}
307

31-
public override string ToString()
32-
{
33-
return $"{fcn} {op} {expression}";
34-
}
35-
36-
internal void Execute(Engines.IEngine engine, IRowValueAccessor outputaccessor, IRowValueAccessor inputAccessor, Dictionary<string, ExpressionOperand> bindValues)
37-
{
38-
if (op != SetOperator.ASSIGN)
39-
throw new NotImplementedException();
40-
41-
ExpressionOperand val = expression.Evaluate(inputAccessor, engine, bindValues);
42-
outputaccessor.SetValue(fcn, val);
43-
}
44-
}
458

469
internal class UpdateContext : IExecutableContext
4710
{
4811
private readonly FullTableName tableName;
4912
private readonly TSqlParser.Update_statementContext context;
50-
private readonly List<SetOperation> setList = new ();
13+
private readonly List<UpdateSetOperation> setList = new ();
5114

5215
private Expression? predicateExpression;
5316

@@ -74,7 +37,7 @@ public object Clone()
7437

7538
clone.predicateExpression = predicateExpression != null ? (Expression)predicateExpression.Clone() : null;
7639

77-
foreach (SetOperation op in setList)
40+
foreach (UpdateSetOperation op in setList)
7841
clone.setList.Add(op);
7942

8043
return clone;
@@ -130,7 +93,7 @@ public ExecuteResult Execute(IEngine engine, IRowValueAccessor? outerAccessor, D
13093

13194
internal void AddAssignment(FullColumnName fcn, Expression x)
13295
{
133-
SetOperation op = new (fcn, SetOperator.ASSIGN, x);
96+
UpdateSetOperation op = new (fcn, UpdateSetOperator.ASSIGN, x);
13497
setList.Add(op);
13598
}
13699

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace JankSQL.Contexts
2+
{
3+
using JankSQL.Expressions;
4+
5+
internal enum UpdateSetOperator
6+
{
7+
ASSIGN,
8+
ADD_ASSIGN,
9+
SUB_ASSIGN,
10+
MUL_ASSIGN,
11+
DIV_ASSIGN,
12+
MOD_ASSIGN,
13+
}
14+
15+
internal class UpdateSetOperation
16+
{
17+
private readonly FullColumnName fcn;
18+
private readonly Expression expression;
19+
private readonly UpdateSetOperator op;
20+
21+
internal UpdateSetOperation(FullColumnName fcn, UpdateSetOperator op, Expression expression)
22+
{
23+
this.fcn = fcn;
24+
this.op = op;
25+
this.expression = expression;
26+
}
27+
28+
public override string ToString()
29+
{
30+
return $"{fcn} {op} {expression}";
31+
}
32+
33+
internal void Execute(Engines.IEngine engine, IRowValueAccessor outputaccessor, IRowValueAccessor inputAccessor, Dictionary<string, ExpressionOperand> bindValues)
34+
{
35+
if (op != UpdateSetOperator.ASSIGN)
36+
throw new NotImplementedException();
37+
38+
ExpressionOperand val = expression.Evaluate(inputAccessor, engine, bindValues);
39+
outputaccessor.SetValue(fcn, val);
40+
}
41+
}
42+
}

JankSQL/Operators/Update.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ internal class Update : IComponentOutput
1010
private readonly Expression? predicateExpression;
1111
private readonly List<ExpressionOperandBookmark> bookmarksToDelete = new ();
1212
private readonly List<Tuple> rowsToInsert = new ();
13-
private readonly List<SetOperation> setList;
13+
private readonly List<UpdateSetOperation> setList;
1414

1515
private int rowsAffected;
1616

17-
internal Update(Engines.IEngineTable targetTable, IComponentOutput input, Expression? predicateExpression, List<SetOperation> setList)
17+
internal Update(Engines.IEngineTable targetTable, IComponentOutput input, Expression? predicateExpression, List<UpdateSetOperation> setList)
1818
{
1919
myInput = input;
2020
engineTable = targetTable;

0 commit comments

Comments
 (0)