//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd;
using System;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
///
/// Assistive class for inspecting SQLite database schemas.
///
public static class SchemaInspector
{
///
/// Retrieves the schema information for the specified SQLite database.
///
/// The connection string for the database.
/// The schema information.
/// Thrown if the database can't be accessed, or something else goes wrong.
public static Dictionary> GetDatabaseSchema(string connectionString)
{
try
{
using var connection = new SqliteConnection(connectionString);
connection.Open();
var dict = new Dictionary>();
using var tableCommand = new SqliteCommand("SELECT name FROM sqlite_master WHERE type='table';", connection);
using var tableReader = tableCommand.ExecuteReader();
while (tableReader.Read())
{
var table = tableReader.GetString(0);
var columns = new List();
using var columnCommand = new SqliteCommand($"PRAGMA table_info({table});", connection);
using var cr = columnCommand.ExecuteReader();
while (cr.Read())
{
columns.Add(new ColumnInfo(
Cid: cr.GetInt64(cr.GetOrdinal("cid")),
Name: cr.GetString(cr.GetOrdinal("name")),
Type: cr.GetString(cr.GetOrdinal("type")),
NotNull: cr.GetInt64(cr.GetOrdinal("notnull")) > 0,
DefaultValue: cr["dflt_value"],
PrimaryKey: cr.GetInt64(cr.GetOrdinal("pk")) > 0));
}
dict[table] = columns;
}
connection.Close();
return dict;
}
catch (Exception ex)
{
throw new SlskdException($"Failed to retrieve schema information for database '{connectionString}'. The database might be corrupt or in use by another application; if the problem persists the backing file may need to be deleted", ex);
}
}
///
/// Retrieves the index information for the specified SQLite database.
///
/// The connection string for the database.
/// The index information.
/// Thrown if the database can't be accessed, or something else goes wrong.
public static Dictionary> GetDatabaseIndexes(string connectionString)
{
try
{
using var connection = new SqliteConnection(connectionString);
connection.Open();
var dict = new Dictionary>();
using var tableCommand = new SqliteCommand("SELECT name FROM sqlite_master WHERE type='table';", connection);
using var tableReader = tableCommand.ExecuteReader();
while (tableReader.Read())
{
var table = tableReader.GetString(0);
var indexes = new List();
using var command = new SqliteCommand($"PRAGMA index_list({table});", connection);
using var cr = command.ExecuteReader();
while (cr.Read())
{
indexes.Add(new IndexInfo(
Seq: cr.GetInt64(cr.GetOrdinal("seq")),
Name: cr.GetString(cr.GetOrdinal("name")),
Unique: cr.GetInt64(cr.GetOrdinal("unique")) > 0,
Origin: cr.GetString(cr.GetOrdinal("origin")),
Partial: cr.GetInt64(cr.GetOrdinal("partial")) > 0));
}
dict[table] = indexes;
}
connection.Close();
return dict;
}
catch (Exception ex)
{
throw new SlskdException($"Failed to retrieve index information for database '{connectionString}'. The database might be corrupt or in use by another application; if the problem persists the backing file may need to be deleted", ex);
}
}
///
/// Represents a SQLite database column.
///
public record ColumnInfo(long Cid, string Name, string Type, bool NotNull, object DefaultValue, bool PrimaryKey);
///
/// Represents a SQLite database index.
///
public record IndexInfo(long Seq, string Name, bool Unique, string Origin, bool Partial);
}