📄 collecteddataprovider.cs
字号:
#region Namespaces
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Text;
using Epi;
using Epi.Data;
using Epi.Fields;
using Epi.DataSets;
using System.Text.RegularExpressions;
using Epi.Resources;
#endregion //Namespaces
namespace Epi.Data.Services
{
/// <summary>
/// Data access class for Epi Info collected data
/// </summary>
public class CollectedDataProvider //: IDbDriver
{
#region Fields
private Project project;
/// <summary>
/// The underlying physical database
/// </summary>
protected IDbDriver db;
private IDbDriverFactory dbFactory;
//private DbConnectionStringBuilder cnnStringBuilder;
#endregion Fields
#region Constructors
/// <summary>
/// Constructor for the class.
/// </summary>
/// <param name="proj">Project the collected data belongs to</param>
// public CollectedDataProvider(Project proj, bool createDatabase)
public CollectedDataProvider(Project proj)
{
#region Input validation
if (proj == null)
throw new System.ArgumentNullException("proj");
#endregion Input validation
project = proj;
//string connectionString = proj.CollectedDataConnectionString;
//if (Util.IsFilePath(connectionString) && !Path.IsPathRooted(connectionString))
//{
// connectionString = Path.Combine(proj.Location, connectionString);
//}
//db = DatabaseFactory.CreateDatabaseInstance(project.CollectedDataDriver, connectionString);
//if (createDatabase)
//{
// this.CreateDatabase(new ConnectionStringInfo(connectionString).DbName);
//}
//else
//{
// // if you've come this far, the connection string should be valid
// db.TestConnection();
//}
}
#endregion //Constructors
#region Public Methods
/// <summary>
/// Creates database
/// </summary>
/// <param name="collectDbInfo">Database information.</param>
/// <param name="driver">Database driver.</param>
/// <param name="createDatabase">Create database.</param>
public void Initialize(DbDriverInfo collectDbInfo, string driver, bool createDatabase)
{
dbFactory = DbDriverFactoryCreator.GetDbDriverFactory(driver);
db = dbFactory.CreateDatabaseObject(collectDbInfo.DBCnnStringBuilder);
if (createDatabase)
{
this.CreateDatabase(collectDbInfo);
}
db.TestConnection();
}
/// <summary>
/// Returns the current db driver.
/// </summary>
/// <returns></returns>
public IDbDriver GetDbDriver()
{
return this.db;
}
#endregion
#region Public Properties
/// <summary>
/// The project
/// </summary>
public Project Project
{
get
{
return project;
}
}
#endregion //Public Properties
#region Public Methods
/// <summary>
/// Synchronizes a view and its data table
/// </summary>
/// <param name="view">View of a table.</param>
public void SynchronizeDataTable(View view)
{
if (!db.TableExists(view.TableName))
{
CreateDataTableForView(view, 1);
}
SynchronizeTableColumns(view);
view.SetTableName(view.TableName);
}
/// <summary>
/// Creates the SysDataTable for collected data
/// </summary>
/// <param name="tableName">Name of the table.</param>
public void CreateSysDataTable(string tableName)
{
#region Input validation
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentNullException("Table Name");
}
#endregion
Query createQuery = db.CreateQuery("CREATE TABLE sysDataTables " +
" (TableName VARCHAR(64) PRIMARY KEY NOT NULL, ViewId TINYINT NOT NULL)");
try
{
db.ExecuteNonQuery(createQuery);
}
finally
{
}
}
/// <summary>
/// Gets primary view's Id.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>View's Id</returns>
public int GetPrimaryViewId(string tableName)
{
try
{
Query query = db.CreateQuery("SELECT DISTINCT [ViewId] FROM sysDataTables WHERE tableName = @tableName");
query.Parameters.Add(new QueryParameter("@tableName", DbType.String, tableName));
return (int)db.ExecuteScalar(query);
}
catch (Exception ex)
{
throw new GeneralException("Could not retrieve view Id", ex);
}
finally
{
}
}
/// <summary>
/// Inserts the project parameters into the metadata database
/// </summary>
private void InsertProject()
{
try
{
ApplicationIdentity appId = new ApplicationIdentity(typeof(Configuration).Assembly);
Query query = db.CreateQuery("INSERT INTO metaDBInfo([ProjectId], [ProjectName], [ProjectLocation], [EpiVersion], [Purpose]) VALUES (@ProjectId, @ProjectName, @ProjectLocation, @EpiVersion, @Purpose)");
query.Parameters.Add(new QueryParameter("@ProjectId", DbType.Guid, this.Project.Id));
query.Parameters.Add(new QueryParameter("@ProjectName", DbType.String, Project.Name));
query.Parameters.Add(new QueryParameter("@ProjectLocation", DbType.String, Project.Location));
query.Parameters.Add(new QueryParameter("@EpiVersion", DbType.String, appId.Version));
if (Project.UseMetadataDbForCollectedData)
query.Parameters.Add(new QueryParameter("@Purpose", DbType.Int32, (int)DatabasePurpose.MetadataAndCollectedData));
else
query.Parameters.Add(new QueryParameter("@Purpose", DbType.Int32, (int)DatabasePurpose.CollectedData));
db.ExecuteNonQuery(query);
}
catch (Exception ex)
{
throw new GeneralException("Could not create new project", ex);
}
finally
{
}
}
/// <summary>
/// Gets the number of tables in the database.
/// </summary>
/// <returns>The number of tables in the database.</returns>
public int GetTableCount()
{
return db.GetTableCount();
}
/// <summary>
/// Get table column names
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>List of column names.</returns>
public List<System.String> GetTableColumnNames(string tableName)
{
//if (string.IsNullOrEmpty(tableName))
//{
// throw new ArgumentException("tableName");
//}
return db.GetTableColumnNames(tableName);
}
/// <summary>
/// Creates physical database.
/// </summary>
/// <param name="dbInfo">Database driver information.</param>
public void CreateDatabase(DbDriverInfo dbInfo)
{
dbFactory.CreatePhysicalDatabase(dbInfo);
}
/// <summary>
/// Deletes a specific table in the database.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>Results of a successful deletion.</returns>
public bool DeleteTable(string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentException("tableName");
}
return db.DeleteTable(tableName);
}
/// <summary>
/// Gets a value indicating whether or not a specific table exists in the database
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>Results of successful table existence test.</returns>
public bool TableExists(string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentException("tableName");
}
return db.TableExists(tableName);
}
/// <summary>
/// Gets primary_key schema information about an OLE table.
/// </summary>
/// <param name="tableName">The name of the table</param>
/// <returns>Represents one table of in-memory data.</returns>
public DataTable GetTableColumnSchema(string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentException("tableName");
}
return db.GetTableColumnSchema(tableName);
}
/// <summary>
/// Gets an OLE-table as a DataReader.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>Forward-only stream of result sets obtained by executing a command.</returns>
public IDataReader GetTableDataReader(string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentException("tableName");
}
return db.GetTableDataReader(tableName);
}
/// <summary>
/// Gets column names as text in a System.Data.DataView.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>Represents a databindable, customized view of a <see cref="System.Data.DataTable"/></returns>
public DataView GetTextColumnNames(string tableName)
{
if (string.IsNullOrEmpty(tableName))
{
throw new ArgumentException("tableName");
}
return db.GetTextColumnNames(tableName);
}
/// <summary>
/// Returns the database instance.
/// </summary>
/// <returns>Abstract data type for databases.</returns>
public IDbDriver GetDatabase()
{
return this.db;
}
#endregion //Public Methods
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -