forked from georgebarwood/Database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpStd.cs
241 lines (207 loc) · 6.36 KB
/
ExpStd.cs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
namespace SQLNS
{
using G = System.Collections.Generic;
using DBNS;
// Standard functions.
class StdExp : Exp
{
protected Exp [] Arg;
DataType [] Types;
public StdExp( G.List<Exp>arg, DataType type, DataType [] types, Exec e )
{
Arg = arg.ToArray();
Type = type;
Types = types;
if ( Arg.Length != Types.Length ) e.Error( this + " takes " + Types.Length + " argument(s)" );
}
public override DataType Bind( SqlExec e )
{
for ( int i = 0; i < Types.Length; i += 1 )
{
DataType t = Arg[i].Bind( e );
if ( t != Types[ i ] )
e.Error( this + " parameter type error, arg " + i + " expected "
+ DTI.Name( Types[ i ] ) + " actual " + DTI.Name( t ) );
}
return Type;
}
}
class EXCEPTION : Exp
{
public EXCEPTION( G.List<Exp> parms, Exec e )
{
if ( parms.Count != 0 ) e.Error( "EXCEPTION takes no parameters" );
Type = DataType.String;
}
public override Value Eval( EvalEnv e )
{
Value result = new Value();
var ex = e.ResultSet.Exception;
result.O = ex == null ? "" : ex.ToString(); // .Message or .ToString() for full debug info.
e.ResultSet.Exception = null;
return result;
}
} // end class EXCEPTION
class REPLACE : StdExp
{
public REPLACE( G.List<Exp>args, Exec e ) : base( args, DataType.String,
new DataType[]{ DataType.String, DataType.String, DataType.String }, e ){}
public override Value Eval( EvalEnv e )
{
Value s = Arg[0].Eval( e );
string pat = (string)( Arg[1].Eval( e )._O );
string sub = (string)( Arg[2].Eval( e )._O );
s.O = ((string)s._O).Replace( pat, sub );
return s;
}
} // end class REPLACE
class SUBSTRING : StdExp
{
public SUBSTRING( G.List<Exp>args, Exec e ) : base( args, DataType.String,
new DataType[]{ DataType.String, DataType.Bigint, DataType.Bigint }, e ){}
public override Value Eval( EvalEnv e )
{
Value s = Arg[0].Eval( e );
string x = (string)s._O;
int start = (int) Arg[1].Eval( e ).L - 1;
int len = (int) Arg[2].Eval( e ).L;
if ( start < 0 ) start = 0;
if ( start > x.Length ) start = x.Length;
if ( len < 0 ) len = 0; // Maybe should raise exception
if ( len > x.Length - start ) len = x.Length - start;
s.O = ((string)s._O).Substring( start, len );
return s;
}
} // end class SUBSTRING
class LEN : UnaryExp
{
DataType SType;
public LEN( G.List<Exp>args, Exec e )
{
if ( args.Count != 1 ) e.Error( this + "takes one argument" );
E = args[0];
Type = DataType.Bigint;
}
public override DataType Bind( SqlExec e )
{
SType = E.Bind( e );
if ( SType != DataType.String && SType != DataType.Binary ) e.Error( "LEN argument must be string or binary" );
return Type;
}
public override Value Eval( EvalEnv e )
{
object o = E.Eval( e )._O;
Value result = new Value();
result.L = SType == DataType.String ? ((string)o).Length : ((byte[])o).Length;
return result;
}
} // end class LEN
class PARSEINT : StdExp
{
public PARSEINT( G.List<Exp>args, Exec e ) : base( args, DataType.Bigint, new DataType[]{ DataType.String }, e ){}
public override Value Eval( EvalEnv e )
{
string s = (string)Arg[0].Eval( e )._O;
try
{
Value result = new Value();
result.L = long.Parse( s );
return result;
}
catch ( System.Exception )
{
throw new System.Exception( "Error converting [" + s + "] to integer" );
}
}
} // end class PARSEINT
class PARSEDECIMAL : StdExp
{
public PARSEDECIMAL( G.List<Exp>args, Exec e ) : base( args, DataType.ScaledInt, new DataType[]{ DataType.String, DataType.Bigint }, e ){}
public override Value Eval( EvalEnv e )
{
string s = (string)Arg[0].Eval( e )._O;
DataType t = (DataType)Arg[1].Eval( e ).L;
try
{
Value result = new Value();
result.L = (long) ( decimal.Parse( s ) * Util.PowerTen( DTI.Scale(t) ) );
return result;
}
catch ( System.Exception )
{
throw new System.Exception( "Error converting [" + s + "] to decimal" );
}
}
} // end class PARSEDECIMAL
class PARSEDOUBLE : StdExp
{
public PARSEDOUBLE( G.List<Exp>args, Exec e ) : base( args, DataType.Double, new DataType[]{ DataType.String }, e ){}
public override Value Eval( EvalEnv e )
{
string s = (string)Arg[0].Eval( e )._O;
try
{
Value result = new Value();
result.D = double.Parse( s );
return result;
}
catch ( System.Exception )
{
throw new System.Exception( "Error converting [" + s + "] to double" );
}
}
} // end class PARSEDOUBLE
// Functions which access the global state : LASTID, EXCEPTION, ARG, FILEATTR, FILECONTENT.
class LASTID : Exp
{
public LASTID( G.List<Exp> parms, Exec e )
{
if ( parms.Count != 0 ) e.Error( "LASTID takes no parameters" );
Type = DataType.Bigint;
}
public override Value Eval( EvalEnv e )
{
Value result = new Value();
result.L = e.ResultSet.LastIdInserted;
return result;
}
} // end class LASTID
class ARG : StdExp
{
public ARG( G.List<Exp>args, Exec e ) : base( args, DataType.String,
new DataType[]{ DataType.Bigint, DataType.String }, e ){}
public override Value Eval( EvalEnv e )
{
int kind = (int)Arg[0].Eval( e ).L;
string name = (string)Arg[1].Eval( e )._O;
Value result = new Value();
result.O = e.ResultSet.Arg( kind, name );
return result;
}
} // end class ARG
class FILEATTR : StdExp
{
public FILEATTR( G.List<Exp>args, Exec e ) : base( args, DataType.String,
new DataType[]{ DataType.Bigint, DataType.Bigint }, e ){}
public override Value Eval( EvalEnv e )
{
Value result = new Value();
int ix = (int)Arg[0].Eval( e ).L;
int kind = (int)Arg[1].Eval( e ).L;
result.O = e.ResultSet.FileAttr( ix, kind );
return result;
}
} // end class FILEATTR
class FILECONTENT : StdExp
{
public FILECONTENT( G.List<Exp>args, Exec e ) : base( args, DataType.Binary,
new DataType[]{ DataType.Bigint }, e ){}
public override Value Eval( EvalEnv e )
{
Value result = new Value();
int ix = (int)Arg[0].Eval( e ).L;
result.O = e.ResultSet.FileContent( ix );
return result;
}
} // end class FILECONTENT
} // end namespace SQLNS