251 lines
8.8 KiB
C#
251 lines
8.8 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using FirebirdSql.Data.FirebirdClient;
|
|
//using FirebirdSql.Data.Firebird;
|
|
using System.Xml;
|
|
//using GZTW.Common;
|
|
|
|
namespace GZTW.Data.FireBird
|
|
{
|
|
/// <summary>
|
|
/// <para>Represents a FireBird Database.</para>
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Firebird database
|
|
/// </para>
|
|
/// </remarks>
|
|
public class FireBirdDatabase : GZTWDatabase
|
|
{
|
|
/// <summary>
|
|
/// Initialize a new instance of the <see cref="FireBirdDatabase"/> class.
|
|
/// </summary>
|
|
public FireBirdDatabase() : base()
|
|
{
|
|
}
|
|
|
|
public override void ClearPool()
|
|
{
|
|
FbConnection.ClearAllPools();
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get the DataBaseType enum for the active db type
|
|
/// Used by higher layers when some DB specific code needs to be
|
|
/// executed
|
|
/// </summary>
|
|
/// <value>
|
|
/// <para>DataBaseType enumeration value</para>
|
|
/// </value>
|
|
public override DataBaseType DBType
|
|
{
|
|
get
|
|
{
|
|
return DataBaseType.FireBird;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the schema fingerprint for database
|
|
/// </summary>
|
|
public override string SchemaFingerPrint//case 856
|
|
{
|
|
get
|
|
{
|
|
string _fingerprint = "";
|
|
using (FbConnection cn = new FbConnection(ConnectionString))
|
|
{
|
|
try
|
|
{
|
|
cn.Open();
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
DataTable t=cn.GetSchema("Tables", new string[] { null, null,null, "TABLE" });
|
|
//iterate all tables
|
|
DataView dvTables = t.DefaultView;
|
|
dvTables.Sort = "TABLE_NAME";
|
|
|
|
foreach (DataRowView dvTable in dvTables)
|
|
{
|
|
sb.Append(dvTable["TABLE_NAME"].ToString());
|
|
sb.Append("\r\n");
|
|
DataTable tColumns = cn.GetSchema("Columns", new string[] { null, null, dvTable["TABLE_NAME"].ToString() });
|
|
DataView dvColumns = tColumns.DefaultView;
|
|
dvColumns.Sort = "COLUMN_NAME";
|
|
|
|
foreach (DataRowView dvColumn in dvColumns)
|
|
{
|
|
|
|
sb.Append("\t");
|
|
sb.Append(dvColumn["COLUMN_NAME"].ToString());
|
|
//sb.Append(" ");
|
|
//string sType = "";
|
|
//if (dvColumn["DOMAIN_NAME"] != System.DBNull.Value)
|
|
//{
|
|
// sType = dvColumn["DOMAIN_NAME"].ToString();
|
|
|
|
//}
|
|
//else
|
|
// sType = dvColumn["COLUMN_DATA_TYPE"].ToString();
|
|
//sb.Append(sType);
|
|
sb.Append("\r\n");
|
|
|
|
}
|
|
}
|
|
|
|
|
|
_fingerprint = sb.ToString().Trim();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
finally
|
|
{
|
|
if (cn != null)
|
|
cn.Close();
|
|
}
|
|
}
|
|
|
|
return _fingerprint;
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Gets the parameter token used to delimit parameters for the Sql Database.</para>
|
|
/// </summary>
|
|
/// <value>
|
|
/// <para>The '?' symbol.</para>
|
|
/// </value>
|
|
protected override char ParameterToken
|
|
{
|
|
get { return '@'; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Get the connection for this database.</para>
|
|
/// <seealso cref="IDbConnection"/>
|
|
/// <seealso cref="FbConnection"/>
|
|
/// </summary>
|
|
/// <returns>
|
|
/// <para>The <see cref="FbConnection"/> for this database.</para>
|
|
/// </returns>
|
|
public override IDbConnection GetConnection()
|
|
{
|
|
|
|
//try
|
|
//{
|
|
return new FbConnection(ConnectionString);
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
// System.Diagnostics.Debugger.Break();
|
|
// ClearPool();
|
|
// return new FbConnection(ConnectionString);
|
|
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// We don't need stored procedures currently so throw and exception
|
|
/// </summary>
|
|
/// <param name="storedProcedureName"></param>
|
|
/// <returns></returns>
|
|
// public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName)
|
|
// {
|
|
//// ArgumentValidation.CheckForNullReference(storedProcedureName, "storedProcedureName");
|
|
//// ArgumentValidation.CheckForEmptyString(storedProcedureName, "storedProcedureName");
|
|
////
|
|
//// return new SqlCommandWrapper(storedProcedureName, CommandType.StoredProcedure, ParameterToken);
|
|
// }
|
|
|
|
/// <summary>
|
|
/// Not supported
|
|
/// </summary>
|
|
/// <param name="storedProcedureName"></param>
|
|
/// <param name="parameterValues"></param>
|
|
/// <returns></returns>
|
|
// public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName, params object[] parameterValues)
|
|
// {
|
|
//// ArgumentValidation.CheckForNullReference(storedProcedureName, "storedProcedureName");
|
|
//// ArgumentValidation.CheckForEmptyString(storedProcedureName, "storedProcedureName");
|
|
//// ArgumentValidation.CheckForNullReference(parameterValues, "parameterValues");
|
|
////
|
|
//// return new SqlCommandWrapper(storedProcedureName, CommandType.StoredProcedure, ParameterToken, parameterValues);
|
|
// }
|
|
|
|
/// <summary>
|
|
/// <para>Create an <see cref="FbCommandWrapper"/> for a SQL query.</para>
|
|
/// </summary>
|
|
/// <param name="query"><para>The text of the query.</para></param>
|
|
/// <returns><para>The <see cref="DBCommandWrapper"/> for the SQL query.</para></returns>
|
|
/// <exception cref="ArgumentNullException">
|
|
/// <para><paramref name="query"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
|
/// </exception>
|
|
/// <exception cref="ArgumentException">
|
|
/// <para><paramref name="query"/> hast not been initialized.</para>
|
|
/// </exception>
|
|
public override DBCommandWrapper GetSqlStringCommandWrapper(string query)
|
|
{
|
|
ArgumentValidation.CheckForNullReference(query, "query");
|
|
ArgumentValidation.CheckForEmptyString(query, "query");
|
|
//Firebird uses the keyword FIRST exactly the same as ms sql TOP
|
|
query=query.Replace(" TOP "," FIRST ");
|
|
return new FireBirdCommandWrapper(query, CommandType.Text, ParameterToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>Create a <see cref="FbDataAdapter"/> with the given update behavior and connection.</para>
|
|
/// </summary>
|
|
/// <param name="updateBehavior">
|
|
/// <para>One of the <see cref="UpdateBehavior"/> values.</para>
|
|
/// </param>
|
|
/// <param name="connection">
|
|
/// <para>The open connection to the database.</para>
|
|
/// </param>
|
|
/// <returns>An <see cref="DbDataAdapter"/>.</returns>
|
|
/// <exception cref="ArgumentNullException">
|
|
/// <para><paramref name="connection"/> can not be <see langword="null"/> (Nothing in Visual Basic).</para>
|
|
/// </exception>
|
|
protected override DbDataAdapter GetDataAdapter(UpdateBehavior updateBehavior, IDbConnection connection)
|
|
{
|
|
string queryStringToBeFilledInLater = String.Empty;
|
|
FbDataAdapter adapter = new FbDataAdapter(queryStringToBeFilledInLater, (FbConnection)connection);
|
|
|
|
if (updateBehavior == UpdateBehavior.Continue)
|
|
{
|
|
adapter.RowUpdated += new FbRowUpdatedEventHandler(OnFireBirdRowUpdated);
|
|
}
|
|
return adapter;
|
|
}
|
|
|
|
|
|
public override IDataReader ExecuteReader(DBCommandWrapper commandWrapper)
|
|
{
|
|
return new FireBirdDataReaderWrapper((FbDataReader)base.ExecuteReader(commandWrapper));
|
|
}
|
|
|
|
|
|
public override IDataReader ExecuteReader(DBCommandWrapper commandWrapper, IDbTransaction transaction)
|
|
{
|
|
return new FireBirdDataReaderWrapper((FbDataReader)base.ExecuteReader(commandWrapper, transaction));
|
|
}
|
|
|
|
/// <devdoc>
|
|
/// Listens for the RowUpdate event on a dataadapter to support UpdateBehavior.Continue
|
|
/// </devdoc>
|
|
private void OnFireBirdRowUpdated(object sender, FbRowUpdatedEventArgs rowThatCouldNotBeWritten)
|
|
{
|
|
if (rowThatCouldNotBeWritten.RecordsAffected == 0)
|
|
{
|
|
if (rowThatCouldNotBeWritten.Errors != null)
|
|
{
|
|
rowThatCouldNotBeWritten.Row.RowError = SR.ExceptionMessageUpdateDataSetRowFailure;
|
|
rowThatCouldNotBeWritten.Status = UpdateStatus.SkipCurrentRow;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |