source: trunk/DbEngines/EngineMySQL.pas

Last change on this file was 32, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from units names.
  • Modified: Use TFormEx for all forms.
File size: 3.6 KB
Line 
1unit EngineMySQL;
2
3interface
4
5uses
6 Classes, Dialogs, SysUtils, DbEngine, USqlDatabase, HtmlClasses,
7 Generics;
8
9type
10
11 { TDatabaseMySQL }
12
13 TDatabaseMySQL = class(TDbClient)
14 private
15 procedure LoadFields(Table: TTable);
16 protected
17 public
18 SqlDatabase: TSqlDatabase;
19 procedure Query(Text: string; DbRows: TDbRows = nil); override;
20 constructor Create; override;
21 destructor Destroy; override;
22 procedure Load; override;
23 procedure Save; override;
24 end;
25
26 { TDbConnectParamsMySQL }
27
28 TDbConnectParamsMySQL = class(TDbConnectParams)
29 protected
30 function GetConnectionString: string; override;
31 procedure SetConnectionString(AValue: string); override;
32 public
33 Host: string;
34 Port: Word;
35 end;
36
37
38implementation
39
40{ TDbConnectParamsMySQL }
41
42function TDbConnectParamsMySQL.GetConnectionString: string;
43begin
44 Result := 'mysql://' + Host + ':' + IntToStr(Port);
45end;
46
47procedure TDbConnectParamsMySQL.SetConnectionString(AValue: string);
48var
49 URL: TURL;
50begin
51 URL := TURL.Create;
52 try
53 URL.AsString := AValue;
54 if (URL.Scheme <> 'mysql') and (AValue <> '') then
55 raise Exception.Create('Wrong connection string. Required mysql scheme.');
56 Host := URL.Host.AsString;
57 Port := URL.Port;
58 finally
59 URL.Free;
60 end;
61end;
62
63{ TDatabaseMySQL }
64
65procedure TDatabaseMySQL.LoadFields(Table: TTable);
66var
67 DbRows: TDbRows;
68 DbRows2: TDbRows;
69 NewField: TField;
70 I: Integer;
71 DataType: Integer;
72 TypeName: string;
73begin
74 DbRows := TDbRows.Create;
75 DbRows2 := TDbRows.Create;
76 try
77 SqlDatabase.Query(DbRows, 'SELECT * FROM `ModelField` WHERE `Model` = ' + IntToStr(Table.Id) + '');
78 for I := 0 to DbRows.Count - 1 do begin
79 NewField := TField.Create;
80 NewField.Table := Table;
81 NewField.Name := TDictionaryStringString(DbRows[I]).Items['Name'];
82 NewField.TextBefore := TDictionaryStringString(DbRows[I]).Items['Title'];
83 DataType := StrToInt(TDictionaryStringString(DbRows[I]).Items['Type']);
84 SqlDatabase.Query(DbRows2, 'SELECT * FROM `DataType` WHERE `Id` = ' + IntToStr(DataType) + '');
85 if DbRows2.Count > 0 then begin
86 TypeName := TDictionaryStringString(DbRows2[0]).Items['Name'];
87 NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByName(TypeName);
88 if not Assigned(NewField.DataType) then
89 NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByType(ftString);
90 end else begin
91 // Use string as default
92 NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByType(ftString);
93 end;
94 Table.Fields.Add(NewField);
95 end;
96 finally
97 DbRows.Free;
98 DbRows2.Free;
99 end;
100end;
101
102procedure TDatabaseMySQL.Query(Text: string; DbRows: TDbRows = nil);
103begin
104 SqlDatabase.Query(DbRows, Text);
105end;
106
107constructor TDatabaseMySQL.Create;
108begin
109 inherited Create;
110 SqlDatabase := TSqlDatabase.Create(nil);
111end;
112
113destructor TDatabaseMySQL.Destroy;
114begin
115 SqlDatabase.Free;
116 inherited Destroy;
117end;
118
119procedure TDatabaseMySQL.Load;
120var
121 URL: TURL;
122begin
123 URL := TURL.Create;
124 try
125 URL.AsString := ConnectProfile.Params.ConnectionString;
126 if URL.Scheme <> 'mysql' then
127 raise Exception.Create('Wrong connection string. Required mysql protocol.');
128
129 SqlDatabase.HostName := URL.Host.AsString;
130 if URL.Port <> 0 then
131 SqlDatabase.Port := URL.Port;
132 SqlDatabase.UserName := URL.UserName;
133 SqlDatabase.Password := URL.Password;
134 if Copy(URL.Path, 1, 1) = '/' then
135 SqlDatabase.Database := Copy(URL.Path, 2, High(Integer));
136 SqlDatabase.Connect;
137 finally
138 URL.Free;
139 end;
140 inherited Load;
141end;
142
143procedure TDatabaseMySQL.Save;
144begin
145 SqlDatabase.Disconnect;
146 inherited Save;
147end;
148
149end.
150
Note: See TracBrowser for help on using the repository browser.