|
| 1 | +using System; |
| 2 | +using System.Data.Common; |
| 3 | + |
| 4 | +namespace NHibernate.Type |
| 5 | +{ |
| 6 | + internal static class DbRecordSetExtensions |
| 7 | + { |
| 8 | + public static bool TryGetBoolean(this DbDataReader rs, int ordinal, out bool value) |
| 9 | + { |
| 10 | + try |
| 11 | + { |
| 12 | + value = rs.GetBoolean(ordinal); |
| 13 | + return true; |
| 14 | + } |
| 15 | + catch (InvalidCastException) |
| 16 | + { |
| 17 | + value = default; |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public static bool TryGetByte(this DbDataReader rs, int ordinal, out byte value) |
| 23 | + { |
| 24 | + try |
| 25 | + { |
| 26 | + value = rs.GetByte(ordinal); |
| 27 | + return true; |
| 28 | + } |
| 29 | + catch (InvalidCastException) |
| 30 | + { |
| 31 | + value = default; |
| 32 | + return false; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static bool TryGetChar(this DbDataReader rs, int ordinal, out char value) |
| 37 | + { |
| 38 | + try |
| 39 | + { |
| 40 | + value = rs.GetChar(ordinal); |
| 41 | + return true; |
| 42 | + } |
| 43 | + catch (InvalidCastException) |
| 44 | + { |
| 45 | + value = default; |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static bool TryGetDecimal(this DbDataReader rs, int ordinal, out decimal value) |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + value = rs.GetDecimal(ordinal); |
| 55 | + return true; |
| 56 | + } |
| 57 | + catch (InvalidCastException) |
| 58 | + { |
| 59 | + value = default; |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static bool TryGetDouble(this DbDataReader rs, int ordinal, out double value) |
| 65 | + { |
| 66 | + try |
| 67 | + { |
| 68 | + value = rs.GetDouble(ordinal); |
| 69 | + return true; |
| 70 | + } |
| 71 | + catch (InvalidCastException) |
| 72 | + { |
| 73 | + value = default; |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static bool TryGetDateTime(this DbDataReader rs, int ordinal, out DateTime value) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + value = rs.GetDateTime(ordinal); |
| 83 | + return true; |
| 84 | + } |
| 85 | + catch (InvalidCastException) |
| 86 | + { |
| 87 | + value = default; |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + public static bool TryGetFloat(this DbDataReader rs, int ordinal, out float value) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + value = rs.GetFloat(ordinal); |
| 96 | + return true; |
| 97 | + } |
| 98 | + catch (InvalidCastException) |
| 99 | + { |
| 100 | + value = default; |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + public static bool TryGetGuid(this DbDataReader rs, int ordinal, out Guid value) |
| 105 | + { |
| 106 | + try |
| 107 | + { |
| 108 | + value = rs.GetGuid(ordinal); |
| 109 | + return true; |
| 110 | + } |
| 111 | + catch (InvalidCastException) |
| 112 | + { |
| 113 | + value = default; |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static bool TryGetUInt16(this DbDataReader rs, int ordinal, out ushort value) |
| 119 | + { |
| 120 | + var dbValue = rs[ordinal]; |
| 121 | + |
| 122 | + if (dbValue is ushort) |
| 123 | + { |
| 124 | + value = (ushort)dbValue; |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + value = default; |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + public static bool TryGetInt16(this DbDataReader rs, int ordinal, out short value) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + value = rs.GetInt16(ordinal); |
| 137 | + return true; |
| 138 | + } |
| 139 | + catch (InvalidCastException) |
| 140 | + { |
| 141 | + value = default; |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + public static bool TryGetInt32(this DbDataReader rs, int ordinal, out int value) |
| 146 | + { |
| 147 | + try |
| 148 | + { |
| 149 | + value = rs.GetInt32(ordinal); |
| 150 | + return true; |
| 151 | + } |
| 152 | + catch (InvalidCastException) |
| 153 | + { |
| 154 | + value = default; |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public static bool TryGetUInt32(this DbDataReader rs, int ordinal, out uint value) |
| 160 | + { |
| 161 | + var dbValue = rs[ordinal]; |
| 162 | + |
| 163 | + if (dbValue is uint) |
| 164 | + { |
| 165 | + value = (uint) dbValue; |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + value = default; |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + public static bool TryGetInt64(this DbDataReader rs, int ordinal, out long value) |
| 174 | + { |
| 175 | + try |
| 176 | + { |
| 177 | + value = rs.GetInt64(ordinal); |
| 178 | + return true; |
| 179 | + } |
| 180 | + catch (InvalidCastException) |
| 181 | + { |
| 182 | + value = default; |
| 183 | + return false; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public static bool TryGetUInt64(this DbDataReader rs, int ordinal, out ulong value) |
| 188 | + { |
| 189 | + var dbValue = rs[ordinal]; |
| 190 | + |
| 191 | + if (dbValue is ulong) |
| 192 | + { |
| 193 | + value = (ulong) dbValue; |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + value = default; |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + public static bool TryGetSByte(this DbDataReader rs, int ordinal, out sbyte value) |
| 202 | + { |
| 203 | + var dbValue = rs[ordinal]; |
| 204 | + |
| 205 | + if (dbValue is sbyte) |
| 206 | + { |
| 207 | + value = (sbyte) rs[ordinal]; |
| 208 | + return true; |
| 209 | + } |
| 210 | + |
| 211 | + value = default; |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | + public static bool TryGetString(this DbDataReader rs, int ordinal, out string value) |
| 216 | + { |
| 217 | + try |
| 218 | + { |
| 219 | + value = rs.GetString(ordinal); |
| 220 | + return true; |
| 221 | + } |
| 222 | + catch (InvalidCastException) |
| 223 | + { |
| 224 | + value = default; |
| 225 | + return false; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + public static bool TryGetTimeSpan(this DbDataReader rs, int ordinal, out TimeSpan value) |
| 230 | + { |
| 231 | + var dbValue = rs[ordinal]; |
| 232 | + |
| 233 | + if (dbValue is TimeSpan) |
| 234 | + { |
| 235 | + value = (TimeSpan) dbValue; |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + value = default; |
| 240 | + return false; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments