// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.IO.Exceptions;
using Core.Common.Utils;
using Core.Common.Utils.Builders;
using UtilsResources = Core.Common.Utils.Properties.Resources;
namespace Core.Common.IO.Readers
{
///
/// Base class for database readers.
///
public abstract class SqLiteDatabaseReaderBase : IDisposable
{
private readonly string fullFilePath;
///
/// Creates a new instance of which will use the
/// as its source.
///
/// The path of the database file to open.
/// Thrown when:
///
/// - The contains invalid characters.
/// - No file could be found at .
/// - Preparing the queries to read from the database failed.
///
///
protected SqLiteDatabaseReaderBase(string databaseFilePath)
{
try
{
FileUtils.ValidateFilePath(databaseFilePath);
}
catch (ArgumentException e)
{
throw new CriticalFileReadException(e.Message, e);
}
if (!File.Exists(databaseFilePath))
{
var message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(UtilsResources.Error_File_does_not_exist);
throw new CriticalFileReadException(message);
}
fullFilePath = databaseFilePath;
OpenConnection(databaseFilePath);
}
///
/// Gets the path to the file.
///
public string Path
{
get
{
return fullFilePath;
}
}
///
/// Closes and disposes the existing .
///
public virtual void Dispose()
{
if (Connection != null)
{
Connection.Close();
Connection.Dispose();
}
}
///
/// Gets the .
///
protected SQLiteConnection Connection { get; private set; }
///
/// Opens the connection with the .
///
/// The database file to establish a connection with.
private void OpenConnection(string databaseFile)
{
var connectionStringBuilder = new SQLiteConnectionStringBuilder
{
FailIfMissing = true,
DataSource = databaseFile,
ReadOnly = true,
ForeignKeys = true
}.ConnectionString;
Connection = new SQLiteConnection(connectionStringBuilder);
Connection.Open();
}
}
}