1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
//------------------------------------------------------------------------------
// <copyright file="NameNode.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
// <owner current="false" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data {
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
internal sealed class NameNode : ExpressionNode {
internal char open = '\0';
internal char close = '\0';
internal string name;
internal bool found;
internal bool type = false;
internal DataColumn column;
internal NameNode(DataTable table, char[] text, int start, int pos) : base(table) {
this.name = ParseName(text, start, pos);
}
internal NameNode(DataTable table, string name) : base(table) {
this.name = name;
}
internal override bool IsSqlColumn{
get{
return column.IsSqlType;
}
}
internal override void Bind(DataTable table, List<DataColumn> list) {
BindTable(table);
if (table == null)
throw ExprException.UnboundName(name);
try {
this.column = table.Columns[name];
}
catch (Exception e) {
found = false;
//
if (!Common.ADP.IsCatchableExceptionType(e)) {
throw;
}
throw ExprException.UnboundName(name);
}
if (column == null)
throw ExprException.UnboundName(name);
name = column.ColumnName;
found = true;
// add column to the dependency list, do not add duplicate columns
Debug.Assert(column != null, "Failed to bind column " + name);
int i;
for (i = 0; i < list.Count; i++) {
// walk the list, check if the current column already on the list
DataColumn dataColumn = list[i];
if (column == dataColumn) {
break;
}
}
if (i >= list.Count) {
list.Add(column);
}
}
internal override object Eval() {
// can not eval column without ROW value;
throw ExprException.EvalNoContext();
}
internal override object Eval(DataRow row, DataRowVersion version) {
if (!found) {
throw ExprException.UnboundName(name);
}
if (row == null) {
if(IsTableConstant()) // this column is TableConstant Aggregate Function
return column.DataExpression.Evaluate();
else {
throw ExprException.UnboundName(name);
}
}
return column[row.GetRecordFromVersion(version)];
}
internal override object Eval(int[] records) {
throw ExprException.ComputeNotAggregate(this.ToString());
}
internal override bool IsConstant() {
return false;
}
internal override bool IsTableConstant() {
if (column != null && column.Computed) {
return this.column.DataExpression.IsTableAggregate();
}
return false;
}
internal override bool HasLocalAggregate() {
if (column != null && column.Computed) {
return this.column.DataExpression.HasLocalAggregate();
}
return false;
}
internal override bool HasRemoteAggregate() {
if (column != null && column.Computed) {
return this.column.DataExpression.HasRemoteAggregate();
}
return false;
}
internal override bool DependsOn(DataColumn column) {
if (this.column == column)
return true;
if (this.column.Computed) {
return this.column.DataExpression.DependsOn(column);
}
return false;
}
internal override ExpressionNode Optimize() {
return this;
}
/// <devdoc>
/// Parses given name and checks it validity
/// </devdoc>
internal static string ParseName(char[] text, int start, int pos) {
char esc = '\0';
string charsToEscape = "";
int saveStart = start;
int savePos = pos;
if (text[start] == '`') {
Debug.Assert(text[checked((int)pos-1)] == '`', "Invalid identifyer bracketing, pos = " + pos.ToString(CultureInfo.InvariantCulture) + ", end = " + text[checked((int)pos-1)].ToString(CultureInfo.InvariantCulture));
start = checked((int)start+1);
pos = checked((int)pos-1);
esc = '\\';
charsToEscape = "`";
}
else if (text[start] == '[') {
Debug.Assert(text[checked((int)pos-1)] == ']', "Invalid identifyer bracketing of name " + new string(text, start, pos-start) + " pos = " + pos.ToString(CultureInfo.InvariantCulture) + ", end = " + text[checked((int)pos-1)].ToString(CultureInfo.InvariantCulture));
start = checked((int)start+1);
pos = checked((int)pos-1);
esc = '\\';
charsToEscape = "]\\";
}
if (esc != '\0') {
// scan the name in search for the ESC
int posEcho = start;
for (int i = start; i < pos; i++) {
if (text[i] == esc) {
if (i+1 < pos && charsToEscape.IndexOf(text[i+1]) >= 0) {
i++;
}
}
text[posEcho] = text[i];
posEcho++;
}
pos = posEcho;
}
if (pos == start)
throw ExprException.InvalidName(new string(text, saveStart, savePos - saveStart));
return new string(text, start, pos - start);
}
}
}
|