1 | unit EngineMySQL;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, Dialogs, SysUtils, DbEngine, USqlDatabase, HtmlClasses,
|
---|
7 | Generics;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
38 | implementation
|
---|
39 |
|
---|
40 | { TDbConnectParamsMySQL }
|
---|
41 |
|
---|
42 | function TDbConnectParamsMySQL.GetConnectionString: string;
|
---|
43 | begin
|
---|
44 | Result := 'mysql://' + Host + ':' + IntToStr(Port);
|
---|
45 | end;
|
---|
46 |
|
---|
47 | procedure TDbConnectParamsMySQL.SetConnectionString(AValue: string);
|
---|
48 | var
|
---|
49 | URL: TURL;
|
---|
50 | begin
|
---|
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;
|
---|
61 | end;
|
---|
62 |
|
---|
63 | { TDatabaseMySQL }
|
---|
64 |
|
---|
65 | procedure TDatabaseMySQL.LoadFields(Table: TTable);
|
---|
66 | var
|
---|
67 | DbRows: TDbRows;
|
---|
68 | DbRows2: TDbRows;
|
---|
69 | NewField: TField;
|
---|
70 | I: Integer;
|
---|
71 | DataType: Integer;
|
---|
72 | TypeName: string;
|
---|
73 | begin
|
---|
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;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TDatabaseMySQL.Query(Text: string; DbRows: TDbRows = nil);
|
---|
103 | begin
|
---|
104 | SqlDatabase.Query(DbRows, Text);
|
---|
105 | end;
|
---|
106 |
|
---|
107 | constructor TDatabaseMySQL.Create;
|
---|
108 | begin
|
---|
109 | inherited Create;
|
---|
110 | SqlDatabase := TSqlDatabase.Create(nil);
|
---|
111 | end;
|
---|
112 |
|
---|
113 | destructor TDatabaseMySQL.Destroy;
|
---|
114 | begin
|
---|
115 | SqlDatabase.Free;
|
---|
116 | inherited Destroy;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | procedure TDatabaseMySQL.Load;
|
---|
120 | var
|
---|
121 | URL: TURL;
|
---|
122 | begin
|
---|
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;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TDatabaseMySQL.Save;
|
---|
144 | begin
|
---|
145 | SqlDatabase.Disconnect;
|
---|
146 | inherited Save;
|
---|
147 | end;
|
---|
148 |
|
---|
149 | end.
|
---|
150 |
|
---|