-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathParameterConvert.cs
93 lines (77 loc) · 3.19 KB
/
ParameterConvert.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
// Copyright (c) 2015 Abel Cheng <abelcys@gmail.com> and other contributors.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Repository: https://github.com/DataBooster/DbWebApi
using System;
using System.Linq;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json.Linq;
using DbParallel.DataAccess;
namespace DataBooster.DbWebApi
{
/// <summary>
/// For supporting Table-Valued Parameter (SQL Server 2008+) and Oracle Associative Array Parameter
/// </summary>
public static class ParameterConvert
{
/// <summary>
/// Convert a JArray value to an acceptable parameter type for SQL Server Table-Valued Parameter or Oracle Associative Array Parameter
/// </summary>
/// <param name="jArrayValue">A raw JSON value</param>
/// <returns>A DataTable if the first child element is JObject; Or an object[] if the first child element is JValue; Otherwise the jArrayValue itself.</returns>
public static object AsParameterValue(this JArray jArrayValue)
{
if (jArrayValue == null || !jArrayValue.HasValues)
return null;
var first = jArrayValue.First;
if (first is JObject)
return jArrayValue.ToObject<List<Dictionary<string, object>>>().ToDataTable();
if (first is JValue)
return jArrayValue.ToObject<object[]>()/*.NormalizeNumericArray()*/;
return jArrayValue;
}
/// <summary>
/// Check an input object is an acceptable parameter value type, convert if necessary.
/// </summary>
/// <param name="rawValue">A raw value object</param>
/// <returns>An acceptable type parameter value</returns>
public static object AsParameterValue(object rawValue)
{
if (rawValue == null)
return DBNull.Value;
if (rawValue is IConvertible || rawValue is DataTable || rawValue is DbDataReader || rawValue is byte[])
return rawValue;
JArray jArrayValue = rawValue as JArray;
if (jArrayValue != null)
return jArrayValue.AsParameterValue();
Array arrayValue = rawValue as Array;
if (arrayValue != null)
{
if (arrayValue.Length == 0)
return null; // null: not to send the parameter -- To review for empty SQL Server Table-Valued Parameter and Oracle Associative Array Parameter
if (arrayValue.GetValue(0) is IConvertible)
return arrayValue;
}
Type elementType = rawValue.GetType().GetEnumerableElementType();
if (elementType == null)
return rawValue;
else
return (rawValue as IEnumerable).AsParameterValue(elementType);
}
/// <summary>
/// Pretreat input parameters dictionary, check each input object is an acceptable parameter value type, convert if necessary.
/// </summary>
/// <param name="rawParameters">A input parameters dictionary</param>
/// <returns>A new dictionary that contains all pretreated input parameters</returns>
public static IDictionary<string, object> PretreatInputDictionary(this IDictionary<string, object> rawParameters)
{
if (rawParameters == null || rawParameters.Count == 0)
return rawParameters;
else
return rawParameters.ToDictionary(p => p.Key, p => AsParameterValue(p.Value), StringComparer.OrdinalIgnoreCase);
}
}
}