Changeset 238 for Network


Ignore:
Timestamp:
Apr 30, 2011, 11:49:00 PM (14 years ago)
Author:
george
Message:
  • Modified: TSqlDatabase is now TComponent descendant.
Location:
Network/CoolWeb
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/CoolWeb.lpk

    r237 r238  
    1919    <Description Value="Unit set for generating CGI or server web application."/>
    2020    <License Value="GNU/GPL"/>
    21     <Version Minor="1"/>
     21    <Version Minor="2"/>
    2222    <Files Count="17">
    2323      <Item1>
     
    4747      <Item6>
    4848        <Filename Value="Persistence/USqlDatabase.pas"/>
     49        <HasRegisterProc Value="True"/>
    4950        <UnitName Value="USqlDatabase"/>
    5051      </Item6>
  • Network/CoolWeb/CoolWeb.pas

    r237 r238  
    2121  RegisterUnit('UHTTPSessionFile', @UHTTPSessionFile.Register);
    2222  RegisterUnit('UHTTPSessionMySQL', @UHTTPSessionMySQL.Register);
     23  RegisterUnit('USqlDatabase', @USqlDatabase.Register);
    2324end;
    2425
  • 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
  • Network/CoolWeb/WebServer/UHTTPSessionMySQL.pas

    r237 r238  
    1717  private
    1818    FSessionIdCookieName: string;
     19    FDatabase: TSqlDatabase;
    1920    FTimeout: Integer;
    2021    Lock: TCriticalSection;
     
    2223    procedure GetSessionId(HandlerData: THTTPHandlerData);
    2324  public
    24     SqlDatabase: TSqlDatabase;
    2525    Sessions: TStringList;
    2626    procedure Load(HandlerData: THTTPHandlerData); override;
     
    2929    destructor Destroy; override;
    3030  published
     31    property Database: TSqlDatabase read FDatabase write FDatabase;
    3132    property Timeout: Integer read FTimeout write FTimeout; // in seconds
    3233    property SessionIdCookieName: string read FSessionIdCookieName
     
    5556    try
    5657      DbRows := TDbRows.Create;
    57       SqlDatabase.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
     58      Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
    5859        Result + '"');
    5960      Found := DbRows.Count > 0;
     
    8384    Lock.Acquire;
    8485    DbRows := TDbRows.Create;
    85     SqlDatabase.Query(DbRows, 'DELETE FROM `HTTPSession` WHERE `Time` < DATE_SUB(NOW(), INTERVAL ' +
     86    Database.Query(DbRows, 'DELETE FROM `HTTPSession` WHERE `Time` < DATE_SUB(NOW(), INTERVAL ' +
    8687      IntToStr(Timeout) +' SECOND)');
    87     SqlDatabase.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
     88    Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
    8889      HandlerData.SessionId + '"');
    8990    if DbRows.Count > 0 then begin
     
    108109    DbRows := TDbRows.Create;
    109110    DbRows2 := TDbRows.Create;
    110     SqlDatabase.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
     111    Database.Query(DbRows, 'SELECT * FROM `HTTPSession` WHERE `Identification`="' +
    111112      HandlerData.SessionId + '"');
    112113    if DbRows.Count > 0 then
    113       SqlDatabase.Query(DbRows2, 'UPDATE `HTTPSession` SET `Variables`="' + HandlerData.Session.Text
     114      Database.Query(DbRows2, 'UPDATE `HTTPSession` SET `Variables`="' + HandlerData.Session.Text
    114115        + '", `Time` = NOW() WHERE `Identification`="' + HandlerData.SessionId + '"')
    115     else SqlDatabase.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`,  `Variables`, `Identification`) VALUES (' +
     116    else Database.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`,  `Variables`, `Identification`) VALUES (' +
    116117    'NOW(), "' + HandlerData.Session.Text + '", "' + HandlerData.SessionId + '")');
    117118    HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
Note: See TracChangeset for help on using the changeset viewer.