You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Emby.Server.Implementations/Data/SqliteExtensions.cs

257 lines
7.7 KiB

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
1 year ago
using System.Data;
using System.Globalization;
1 year ago
using Microsoft.Data.Sqlite;
namespace Emby.Server.Implementations.Data
{
public static class SqliteExtensions
{
5 years ago
private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
/// <summary>
/// An array of ISO-8601 DateTime formats that we support parsing.
/// </summary>
private static readonly string[] _datetimeFormats = new string[]
{
"THHmmssK",
"THHmmK",
"HH:mm:ss.FFFFFFFK",
"HH:mm:ssK",
"HH:mmK",
5 years ago
DatetimeFormatUtc,
"yyyy-MM-dd HH:mm:ssK",
"yyyy-MM-dd HH:mmK",
"yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
"yyyy-MM-ddTHH:mmK",
"yyyy-MM-ddTHH:mm:ssK",
"yyyyMMddHHmmssK",
"yyyyMMddHHmmK",
"yyyyMMddTHHmmssFFFFFFFK",
"THHmmss",
"THHmm",
"HH:mm:ss.FFFFFFF",
"HH:mm:ss",
"HH:mm",
5 years ago
DatetimeFormatLocal,
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
"yyyy-MM-ddTHH:mm:ss.FFFFFFF",
"yyyy-MM-ddTHH:mm",
"yyyy-MM-ddTHH:mm:ss",
"yyyyMMddHHmmss",
"yyyyMMddHHmm",
"yyyyMMddTHHmmssFFFFFFF",
"yyyy-MM-dd",
"yyyyMMdd",
"yy-MM-dd"
};
1 year ago
public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
{
if (sqliteConnection.State != ConnectionState.Open)
{
sqliteConnection.Open();
}
1 year ago
using var command = sqliteConnection.CreateCommand();
1 year ago
command.CommandText = commandText;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return reader;
}
}
}
public static void Execute(this SqliteConnection sqliteConnection, string commandText)
{
1 year ago
using var command = sqliteConnection.CreateCommand();
1 year ago
command.CommandText = commandText;
command.ExecuteNonQuery();
}
public static string ToDateTimeParamValue(this DateTime dateValue)
{
var kind = DateTimeKind.Utc;
return (dateValue.Kind == DateTimeKind.Unspecified)
? DateTime.SpecifyKind(dateValue, kind).ToString(
GetDateTimeKindFormat(kind),
CultureInfo.InvariantCulture)
: dateValue.ToString(
GetDateTimeKindFormat(dateValue.Kind),
CultureInfo.InvariantCulture);
}
private static string GetDateTimeKindFormat(DateTimeKind kind)
5 years ago
=> (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
1 year ago
public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
var dateText = reader.GetString(index);
if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out var dateTimeResult))
{
result = dateTimeResult;
return true;
}
4 years ago
result = default;
return false;
}
1 year ago
public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetGuid(index);
return true;
}
1 year ago
public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
8 years ago
{
1 year ago
result = string.Empty;
8 years ago
1 year ago
if (reader.IsDBNull(index))
{
return false;
}
1 year ago
result = reader.GetString(index);
return true;
}
1 year ago
public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
8 years ago
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetBoolean(index);
return true;
}
1 year ago
public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
8 years ago
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetInt32(index);
return true;
8 years ago
}
1 year ago
public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
8 years ago
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetInt64(index);
return true;
}
1 year ago
public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetFloat(index);
return true;
}
1 year ago
public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
8 years ago
{
1 year ago
if (reader.IsDBNull(index))
{
4 years ago
result = default;
return false;
}
1 year ago
result = reader.GetDouble(index);
return true;
8 years ago
}
1 year ago
public static void TryBind(this SqliteCommand statement, string name, Guid value)
8 years ago
{
1 year ago
statement.TryBind(name, value, true);
8 years ago
}
1 year ago
public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
8 years ago
{
1 year ago
var preparedValue = value ?? DBNull.Value;
if (statement.Parameters.Contains(name))
8 years ago
{
1 year ago
statement.Parameters[name].Value = preparedValue;
8 years ago
}
else
{
// Blobs aren't always detected automatically
1 year ago
if (isBlob)
{
statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
}
else
{
statement.Parameters.AddWithValue(name, preparedValue);
}
8 years ago
}
}
1 year ago
public static void TryBindNull(this SqliteCommand statement, string name)
8 years ago
{
1 year ago
statement.TryBind(name, DBNull.Value);
}
1 year ago
public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
{
1 year ago
using (var reader = command.ExecuteReader())
{
1 year ago
while (reader.Read())
{
yield return reader;
}
}
}
1 year ago
public static int SelectScalarInt(this SqliteCommand command)
{
1 year ago
var result = command.ExecuteScalar();
// Can't be null since the method is used to retrieve Count
1 year ago
return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
}
1 year ago
public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
{
1 year ago
var command = sqliteConnection.CreateCommand();
command.CommandText = sql;
return command;
}
}
}