⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainunit.cpp

📁 Direct Oracle Access 非常好的Oracle数据库直接访问组件包 支持个版本的Delphi及C++ Builder 有源码
💻 CPP
字号:
//---------------------------------------------------------------------------
// Direct Oracle Access - DirectPath demo
// Allround Automations
// support@allroundautomations.nl
// http://www.allroundautomations.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Oracle"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
// Load the text file that we will load into the database for the benchmark
void __fastcall TMainForm::LoadFile()
{
  int i;
  TStringList *Lines;
  TFileItem *FileItem;

  Lines = new TStringList;
  Lines->LoadFromFile(Filename);
  FileItems = new TCollection(__classid(TFileItem));
  for (i = 0; i < Lines->Count; i++)
  {
    FileItem = new TFileItem(FileItems);
    FileItem->Line = i + 1;
    FileItem->Text = Lines->Strings[i];
    FileItem->Text.SetLength(80);
  }
  delete Lines;
}
//---------------------------------------------------------------------------
// Truncate the table
void __fastcall TMainForm::TruncateTable()
{
  TruncateTableQuery->Execute();
}
//---------------------------------------------------------------------------
// Perform the Single Insert Benchmark
void __fastcall TMainForm::SingleInsertBenchmark()
{
  int i;
  TFileItem *FileItem;

  for (i = 0; i < FileItems->Count; i++)
  {
    FileItem = (TFileItem *)FileItems->Items[i];
    InsertQuery->SetVariable("line", FileItem->Line);
    InsertQuery->SetVariable("text", FileItem->Text);
    InsertQuery->Execute();
  }
  MainSession->Commit();
}
//---------------------------------------------------------------------------
// Perform the Array Insert Benchmark
void __fastcall TMainForm::ArrayInsertBenchmark()
{
  Variant Line, Text;
  int i, j, ArraySize = 100;
  TFileItem *FileItem;

  // Create arrays to hold the column data for the records
  Line = VarArrayCreate(OPENARRAY(int, (0, ArraySize - 1)), varVariant);
  Text = VarArrayCreate(OPENARRAY(int, (0, ArraySize - 1)), varVariant);
  j = 0;
  for (i = 0; i < FileItems->Count; i++)
  {
    // Copy record to column arrays
    FileItem = (TFileItem *)FileItems->Items[i];
    Line.PutElement(FileItem->Line, j);
    Text.PutElement(FileItem->Text, j);
    j++;
    // We have filled the array, or we are at the end of the file: execute it
    if ((j == ArraySize) || (i == FileItems->Count - 1))
    {
      InsertQuery->SetVariable("line", Line);
      InsertQuery->SetVariable("text", Text);
      InsertQuery->ExecuteArray(0, j);
      j = 0;
    }
  };
  MainSession->Commit();
};
//---------------------------------------------------------------------------
// Perform the Direct Path Loading Benchmark
void __fastcall TMainForm::DirectPathLoadBenchmark()
{
  int i, Row;
  TFileItem *FileItem;

  // Prepare the loader
  Loader->Prepare();
  // Process all data in batches of <MaxRows> records
  Row = 0;
  for (i = 0; i < FileItems->Count; i++)
  {
    // Copy record to array
    FileItem = (TFileItem *)FileItems->Items[i];
    Loader->Columns->Items[0]->SetData(Row, &FileItem->Line, 0);
    Loader->Columns->Items[1]->SetData(Row, &FileItem->Text[1], FileItem->Text.Length());
    Row++;
    // We have filled the array, or we are at the end of the file: load it
    if ((Row == Loader->MaxRows) || (i == FileItems->Count - 1))
    {
      try
      {
        Loader->Load(Row);
      }
      catch (EOracleError &E)
      {
        ShowMessage(E.Message + "\n\n" +
                   "Row = " + IntToStr(Loader->LastRow) + ", " +
                   "Col = " + IntToStr(Loader->LastColumn));
        Loader->Abort();
        return;
      }
      Row = 0;
    }
  }
  // Commit the loaded data
  Loader -> Finish();
};
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  if (!MainSession->Connected) MainLogon->Execute();
  if (!MainSession->Connected) Application->Terminate();

}
//---------------------------------------------------------------------------
// Create the table
void __fastcall TMainForm::CreateTableBtnClick(TObject *Sender)
{
  CreateTableQuery->Execute();
}
//---------------------------------------------------------------------------
// Select the text file that we will load into the database for the benchmark
void __fastcall TMainForm::SelectFileBtnClick(TObject *Sender)
{
  if (OpenDialog->Execute())
  {
    Filename = OpenDialog->FileName;
    FilenameLabel->Caption = Filename;
  }
}
//---------------------------------------------------------------------------
// Perform all benchmarks
void __fastcall TMainForm::BenchmarkBtnClick(TObject *Sender)
{
  int T1, T2, T3;
  AnsiString s;

  // Load the text file into memory
  LoadFile();
  Screen->Cursor = crHourGlass;
  try
  {
    // Clear the benchmark results
    ResultsMemo->Text = IntToStr(FileItems->Count) + " Lines";
    ResultsMemo->Update();
    // Truncate the table
    TruncateTable();
    // Perform benchmark with single insert statements
    T1 = GetTickCount();
    SingleInsertBenchmark();
    T1 = GetTickCount() - T1;
    s = "Single Inserts: " + FloatToStr((double)T1 / 1000) + " sec";
    ResultsMemo->Lines->Add(s);
    ResultsMemo->Update();
    // Truncate the table again without influencing the benchmark
    TruncateTable();
    // Perform benchmark with array insert statements
    T2 = GetTickCount();
    ArrayInsertBenchmark();
    T2 = GetTickCount() - T2;
    s = "Array Inserts: " + FloatToStr((double)T2 / 1000) + " sec";
    ResultsMemo->Lines->Add(s);
    ResultsMemo->Update();
    if (!OCI81)
      s = "Direct Path Loading requires Net8 8.1";
    else
    {
      // Truncate the table again without influencing the benchmark
      TruncateTable();
      // Perform benchmark with array insert statements
      T3 = GetTickCount();
      DirectPathLoadBenchmark();
      T3 = GetTickCount() - T3;
      s = "Direct Path Loading: " + FloatToStr((double)T3 / 1000) + " sec";
    }
    ResultsMemo->Lines->Add(s);
  }
  __finally
  {
    delete FileItems;
    Screen->Cursor = crDefault;
  }
}
//---------------------------------------------------------------------------
// Drop the table
void __fastcall TMainForm::DropTableBtnClick(TObject *Sender)
{
  DropTableQuery->Execute();
}
//---------------------------------------------------------------------------
// FileItem constructor
__fastcall TFileItem::TFileItem(TCollection* Collection)
    : TCollectionItem(Collection)
{
  Line = 0;
  Text = "";
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -