using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using YariSoft.DBUtil;
//using Hargal;
namespace YariSoft.DBCommander{
public class TableManager : IManager
{
#region Local variables
#endregion
#region Properties
#endregion
#region Constructor/Destructor
public TableManager( OleDbConnection Connecton, SchemaGenerator SchemaGenerator )
: base ( Connecton, SchemaGenerator )
{
}
public override void Dispose(){
}
#endregion
#region Public functions
public override DataView FillData( object SelectValue )
{
DataView result = null;
string tableName = SelectValue.ToString().Trim();
if( tableName == "" ){
return result;
}
result = this.GetData( tableName );
if( result != null ){
return result;
}
if( !this.OpenConnection() ){
return result;
}
try {
YOleDbAdapter adapter = null;
if( this.schema.Adapters.ContainsKey( tableName ) ){
adapter = ( YOleDbAdapter )this.schema.Adapters[ tableName ];
} else {
adapter = this.PrepareDataAdapter ( tableName );
this.schema.Adapters.Add( tableName, adapter );
}
adapter.DataAdapter.Fill( this.schema.Schema, tableName );
result = this.schema.Schema.Tables[tableName].DefaultView;
this.needRefresh = false;
} catch(Exception Exp ) {
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
if( result != null ){
result.Dispose();
}
}
this.CloseConnection();
return result;
}
public override DataView GetData( object SelectValue )
{
this.schema.Schema.Tables[SelectValue.ToString()].DefaultView.RowFilter = "";
if( this.schema.Schema.Tables[SelectValue.ToString()].DefaultView.Count > 0 ){
return this.schema.Schema.Tables[SelectValue.ToString()].DefaultView;
}
return null;
}
public override DataView RefreshData( object SelectValue )
{
DataView result = null;
string tableName = SelectValue.ToString().Trim();
if( tableName == "" ){
return result;
}
if( this.schema.Schema.Tables[tableName] != null ){
while( this.schema.Schema.Tables[tableName].Rows.Count < this.schema.Schema.Tables[tableName].DefaultView.Count ){
this.schema.Schema.Tables[tableName].DefaultView.Delete( this.schema.Schema.Tables[tableName].DefaultView.Count - 1 );
}
this.schema.Schema.Tables[tableName].Clear();
}
return this.FillData( SelectValue ) ;
}
public override bool UpdateData( object UpdateObject )
{
bool result = true;
if( (( DataTable )UpdateObject).GetChanges() == null ){
return result;
}
YOleDbAdapter adapter = null;
try{
DataTable changes = (( DataTable )UpdateObject);
if( changes == null ){
return true;
}
adapter = ( YOleDbAdapter )this.schema.Adapters[(( DataTable )UpdateObject).TableName];
if( adapter != null ){
adapter.DataAdapter.Update( changes );
}
} catch( Exception Exp ) {
result = false;
if ( Exp is DBConcurrencyException && adapter != null ){
result = YariSoft.DBUtil.Util.TryToSaveConcurrencyData ( ( DBConcurrencyException )Exp, adapter.DataAdapter );
}
if( !result ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
(( DataTable )UpdateObject).RejectChanges();
this.needRefresh = true;
}
return result;
}
public override bool FillTableDataByFK ( string FKName )
{
string childTableName = YariSoft.DBUtil.Util.GetTableNameByFK( FKName, this.schema.Schema );
if( childTableName != "" ){
if( this.schema.Schema.Tables[childTableName].Rows.Count > 0 ){
return true;
}
this.FillData( childTableName );
return true;
}
return false;
}
#endregion
#region Private functions
private YOleDbAdapter PrepareDataAdapter ( string TableName )
{
OleDbDataAdapter dataAdapter = new System.Data.OleDb.OleDbDataAdapter( "SELECT * FROM "+ DBUtil.Util.GetTableName( TableName ), this.connecton );
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder( dataAdapter );
commandBuilder.QuotePrefix = "[";
commandBuilder.QuoteSuffix = "]";
return new YOleDbAdapter ( dataAdapter, commandBuilder );
}
private void OnListChanged(object sender, System.ComponentModel.ListChangedEventArgs args)
{
if( args.ListChangedType == System.ComponentModel.ListChangedType.ItemAdded ||
args.ListChangedType == System.ComponentModel.ListChangedType.ItemChanged ||
args.ListChangedType == System.ComponentModel.ListChangedType.ItemDeleted ){
this.UpdateData( (( DataView )sender).Table );
}
}
private void OnDataTableChanged(object sender, System.Data.DataRowChangeEventArgs e)
{
this.UpdateData( sender );
}
#endregion
}
}
|