forked from ServiceStack/ServiceStack.OrmLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql.cs
103 lines (84 loc) · 2.43 KB
/
Sql.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
using System.Collections;
using System.Linq;
using System.Collections.Generic;
namespace ServiceStack.OrmLite
{
public static class Sql
{
public static bool In<T, TItem>(T value, params TItem[] list)
{
return value != null && Flatten(list).Any(obj => obj.ToString() == value.ToString());
}
public static bool In<T, TItem>(T value, SqlExpression<TItem> query)
{
return value != null && query != null;
}
public static List<object> Flatten(IEnumerable list)
{
var ret = new List<object>();
if (list == null) return ret;
foreach (var item in list)
{
if (item == null) continue;
var arr = item as IEnumerable;
if (arr != null && !(item is string))
{
ret.AddRange(arr.Cast<object>());
}
else
{
ret.Add(item);
}
}
return ret;
}
public static string Desc<T>(T value)
{
return value == null ? "" : value.ToString() + " DESC";
}
public static string As<T>(T value, object asValue)
{
return value == null ? "" : string.Format("{0} AS {1}", value.ToString(), asValue);
}
public static T Sum<T>(T value)
{
return value;
}
public static string Sum(string value)
{
return "SUM({0})".Fmt(value);
}
public static T Count<T>(T value)
{
return value;
}
public static string Count(string value)
{
return "COUNT({0})".Fmt(value);
}
public static T Min<T>(T value)
{
return value;
}
public static string Min(string value)
{
return "MIN({0})".Fmt(value);
}
public static T Max<T>(T value)
{
return value;
}
public static string Max(string value)
{
return "MAX({0})".Fmt(value);
}
public static T Avg<T>(T value)
{
return value;
}
public static string Avg(string value)
{
return "AVG({0})".Fmt(value);
}
}
}