ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/runtime/IBStoredProc.pas
(Generate patch)

Comparing ibx/trunk/runtime/IBStoredProc.pas (file contents):
Revision 5 by tony, Fri Feb 18 16:26:16 2011 UTC vs.
Revision 27 by tony, Tue Apr 14 13:10:23 2015 UTC

# Line 1 | Line 1
1 < {************************************************************************}
2 < {                                                                        }
3 < {       Borland Delphi Visual Component Library                          }
4 < {       InterBase Express core components                                }
5 < {                                                                        }
6 < {       Copyright (c) 1998-2000 Inprise Corporation                      }
7 < {                                                                        }
8 < {    InterBase Express is based in part on the product                   }
9 < {    Free IB Components, written by Gregory H. Deatz for                 }
10 < {    Hoagland, Longo, Moran, Dunst & Doukas Company.                     }
11 < {    Free IB Components is used under license.                           }
12 < {                                                                        }
13 < {    The contents of this file are subject to the InterBase              }
14 < {    Public License Version 1.0 (the "License"); you may not             }
15 < {    use this file except in compliance with the License. You            }
16 < {    may obtain a copy of the License at http://www.Inprise.com/IPL.html }
17 < {    Software distributed under the License is distributed on            }
18 < {    an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either              }
19 < {    express or implied. See the License for the specific language       }
20 < {    governing rights and limitations under the License.                 }
21 < {    The Original Code was created by InterBase Software Corporation     }
22 < {       and its successors.                                              }
23 < {    Portions created by Inprise Corporation are Copyright (C) Inprise   }
24 < {       Corporation. All Rights Reserved.                                }
25 < {    Contributor(s): Jeff Overcash                                       }
26 < {                                                                        }
27 < {************************************************************************}
28 <
29 < unit IBStoredProc;
30 <
31 < {$Mode Delphi}
32 <
33 < interface
34 <
35 < uses SysUtils, Classes, DB, IB, IBDatabase, IBCustomDataSet,
36 <     IBHeader, IBSQL, IBUtils;
37 <    
38 < { TIBStoredProc }
39 < type
40 <
41 <  TIBStoredProc = class(TIBCustomDataSet)
42 <  private
43 <    FIBLoaded: Boolean;
44 <    FStmtHandle: TISC_STMT_HANDLE;
45 <    FProcName: string;
46 <    FParams: TParams;
47 <    FPrepared: Boolean;
48 <    FNameList: TStrings;
49 <    procedure SetParamsList(Value: TParams);
50 <    procedure FreeStatement;
51 <    function GetStoredProcedureNames: TStrings;
52 <    procedure GetStoredProcedureNamesFromServer;
53 <    procedure CreateParamDesc;
54 <    procedure SetParams;
55 <    procedure SetParamsFromCursor;
56 <    procedure GenerateSQL;
57 <    procedure FetchDataIntoOutputParams;
58 <    procedure ReadParamData(Reader: TReader);
59 <    procedure WriteParamData(Writer: TWriter);
60 <
61 <  protected
62 <
63 <    procedure DefineProperties(Filer: TFiler); override;
64 <    procedure SetFiltered(Value: Boolean); override;
65 <    function GetParamsCount: Word;
66 <    procedure SetPrepared(Value: Boolean);
67 <    procedure SetPrepare(Value: Boolean);
68 <    procedure SetProcName(Value: string);
69 <    procedure Disconnect; override;
70 <    procedure InternalOpen; override;
71 <
72 <  public
73 <    constructor Create(AOwner: TComponent); override;
74 <    destructor Destroy; override;
75 <    procedure CopyParams(Value: TParams);
76 <    procedure ExecProc;
77 <    function ParamByName(const Value: string): TParam;
78 <    procedure Prepare;
79 <    procedure UnPrepare;
80 <    property ParamCount: Word read GetParamsCount;
81 <    property StmtHandle: TISC_STMT_HANDLE read FStmtHandle;
82 <    property Prepared: Boolean read FPrepared write SetPrepare;
83 <    property StoredProcedureNames: TStrings read GetStoredProcedureNames;
84 <
85 <  published
86 <    property StoredProcName: string read FProcName write SetProcName;
87 <    property Params: TParams read FParams write SetParamsList;
88 <    property Filtered;
89 <
90 <    property BeforeDatabaseDisconnect;
91 <    property AfterDatabaseDisconnect;
92 <    property DatabaseFree;
93 <    property BeforeTransactionEnd;
94 <    property AfterTransactionEnd;
95 <    property TransactionFree;
96 <    property OnFilterRecord;
97 <  end;
98 <
99 < implementation
100 <
101 < uses
102 <   IBIntf;
103 <
104 < { TIBStoredProc }
105 <
106 < constructor TIBStoredProc.Create(AOwner: TComponent);
107 < begin
108 <  inherited Create(AOwner);
109 <  FIBLoaded := False;
110 <  CheckIBLoaded;
111 <  FIBLoaded := True;
112 <  FParams := TParams.Create (self);
113 <  FNameList := TStringList.Create;
114 < end;
115 <
116 < destructor TIBStoredProc.Destroy;
117 < begin
118 <  if FIBLoaded then
119 <  begin
120 <    Destroying;
121 <    Disconnect;
122 <    FParams.Free;
123 <    FNameList.Destroy;
124 <  end;
125 <  inherited Destroy;
126 < end;
127 <
128 < procedure TIBStoredProc.Disconnect;
129 < begin
130 <  Close;
131 <  UnPrepare;
132 < end;
133 <
134 < procedure TIBStoredProc.ExecProc;
135 < var
136 <  DidActivate: Boolean;
137 < begin
138 <  CheckInActive;
139 <  if StoredProcName = '' then
140 <    IBError(ibxeNoStoredProcName, [nil]);
141 <  ActivateConnection;
142 <  DidActivate := ActivateTransaction;
143 <  try
144 <    SetPrepared(True);
145 <    if DataSource <> nil then SetParamsFromCursor;
146 <    if FParams.Count > 0 then SetParams;
147 <    InternalExecQuery;
148 <    FetchDataIntoOutputParams;
149 <  finally
150 <    if DidActivate then
151 <      DeactivateTransaction;
152 <  end;
153 < end;
154 <
155 < procedure TIBStoredProc.SetProcName(Value: string);
156 < begin
157 <  if not (csReading in ComponentState) then
158 <  begin
159 <    CheckInactive;
160 <    if Value <> FProcName then
161 <    begin
162 <      FProcName := Value;
163 <      FreeStatement;
164 <      FParams.Clear;
165 <      if (Value <> '') and
166 <        (Database <> nil) then
167 <        GenerateSQL;
168 <    end;
169 <  end else begin
170 <    FProcName := Value;
171 <  if (Value <> '') and
172 <    (Database <> nil) then
173 <    GenerateSQL;
174 <  end;
175 < end;
176 <
177 < function TIBStoredProc.GetParamsCount: Word;
178 < begin
179 <  Result := FParams.Count;
180 < end;
181 <
182 < procedure TIBStoredProc.SetFiltered(Value: Boolean);
183 < begin
184 <  if(Filtered <> Value) then
185 <  begin
186 <    inherited SetFiltered(value);
187 <    if Active then
188 <    begin
189 <      Close;
190 <      Open;
191 <    end;
192 <  end
193 <  else
194 <    inherited SetFiltered(value);
195 < end;
196 <
197 < procedure TIBStoredProc.GenerateSQL;
198 < var
199 <  Query : TIBSQL;
200 <  input : string;
201 < begin
202 <  ActivateConnection;
203 <  Database.InternalTransaction.StartTransaction;
204 <  Query := TIBSQL.Create(self);
205 <  try
206 <    Query.Database := DataBase;
207 <    Query.Transaction := Database.InternalTransaction;
208 <    Query.SQL.Text := 'SELECT RDB$PARAMETER_NAME,  RDB$PARAMETER_TYPE ' + {do not localize}
209 <                       'FROM RDB$PROCEDURE_PARAMETERS ' + {do not localize}
210 <                       'WHERE RDB$PROCEDURE_NAME = ' + {do not localize}
211 <                       '''' + FormatIdentifierValue(Database.SQLDialect, FProcName) + '''' +
212 <                       ' ORDER BY RDB$PARAMETER_NUMBER'; {do not localize}
213 <    Query.Prepare;
214 <    Query.GoToFirstRecordOnExecute := False;
215 <    Query.ExecQuery;
216 <    while (not Query.EOF) and (Query.Next <> nil) do begin
217 <      if (Query.Current.ByName('RDB$PARAMETER_TYPE').AsInteger = 0) then begin {do not localize}
218 <        if (input <> '') then
219 <          input := input + ', :' +
220 <            FormatIdentifier(Database.SQLDialect, Query.Current.ByName('RDB$PARAMETER_NAME').AsString) else {do not localize}
221 <          input := ':' +
222 <            FormatIdentifier(Database.SQLDialect, Query.Current.ByName('RDB$PARAMETER_NAME').AsString); {do not localize}
223 <      end
224 <    end;
225 <    SelectSQL.Text := 'Execute Procedure ' + {do not localize}
226 <                FormatIdentifier(Database.SQLDialect, FProcName) + ' ' + input;
227 <  finally
228 <    Query.Free;
229 <    Database.InternalTransaction.Commit;
230 <  end;
231 < end;
232 <
233 < procedure TIBStoredProc.CreateParamDesc;
234 < var
235 <  i : integer;
236 <  DataType : TFieldType;
237 < begin
238 <  DataType := ftUnknown;
239 <  for i := 0 to QSelect.Current.Count - 1 do begin
240 <  case QSelect.Fields[i].SQLtype of
241 <    SQL_TYPE_DATE: DataType := ftDate;
242 <    SQL_TYPE_TIME: DataType := ftTime;
243 <    SQL_TIMESTAMP: DataType := ftDateTime;
244 <    SQL_SHORT:
245 <      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
246 <        DataType := ftSmallInt
247 <      else
248 <        DataType := ftBCD;
249 <    SQL_LONG:
250 <      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
251 <        DataType := ftInteger
252 <      else if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale >= (-4)) then
253 <        DataType := ftBCD
254 <      else
255 <        DataType := ftFloat;
256 <    SQL_INT64:
257 <      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
258 <        DataType := ftLargeInt
259 <      else if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale >= (-4)) then
260 <        DataType := ftBCD
261 <      else
262 <        DataType := ftFloat;
263 <    SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT: DataType := ftFloat;
264 <    SQL_TEXT: DataType := ftString;
265 <    SQL_VARYING:
266 <      if ((QSelect.Fields[i].AsXSQLVar)^.sqllen < 1024) then
267 <        DataType := ftString
268 <      else DataType := ftBlob;
269 <    SQL_BLOB, SQL_ARRAY, SQL_QUAD: DataType := ftBlob;
270 <    end;
271 <    FParams.CreateParam(DataType, Trim(QSelect.Fields[i].Name), ptOutput);
272 <  end;
273 <
274 <  DataType := ftUnknown;
275 <  for i := 0 to QSelect.Params.Count - 1 do begin
276 <  case QSelect.Params[i].SQLtype of
277 <    SQL_TYPE_DATE: DataType := ftDate;
278 <    SQL_TYPE_TIME: DataType := ftTime;
279 <    SQL_TIMESTAMP: DataType := ftDateTime;
280 <    SQL_SHORT:
281 <      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
282 <        DataType := ftSmallInt
283 <      else
284 <        DataType := ftBCD;
285 <    SQL_LONG:
286 <      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
287 <        DataType := ftInteger
288 <      else if ((QSelect.Params[i].AsXSQLVar)^.sqlscale >= (-4)) then
289 <        DataType := ftBCD
290 <      else DataType := ftFloat;
291 <    SQL_INT64:
292 <      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
293 <        DataType := ftLargeInt
294 <      else if ((QSelect.Params[i].AsXSQLVar)^.sqlscale >= (-4)) then
295 <        DataType := ftBCD
296 <      else DataType := ftFloat;
297 <    SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT: DataType := ftFloat;
298 <    SQL_TEXT: DataType := ftString;
299 <    SQL_VARYING:
300 <      if ((QSelect.Params[i].AsXSQLVar)^.sqllen < 1024) then
301 <        DataType := ftString
302 <      else DataType := ftBlob;
303 <    SQL_BLOB, SQL_ARRAY, SQL_QUAD: DataType := ftBlob;
304 <    end;
305 <    FParams.CreateParam(DataType, Trim(QSelect.Params[i].Name), ptInput);
306 <  end;
307 < end;
308 <
309 < procedure TIBStoredProc.SetPrepared(Value: Boolean);
310 < begin
311 <  if Prepared <> Value then
312 <  begin
313 <    if Value then
314 <      try
315 <        if SelectSQL.Text = '' then GenerateSQL;
316 <        InternalPrepare;
317 <        if FParams.Count = 0 then CreateParamDesc;
318 <        FPrepared := True;
319 <      except
320 <        FreeStatement;
321 <        raise;
322 <      end
323 <    else FreeStatement;
324 <  end;
325 <
326 < end;
327 <
328 < procedure TIBStoredProc.Prepare;
329 < begin
330 <  SetPrepared(True);
331 < end;
332 <
333 < procedure TIBStoredProc.UnPrepare;
334 < begin
335 <  SetPrepared(False);
336 < end;
337 <
338 < procedure TIBStoredProc.FreeStatement;
339 < begin
340 <  InternalUnPrepare;
341 <  FPrepared := False;
342 < end;
343 <
344 < procedure TIBStoredProc.SetPrepare(Value: Boolean);
345 < begin
346 <  if Value then Prepare
347 <  else UnPrepare;
348 < end;
349 <
350 < procedure TIBStoredProc.CopyParams(Value: TParams);
351 < begin
352 <  if not Prepared and (FParams.Count = 0) then
353 <  try
354 <    Prepare;
355 <    Value.Assign(FParams);
356 <  finally
357 <    UnPrepare;
358 <  end else
359 <    Value.Assign(FParams);
360 < end;
361 <
362 < procedure TIBStoredProc.SetParamsList(Value: TParams);
363 < begin
364 <  CheckInactive;
365 <  if Prepared then
366 <  begin
367 <    SetPrepared(False);
368 <    FParams.Assign(Value);
369 <    SetPrepared(True);
370 <  end else
371 <    FParams.Assign(Value);
372 < end;
373 <
374 < function TIBStoredProc.ParamByName(const Value: string): TParam;
375 < begin
376 <  Result := FParams.ParamByName(Value);
377 < end;
378 <
379 < function TIBStoredProc.GetStoredProcedureNames: TStrings;
380 < begin
381 <  FNameList.clear;
382 <  GetStoredProcedureNamesFromServer;
383 <  Result := FNameList;
384 < end;
385 <
386 < procedure TIBStoredProc.GetStoredProcedureNamesFromServer;
387 < var
388 <  Query : TIBSQL;
389 < begin
390 <  if not (csReading in ComponentState) then begin
391 <    ActivateConnection;
392 <    Database.InternalTransaction.StartTransaction;
393 <    Query := TIBSQL.Create(self);
394 <    try
395 <      Query.GoToFirstRecordOnExecute := False;
396 <      Query.Database := DataBase;
397 <      Query.Transaction := Database.InternalTransaction;
398 <      Query.SQL.Text := 'Select RDB$PROCEDURE_NAME from RDB$PROCEDURES'; {do not localize}
399 <      Query.Prepare;
400 <      Query.ExecQuery;
401 <      while (not Query.EOF) and (Query.Next <> nil) do
402 <        FNameList.Add(TrimRight(Query.Current.ByName('RDB$PROCEDURE_NAME').AsString)); {do not localize}
403 <    finally
404 <      Query.Free;
405 <      Database.InternalTransaction.Commit;
406 <    end;
407 <  end;
408 < end;
409 <
410 < procedure TIBStoredProc.SetParams;
411 < var
412 < i : integer;
413 < j: integer;
414 < begin
415 <  i := 0;
416 <  for j := 0 to FParams.Count - 1 do
417 <  begin
418 <    if (Params[j].ParamType <> ptInput) then
419 <      continue;
420 <    if not Params[j].Bound then
421 <      IBError(ibxeRequiredParamNotSet, [nil]);
422 <    if Params[j].IsNull then
423 <      SQLParams[i].IsNull := True
424 <    else begin
425 <      SQLParams[i].IsNull := False;
426 <      case Params[j].DataType of
427 <        ftString:
428 <          SQLParams[i].AsString := Params[j].AsString;
429 <        ftBoolean, ftSmallint, ftWord:
430 <          SQLParams[i].AsShort := Params[j].AsSmallInt;
431 <        ftInteger:
432 <          SQLParams[i].AsLong := Params[j].AsInteger;
433 < {        ftLargeInt:
434 <          SQLParams[i].AsInt64 := Params[j].AsLargeInt; }
435 <        ftFloat, ftCurrency:
436 <         SQLParams[i].AsDouble := Params[j].AsFloat;
437 <        ftBCD:
438 <          SQLParams[i].AsCurrency := Params[j].AsCurrency;
439 <        ftDate:
440 <          SQLParams[i].AsDate := Params[j].AsDateTime;
441 <        ftTime:
442 <          SQLParams[i].AsTime := Params[j].AsDateTime;
443 <        ftDateTime:
444 <          SQLParams[i].AsDateTime := Params[j].AsDateTime;
445 <        ftBlob, ftMemo:
446 <          SQLParams[i].AsString := Params[j].AsString;
447 <        else
448 <          IBError(ibxeNotSupported, [nil]);
449 <      end;
450 <    end;
451 <    Inc(i);
452 <  end;
453 < end;
454 <
455 < procedure TIBStoredProc.SetParamsFromCursor;
456 < var
457 <  I: Integer;
458 <  DataSet: TDataSet;
459 < begin
460 <  if DataSource <> nil then
461 <  begin
462 <    DataSet := DataSource.DataSet;
463 <    if DataSet <> nil then
464 <    begin
465 <      DataSet.FieldDefs.Update;
466 <      for I := 0 to FParams.Count - 1 do
467 <        with FParams[I] do
468 <          if (not Bound) and
469 <            ((ParamType = ptInput) or (ParamType =  ptInputOutput)) then
470 <            AssignField(DataSet.FieldByName(Name));
471 <    end;
472 <  end;
473 < end;
474 <
475 < procedure TIBStoredProc.FetchDataIntoOutputParams;
476 < var
477 < i,j : Integer;
478 < begin
479 <  j := 0;
480 <  for i := 0 to FParams.Count - 1 do
481 <    with Params[I] do
482 <      if ParamType = ptOutput then begin
483 <         Value := QSelect.Fields[j].Value;
484 <         Inc(j);
485 <      end;
486 < end;
487 <
488 < procedure TIBStoredProc.InternalOpen;
489 < begin
490 <  IBError(ibxeIsAExecuteProcedure,[nil]);
491 < end;
492 <
493 < procedure TIBStoredProc.DefineProperties(Filer: TFiler);
494 <
495 <  function WriteData: Boolean;
496 <  begin
497 <    if Filer.Ancestor <> nil then
498 <      Result := not FParams.IsEqual(TIBStoredProc(Filer.Ancestor).FParams) else
499 <      Result := FParams.Count > 0;
500 <  end;
501 <
502 < begin
503 <  inherited DefineProperties(Filer);
504 <  Filer.DefineProperty('ParamData', ReadParamData, WriteParamData, WriteData); {do not localize}
505 < end;
506 <
507 < procedure TIBStoredProc.WriteParamData(Writer: TWriter);
508 < begin
509 <  Writer.WriteCollection(Params);
510 < end;
511 <
512 < procedure TIBStoredProc.ReadParamData(Reader: TReader);
513 < begin
514 <  Reader.ReadValue;
515 <  Reader.ReadCollection(Params);
516 < end;
517 <
518 < end.
1 > {************************************************************************}
2 > {                                                                        }
3 > {       Borland Delphi Visual Component Library                          }
4 > {       InterBase Express core components                                }
5 > {                                                                        }
6 > {       Copyright (c) 1998-2000 Inprise Corporation                      }
7 > {                                                                        }
8 > {    InterBase Express is based in part on the product                   }
9 > {    Free IB Components, written by Gregory H. Deatz for                 }
10 > {    Hoagland, Longo, Moran, Dunst & Doukas Company.                     }
11 > {    Free IB Components is used under license.                           }
12 > {                                                                        }
13 > {    The contents of this file are subject to the InterBase              }
14 > {    Public License Version 1.0 (the "License"); you may not             }
15 > {    use this file except in compliance with the License. You            }
16 > {    may obtain a copy of the License at http://www.Inprise.com/IPL.html }
17 > {    Software distributed under the License is distributed on            }
18 > {    an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either              }
19 > {    express or implied. See the License for the specific language       }
20 > {    governing rights and limitations under the License.                 }
21 > {    The Original Code was created by InterBase Software Corporation     }
22 > {       and its successors.                                              }
23 > {    Portions created by Inprise Corporation are Copyright (C) Inprise   }
24 > {       Corporation. All Rights Reserved.                                }
25 > {    Contributor(s): Jeff Overcash                                       }
26 > {                                                                        }
27 > {    IBX For Lazarus (Firebird Express)                                  }
28 > {    Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
29 > {    Portions created by MWA Software are copyright McCallum Whyman      }
30 > {    Associates Ltd 2011                                                 }
31 > {                                                                        }
32 > {************************************************************************}
33 >
34 > unit IBStoredProc;
35 >
36 > {$Mode Delphi}
37 >
38 > interface
39 >
40 > uses SysUtils, Classes, DB, IB, IBDatabase, IBCustomDataSet,
41 >     IBHeader, IBSQL, IBUtils;
42 >    
43 > { TIBStoredProc }
44 > type
45 >
46 >  TIBStoredProc = class(TIBCustomDataSet)
47 >  private
48 >    FIBLoaded: Boolean;
49 >    FStmtHandle: TISC_STMT_HANDLE;
50 >    FProcName: string;
51 >    FParams: TParams;
52 >    FPrepared: Boolean;
53 >    FNameList: TStrings;
54 >    procedure SetParamsList(Value: TParams);
55 >    procedure FreeStatement;
56 >    function GetStoredProcedureNames: TStrings;
57 >    procedure GetStoredProcedureNamesFromServer;
58 >    procedure CreateParamDesc;
59 >    procedure SetParams;
60 >    procedure SetParamsFromCursor;
61 >    procedure GenerateSQL;
62 >    procedure FetchDataIntoOutputParams;
63 >    procedure ReadParamData(Reader: TReader);
64 >    procedure WriteParamData(Writer: TWriter);
65 >
66 >  protected
67 >
68 >    procedure DefineProperties(Filer: TFiler); override;
69 >    procedure SetFiltered(Value: Boolean); override;
70 >    procedure InitFieldDefs; override;
71 >    function GetParamsCount: Word;
72 >    procedure SetPrepared(Value: Boolean);
73 >    procedure SetPrepare(Value: Boolean);
74 >    procedure SetProcName(Value: string);
75 >    procedure Disconnect; override;
76 >    procedure InternalOpen; override;
77 >
78 >  public
79 >    constructor Create(AOwner: TComponent); override;
80 >    destructor Destroy; override;
81 >    procedure CopyParams(Value: TParams);
82 >    procedure ExecProc;
83 >    function ParamByName(const Value: string): TParam;
84 >    procedure Prepare;
85 >    procedure UnPrepare;
86 >    property ParamCount: Word read GetParamsCount;
87 >    property StmtHandle: TISC_STMT_HANDLE read FStmtHandle;
88 >    property Prepared: Boolean read FPrepared write SetPrepare;
89 >    property StoredProcedureNames: TStrings read GetStoredProcedureNames;
90 >
91 >  published
92 >    property StoredProcName: string read FProcName write SetProcName;
93 >    property Params: TParams read FParams write SetParamsList;
94 >    property Filtered;
95 >
96 >    property BeforeDatabaseDisconnect;
97 >    property AfterDatabaseDisconnect;
98 >    property DatabaseFree;
99 >    property BeforeTransactionEnd;
100 >    property AfterTransactionEnd;
101 >    property TransactionFree;
102 >    property OnFilterRecord;
103 >  end;
104 >
105 > implementation
106 >
107 > uses
108 >   IBIntf;
109 >
110 > { TIBStoredProc }
111 >
112 > constructor TIBStoredProc.Create(AOwner: TComponent);
113 > begin
114 >  inherited Create(AOwner);
115 >  FIBLoaded := False;
116 >  CheckIBLoaded;
117 >  FIBLoaded := True;
118 >  FParams := TParams.Create (self);
119 >  FNameList := TStringList.Create;
120 > end;
121 >
122 > destructor TIBStoredProc.Destroy;
123 > begin
124 >  if FIBLoaded then
125 >  begin
126 >    Destroying;
127 >    Disconnect;
128 >    FParams.Free;
129 >    FNameList.Destroy;
130 >  end;
131 >  inherited Destroy;
132 > end;
133 >
134 > procedure TIBStoredProc.Disconnect;
135 > begin
136 >  Close;
137 >  UnPrepare;
138 > end;
139 >
140 > procedure TIBStoredProc.ExecProc;
141 > var
142 >  DidActivate: Boolean;
143 > begin
144 >  CheckInActive;
145 >  if StoredProcName = '' then
146 >    IBError(ibxeNoStoredProcName, [nil]);
147 >  ActivateConnection;
148 >  DidActivate := ActivateTransaction;
149 >  try
150 >    SetPrepared(True);
151 >    if DataSource <> nil then SetParamsFromCursor;
152 >    if FParams.Count > 0 then SetParams;
153 >    InternalExecQuery;
154 >    FetchDataIntoOutputParams;
155 >  finally
156 >    if DidActivate then
157 >      DeactivateTransaction;
158 >  end;
159 > end;
160 >
161 > procedure TIBStoredProc.SetProcName(Value: string);
162 > begin
163 >  if not (csReading in ComponentState) then
164 >  begin
165 >    CheckInactive;
166 >    if Value <> FProcName then
167 >    begin
168 >      FProcName := Value;
169 >      FreeStatement;
170 >      FParams.Clear;
171 >      if (Value <> '') and
172 >        (Database <> nil) then
173 >        GenerateSQL;
174 >    end;
175 >  end else begin
176 >    FProcName := Value;
177 >  if (Value <> '') and
178 >    (Database <> nil) then
179 >    GenerateSQL;
180 >  end;
181 > end;
182 >
183 > function TIBStoredProc.GetParamsCount: Word;
184 > begin
185 >  Result := FParams.Count;
186 > end;
187 >
188 > procedure TIBStoredProc.SetFiltered(Value: Boolean);
189 > begin
190 >  if(Filtered <> Value) then
191 >  begin
192 >    inherited SetFiltered(value);
193 >    if Active then
194 >    begin
195 >      Close;
196 >      Open;
197 >    end;
198 >  end
199 >  else
200 >    inherited SetFiltered(value);
201 > end;
202 >
203 > procedure TIBStoredProc.InitFieldDefs;
204 > begin
205 >  if SelectSQL.Text = '' then
206 >     GenerateSQL;
207 >  inherited InitFieldDefs;
208 > end;
209 >
210 > procedure TIBStoredProc.GenerateSQL;
211 >
212 > var Params: TStringList;
213 >
214 >  function FormatParameter(Dialect: Integer; Value: String): String;
215 >  var j: integer;
216 >  begin
217 >    Value := Trim(Value);
218 >    if Dialect = 1 then
219 >       Result := AnsiUpperCase(Value)
220 >    else
221 >    begin
222 >      j := 1;
223 >      Value := Space2Underscore(AnsiUpperCase(Value));
224 >      Result := Value;
225 >      while Params.IndexOf(Result) <> -1 do
226 >      begin
227 >        Result := Value + IntToStr(j);
228 >        Inc(j)
229 >      end;
230 >      Params.Add(Result)
231 >    end;
232 >  end;
233 >
234 > var
235 >  Query : TIBSQL;
236 >  input : string;
237 > begin
238 >  input := '';
239 >  if FProcName = '' then
240 >     IBError(ibxeNoStoredProcName,[nil]);
241 >  ActivateConnection;
242 >  Database.InternalTransaction.StartTransaction;
243 >  Params := TStringList.Create;
244 >  Query := TIBSQL.Create(self);
245 >  try
246 >    Query.Database := DataBase;
247 >    Query.Transaction := Database.InternalTransaction;
248 >    Query.SQL.Text := 'SELECT RDB$PARAMETER_NAME,  RDB$PARAMETER_TYPE ' + {do not localize}
249 >                       'FROM RDB$PROCEDURE_PARAMETERS ' + {do not localize}
250 >                       'WHERE RDB$PROCEDURE_NAME = ' + {do not localize}
251 >                       '''' + FormatIdentifierValue(Database.SQLDialect, FProcName) + '''' +
252 >                       ' ORDER BY RDB$PARAMETER_NUMBER'; {do not localize}
253 >    Query.Prepare;
254 >    Query.GoToFirstRecordOnExecute := False;
255 >    Query.ExecQuery;
256 >    while (not Query.EOF) and (Query.Next <> nil) do begin
257 >      if (Query.Current.ByName('RDB$PARAMETER_TYPE').AsInteger = 0) then begin {do not localize}
258 >        if (input <> '') then
259 >          input := input + ', :' +
260 >            FormatParameter(Database.SQLDialect, Query.Current.ByName('RDB$PARAMETER_NAME').AsString) else {do not localize}
261 >          input := ':' +
262 >            FormatParameter(Database.SQLDialect, Query.Current.ByName('RDB$PARAMETER_NAME').AsString); {do not localize}
263 >      end
264 >    end;
265 >    SelectSQL.Text := 'Execute Procedure ' + {do not localize}
266 >                FormatParameter(Database.SQLDialect, FProcName) + ' ' + input;
267 > {   writeln(SelectSQL.Text);}
268 >  finally
269 >    Query.Free;
270 >    Params.Free;
271 >    Database.InternalTransaction.Commit;
272 >  end;
273 > end;
274 >
275 > procedure TIBStoredProc.CreateParamDesc;
276 > var
277 >  i : integer;
278 >  DataType : TFieldType;
279 > begin
280 >  DataType := ftUnknown;
281 >  for i := 0 to QSelect.Current.Count - 1 do begin
282 >  case QSelect.Fields[i].SQLtype of
283 >    SQL_TYPE_DATE: DataType := ftDate;
284 >    SQL_TYPE_TIME: DataType := ftTime;
285 >    SQL_TIMESTAMP: DataType := ftDateTime;
286 >    SQL_SHORT:
287 >      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
288 >        DataType := ftSmallInt
289 >      else
290 >        DataType := ftBCD;
291 >    SQL_LONG:
292 >      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
293 >        DataType := ftInteger
294 >      else if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale >= (-4)) then
295 >        DataType := ftBCD
296 >      else
297 >        DataType := ftFloat;
298 >    SQL_INT64:
299 >      if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale = 0) then
300 >        DataType := ftLargeInt
301 >      else if ((QSelect.Fields[i].AsXSQLVar)^.sqlscale >= (-4)) then
302 >        DataType := ftBCD
303 >      else
304 >        DataType := ftFloat;
305 >    SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT: DataType := ftFloat;
306 >    SQL_BOOLEAN:
307 >      DataType := ftBoolean;
308 >    SQL_TEXT: DataType := ftString;
309 >    SQL_VARYING:
310 >      if ((QSelect.Fields[i].AsXSQLVar)^.sqllen < 1024) then
311 >        DataType := ftString
312 >      else DataType := ftBlob;
313 >    SQL_BLOB, SQL_ARRAY, SQL_QUAD: DataType := ftBlob;
314 >    end;
315 >    FParams.CreateParam(DataType, Trim(QSelect.Fields[i].Name), ptOutput);
316 >  end;
317 >
318 >  DataType := ftUnknown;
319 >  for i := 0 to QSelect.Params.Count - 1 do begin
320 >  case QSelect.Params[i].SQLtype of
321 >    SQL_TYPE_DATE: DataType := ftDate;
322 >    SQL_TYPE_TIME: DataType := ftTime;
323 >    SQL_TIMESTAMP: DataType := ftDateTime;
324 >    SQL_SHORT:
325 >      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
326 >        DataType := ftSmallInt
327 >      else
328 >        DataType := ftBCD;
329 >    SQL_LONG:
330 >      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
331 >        DataType := ftInteger
332 >      else if ((QSelect.Params[i].AsXSQLVar)^.sqlscale >= (-4)) then
333 >        DataType := ftBCD
334 >      else DataType := ftFloat;
335 >    SQL_INT64:
336 >      if ((QSelect.Params[i].AsXSQLVar)^.sqlscale = 0) then
337 >        DataType := ftLargeInt
338 >      else if ((QSelect.Params[i].AsXSQLVar)^.sqlscale >= (-4)) then
339 >        DataType := ftBCD
340 >      else DataType := ftFloat;
341 >    SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT: DataType := ftFloat;
342 >    SQL_BOOLEAN:
343 >      DataType := ftBoolean;
344 >    SQL_TEXT: DataType := ftString;
345 >    SQL_VARYING:
346 >      if ((QSelect.Params[i].AsXSQLVar)^.sqllen < 1024) then
347 >        DataType := ftString
348 >      else DataType := ftBlob;
349 >    SQL_BLOB, SQL_ARRAY, SQL_QUAD: DataType := ftBlob;
350 >    end;
351 >    FParams.CreateParam(DataType, Trim(QSelect.Params[i].Name), ptInput);
352 >  end;
353 > end;
354 >
355 > procedure TIBStoredProc.SetPrepared(Value: Boolean);
356 > begin
357 >  if Prepared <> Value then
358 >  begin
359 >    if Value then
360 >      try
361 >        if SelectSQL.Text = '' then GenerateSQL;
362 >        InternalPrepare;
363 >        if FParams.Count = 0 then CreateParamDesc;
364 >        FPrepared := True;
365 >      except
366 >        FreeStatement;
367 >        raise;
368 >      end
369 >    else FreeStatement;
370 >  end;
371 >
372 > end;
373 >
374 > procedure TIBStoredProc.Prepare;
375 > begin
376 >  SetPrepared(True);
377 > end;
378 >
379 > procedure TIBStoredProc.UnPrepare;
380 > begin
381 >  SetPrepared(False);
382 > end;
383 >
384 > procedure TIBStoredProc.FreeStatement;
385 > begin
386 >  InternalUnPrepare;
387 >  FPrepared := False;
388 > end;
389 >
390 > procedure TIBStoredProc.SetPrepare(Value: Boolean);
391 > begin
392 >  if Value then Prepare
393 >  else UnPrepare;
394 > end;
395 >
396 > procedure TIBStoredProc.CopyParams(Value: TParams);
397 > begin
398 >  if not Prepared and (FParams.Count = 0) then
399 >  try
400 >    Prepare;
401 >    Value.Assign(FParams);
402 >  finally
403 >    UnPrepare;
404 >  end else
405 >    Value.Assign(FParams);
406 > end;
407 >
408 > procedure TIBStoredProc.SetParamsList(Value: TParams);
409 > begin
410 >  CheckInactive;
411 >  if Prepared then
412 >  begin
413 >    SetPrepared(False);
414 >    FParams.Assign(Value);
415 >    SetPrepared(True);
416 >  end else
417 >    FParams.Assign(Value);
418 > end;
419 >
420 > function TIBStoredProc.ParamByName(const Value: string): TParam;
421 > begin
422 >  Prepare;
423 >  Result := FParams.ParamByName(Value);
424 > end;
425 >
426 > function TIBStoredProc.GetStoredProcedureNames: TStrings;
427 > begin
428 >  FNameList.clear;
429 >  GetStoredProcedureNamesFromServer;
430 >  Result := FNameList;
431 > end;
432 >
433 > procedure TIBStoredProc.GetStoredProcedureNamesFromServer;
434 > var
435 >  Query : TIBSQL;
436 > begin
437 >  if not (csReading in ComponentState) then begin
438 >    ActivateConnection;
439 >    Database.InternalTransaction.StartTransaction;
440 >    Query := TIBSQL.Create(self);
441 >    try
442 >      Query.GoToFirstRecordOnExecute := False;
443 >      Query.Database := DataBase;
444 >      Query.Transaction := Database.InternalTransaction;
445 >      Query.SQL.Text := 'Select RDB$PROCEDURE_NAME from RDB$PROCEDURES'; {do not localize}
446 >      Query.Prepare;
447 >      Query.ExecQuery;
448 >      while (not Query.EOF) and (Query.Next <> nil) do
449 >        FNameList.Add(TrimRight(Query.Current.ByName('RDB$PROCEDURE_NAME').AsString)); {do not localize}
450 >    finally
451 >      Query.Free;
452 >      Database.InternalTransaction.Commit;
453 >    end;
454 >  end;
455 > end;
456 >
457 > procedure TIBStoredProc.SetParams;
458 > var
459 > i : integer;
460 > j: integer;
461 > begin
462 >  i := 0;
463 >  for j := 0 to FParams.Count - 1 do
464 >  begin
465 >    if (Params[j].ParamType <> ptInput) then
466 >      continue;
467 >    if not Params[j].Bound then
468 >      IBError(ibxeRequiredParamNotSet, [Params[j].Name]);
469 >    if Params[j].IsNull then
470 >      SQLParams[i].IsNull := True
471 >    else begin
472 >      SQLParams[i].IsNull := False;
473 >      case Params[j].DataType of
474 >        ftString:
475 >          SQLParams[i].AsString := Params[j].AsString;
476 >        ftSmallint, ftWord:
477 >          SQLParams[i].AsShort := Params[j].AsSmallInt;
478 >        ftBoolean:
479 >           SQLParams[i].AsBoolean := Params[j].AsBoolean;
480 >        ftInteger:
481 >          SQLParams[i].AsLong := Params[j].AsInteger;
482 >        ftLargeInt:
483 >          SQLParams[i].AsInt64 := Params[j].AsLargeInt;
484 >        ftFloat, ftCurrency:
485 >         SQLParams[i].AsDouble := Params[j].AsFloat;
486 >        ftBCD:
487 >          SQLParams[i].AsCurrency := Params[j].AsCurrency;
488 >        ftDate:
489 >          SQLParams[i].AsDate := Params[j].AsDateTime;
490 >        ftTime:
491 >          SQLParams[i].AsTime := Params[j].AsDateTime;
492 >        ftDateTime:
493 >          SQLParams[i].AsDateTime := Params[j].AsDateTime;
494 >        ftBlob, ftMemo:
495 >          SQLParams[i].AsString := Params[j].AsString;
496 >        else
497 >          IBError(ibxeNotSupported, [nil]);
498 >      end;
499 >    end;
500 >    Inc(i);
501 >  end;
502 > end;
503 >
504 > procedure TIBStoredProc.SetParamsFromCursor;
505 > var
506 >  I: Integer;
507 >  DataSet: TDataSet;
508 > begin
509 >  if DataSource <> nil then
510 >  begin
511 >    DataSet := DataSource.DataSet;
512 >    if DataSet <> nil then
513 >    begin
514 >      DataSet.FieldDefs.Update;
515 >      for I := 0 to FParams.Count - 1 do
516 >        with FParams[I] do
517 >          if (not Bound) and
518 >            ((ParamType = ptInput) or (ParamType =  ptInputOutput)) then
519 >            AssignField(DataSet.FieldByName(Name));
520 >    end;
521 >  end;
522 > end;
523 >
524 > procedure TIBStoredProc.FetchDataIntoOutputParams;
525 > var
526 > i,j : Integer;
527 > begin
528 >  j := 0;
529 >  for i := 0 to FParams.Count - 1 do
530 >    with Params[I] do
531 >      if ParamType = ptOutput then begin
532 >         Value := QSelect.Fields[j].Value;
533 >         Inc(j);
534 >      end;
535 > end;
536 >
537 > procedure TIBStoredProc.InternalOpen;
538 > begin
539 >  IBError(ibxeIsAExecuteProcedure,[nil]);
540 > end;
541 >
542 > procedure TIBStoredProc.DefineProperties(Filer: TFiler);
543 >
544 >  function WriteData: Boolean;
545 >  begin
546 >    if Filer.Ancestor <> nil then
547 >      Result := not FParams.IsEqual(TIBStoredProc(Filer.Ancestor).FParams) else
548 >      Result := FParams.Count > 0;
549 >  end;
550 >
551 > begin
552 >  inherited DefineProperties(Filer);
553 >  Filer.DefineProperty('ParamData', ReadParamData, WriteParamData, WriteData); {do not localize}
554 > end;
555 >
556 > procedure TIBStoredProc.WriteParamData(Writer: TWriter);
557 > begin
558 >  Writer.WriteCollection(Params);
559 > end;
560 >
561 > procedure TIBStoredProc.ReadParamData(Reader: TReader);
562 > begin
563 >  Reader.ReadValue;
564 >  Reader.ReadCollection(Params);
565 > end;
566 >
567 > end.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines