// Copyright (C) Stichting Deltares 2017. 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;
using System.Data.SQLite;
using System.IO;
using Core.Common.Base.IO;
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
{
///
/// 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 .
/// - Unable to open database file.
///
///
protected SqLiteDatabaseReaderBase(string databaseFilePath)
{
try
{
IOUtils.ValidateFilePath(databaseFilePath);
Path = databaseFilePath;
if (!File.Exists(databaseFilePath))
{
string message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(UtilsResources.Error_File_does_not_exist);
throw new CriticalFileReadException(message);
}
OpenConnection(databaseFilePath);
}
catch (ArgumentException e)
{
throw new CriticalFileReadException(e.Message, e);
}
catch (SQLiteException e)
{
throw new CriticalFileReadException(e.Message, e);
}
}
///
/// Gets the path to the file.
///
public string Path { get; }
///
/// Closes and disposes the existing .
///
public virtual void Dispose()
{
CloseConnection();
}
///
/// Gets the .
///
protected SQLiteConnection Connection { get; private set; }
///
/// Moves to and reads the next result set in multiple row-returning SQL command.
///
/// The to process.
/// True if the command was successful and a new result set is available, false otherwise.
protected static bool MoveNext(IDataReader sqliteDataReader)
{
return sqliteDataReader.Read() || sqliteDataReader.NextResult() && sqliteDataReader.Read();
}
///
/// Creates a new , based upon and .
///
/// The query to execute.
/// Parameters the is depended on.
/// A new instance of .
/// The execution of failed.
protected IDataReader CreateDataReader(string queryString, params SQLiteParameter[] parameters)
{
using (var query = new SQLiteCommand(Connection)
{
CommandText = queryString
})
{
if (parameters != null)
{
query.Parameters.AddRange(parameters);
}
return query.ExecuteReader();
}
}
protected void CloseConnection()
{
if (Connection != null)
{
Connection.Close();
Connection.Dispose();
Connection = null;
}
}
///
/// Opens the connection with the .
///
/// The database file to establish a connection with.
private void OpenConnection(string databaseFile)
{
string connectionStringBuilder = new SQLiteConnectionStringBuilder
{
FailIfMissing = true,
DataSource = databaseFile,
ReadOnly = true,
ForeignKeys = true
}.ConnectionString;
Connection = new SQLiteConnection(connectionStringBuilder, true);
Connection.Open();
}
}
}