diff options
| author | Bond-009 <bond.009@outlook.com> | 2021-05-20 22:40:25 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-20 22:40:25 +0200 |
| commit | e8790bfa963284863dc1126a83df72309ec30561 (patch) | |
| tree | 519501b93593e3bbbf1551c1de95afb21bbfd624 /Emby.Server.Implementations/Data/SqliteExtensions.cs | |
| parent | c6ca722e57c7b83c5105bbf9b0de63b616d556de (diff) | |
| parent | e0f793799afccd4e2d640dbf5554f72d8a8f8b02 (diff) | |
Merge pull request #6054 from cvium/allocationz_2_extreme_db_plsdonthurtme
Diffstat (limited to 'Emby.Server.Implementations/Data/SqliteExtensions.cs')
| -rw-r--r-- | Emby.Server.Implementations/Data/SqliteExtensions.cs | 111 |
1 files changed, 101 insertions, 10 deletions
diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index a04d63088..a8f3feb58 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using SQLitePCL.pretty; @@ -96,21 +97,43 @@ namespace Emby.Server.Implementations.Data DateTimeStyles.None).ToUniversalTime(); } - public static DateTime? TryReadDateTime(this IResultSetValue result) + public static bool TryReadDateTime(this IReadOnlyList<IResultSetValue> reader, int index, out DateTime result) { - var dateText = result.ToString(); + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + var dateText = item.ToString(); if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult)) { - return dateTimeResult.ToUniversalTime(); + result = dateTimeResult.ToUniversalTime(); + return true; + } + + result = default; + return false; + } + + public static bool TryGetGuid(this IReadOnlyList<IResultSetValue> reader, int index, out Guid result) + { + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; } - return null; + result = item.ReadGuidFromBlob(); + return true; } - public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index) + public static bool IsDbNull(this IResultSetValue result) { - return result[index].SQLiteType == SQLiteType.Null; + return result.SQLiteType == SQLiteType.Null; } public static string GetString(this IReadOnlyList<IResultSetValue> result, int index) @@ -118,14 +141,48 @@ namespace Emby.Server.Implementations.Data return result[index].ToString(); } + public static bool TryGetString(this IReadOnlyList<IResultSetValue> reader, int index, out string result) + { + result = null; + var item = reader[index]; + if (item.IsDbNull()) + { + return false; + } + + result = item.ToString(); + return true; + } + public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index) { return result[index].ToBool(); } - public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index) + public static bool TryGetBoolean(this IReadOnlyList<IResultSetValue> reader, int index, out bool result) + { + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + result = item.ToBool(); + return true; + } + + public static bool TryGetInt32(this IReadOnlyList<IResultSetValue> reader, int index, out int result) { - return result[index].ToInt(); + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + result = item.ToInt(); + return true; } public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index) @@ -133,9 +190,43 @@ namespace Emby.Server.Implementations.Data return result[index].ToInt64(); } - public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index) + public static bool TryGetInt64(this IReadOnlyList<IResultSetValue> reader, int index, out long result) + { + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + result = item.ToInt64(); + return true; + } + + public static bool TryGetSingle(this IReadOnlyList<IResultSetValue> reader, int index, out float result) + { + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + result = item.ToFloat(); + return true; + } + + public static bool TryGetDouble(this IReadOnlyList<IResultSetValue> reader, int index, out double result) { - return result[index].ToFloat(); + var item = reader[index]; + if (item.IsDbNull()) + { + result = default; + return false; + } + + result = item.ToDouble(); + return true; } public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index) |
