|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Data; |
| 6 | + |
| 7 | +namespace ServiceStack.OrmLite |
| 8 | +{ |
| 9 | + public class OrmLiteSPStatement |
| 10 | + { |
| 11 | + private IDbCommand command { get; set; } |
| 12 | + |
| 13 | + public OrmLiteSPStatement(IDbCommand cmd) |
| 14 | + { |
| 15 | + command = cmd; |
| 16 | + } |
| 17 | + |
| 18 | + public List<T> ConvertToList<T>() where T : new() |
| 19 | + { |
| 20 | + if ((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String))) |
| 21 | + throw new Exception("Type " + typeof(T).Name + " is a primitive type. Use ConvertScalarToList function."); |
| 22 | + |
| 23 | + IDataReader reader = null; |
| 24 | + try |
| 25 | + { |
| 26 | + reader = command.ExecuteReader(); |
| 27 | + return reader.ConvertToList<T>(); |
| 28 | + } |
| 29 | + finally |
| 30 | + { |
| 31 | + if (reader != null) |
| 32 | + reader.Close(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public List<T> ConvertToScalarList<T>() |
| 37 | + { |
| 38 | + if (!((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String)))) |
| 39 | + throw new Exception("Type " + typeof(T).Name + " is a non primitive type. Use ConvertToList function."); |
| 40 | + |
| 41 | + IDataReader reader = null; |
| 42 | + try |
| 43 | + { |
| 44 | + reader = command.ExecuteReader(); |
| 45 | +#pragma warning disable 618 |
| 46 | + return reader.GetFirstColumn<T>(); |
| 47 | +#pragma warning restore 618 |
| 48 | + } |
| 49 | + finally |
| 50 | + { |
| 51 | + if (reader != null) |
| 52 | + reader.Close(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public T ConvertTo<T>() where T : new() |
| 57 | + { |
| 58 | + if ((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String))) |
| 59 | + throw new Exception("Type " + typeof(T).Name + " is a primitive type. Use ConvertScalarTo function."); |
| 60 | + |
| 61 | + IDataReader reader = null; |
| 62 | + try |
| 63 | + { |
| 64 | + reader = command.ExecuteReader(); |
| 65 | + return reader.ConvertTo<T>(); |
| 66 | + } |
| 67 | + finally |
| 68 | + { |
| 69 | + if (reader != null) |
| 70 | + reader.Close(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public T ConvertToScalar<T>() |
| 75 | + { |
| 76 | + if (!((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String)))) |
| 77 | + throw new Exception("Type " + typeof(T).Name + " is a non primitive type. Use ConvertTo function."); |
| 78 | + |
| 79 | + IDataReader reader = null; |
| 80 | + try |
| 81 | + { |
| 82 | + reader = command.ExecuteReader(); |
| 83 | +#pragma warning disable 618 |
| 84 | + return reader.GetScalar<T>(); |
| 85 | +#pragma warning restore 618 |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + if (reader != null) |
| 90 | + reader.Close(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public List<T> ConvertFirstColumnToList<T>() |
| 95 | + { |
| 96 | + if (!((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String)))) |
| 97 | + throw new Exception("Type " + typeof(T).Name + " is a non primitive type. Only primitive type can be used."); |
| 98 | + |
| 99 | + IDataReader reader = null; |
| 100 | + try |
| 101 | + { |
| 102 | + reader = command.ExecuteReader(); |
| 103 | +#pragma warning disable 618 |
| 104 | + return reader.GetFirstColumn<T>(); |
| 105 | +#pragma warning restore 618 |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + if (reader != null) |
| 110 | + reader.Close(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public HashSet<T> ConvertFirstColumnToListDistinct<T>() where T : new() |
| 115 | + { |
| 116 | + if (!((typeof(T).IsPrimitive) || (typeof(T) == typeof(string)) || (typeof(T) == typeof(String)))) |
| 117 | + throw new Exception("Type " + typeof(T).Name + " is a non primitive type. Only primitive type can be used."); |
| 118 | + |
| 119 | + IDataReader reader = null; |
| 120 | + try |
| 121 | + { |
| 122 | + reader = command.ExecuteReader(); |
| 123 | +#pragma warning disable 618 |
| 124 | + return reader.GetFirstColumnDistinct<T>(); |
| 125 | +#pragma warning restore 618 |
| 126 | + } |
| 127 | + finally |
| 128 | + { |
| 129 | + if (reader != null) |
| 130 | + reader.Close(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public int ExecuteNonQuery() |
| 135 | + { |
| 136 | + return command.ExecuteNonQuery(); |
| 137 | + } |
| 138 | + |
| 139 | + public bool HasResult() |
| 140 | + { |
| 141 | + IDataReader reader = null; |
| 142 | + try |
| 143 | + { |
| 144 | + reader = command.ExecuteReader(); |
| 145 | + if (reader.Read()) |
| 146 | + return true; |
| 147 | + else |
| 148 | + return false; |
| 149 | + } |
| 150 | + finally |
| 151 | + { |
| 152 | + if (reader != null) |
| 153 | + reader.Close(); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments