Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ EvalFunction()

object System.Data.FunctionNode.EvalFunction ( FunctionId id,
object[] argumentValues,
DataRow row,
DataRowVersion version )
inlineprivate

Definition at line 287 of file FunctionNode.cs.

288 {
289 switch (id)
290 {
291 case FunctionId.Abs:
292 {
294 if (ExpressionNode.IsInteger(storageType2))
295 {
296 return Math.Abs((long)argumentValues[0]);
297 }
298 if (ExpressionNode.IsNumeric(storageType2))
299 {
300 return Math.Abs((double)argumentValues[0]);
301 }
302 throw ExprException.ArgumentTypeInteger(s_funcs[_info]._name, 1);
303 }
304 case FunctionId.cBool:
305 return DataStorage.GetStorageType(argumentValues[0].GetType()) switch
306 {
308 StorageType.Int32 => (int)argumentValues[0] != 0,
309 StorageType.Double => (double)argumentValues[0] != 0.0,
310 StorageType.String => bool.Parse((string)argumentValues[0]),
311 _ => throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool)),
312 };
313 case FunctionId.cInt:
314 return Convert.ToInt32(argumentValues[0], base.FormatProvider);
315 case FunctionId.cDate:
316 return Convert.ToDateTime(argumentValues[0], base.FormatProvider);
317 case FunctionId.cDbl:
318 return Convert.ToDouble(argumentValues[0], base.FormatProvider);
319 case FunctionId.cStr:
320 return Convert.ToString(argumentValues[0], base.FormatProvider);
321 case FunctionId.Charindex:
323 {
324 return DBNull.Value;
325 }
327 {
328 argumentValues[0] = ((SqlString)argumentValues[0]).Value;
329 }
331 {
332 argumentValues[1] = ((SqlString)argumentValues[1]).Value;
333 }
334 return ((string)argumentValues[1]).IndexOf((string)argumentValues[0], StringComparison.Ordinal);
335 case FunctionId.Iif:
336 {
337 object value = _arguments[0].Eval(row, version);
338 if (DataExpression.ToBoolean(value))
339 {
340 return _arguments[1].Eval(row, version);
341 }
342 return _arguments[2].Eval(row, version);
343 }
344 case FunctionId.In:
345 throw ExprException.NYI(s_funcs[_info]._name);
346 case FunctionId.IsNull:
348 {
349 return argumentValues[1];
350 }
351 return argumentValues[0];
352 case FunctionId.Len:
354 {
356 {
357 return DBNull.Value;
358 }
359 argumentValues[0] = ((SqlString)argumentValues[0]).Value;
360 }
361 return ((string)argumentValues[0]).Length;
362 case FunctionId.Substring:
363 {
364 int num = (int)argumentValues[1] - 1;
365 int num2 = (int)argumentValues[2];
366 if (num < 0)
367 {
368 throw ExprException.FunctionArgumentOutOfRange("index", "Substring");
369 }
370 if (num2 < 0)
371 {
372 throw ExprException.FunctionArgumentOutOfRange("length", "Substring");
373 }
374 if (num2 == 0)
375 {
376 return string.Empty;
377 }
379 {
380 argumentValues[0] = ((SqlString)argumentValues[0]).Value;
381 }
382 int length = ((string)argumentValues[0]).Length;
383 if (num > length)
384 {
385 return DBNull.Value;
386 }
387 if (num + num2 > length)
388 {
389 num2 = length - num;
390 }
391 return ((string)argumentValues[0]).Substring(num, num2);
392 }
393 case FunctionId.Trim:
395 {
396 return DBNull.Value;
397 }
399 {
400 argumentValues[0] = ((SqlString)argumentValues[0]).Value;
401 }
402 return ((string)argumentValues[0]).Trim();
403 case FunctionId.Convert:
404 {
405 if (_argumentCount != 2)
406 {
407 throw ExprException.FunctionArgumentCount(_name);
408 }
409 if (argumentValues[0] == DBNull.Value)
410 {
411 return DBNull.Value;
412 }
416 if (storageType == StorageType.DateTimeOffset && storageType2 == StorageType.String)
417 {
418 return SqlConvert.ConvertStringToDateTimeOffset((string)argumentValues[0], base.FormatProvider);
419 }
420 if (StorageType.Object != storageType)
421 {
422 if (storageType == StorageType.Guid && storageType2 == StorageType.String)
423 {
424 return new Guid((string)argumentValues[0]);
425 }
426 if (ExpressionNode.IsFloatSql(storageType2) && ExpressionNode.IsIntegerSql(storageType))
427 {
428 if (StorageType.Single == storageType2)
429 {
430 return SqlConvert.ChangeType2((float)SqlConvert.ChangeType2(argumentValues[0], StorageType.Single, typeof(float), base.FormatProvider), storageType, type, base.FormatProvider);
431 }
432 if (StorageType.Double == storageType2)
433 {
434 return SqlConvert.ChangeType2((double)SqlConvert.ChangeType2(argumentValues[0], StorageType.Double, typeof(double), base.FormatProvider), storageType, type, base.FormatProvider);
435 }
436 if (StorageType.Decimal == storageType2)
437 {
438 return SqlConvert.ChangeType2((decimal)SqlConvert.ChangeType2(argumentValues[0], StorageType.Decimal, typeof(decimal), base.FormatProvider), storageType, type, base.FormatProvider);
439 }
440 }
443 {
444 return SqlConvert.ChangeType2(argumentValues[0], storageType, type, base.FormatProvider);
445 }
446 }
447 return argumentValues[0];
448 }
449 case FunctionId.DateTimeOffset:
450 if (argumentValues[0] == DBNull.Value || argumentValues[1] == DBNull.Value || argumentValues[2] == DBNull.Value)
451 {
452 return DBNull.Value;
453 }
454 switch (((DateTime)argumentValues[0]).Kind)
455 {
456 case DateTimeKind.Utc:
457 if ((int)argumentValues[1] != 0 && (int)argumentValues[2] != 0)
458 {
459 throw ExprException.MismatchKindandTimeSpan();
460 }
461 break;
462 case DateTimeKind.Local:
463 if (DateTimeOffset.Now.Offset.Hours != (int)argumentValues[1] && DateTimeOffset.Now.Offset.Minutes != (int)argumentValues[2])
464 {
465 throw ExprException.MismatchKindandTimeSpan();
466 }
467 break;
468 }
469 if ((int)argumentValues[1] < -14 || (int)argumentValues[1] > 14)
470 {
471 throw ExprException.InvalidHoursArgument();
472 }
473 if ((int)argumentValues[2] < -59 || (int)argumentValues[2] > 59)
474 {
475 throw ExprException.InvalidMinutesArgument();
476 }
477 if ((int)argumentValues[1] == 14 && (int)argumentValues[2] > 0)
478 {
479 throw ExprException.InvalidTimeZoneRange();
480 }
481 if ((int)argumentValues[1] == -14 && (int)argumentValues[2] < 0)
482 {
483 throw ExprException.InvalidTimeZoneRange();
484 }
485 return new DateTimeOffset((DateTime)argumentValues[0], new TimeSpan((int)argumentValues[1], (int)argumentValues[2], 0));
486 default:
487 throw ExprException.UndefinedFunction(s_funcs[_info]._name);
488 }
489 }
static bool IsObjectNull(object value)
static StorageType GetStorageType(Type dataType)
static DateTimeOffset ConvertStringToDateTimeOffset(string value, IFormatProvider formatProvider)
static object ChangeType2(object value, StorageType stype, Type type, IFormatProvider formatProvider)
ExpressionNode(DataTable table)
readonly TypeLimiter _capturedLimiter
static readonly Function[] s_funcs
readonly string _name
ExpressionNode[] _arguments
static DeserializationToken StartDeserialization()

