// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Data.SQLite;
using System.IO;
using Core.Common.Base.Data;
using Core.Common.Base.Storage;
namespace Application.Ringtoets.Storage.TestUtil
{
///
/// This class is used to prepare test databases for test.
///
public static class SqLiteDatabaseHelper
{
///
/// Creates a new Sqlite database file with the structure defined in .
///
/// Path to database file.
/// Script that containts the schema to execute on the database.
/// Thrown when executing failed.
public static void CreateDatabaseFile(string databaseFilePath, string databaseSchemaQuery)
{
if (databaseSchemaQuery == null)
{
throw new ArgumentNullException("databaseSchemaQuery");
}
if (File.Exists(databaseFilePath))
{
TearDownTempFile(databaseFilePath);
}
SQLiteConnection.CreateFile(databaseFilePath);
var connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath);
using (var dbContext = new SQLiteConnection(connectionString))
{
using (var command = dbContext.CreateCommand())
{
dbContext.Open();
command.CommandText = databaseSchemaQuery;
command.ExecuteNonQuery();
}
}
}
///
/// Converts the into a new Ringtoets database file.
///
/// Path to database file.
/// to save.
/// Thrown when is null.
/// is invalid.
/// Thrown when:
///
/// - The database does not contain the table version
/// - THe file was not created.
/// - Saving the to the database failed.
/// - The connection to the database file failed.
///
///
public static void CreateValidRingtoetsDatabase(string databaseFilePath, Project project)
{
var storageSqLite = new StorageSqLite();
storageSqLite.SaveProjectAs(databaseFilePath, project);
}
///
/// Returns a corrupt databaseschema that will pass validation.
///
/// The corrupt databaseschema that will pass validation.
public static string GetCorruptSchema()
{
return "DROP TABLE IF EXISTS 'Version'; " +
"CREATE TABLE Version (VersionId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" FromVersion VARCHAR (16), ToVersion VARCHAR (16),Timestamp NUMERIC); ";
}
///
/// Removes the .
///
/// The file to delete.
public static void TearDownTempFile(string filePath)
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
}