Ignore:
Timestamp:
Apr 30, 2011, 11:49:00 PM (14 years ago)
Author:
george
Message:
  • Modified: TSqlDatabase is now TComponent descendant.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/Persistence/USqlDatabase.pas

    r104 r238  
    3030  end;
    3131
    32   TSqlDatabase = class
    33     procedure mySQLClient1ConnectError(Sender: TObject; Msg: String);
     32  { TSqlDatabase }
     33
     34  TSqlDatabase = class(TComponent)
    3435  private
     36    FEncoding: string;
     37    FHostName: string;
     38    FPassword: string;
    3539    FSession: PMYSQL;
    3640    FConnected: Boolean;
    3741    FDatabase: string;
     42    FUserName: string;
     43    procedure mySQLClient1ConnectError(Sender: TObject; Msg: String);
    3844    function GetConnected: Boolean;
    3945    function GetLastErrorMessage: string;
    4046    function GetLastErrorNumber: Integer;
    4147    function GetCharset: string;
     48    procedure SetConnected(const AValue: Boolean);
    4249    procedure SetDatabase(const Value: string);
    4350  public
    44     Hostname: string;
    45     UserName: string;
    46     Password: string;
    47     Encoding: string;
    48     Table: string;
    49     RepeatLastAction: Boolean;
     51    LastUsedTable: string;
    5052    LastQuery: string;
    5153    procedure CreateDatabase;
     
    6365    property LastErrorMessage: string read GetLastErrorMessage;
    6466    property LastErrorNumber: Integer read GetLastErrorNumber;
    65     property Connected: Boolean read GetConnected;
    66     constructor Create;
     67    constructor Create(AOwner: TComponent); override;
    6768    destructor Destroy; override;
    6869    property Charset: string read GetCharset;
     70  published
     71    property Connected: Boolean read GetConnected write SetConnected;
    6972    property Database: string read FDatabase write SetDatabase;
     73    property HostName: string read FHostName write FHostName;
     74    property UserName: string read FUserName write FUserName;
     75    property Password: string read FPassword write FPassword;
     76    property Encoding: string read FEncoding write FEncoding;
    7077  end;
    7178
     
    7481  function SQLToDateTime(Value: string): TDateTime;
    7582  function DateTimeToSQL(Value: TDateTime): string;
     83
     84procedure Register;
     85
    7686
    7787implementation
     
    98108  CLIENT_TRANSACTIONS = 8192;    // Client knows about transactions
    99109
     110procedure Register;
     111begin
     112  RegisterComponents('CoolWeb', [TSqlDatabase]);
     113end;
     114
    100115function MySQLFloatToStr(F: Real): string;
    101116var
     
    157172  Rows: TDbRows;
    158173begin
    159   RepeatLastAction := False;
    160174//  mySQLClient1.Connect;
    161175  FSession := mysql_init(FSession);
     
    187201  DbResult: TDbRows;
    188202begin
    189   Table := ATable;
     203  LastUsedTable := ATable;
    190204  DbNames := '';
    191205  DbValues := '';
     
    201215  try
    202216    DbResult := TDbRows.Create;
    203     Query(DbResult, 'INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     217    Query(DbResult, 'INSERT INTO `' + ATable + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    204218  finally
    205219    DbResult.Free;
     
    215229  DbRows.Clear;
    216230  //DebugLog('SqlDatabase query: '+Data);
    217   RepeatLastAction := False;
    218231  LastQuery := Data;
    219232  mysql_query(FSession, PChar(Data));
     
    247260  DbResult: TDbRows;
    248261begin
    249   Table := ATable;
     262  LastUsedTable := ATable;
    250263  DbNames := '';
    251264  DbValues := '';
     
    261274  try
    262275    DbResult := TDbRows.Create;
    263     Query(DbResult, 'REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');
     276    Query(DbResult, 'REPLACE INTO `' + ATable + '` (' + DbNames + ') VALUES (' + DbValues + ')');
    264277  finally
    265278    DbResult.Free;
     
    269282procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1');
    270283begin
    271   Table := ATable;
    272   Query(DbRows, 'SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);
     284  LastUsedTable := ATable;
     285  Query(DbRows, 'SELECT ' + Filter + ' FROM `' + ATable + '` WHERE ' + Condition);
    273286end;
    274287
     
    280293  DbResult: TDbRows;
    281294begin
    282   Table := ATable;
     295  LastUsedTable := ATable;
    283296  DbValues := '';
    284297  for I := 0 to Data.Count - 1 do begin
     
    291304  try
    292305    DbResult := TDbRows.Create;
    293     Query(DbResult, 'UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);
     306    Query(DbResult, 'UPDATE `' + ATable + '` SET (' + DbValues + ') WHERE ' + Condition);
    294307  finally
    295308    DbResult.Free;
     
    306319  DbResult: TDbRows;
    307320begin
    308   Table := ATable;
     321  LastUsedTable := ATable;
    309322  try
    310323    DbResult := TDbRows.Create;
    311     Query(DbResult, 'DELETE FROM `' + Table + '` WHERE ' + Condition);
     324    Query(DbResult, 'DELETE FROM `' + ATable + '` WHERE ' + Condition);
    312325  finally
    313326    DbResult.Free;
     
    326339end;
    327340
    328 constructor TSqlDatabase.Create;
     341constructor TSqlDatabase.Create(AOwner: TComponent);
    329342begin
    330343  inherited;
     
    406419end;
    407420
     421procedure TSqlDatabase.SetConnected(const AValue: Boolean);
     422begin
     423  if AValue = FConnected then Exit;
     424  if AValue then Connect
     425    else Disconnect;
     426end;
     427
    408428procedure TSqlDatabase.SetDatabase(const Value: string);
    409429begin
Note: See TracChangeset for help on using the changeset viewer.