References System.Data.FunctionNode._argumentCount, System.Data.FunctionNode._arguments, System.Data.FunctionNode._capturedLimiter, System.Data.FunctionNode._info, System.Data.FunctionNode._name, System.Math.Abs(), System.Data.ExprException.ArgumentTypeInteger(), System.Data.Common.SqlConvert.ChangeType2(), System.Data.Common.SqlConvert.ConvertStringToDateTimeOffset(), System.Data.ExprException.DatatypeConvertion(), System.Data.DateTimeOffset, System.Runtime.Serialization.Dictionary, System.Data.ExpressionNode.Eval(), System.Data.ExprException.FunctionArgumentCount(), System.Data.ExprException.FunctionArgumentOutOfRange(), System.Data.Common.DataStorage.GetStorageType(), System.Data.Guid, System.Data.ExprException.InvalidHoursArgument(), System.Data.ExprException.InvalidMinutesArgument(), System.Data.ExprException.InvalidTimeZoneRange(), System.Data.ExpressionNode.IsFloatSql(), System.Data.ExpressionNode.IsInteger(), System.Data.ExpressionNode.IsIntegerSql(), System.Data.IsNull, System.Data.ExpressionNode.IsNumeric(), System.Data.Common.DataStorage.IsObjectNull(), System.length, System.Data.ExprException.MismatchKindandTimeSpan(), System.DateTimeOffset.Now, System.Data.ExprException.NYI(), System.Data.FunctionNode.s_funcs, System.Data.Common.SqlString, System.Runtime.Serialization.SerializationInfo.StartDeserialization(), System.Data.Substring, System.Data.Common.TimeSpan, System.Data.DataExpression.ToBoolean(), System.Convert.ToDateTime(), System.Convert.ToDouble(), System.Convert.ToInt32(), System.Convert.ToString(), System.Data.Trim, System.Data.Common.Type, System.type, System.Data.ExprException.UndefinedFunction(), System.DBNull.Value, and System.value.

Referenced by System.Data.FunctionNode.Eval().