#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using MiniSqlQuery.Core;
namespace MiniSqlQuery.PlugIns.TemplateViewer
{
/// The template data.
public class TemplateData : IDisposable
{
/// The _data tables.
private readonly Dictionary _dataTables = new Dictionary();
/// The _db connection.
private DbConnection _dbConnection;
/// Initializes a new instance of the class.
/// The services.
public TemplateData(IApplicationServices services)
{
Services = services;
}
/// Gets Services.
public IApplicationServices Services { get; private set; }
/// Helper for getting the value of a row - avoids "get_Item()" usage.
/// The row.
/// Name of the column.
/// The column value.
public object ColumnValue(DataRow row, string columnName)
{
return row[columnName];
}
/// The get.
/// The schema name.
/// The view or table name.
///
public DataTable Get(string schema, string viewOrTableName)
{
return Get(Utility.RenderSafeSchemaObjectName(schema, viewOrTableName));
}
/// The get.
/// The view or table name.
///
public DataTable Get(string viewOrTableName)
{
DbDataAdapter adapter = null;
DbCommand cmd = null;
DataTable dt = null;
QueryBatch batch = new QueryBatch();
Query query = new Query("SELECT * FROM " + viewOrTableName);
if (string.IsNullOrEmpty(viewOrTableName))
{
return null;
}
if (_dataTables.ContainsKey(viewOrTableName))
{
return _dataTables[viewOrTableName];
}
try
{
if (_dbConnection == null || _dbConnection.State != ConnectionState.Open)
{
_dbConnection = Services.Settings.GetOpenConnection();
}
query.Result = new DataSet(viewOrTableName + " View");
batch.Clear();
batch.Add(query);
adapter = Services.Settings.ProviderFactory.CreateDataAdapter();
cmd = _dbConnection.CreateCommand();
cmd.CommandText = query.Sql;
cmd.CommandType = CommandType.Text;
adapter.SelectCommand = cmd;
adapter.Fill(query.Result);
}
// catch (Exception exp)
// {
// throw;
// }
finally
{
if (adapter != null)
{
adapter.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
}
if (query.Result.Tables.Count > 0)
{
dt = query.Result.Tables[0];
_dataTables[viewOrTableName] = dt;
}
return dt;
}
/// The query.
/// The sql.
///
public DataTable Query(string sql)
{
DbDataAdapter adapter = null;
DbCommand cmd = null;
DataTable dt = null;
QueryBatch batch = new QueryBatch();
Query query = new Query(sql);
if (string.IsNullOrEmpty(sql))
{
return null;
}
if (_dataTables.ContainsKey(sql))
{
return _dataTables[sql];
}
try
{
if (_dbConnection == null || _dbConnection.State != ConnectionState.Open)
{
_dbConnection = Services.Settings.GetOpenConnection();
}
string dataSetName = sql;
query.Result = new DataSet(dataSetName);
batch.Clear();
batch.Add(query);
adapter = Services.Settings.ProviderFactory.CreateDataAdapter();
cmd = _dbConnection.CreateCommand();
cmd.CommandText = query.Sql;
cmd.CommandType = CommandType.Text;
adapter.SelectCommand = cmd;
adapter.Fill(query.Result);
}
// catch (Exception exp)
// {
// throw;
// }
finally
{
if (adapter != null)
{
adapter.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
}
if (query.Result.Tables.Count > 0)
{
dt = query.Result.Tables[0];
_dataTables[sql] = dt;
}
return dt;
}
/// The dispose.
public void Dispose()
{
if (_dbConnection != null)
{
_dbConnection.Dispose();
_dbConnection = null;
}
foreach (var dataTable in _dataTables)
{
dataTable.Value.Dispose();
}
}
}
}