//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ModDBStruc.h"
#include "tu_utility.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ComboBox"
#pragma link "dxExtCtrls"
#pragma link "hComboBox98"
#pragma resource "*.dfm"
TFormModDBStruc *FormModDBStruc;
//---------------------------------------------------------------------------
__fastcall TFormModDBStruc::TFormModDBStruc(TComponent* Owner) : TForm(Owner)
{
this->OnShow = FormShow;
ListDatabase = new TStringList;
ListDatabase->Sorted = true;
ListTable = new TStringList;
ListTable->Sorted = true;
ListField = new TStringList;
ListField->Sorted = true;
StringGridDataBase->Options.Clear();
StringGridDataBase->Options
<< goFixedVertLine
<< goFixedHorzLine
<< goVertLine
<< goHorzLine
<< goRowSizing
<< goColSizing
<< goTabs
<< goRowSelect
;
StringGridDataBase->OnClick = StringGridDataBaseClick;
StringGridDataBase->OnDblClick = StringGridDataBaseClick;
StringGridTable->Options.Clear();
StringGridTable->Options
<< goFixedVertLine
<< goFixedHorzLine
<< goVertLine
<< goHorzLine
<< goRowSizing
<< goColSizing
<< goTabs
<< goRowSelect
;
StringGridTable->OnClick = StringGridTableClick;
StringGridTable->OnDblClick = StringGridTableClick;
StringGridField->Options.Clear();
StringGridField->Options
<< goFixedVertLine
<< goFixedHorzLine
<< goVertLine
<< goHorzLine
<< goRowSizing
<< goColSizing
<< goTabs
<< goRowSelect
;
ComboBoxFieldType->Items->Clear();
ComboBoxFieldType->Items->Add(tu_GetCreateFieldType(enVarchar, 2, 0, "") );
ComboBoxFieldType->Items->Add(tu_GetCreateFieldType(enDecimal, 2, 0, "") );
ComboBoxFieldType->Items->Add(tu_GetCreateFieldType(enInteger, 2, 0, "") );
ComboBoxFieldType->Items->Add(tu_GetCreateFieldType(enCurrency, 2, 0, "") );
ComboBoxFieldType->Text = tu_GetCreateFieldType(enVarchar, 2, 0, "");
//this->DatabaseName = "";
//this->DatabaseName = " ";
//this->DatabaseName = "sy";
}
//---------------------------------------------------------------------------
__fastcall TFormModDBStruc::~TFormModDBStruc()
{
delete ListDatabase;
delete ListTable;
delete ListField;
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::AddDatabase(AnsiString DatabaseName)
{
ListDatabase->Clear();
if ( DatabaseName == "" )
Session->GetDatabaseNames(ListDatabase);
else
ListDatabase->Add(DatabaseName);
TStringGrid *StringGrid1 = StringGridDataBase;
StringGrid1->RowCount=2;
StringGrid1->ColCount=1;
StringGrid1->FixedCols=0;
StringGrid1->FixedRows=1;
StringGrid1->DefaultRowHeight=18;
StringGrid1->ColWidths[0]=300;
StringGrid1->Cells[0][0]="数据库名";
for (int i = 0; i < ListDatabase->Count; i++)
{
StringGrid1->Cells[0][StringGrid1->RowCount-1] = ListDatabase->Strings;
StringGrid1->RowCount ++;
}
StringGrid1->RowCount --;
//ShowMessage(StringGrid1->Cells[0][1]);
this->DatabaseName = StringGrid1->Cells[0][1];
AddTable(this->DatabaseName);
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::AddTable(AnsiString DatabaseName)
{
bool CanShow;
this->DatabaseName = DatabaseName;
ListTable->Clear();
Session->GetTableNames(DatabaseName, "*.*", true, false, ListTable);
TStringGrid *StringGrid1 = StringGridTable;
StringGrid1->RowCount=2;
StringGrid1->ColCount=1;
StringGrid1->FixedCols=0;
StringGrid1->FixedRows=1;
StringGrid1->DefaultRowHeight=18;
StringGrid1->ColWidths[0]=300;
StringGrid1->Cells[0][0]="表名";
for (int i = 0; i < ListTable->Count; i++)
{
CanShow = true;
if ( TableShow != NULL )
CanShow = TableShow(this->DatabaseName, ListTable->Strings);
if ( CanShow )
{
StringGrid1->Cells[0][StringGrid1->RowCount-1] = ListTable->Strings;
StringGrid1->RowCount ++;
}
}
StringGrid1->RowCount --;
this->TableName = StringGrid1->Cells[0][1];
AddField(this->DatabaseName, this->TableName);
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::AddField(AnsiString DatabaseName, AnsiString TableName)
{
TTable *Table1 = new TTable(this);
Table1->DatabaseName = DatabaseName;
Table1->TableName = TableName;
Table1->Open();
ListField->Clear();
TStringGrid *StringGrid1 = StringGridField;
StringGrid1->RowCount=2;
StringGrid1->ColCount=3;
StringGrid1->FixedCols=0;
StringGrid1->FixedRows=1;
StringGrid1->DefaultRowHeight=18;
StringGrid1->ColWidths[0]=60;
StringGrid1->ColWidths[1]=80;
StringGrid1->ColWidths[2]=30;
StringGrid1->Cells[0][0]="域名";
StringGrid1->Cells[1][0]="类型";
StringGrid1->Cells[2][0]="宽度";
for(int i=0;i<Table1->FieldCount ;++i)
{
ListField->Add(Table1->Fields->Fields->FieldName);
StringGrid1->Cells[0][StringGrid1->RowCount-1]=Table1->Fields->Fields->FieldName;
StringGrid1->Cells[1][StringGrid1->RowCount-1]=tu_GetFieldTypeStrIDStrDesc(Table1->Fields->Fields->DataType, 1, 0);
StringGrid1->Cells[2][StringGrid1->RowCount-1]=Table1->Fields->Fields->DataSize-1;
StringGrid1->RowCount ++;
}
StringGrid1->RowCount --;
Table1->Free();
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::StringGridDataBaseClick(TObject *Sender)
{
this->DatabaseName = StringGridDataBase->Cells[0][StringGridDataBase->Row];
AddTable(this->DatabaseName);
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::StringGridTableClick(TObject *Sender)
{
//ShowMessage(ListTable->Strings[StringGridTable->Row-1]);
this->TableName = StringGridTable->Cells[0][StringGridTable->Row];
AddField(this->DatabaseName, this->TableName);
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::BitBtnOKClick(TObject *Sender)
{
AnsiString strSQL =
"Alter Table " + this->TableName
+ " Add Column " + ComboBoxFieldName->Text
+ " " + tu_GetCreateFieldType(StrToIntDef( tu_GetCreateFieldType(0, 0, 0, ComboBoxFieldType->Text), 0) , 1, 0, "")
;
if ( ComboBoxFieldType->Text == tu_GetCreateFieldType(enDecimal, 2, 0, "")
|| ComboBoxFieldType->Text == tu_GetCreateFieldType(enVarchar, 2, 0, "")
)
strSQL = strSQL + "(" + IntToStr(((int)(dxSpinEditFieldLen->Value)));
if ( ComboBoxFieldType->Text == tu_GetCreateFieldType(enDecimal, 2, 0, "") )
strSQL = strSQL + "," + IntToStr(((int)(dxSpinEditFieldDec->Value)));
if ( ComboBoxFieldType->Text == tu_GetCreateFieldType(enDecimal, 2, 0, "")
|| ComboBoxFieldType->Text == tu_GetCreateFieldType(enVarchar, 2, 0, "")
)
strSQL = strSQL + ")";
if ( CheckBoxNull->Checked )
strSQL = strSQL + " null";
else
strSQL = strSQL + " not null";
if ( IDCANCEL != MessageBox(this->Handle, ("添加列 " + ComboBoxFieldName->Text + "/n" + strSQL).c_str(), "确认", MB_ICONQUESTION || MB_OKCANCEL ) )
{
tu_EXECstrSQL(this->DatabaseName, strSQL);
if ( AfterAddField != NULL )
AfterAddField(this->DatabaseName, this->TableName, ComboBoxFieldName->Text);
AddField(this->DatabaseName, this->TableName);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::BitBtnDeleteFieldClick(TObject *Sender)
{
bool CanDelete;
AnsiString strSQL =
"Alter Table " + this->TableName
+ " Drop Column " + StringGridField->Cells[0][StringGridField->Row]
;
CanDelete = true;
if ( FieldCanDelete != NULL )
CanDelete = FieldCanDelete(this->DatabaseName, this->TableName, StringGridField->Cells[0][StringGridField->Row]);
if ( !CanDelete )
MessageBox(this->Handle, ("不允许删除列 " + StringGridField->Cells[0][StringGridField->Row] + "/n" + strSQL).c_str(), "提示", MB_ICONINFORMATION );
else
if ( IDCANCEL != MessageBox(this->Handle, ("删除列 " + StringGridField->Cells[0][StringGridField->Row] + "/n" + strSQL).c_str(), "确认", MB_ICONQUESTION || MB_OKCANCEL ) )
{
tu_EXECstrSQL(this->DatabaseName, strSQL);
if ( AfterDeleteField != NULL )
AfterDeleteField(this->DatabaseName, this->TableName, StringGridField->Cells[0][StringGridField->Row]);
AddField(this->DatabaseName, this->TableName);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormModDBStruc::FormShow(TObject *Sender)
{
AddDatabase(this->DatabaseName);
}
//---------------------------------------------------------------------------