< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Data.SqliteExtensions
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Data/SqliteExtensions.cs
Line coverage
75%
Covered lines: 82
Uncovered lines: 27
Coverable lines: 109
Total lines: 263
Line coverage: 75.2%
Branch coverage
60%
Covered branches: 18
Total branches: 30
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Execute(...)100%11100%
ToDateTimeParamValue(...)0%620%
GetDateTimeKindFormat(...)0%620%
TryReadDateTime(...)83.33%6.22681.81%
TryGetGuid(...)100%22100%
TryGetString(...)100%22100%
TryGetBoolean(...)50%2.26260%
TryGetInt32(...)50%2.26260%
TryGetInt64(...)50%2.26260%
TryGetSingle(...)50%2.26260%
TryGetDouble(...)0%620%
TryBind(...)100%11100%
TryBind(...)83.33%6.11685.71%
TryBindNull(...)100%11100%
SelectScalarInt(...)100%210%
PrepareStatement(...)100%11100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Data/SqliteExtensions.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Data;
 6using System.Globalization;
 7using Microsoft.Data.Sqlite;
 8
 9namespace Emby.Server.Implementations.Data
 10{
 11    public static class SqliteExtensions
 12    {
 13        private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
 14        private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
 15
 16        /// <summary>
 17        /// An array of ISO-8601 DateTime formats that we support parsing.
 18        /// </summary>
 119        private static readonly string[] _datetimeFormats = new string[]
 120        {
 121            "THHmmssK",
 122            "THHmmK",
 123            "HH:mm:ss.FFFFFFFK",
 124            "HH:mm:ssK",
 125            "HH:mmK",
 126            DatetimeFormatUtc,
 127            "yyyy-MM-dd HH:mm:ssK",
 128            "yyyy-MM-dd HH:mmK",
 129            "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
 130            "yyyy-MM-ddTHH:mmK",
 131            "yyyy-MM-ddTHH:mm:ssK",
 132            "yyyyMMddHHmmssK",
 133            "yyyyMMddHHmmK",
 134            "yyyyMMddTHHmmssFFFFFFFK",
 135            "THHmmss",
 136            "THHmm",
 137            "HH:mm:ss.FFFFFFF",
 138            "HH:mm:ss",
 139            "HH:mm",
 140            DatetimeFormatLocal,
 141            "yyyy-MM-dd HH:mm:ss",
 142            "yyyy-MM-dd HH:mm",
 143            "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
 144            "yyyy-MM-ddTHH:mm",
 145            "yyyy-MM-ddTHH:mm:ss",
 146            "yyyyMMddHHmmss",
 147            "yyyyMMddHHmm",
 148            "yyyyMMddTHHmmssFFFFFFF",
 149            "yyyy-MM-dd",
 150            "yyyyMMdd",
 151            "yy-MM-dd"
 152        };
 53
 54        public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
 55        {
 56            if (sqliteConnection.State != ConnectionState.Open)
 57            {
 58                sqliteConnection.Open();
 59            }
 60
 61            using var command = sqliteConnection.CreateCommand();
 62            command.CommandText = commandText;
 63            using (var reader = command.ExecuteReader())
 64            {
 65                while (reader.Read())
 66                {
 67                    yield return reader;
 68                }
 69            }
 70        }
 71
 72        public static void Execute(this SqliteConnection sqliteConnection, string commandText)
 73        {
 589974            using var command = sqliteConnection.CreateCommand();
 589975            command.CommandText = commandText;
 589976            command.ExecuteNonQuery();
 1179877        }
 78
 79        public static string ToDateTimeParamValue(this DateTime dateValue)
 80        {
 081            var kind = DateTimeKind.Utc;
 82
 083            return (dateValue.Kind == DateTimeKind.Unspecified)
 084                ? DateTime.SpecifyKind(dateValue, kind).ToString(
 085                    GetDateTimeKindFormat(kind),
 086                    CultureInfo.InvariantCulture)
 087                : dateValue.ToString(
 088                    GetDateTimeKindFormat(dateValue.Kind),
 089                    CultureInfo.InvariantCulture);
 90        }
 91
 92        private static string GetDateTimeKindFormat(DateTimeKind kind)
 093            => (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
 94
 95        public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
 96        {
 60297            if (reader.IsDBNull(index))
 98            {
 33899                result = default;
 338100                return false;
 101            }
 102
 264103            var dateText = reader.GetString(index);
 104
 264105            if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.Adju
 106            {
 107                // If the resulting DateTimeKind is Unspecified it is actually Utc.
 108                // This is required downstream for the Json serializer.
 264109                if (dateTimeResult.Kind == DateTimeKind.Unspecified)
 110                {
 264111                    dateTimeResult = DateTime.SpecifyKind(dateTimeResult, DateTimeKind.Utc);
 112                }
 113
 264114                result = dateTimeResult;
 264115                return true;
 116            }
 117
 0118            result = default;
 0119            return false;
 120        }
 121
 122        public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
 123        {
 258124            if (reader.IsDBNull(index))
 125            {
 172126                result = default;
 172127                return false;
 128            }
 129
 86130            result = reader.GetGuid(index);
 86131            return true;
 132        }
 133
 134        public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
 135        {
 3428136            result = string.Empty;
 137
 3428138            if (reader.IsDBNull(index))
 139            {
 1886140                return false;
 141            }
 142
 1542143            result = reader.GetString(index);
 1542144            return true;
 145        }
 146
 147        public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
 148        {
 258149            if (reader.IsDBNull(index))
 150            {
 0151                result = default;
 0152                return false;
 153            }
 154
 258155            result = reader.GetBoolean(index);
 258156            return true;
 157        }
 158
 159        public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
 160        {
 602161            if (reader.IsDBNull(index))
 162            {
 602163                result = default;
 602164                return false;
 165            }
 166
 0167            result = reader.GetInt32(index);
 0168            return true;
 169        }
 170
 171        public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
 172        {
 172173            if (reader.IsDBNull(index))
 174            {
 172175                result = default;
 172176                return false;
 177            }
 178
 0179            result = reader.GetInt64(index);
 0180            return true;
 181        }
 182
 183        public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
 184        {
 344185            if (reader.IsDBNull(index))
 186            {
 344187                result = default;
 344188                return false;
 189            }
 190
 0191            result = reader.GetFloat(index);
 0192            return true;
 193        }
 194
 195        public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
 196        {
 0197            if (reader.IsDBNull(index))
 198            {
 0199                result = default;
 0200                return false;
 201            }
 202
 0203            result = reader.GetDouble(index);
 0204            return true;
 205        }
 206
 207        public static void TryBind(this SqliteCommand statement, string name, Guid value)
 208        {
 671209            statement.TryBind(name, value, true);
 671210        }
 211
 212        public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
 213        {
 5225214            var preparedValue = value ?? DBNull.Value;
 5225215            if (statement.Parameters.Contains(name))
 216            {
 0217                statement.Parameters[name].Value = preparedValue;
 218            }
 219            else
 220            {
 221                // Blobs aren't always detected automatically
 5225222                if (isBlob)
 223                {
 691224                    statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
 225                }
 226                else
 227                {
 4534228                    statement.Parameters.AddWithValue(name, preparedValue);
 229                }
 230            }
 4534231        }
 232
 233        public static void TryBindNull(this SqliteCommand statement, string name)
 234        {
 1728235            statement.TryBind(name, DBNull.Value);
 1728236        }
 237
 238        public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
 239        {
 240            using (var reader = command.ExecuteReader())
 241            {
 242                while (reader.Read())
 243                {
 244                    yield return reader;
 245                }
 246            }
 247        }
 248
 249        public static int SelectScalarInt(this SqliteCommand command)
 250        {
 0251            var result = command.ExecuteScalar();
 252            // Can't be null since the method is used to retrieve Count
 0253            return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
 254        }
 255
 256        public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
 257        {
 61258            var command = sqliteConnection.CreateCommand();
 61259            command.CommandText = sql;
 61260            return command;
 261        }
 262    }
 263}