-
Notifications
You must be signed in to change notification settings - Fork 5
/
MiniREST.SQL.Firebird.pas
208 lines (193 loc) · 7.47 KB
/
MiniREST.SQL.Firebird.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{$IFDEF FPC}
{$mode DELPHI}
{$IFEND}
unit MiniREST.SQL.Firebird;
interface
uses SysUtils, MiniREST.SQL.Intf, MiniREST.SQL.Common;
type
TMiniRESTSQLDatabaseInfoFirebird = class(TInterfacedObject, IMiniRESTSQLDatabaseInfo)
private
//Declared as pointer to avoid circular reference that causes memory leak
FConnection: Pointer;
function CreateQuery: IMiniRESTSQLQuery;
public
constructor Create(AConnection: IMiniRESTSQLConnection);
function FieldExists(const ATableName: string;
const AFieldName: string): Boolean;
function GetForeignKeys(const ATableName: string): TArray<IMiniRESTSQLForeignKeyInfo>;
function GetPrimaryKey(const ATableName: string): IMiniRESTSQLPrimaryKeyInfo;
function TableExists(const ATableName: string): Boolean;
function DatabaseType: TMiniRESTSQLDatabaseType;
function GetColumns(const ATableName: string): TArray<IMiniRESTSQLColumnInfo>;
end;
implementation
uses MiniREST.SQL.Base, MiniREST.Common.Utils;
{ TMiniRESTSQLDatabaseInfoFirebird }
constructor TMiniRESTSQLDatabaseInfoFirebird.Create(
AConnection: IMiniRESTSQLConnection);
begin
FConnection := AConnection;
end;
function TMiniRESTSQLDatabaseInfoFirebird.CreateQuery: IMiniRESTSQLQuery;
begin
Result := IMiniRESTSQLConnection(FConnection).GetQuery;
end;
function TMiniRESTSQLDatabaseInfoFirebird.DatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := dbtFirebird;
end;
function TMiniRESTSQLDatabaseInfoFirebird.FieldExists(const ATableName,
AFieldName: string): Boolean;
var
LConnection: IMiniRESTSQLConnection;
LQry: IMiniRESTSQLQuery;
begin
LConnection := IMiniRESTSQLConnection(FConnection);
LQry := LConnection.GetQuery('SELECT FIRST 1 0 FROM RDB$RELATION_FIELDS R WHERE (UPPER(R.RDB$RELATION_NAME) = UPPER(:TABLE)) ' +
' AND (UPPER(R.RDB$FIELD_NAME) = UPPER(:FIELD))');
LQry.ParamByName('TABLE').AsString := ATableName;
LQry.ParamByName('FIELD').AsString := AFieldName;
LQry.Open;
Result := not LQry.DataSet.IsEmpty;
end;
function TMiniRESTSQLDatabaseInfoFirebird.GetColumns(
const ATableName: string): TArray<IMiniRESTSQLColumnInfo>;
var
LQry: IMiniRESTSQLQuery;
LColumnInfo: IMiniRESTSQLColumnInfo;
begin
SetLength(Result, 0);
LQry := CreateQuery;
LQry.SQL := 'select f.rdb$relation_name, f.rdb$field_name' + #13#10 +
'from rdb$relation_fields f' + #13#10 +
'join rdb$relations r on f.rdb$relation_name = r.rdb$relation_name' + #13#10 +
'and r.rdb$view_blr is null' + #13#10 +
'and (r.rdb$system_flag is null or r.rdb$system_flag = 0)' + #13#10 +
'where f.rdb$relation_name = :TABLE' + #13#10 +
'order by 1, f.rdb$field_position';
LQry.ParamByName('TABLE').AsString := ATableName;
LQry.Open;
while not LQry.DataSet.Eof do
begin
LColumnInfo := TMiniRESTSQLColumnInfo.Create(
Trim(LQry.DataSet.FieldByName('RDB$FIELD_NAME').AsString)
);
TMiniRESTCommonUtils{$IFDEF FPC}<IMiniRESTSQLColumnInfo>{$IFEND}
.AddToArray{$IFNDEF FPC}<IMiniRESTSQLColumnInfo>{$IFEND}(LColumnInfo, Result);
LQry.DataSet.Next;
end;
end;
function TMiniRESTSQLDatabaseInfoFirebird.GetForeignKeys(
const ATableName: string): TArray<IMiniRESTSQLForeignKeyInfo>;
var
LConnection: IMiniRESTSQLConnection;
LQry: IMiniRESTSQLQuery;
LSQL: string;
LForeignKey: IMiniRESTSQLForeignKeyInfo;
LFields, LFKFields: TArray<string>;
begin
LForeignKey := nil;
SetLength(LFields, 0);
SetLength(LFKFields, 0);
SetLength(Result, 0);
LConnection := IMiniRESTSQLConnection(FConnection);
LSQL := 'SELECT IDX.RDB$FIELD_POSITION AS ORDEM, RL.RDB$CONSTRAINT_NAME AS NAME, RL.RDB$RELATION_NAME AS TBL, IDX.RDB$FIELD_NAME AS FIELD, RL2.RDB$RELATION_NAME AS TBL_FK,' + #13#10 +
'IDX2.RDB$FIELD_NAME AS FIELD_FK' + #13#10 +
'FROM RDB$RELATION_CONSTRAINTS RL' + #13#10 +
'JOIN RDB$INDEX_SEGMENTS IDX ON RL.RDB$INDEX_NAME = IDX.RDB$INDEX_NAME' + #13#10 +
'JOIN RDB$REF_CONSTRAINTS REF ON REF.RDB$CONSTRAINT_NAME = RL.RDB$CONSTRAINT_NAME' + #13#10 +
'JOIN RDB$RELATION_CONSTRAINTS RL2 ON RL2.RDB$CONSTRAINT_NAME = REF.RDB$CONST_NAME_UQ' + #13#10 +
'JOIN RDB$INDEX_SEGMENTS IDX2 ON IDX2.RDB$INDEX_NAME = RL2.RDB$INDEX_NAME AND' + #13#10 +
'IDX2.RDB$FIELD_POSITION = IDX.RDB$FIELD_POSITION' + #13#10 +
'WHERE RL.RDB$RELATION_NAME = :TABLE AND' + #13#10 +
'RL.RDB$CONSTRAINT_TYPE = ''FOREIGN KEY''' + #13#10 +
'ORDER BY 1 ASC';
LQry := LConnection.GetQuery(LSQL);
LQry.ParamByName('TABLE').AsString := ATableName;
LQry.Open;
while not LQry.DataSet.Eof do
begin
if (not Assigned(LForeignKey)) or
(LForeignKey.Name <> LQry.DataSet.FieldByName('NAME').AsString) then
begin
if (not Assigned(LForeignKey)) then
begin
LForeignKey := TMiniRESTSQLForeignKeyInfo.Create;
end
else
begin
LForeignKey.Fields := LFields;
LForeignKey.FKFields := LFKFields;
LForeignKey := TMiniRESTSQLForeignKeyInfo.Create;
end;
LForeignKey.Name := LQry.DataSet.FieldByName('NAME').AsString;
SetLength(LFields, 1);
SetLength(LFKFields, 1);
LFields[0] := LQry.DataSet.FieldByName('FIELD').AsString;
LFKFields[0] := LQry.DataSet.FieldByName('FIELD_FK').AsString;
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := LForeignKey;
end
else
begin
SetLength(LFields, Length(LFields) + 1);
SetLength(LFKFields, Length(LFKFields) + 1);
LFields[Length(LFields) - 1] := LQry.DataSet.FieldByName('FIELD').AsString;
LFKFields[Length(LFKFields) - 1] := LQry.DataSet.FieldByName('FIELD_FK').AsString;
end;
LQry.DataSet.Next;
if LQry.DataSet.Eof then
begin
LForeignKey.Fields := LFields;
LForeignKey.FKFields := LFKFields;
end;
end;
end;
function TMiniRESTSQLDatabaseInfoFirebird.GetPrimaryKey(
const ATableName: string): IMiniRESTSQLPrimaryKeyInfo;
var
LConnection: IMiniRESTSQLConnection;
LQry: IMiniRESTSQLQuery;
LSQL: string;
LFields: TArray<string>;
begin
Result := nil;
SetLength(LFields, 0);
LConnection := IMiniRESTSQLConnection(FConnection);
LSQL := 'SELECT IDX.RDB$FIELD_POSITION AS ORDEM, IDX.RDB$FIELD_NAME AS FIELD,' + #13#10 +
'RL.RDB$CONSTRAINT_NAME AS NAME' + #13#10 +
'FROM RDB$RELATION_CONSTRAINTS RL' + #13#10 +
'JOIN RDB$INDEX_SEGMENTS IDX ON RL.RDB$INDEX_NAME = IDX.RDB$INDEX_NAME' + #13#10 +
'WHERE RL.RDB$RELATION_NAME = :TABLE AND' + #13#10 +
'RL.RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''' + #13#10 +
'ORDER BY 1';
LQry := LConnection.GetQuery(LSQL);
LQry.ParamByName('TABLE').AsString := ATableName;
LQry.Open;
if not LQry.DataSet.IsEmpty then
begin
Result := TMiniRESTSQLPrimaryKeyInfo.Create;
Result.Name := Trim(LQry.DataSet.FieldByName('NAME').AsString);
while not LQry.DataSet.Eof do
begin
SetLength(LFields, Length(LFields) + 1);
LFields[Length(LFields) - 1] := Trim(LQry.DataSet.FieldByName('FIELD').AsString);
LQry.DataSet.Next;
end;
Result.Fields := LFields;
end;
end;
function TMiniRESTSQLDatabaseInfoFirebird.TableExists(
const ATableName: string): Boolean;
var
LConnection: IMiniRESTSQLConnection;
LQry: IMiniRESTSQLQuery;
begin
LConnection := IMiniRESTSQLConnection(FConnection);
LQry := LConnection.GetQuery('SELECT FIRST 1 0 FROM RDB$RELATIONS R WHERE ' +
'UPPER(R.RDB$RELATION_NAME) = UPPER(:TABELA)');
LQry.ParamByName('TABELA').AsString := ATableName;
LQry.Open;
Result := not LQry.DataSet.IsEmpty;
end;
end.