2021-06-08 14:56:45 -06:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-12-16 12:06:48 +11:00
|
|
|
using System.Linq;
|
2022-01-13 23:46:21 +00:00
|
|
|
using Umbraco.Cms.Core.Persistence;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
2021-06-08 14:56:45 -06:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2016-12-16 14:18:37 +01:00
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence
|
2016-12-16 14:18:37 +01:00
|
|
|
{
|
|
|
|
|
internal static class UmbracoDatabaseExtensions
|
|
|
|
|
{
|
|
|
|
|
public static UmbracoDatabase AsUmbracoDatabase(this IUmbracoDatabase database)
|
|
|
|
|
{
|
2021-06-08 14:56:45 -06:00
|
|
|
if (database is not UmbracoDatabase asDatabase)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("oops: database.");
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 14:18:37 +01:00
|
|
|
return asDatabase;
|
|
|
|
|
}
|
2020-03-28 15:13:50 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-06-08 14:56:45 -06:00
|
|
|
/// Gets a dictionary of key/values directly from the database, no scope, nothing.
|
2020-03-28 15:13:50 +01:00
|
|
|
/// </summary>
|
2020-11-10 08:50:47 +00:00
|
|
|
/// <remarks>Used by <see cref="CoreRuntimeBootstrapper"/> to determine the runtime state.</remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static IReadOnlyDictionary<string, string?>? GetFromKeyValueTable(this IUmbracoDatabase database, string keyPrefix)
|
2020-03-28 15:13:50 +01:00
|
|
|
{
|
|
|
|
|
if (database is null) return null;
|
|
|
|
|
|
2021-06-08 14:56:45 -06:00
|
|
|
// create the wildcard where clause
|
|
|
|
|
ISqlSyntaxProvider sqlSyntax = database.SqlContext.SqlSyntax;
|
|
|
|
|
var whereParam = sqlSyntax.GetStringColumnWildcardComparison(
|
|
|
|
|
sqlSyntax.GetQuotedColumnName("key"),
|
|
|
|
|
0,
|
2022-01-13 23:46:21 +00:00
|
|
|
TextColumnType.NVarchar);
|
2021-06-08 14:56:45 -06:00
|
|
|
|
2020-03-28 15:13:50 +01:00
|
|
|
var sql = database.SqlContext.Sql()
|
|
|
|
|
.Select<KeyValueDto>()
|
|
|
|
|
.From<KeyValueDto>()
|
2021-06-08 14:56:45 -06:00
|
|
|
.Where(whereParam, keyPrefix + sqlSyntax.GetWildcardPlaceholder());
|
|
|
|
|
|
|
|
|
|
return database.Fetch<KeyValueDto>(sql)
|
2022-02-24 09:24:56 +01:00
|
|
|
.ToDictionary(x => x.Key!, x => x.Value);
|
2020-03-28 15:13:50 +01:00
|
|
|
}
|
2021-01-18 15:40:22 +01:00
|
|
|
|
|
|
|
|
|
2020-12-16 12:06:48 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if the database contains the specified table
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="database"></param>
|
|
|
|
|
/// <param name="tableName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool HasTable(this IUmbracoDatabase database, string tableName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return database.SqlContext.SqlSyntax.GetTablesInSchema(database).Any(table => table.InvariantEquals(tableName));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false; // will occur if the database cannot connect
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if the database contains no tables
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="database"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsDatabaseEmpty(this IUmbracoDatabase database)
|
|
|
|
|
=> database.SqlContext.SqlSyntax.GetTablesInSchema(database).Any() == false;
|
|
|
|
|
|
2016-12-16 14:18:37 +01:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|