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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines