1 {*********************************************************}
3 { Zeos Database Objects }
4 { Abstract Database Connectivity Classes }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
64 {$IFDEF WITH_LCONVENCODING}
67 Types, Classes, {$IFDEF MSEgui}mclasses,{$ENDIF} SysUtils,
68 ZClasses, ZDbcIntfs, ZTokenizer, ZCompatibility, ZGenericSqlToken,
69 ZGenericSqlAnalyser, ZPlainDriver, ZURL, ZCollections, ZVariant;
73 {** Implements Abstract Database Driver. }
74 {$WARNINGS OFF} //to supress the deprecated Warning of connect
75 TZAbstractDriver = class(TInterfacedObject, IZDriver)
77 FCachedPlainDrivers: IZHashMap;
78 FSupportedProtocols: TStringDynArray;
79 procedure AddSupportedProtocol(AProtocol: String);
80 function AddPlainDriverToCache(PlainDriver: IZPlainDriver; const Protocol: string = ''; LibLocation: string = ''): String;
81 function GetPlainDriverFromCache(const Protocol, LibLocation: string): IZPlainDriver;
82 function GetPlainDriver(const Url: TZURL; const InitDriver: Boolean = True): IZPlainDriver; virtual;
84 constructor Create; virtual;
85 destructor Destroy; override;
87 function GetSupportedProtocols: TStringDynArray;
88 function GetSupportedClientCodePages(const Url: TZURL;
89 Const {$IFNDEF UNICODE}AutoEncode, {$ENDIF} SupportedsOnly: Boolean;
90 CtrlsCPType: TZControlsCodePage = cCP_UTF16): TStringDynArray;
91 function Connect(const Url: string; Info: TStrings = nil): IZConnection; overload; deprecated;
92 function Connect(const Url: TZURL): IZConnection; overload; virtual;
93 function AcceptsURL(const Url: string): Boolean; virtual;
95 function GetPropertyInfo(const Url: string; Info: TStrings): TStrings; virtual;
96 function GetMajorVersion: Integer; virtual;
97 function GetMinorVersion: Integer; virtual;
98 function GetSubVersion: Integer; virtual;
99 function GetTokenizer: IZTokenizer; virtual;
100 function GetStatementAnalyser: IZStatementAnalyser; virtual;
101 function GetClientVersion(const Url: string): Integer; virtual;
105 {** Implements Abstract Database Connection. }
107 { TZAbstractConnection }
109 TZAbstractConnection = class(TZCodePagedObject, IZConnection)
112 FIZPlainDriver: IZPlainDriver;
113 FAutoCommit: Boolean;
115 FTransactIsolationLevel: TZTransactIsolationLevel;
118 FUseMetadata: Boolean;
119 function GetHostName: string;
120 procedure SetHostName(const Value: String);
121 function GetPort: Integer;
122 procedure SetConnPort(const Value: Integer);
123 function GetDatabase: string;
124 procedure SetDatabase(const Value: String);
125 function GetUser: string;
126 procedure SetUser(const Value: String);
127 function GetPassword: string;
128 procedure SetPassword(const Value: String);
129 function GetInfo: TStrings;
131 FDisposeCodePage: Boolean;
132 FUndefinedVarcharAsStringLength: Integer; //used for PostgreSQL and SQLite
133 FClientCodePage: String;
134 FMetadata: TContainedObject;
135 {$IFDEF ZEOS_TEST_ONLY}
138 procedure InternalCreate; virtual; abstract;
139 function GetEncoding: TZCharEncoding;
140 function GetConSettings: PZConSettings;
141 procedure CheckCharEncoding(const CharSet: String; const DoArrange: Boolean = False);
142 function GetClientCodePageInformations: PZCodePage; //EgonHugeist
143 function GetAutoEncodeStrings: Boolean; //EgonHugeist
144 procedure SetAutoEncodeStrings(const Value: Boolean);
145 procedure OnPropertiesChange(Sender: TObject); virtual;
146 procedure RaiseUnsupportedException;
148 function CreateRegularStatement(Info: TStrings): IZStatement;
150 function CreatePreparedStatement(const SQL: string; Info: TStrings):
151 IZPreparedStatement; virtual;
152 function CreateCallableStatement(const SQL: string; Info: TStrings):
153 IZCallableStatement; virtual;
155 property Driver: IZDriver read FDriver write FDriver;
156 property PlainDriver: IZPlainDriver read FIZPlainDriver write FIZPlainDriver;
157 property HostName: string read GetHostName write SetHostName;
158 property Port: Integer read GetPort write SetConnPort;
159 property Database: string read GetDatabase write SetDatabase;
160 property User: string read GetUser write SetUser;
161 property Password: string read GetPassword write SetPassword;
162 property Info: TStrings read GetInfo;
163 property AutoCommit: Boolean read FAutoCommit write FAutoCommit;
164 property ReadOnly: Boolean read FReadOnly write FReadOnly;
165 property URL: TZURL read FURL;
166 property TransactIsolationLevel: TZTransactIsolationLevel
167 read FTransactIsolationLevel write FTransactIsolationLevel;
168 property Closed: Boolean read FClosed write FClosed;
170 constructor Create(Driver: IZDriver; const Url: string;
171 PlainDriver: IZPlainDriver; const HostName: string; Port: Integer;
172 const Database: string; const User: string; const Password: string;
173 Info: TStrings); overload; deprecated;
174 constructor Create(const ZUrl: TZURL); overload;
175 destructor Destroy; override;
177 function CreateStatement: IZStatement;
178 function PrepareStatement(const SQL: string): IZPreparedStatement;
179 function PrepareCall(const SQL: string): IZCallableStatement;
181 function CreateStatementWithParams(Info: TStrings): IZStatement;
182 function PrepareStatementWithParams(const SQL: string; Info: TStrings):
184 function PrepareCallWithParams(const SQL: string; Info: TStrings):
187 function CreateNotification(const Event: string): IZNotification; virtual;
188 function CreateSequence(const Sequence: string; BlockSize: Integer):
191 function NativeSQL(const SQL: string): string; virtual;
193 procedure SetAutoCommit(Value: Boolean); virtual;
194 function GetAutoCommit: Boolean; virtual;
196 procedure Commit; virtual;
197 procedure Rollback; virtual;
199 //2Phase Commit Support initially for PostgresSQL (firmos) 21022006
200 procedure PrepareTransaction(const transactionid: string);virtual;
201 procedure CommitPrepared(const transactionid: string);virtual;
202 procedure RollbackPrepared(const transactionid: string);virtual;
204 //Ping Support initially for MySQL 27032006 (firmos)
205 function PingServer: Integer; virtual;
206 function EscapeString(Value: RawByteString): RawByteString; virtual;
208 procedure Open; virtual;
209 procedure Close; virtual;
210 function IsClosed: Boolean; virtual;
212 function GetDriver: IZDriver;
213 function GetIZPlainDriver: IZPlainDriver;
214 function GetMetadata: IZDatabaseMetadata;
215 function GetParameters: TStrings;
216 {ADDED by fduenas 15-06-2006}
217 function GetClientVersion: Integer; virtual;
218 function GetHostVersion: Integer; virtual;
219 {END ADDED by fduenas 15-06-2006}
220 function GetDescription: AnsiString;
221 procedure SetReadOnly(ReadOnly: Boolean); virtual;
222 function IsReadOnly: Boolean; virtual;
224 procedure SetCatalog(const Catalog: string); virtual;
225 function GetCatalog: string; virtual;
227 procedure SetTransactionIsolation(Level: TZTransactIsolationLevel); virtual;
228 function GetTransactionIsolation: TZTransactIsolationLevel; virtual;
230 function GetWarnings: EZSQLWarning; virtual;
231 procedure ClearWarnings; virtual;
232 function GetBinaryEscapeString(const Value: RawByteString): String; overload; virtual;
233 function GetBinaryEscapeString(const Value: TByteDynArray): String; overload; virtual;
234 function GetEscapeString(const Value: ZWideString): ZWideString; overload; virtual;
235 function GetEscapeString(const Value: RawByteString): RawByteString; overload; virtual;
236 function UseMetadata: boolean;
237 procedure SetUseMetadata(Value: Boolean);
238 {$IFDEF ZEOS_TEST_ONLY}
239 function GetTestMode : Byte;
240 procedure SetTestMode(Mode: Byte);
244 {** Implements Abstract Database notification. }
245 TZAbstractNotification = class(TInterfacedObject, IZNotification)
248 FConnection: IZConnection;
250 property EventName: string read FEventName write FEventName;
251 property Connection: IZConnection read FConnection write FConnection;
253 constructor Create(Connection: IZConnection; EventName: string);
254 function GetEvent: string;
255 procedure Listen; virtual;
256 procedure Unlisten; virtual;
257 procedure DoNotify; virtual;
258 function CheckEvents: string; virtual;
260 function GetConnection: IZConnection; virtual;
263 {** Implements Abstract Sequence generator. }
264 TZAbstractSequence = class(TInterfacedObject, IZSequence)
268 FConnection: IZConnection;
270 function GetName: string; virtual;
271 function GetBlockSize: Integer; virtual;
272 procedure SetName(const Value: string); virtual;
273 procedure SetBlockSize(const Value: Integer); virtual;
274 property Connection: IZConnection read FConnection write FConnection;
276 constructor Create(Connection: IZConnection; Name: string;
279 function GetCurrentValue: Int64; virtual;
280 function GetNextValue: Int64; virtual;
282 function GetCurrentValueSQL: string; virtual; abstract;
283 function GetNextValueSQL: string; virtual; abstract;
285 function GetConnection: IZConnection; virtual;
287 property Name: string read GetName write SetName;
288 property BlockSize: Integer read GetBlockSize write SetBlockSize;
293 uses ZMessages, ZSysUtils, ZDbcMetadata, ZDbcUtils, ZEncoding
294 {$IFDEF WITH_UNITANSISTRINGS},AnsiStrings{$ENDIF};
299 Constructs this object with default properties.
301 constructor TZAbstractDriver.Create;
303 FCachedPlainDrivers := TZHashMap.Create;
307 Destroys this object and cleanups the memory.
309 destructor TZAbstractDriver.Destroy;
311 FCachedPlainDrivers.Clear;
312 FCachedPlainDrivers := nil;
316 function TZAbstractDriver.GetSupportedProtocols: TStringDynArray;
318 Result := FSupportedProtocols;
323 Get names of the supported CharacterSets.
324 For example: ASCII, UTF8...
326 function TZAbstractDriver.GetSupportedClientCodePages(const Url: TZURL;
327 Const {$IFNDEF UNICODE}AutoEncode,{$ENDIF} SupportedsOnly: Boolean;
328 CtrlsCPType: TZControlsCodePage = cCP_UTF16): TStringDynArray;
330 Plain: IZPlainDriver;
332 Plain := GetPlainDriverFromCache(Url.Protocol, '');
333 if Assigned(Plain) then
334 Result := Plain.GetSupportedClientCodePages({$IFNDEF UNICODE}AutoEncode,{$ENDIF}
335 not SupportedsOnly, CtrlsCPType);
339 Attempts to make a database connection to the given URL.
340 The driver should return "null" if it realizes it is the wrong kind
341 of driver to connect to the given URL. This will be common, as when
342 the JDBC driver manager is asked to connect to a given URL it passes
343 the URL to each loaded driver in turn.
345 <P>The driver should raise a SQLException if it is the right
346 driver to connect to the given URL, but has trouble connecting to
349 <P>The java.util.Properties argument can be used to passed arbitrary
350 string tag/value pairs as connection arguments.
351 Normally at least "user" and "password" properties should be
352 included in the Properties.
354 @param url the URL of the database to which to connect
355 @param info a list of arbitrary string tag/value pairs as
356 connection arguments. Normally at least a "user" and
357 "password" property should be included.
358 @return a <code>Connection</code> object that represents a
359 connection to the URL
362 function TZAbstractDriver.Connect(const Url: string; Info: TStrings): IZConnection;
366 TempURL := TZURL.Create(Url, Info);
368 Result := Connect(TempURL);
374 function TZAbstractDriver.Connect(const Url: TZURL): IZConnection;
381 Returns true if the driver thinks that it can open a connection
382 to the given URL. Typically drivers will return true if they
383 understand the subprotocol specified in the URL and false if
385 @param url the URL of the database
386 @return true if this driver can connect to the given URL
388 function TZAbstractDriver.AcceptsURL(const Url: string): Boolean;
391 Protocols: TStringDynArray;
394 Protocols := GetSupportedProtocols;
395 for I := Low(Protocols) to High(Protocols) do
397 Result := StartsWith(LowerCase(Url), Format('zdbc:%s:', [LowerCase(Protocols[I])]));
403 procedure TZAbstractDriver.AddSupportedProtocol(AProtocol: String);
405 SetLength(FSupportedProtocols, Length(FSupportedProtocols)+1);
406 FSupportedProtocols[High(FSupportedProtocols)] := AProtocol;
409 function TZAbstractDriver.AddPlainDriverToCache(PlainDriver: IZPlainDriver;
410 const Protocol: string = ''; LibLocation: string = ''): String;
414 if Protocol = '' then
416 Result := PlainDriver.GetProtocol;
417 TempKey := TZAnyValue.CreateWithString(PlainDriver.GetProtocol)
422 TempKey := TZAnyValue.CreateWithString(Protocol+LibLocation);
424 FCachedPlainDrivers.Put(TempKey, PlainDriver);
427 function TZAbstractDriver.GetPlainDriverFromCache(const Protocol, LibLocation: string): IZPlainDriver;
430 TempPlain: IZPlainDriver;
432 TempKey := TZAnyValue.CreateWithString(Protocol+LibLocation);
433 Result := FCachedPlainDrivers.Get(TempKey) as IZPlainDriver;
437 TempKey := TZAnyValue.CreateWithString(Protocol);
438 TempPlain := FCachedPlainDrivers.Get(TempKey) as IZPlainDriver;
439 if Assigned(TempPlain) then
440 Result := TempPlain.Clone;
445 Gets plain driver for selected protocol.
446 @param Url a database connection URL.
447 @return a selected plaindriver.
449 function TZAbstractDriver.GetPlainDriver(const Url: TZURL;
450 const InitDriver: Boolean): IZPlainDriver;
452 Result := GetPlainDriverFromCache(Url.Protocol, Url.LibLocation);
453 if Assigned(Result) and InitDriver then
454 Result.Initialize(Url.LibLocation);
458 Gets information about the possible properties for this driver.
459 <p>The getPropertyInfo method is intended to allow a generic GUI tool to
460 discover what properties it should prompt a human for in order to get
461 enough information to connect to a database. Note that depending on
462 the values the human has supplied so far, additional values may become
463 necessary, so it may be necessary to iterate though several calls
466 @param url the URL of the database to which to connect
467 @param info a proposed list of tag/value pairs that will be sent on
469 @return an array of DriverPropertyInfo objects describing possible
470 properties. This array may be an empty array if no properties
473 function TZAbstractDriver.GetPropertyInfo(const Url: string; Info: TStrings): TStrings;
479 Gets the driver's major version number. Initially this should be 1.
480 @return this driver's major version number
482 function TZAbstractDriver.GetMajorVersion: Integer;
488 Gets the driver's minor version number. Initially this should be 0.
489 @return this driver's minor version number
491 function TZAbstractDriver.GetMinorVersion: Integer;
497 Gets the driver's sub version (revision) number. Initially this should be 0.
498 @return this driver's sub version number
500 function TZAbstractDriver.GetSubVersion: Integer;
505 Creates a generic statement analyser object.
506 @returns a generic statement analyser object.
508 function TZAbstractDriver.GetStatementAnalyser: IZStatementAnalyser;
510 Result := TZGenericStatementAnalyser.Create; { thread save! Allways return a new Analyser! }
514 Creates a generic tokenizer object.
515 @returns a created generic tokenizer object.
517 function TZAbstractDriver.GetTokenizer: IZTokenizer;
519 Result := TZGenericSQLTokenizer.Create;
523 Returns the version of the plain driver library that will be used to open a connection
525 @param url the URL of the database
526 @return the version number of the plain driver library for the give URL
528 function TZAbstractDriver.GetClientVersion(const Url: string): Integer;
533 { TZAbstractConnection }
535 function TZAbstractConnection.GetHostName: string;
537 Result := FURL.HostName;
540 procedure TZAbstractConnection.SetHostName(const Value: String);
542 FURL.HostName := Value;
545 function TZAbstractConnection.GetPort: Integer;
550 procedure TZAbstractConnection.SetConnPort(const Value: Integer);
555 function TZAbstractConnection.GetDatabase: string;
557 Result := FURL.Database;
560 procedure TZAbstractConnection.SetDatabase(const Value: String);
562 FURL.Database := Value;
565 function TZAbstractConnection.GetUser: string;
567 Result := FURL.UserName;
570 procedure TZAbstractConnection.SetUser(const Value: String);
572 FURL.UserName := Value;
575 function TZAbstractConnection.GetPassword: string;
577 Result := FURL.Password;
580 procedure TZAbstractConnection.SetPassword(const Value: String);
582 FURL.Password := Value;
585 function TZAbstractConnection.GetInfo: TStrings;
587 Result := FURL.Properties;
590 function TZAbstractConnection.GetEncoding: TZCharEncoding;
592 Result := ConSettings.ClientCodePage^.Encoding;
595 function TZAbstractConnection.GetConSettings: PZConSettings;
597 Result := ConSettings;
601 EgonHugeist: Check if the given Charset for Compiler/Database-Support!!
602 Not supported means if there is a pissible String-DataLoss.
603 So it raises an Exception if case of settings. This handling
604 is an improofment to inform Zeos-Users about the troubles the given
605 CharacterSet may have.
606 @param CharSet the CharacterSet which has to be proofed
607 @param DoArrange represents a switch to check and set a aternative ZAlias as
608 default. This means it ignores the choosen Client-CharacterSet and sets a
609 "more" Zeos-Compatible Client-CharacterSet if known.
611 procedure TZAbstractConnection.CheckCharEncoding(const CharSet: String;
612 const DoArrange: Boolean = False);
614 ConSettings.ClientCodePage := GetIZPlainDriver.ValidateCharEncoding(CharSet, DoArrange);
615 FClientCodePage := ConSettings.ClientCodePage^.Name; //resets the developer choosen ClientCodePage
616 {$IFDEF WITH_LCONVENCODING}
617 SetConvertFunctions(ConSettings.CTRL_CP, ConSettings.ClientCodePage.CP,
618 ConSettings.PlainConvertFunc, ConSettings.DbcConvertFunc);
620 ZEncoding.SetConvertFunctions(ConSettings);
625 EgonHugeist: this is a compatibility-Option for exiting Applictions.
626 Zeos is now able to preprepare direct insered SQL-Statements.
627 Means do the UTF8-preparation if the CharacterSet was choosen.
628 So we do not need to do the SQLString + UTF8Encode(Edit1.Test) for example.
629 @result True if coPreprepareSQL was choosen in the TZAbstractConnection
631 function TZAbstractConnection.GetAutoEncodeStrings: Boolean;
636 Result := ConSettings.AutoEncode;
640 procedure TZAbstractConnection.SetAutoEncodeStrings(const Value: Boolean);
643 ConSettings.AutoEncode := Value;
648 EgonHugeist and MDeams: The old deprecadet constructor which was used
649 from the descendant classes. We left him here for compatibility reasons to
650 exesting projects which using the DbcConnections directly
652 Constructs this object and assignes the main properties.
653 @param Driver the parent ZDBC driver.
654 @param Url a connection URL.
655 @param PlainDriver a versioned ZPlainDriver object interface.
656 @param HostName a name of the host.
657 @param Port a port number (0 for default port).
658 @param Database a name pof the database.
659 @param User a user name.
660 @param Password a user password.
661 @param Info a string list with extra connection parameters.
663 {$WARNINGS OFF} //suppress the deprecatad warning of calling create from internal
664 constructor TZAbstractConnection.Create(Driver: IZDriver; const Url: string;
665 PlainDriver: IZPlainDriver;
666 const HostName: string; Port: Integer; const Database: string;
667 const User: string; const Password: string; Info: TStrings);
671 TempURL := TZURL.Create(Url, HostName, Port, Database, User, Password, Info);
678 Constructs this object and assignes the main properties.
679 @param Url a connection ZURL-class which exports all connection parameters.
681 constructor TZAbstractConnection.Create(const ZUrl: TZURL);
684 if not assigned(ZUrl) then
685 raise Exception.Create('ZUrl is not assigned!')
687 FURL := TZURL.Create();
688 FDriver := DriverManager.GetDriver(ZURL.URL);
689 FIZPlainDriver := FDriver.GetPlainDriver(ZUrl);
691 FURL.OnPropertiesChange := OnPropertiesChange;
692 FURL.URL := ZUrl.URL;
694 FClientCodePage := Info.Values['codepage'];
696 ConSettings := New(PZConSettings);
698 SetConSettingsFromInfo(Info);
699 CheckCharEncoding(FClientCodePage, True);
702 FReadOnly := False; //EH: Changed! We definitelly did newer ever open a ReadOnly connection by default!
703 FTransactIsolationLevel := tiNone;
704 FUseMetadata := True;
706 {$IFDEF ZEOS_TEST_ONLY}
712 Destroys this object and cleanups the memory.
714 destructor TZAbstractConnection.Destroy;
718 FreeAndNil(FMetadata);
720 FIZPlainDriver := nil;
722 if Assigned(ConSettings) then
723 Dispose(ConSettings);
728 Opens a connection to database server with specified parameters.
730 procedure TZAbstractConnection.Open;
736 Raises unsupported operation exception.
738 procedure TZAbstractConnection.RaiseUnsupportedException;
740 raise EZSQLException.Create(SUnsupportedOperation);
744 Creates a <code>Statement</code> object for sending
745 SQL statements to the database.
746 SQL statements without parameters are normally
747 executed using Statement objects. If the same SQL statement
748 is executed many times, it is more efficient to use a
749 <code>PreparedStatement</code> object.
751 Result sets created using the returned <code>Statement</code>
752 object will by default have forward-only type and read-only concurrency.
754 @return a new Statement object
756 function TZAbstractConnection.CreateStatement: IZStatement;
758 Result := CreateStatementWithParams(nil);
762 Creates a <code>Statement</code> object for sending
763 SQL statements to the database.
764 SQL statements without parameters are normally
765 executed using Statement objects. If the same SQL statement
766 is executed many times, it is more efficient to use a
767 <code>PreparedStatement</code> object.
769 Result sets created using the returned <code>Statement</code>
770 object will by default have forward-only type and read-only concurrency.
772 @param Info a statement parameters.
773 @return a new Statement object
775 function TZAbstractConnection.CreateStatementWithParams(Info: TStrings):
777 var UsedInfo: TStrings;
780 If StrToBoolEx(GetInfo.Values['preferprepared']) then
782 If UsedInfo = nil then
783 UsedInfo := TSTringList.Create;
784 UsedInfo.Append('preferprepared=TRUE');
786 Result := CreateRegularStatement(UsedInfo);
787 if UsedInfo <> Info then UsedInfo.Free;
791 Creates a regular statement object.
792 @param SQL a SQL query string.
793 @param Info a statement parameters.
794 @returns a created statement.
796 function TZAbstractConnection.CreateRegularStatement(
797 Info: TStrings): IZStatement;
800 RaiseUnsupportedException;
804 Creates a <code>PreparedStatement</code> object for sending
805 parameterized SQL statements to the database.
807 A SQL statement with or without IN parameters can be
808 pre-compiled and stored in a PreparedStatement object. This
809 object can then be used to efficiently execute this statement
812 <P><B>Note:</B> This method is optimized for handling
813 parametric SQL statements that benefit from precompilation. If
814 the driver supports precompilation,
815 the method <code>prepareStatement</code> will send
816 the statement to the database for precompilation. Some drivers
817 may not support precompilation. In this case, the statement may
818 not be sent to the database until the <code>PreparedStatement</code> is
819 executed. This has no direct effect on users; however, it does
820 affect which method throws certain SQLExceptions.
822 Result sets created using the returned PreparedStatement will have
823 forward-only type and read-only concurrency, by default.
825 @param sql a SQL statement that may contain one or more '?' IN
826 parameter placeholders
827 @return a new PreparedStatement object containing the
828 pre-compiled statement
830 function TZAbstractConnection.PrepareStatement(const SQL: string): IZPreparedStatement;
832 Result := CreatePreparedStatement(SQL, nil);
836 Creates a <code>PreparedStatement</code> object for sending
837 parameterized SQL statements to the database.
839 @param SQL a SQL statement that may contain one or more '?' IN
840 parameter placeholders
841 @param Info a statement parameters.
842 @return a new PreparedStatement object containing the
843 pre-compiled statement
845 function TZAbstractConnection.PrepareStatementWithParams(const SQL: string;
846 Info: TStrings): IZPreparedStatement;
847 var UsedInfo: TStrings;
850 If StrToBoolEx(GetInfo.Values['preferprepared']) then
852 If UsedInfo = nil then
853 UsedInfo := TSTringList.Create;
854 UsedInfo.Append('preferprepared=TRUE');
856 Result := CreatePreparedStatement(SQL, UsedInfo);
857 if UsedInfo <> Info then UsedInfo.Free;
860 procedure TZAbstractConnection.PrepareTransaction(const transactionid: string);
862 RaiseUnsupportedException;
866 Creates a prepared statement object.
867 @param SQL a SQL query string.
868 @param Info a statement parameters.
869 @returns a created statement.
871 function TZAbstractConnection.CreatePreparedStatement(const SQL: string;
872 Info: TStrings): IZPreparedStatement;
875 RaiseUnsupportedException;
879 Creates a <code>CallableStatement</code> object for calling
880 database stored procedures.
881 The <code>CallableStatement</code> object provides
882 methods for setting up its IN and OUT parameters, and
883 methods for executing the call to a stored procedure.
885 <P><B>Note:</B> This method is optimized for handling stored
886 procedure call statements. Some drivers may send the call
887 statement to the database when the method <code>prepareCall</code>
889 may wait until the <code>CallableStatement</code> object
890 is executed. This has no
891 direct effect on users; however, it does affect which method
892 throws certain SQLExceptions.
894 Result sets created using the returned CallableStatement will have
895 forward-only type and read-only concurrency, by default.
897 @param sql a SQL statement that may contain one or more '?'
898 parameter placeholders. Typically this statement is a JDBC
899 function call escape string.
900 @return a new CallableStatement object containing the
901 pre-compiled SQL statement
904 function TZAbstractConnection.PrepareCall(
905 const SQL: string): IZCallableStatement;
907 Result := CreateCallableStatement(SQL, nil);
911 Creates a <code>CallableStatement</code> object for calling
912 database stored procedures.
913 The <code>CallableStatement</code> object provides
914 methods for setting up its IN and OUT parameters, and
915 methods for executing the call to a stored procedure.
917 @param SQL a SQL statement that may contain one or more '?'
918 parameter placeholders. Typically this statement is a JDBC
919 function call escape string.
920 @param Info a statement parameters.
921 @return a new CallableStatement object containing the
922 pre-compiled SQL statement
924 function TZAbstractConnection.PrepareCallWithParams(const SQL: string;
925 Info: TStrings): IZCallableStatement;
926 var UsedInfo: TStrings;
929 If StrToBoolEx(GetInfo.Values['preferprepared']) then
931 If UsedInfo = nil then
932 UsedInfo := TSTringList.Create;
933 UsedInfo.Append('preferprepared=TRUE');
935 Result := CreateCallableStatement(SQL, UsedInfo);
936 if UsedInfo <> Info then UsedInfo.Free;
940 Creates a callable statement object.
941 @param SQL a SQL query string.
942 @param Info a statement parameters.
943 @returns a created statement.
945 function TZAbstractConnection.CreateCallableStatement(const SQL: string;
946 Info: TStrings): IZCallableStatement;
949 RaiseUnsupportedException;
953 Creates an object to send/recieve notifications from SQL server.
954 @param Event an event name.
955 @returns a created notification object.
957 function TZAbstractConnection.CreateNotification(const Event: string): IZNotification;
960 RaiseUnsupportedException;
964 Creates a sequence generator object.
965 @param Sequence a name of the sequence generator.
966 @param BlockSize a number of unique keys requested in one trip to SQL server.
967 @returns a created sequence object.
969 function TZAbstractConnection.CreateSequence(const Sequence: string;
970 BlockSize: Integer): IZSequence;
973 RaiseUnsupportedException;
977 Converts the given SQL statement into the system's native SQL grammar.
978 A driver may convert the JDBC sql grammar into its system's
979 native SQL grammar prior to sending it; this method returns the
980 native form of the statement that the driver would have sent.
982 @param sql a SQL statement that may contain one or more '?'
983 parameter placeholders
984 @return the native form of this statement
986 function TZAbstractConnection.NativeSQL(const SQL: string): string;
992 Sets this connection's auto-commit mode.
993 If a connection is in auto-commit mode, then all its SQL
994 statements will be executed and committed as individual
995 transactions. Otherwise, its SQL statements are grouped into
996 transactions that are terminated by a call to either
997 the method <code>commit</code> or the method <code>rollback</code>.
998 By default, new connections are in auto-commit mode.
1000 The commit occurs when the statement completes or the next
1001 execute occurs, whichever comes first. In the case of
1002 statements returning a ResultSet, the statement completes when
1003 the last row of the ResultSet has been retrieved or the
1004 ResultSet has been closed. In advanced cases, a single
1005 statement may return multiple results as well as output
1006 parameter values. In these cases the commit occurs when all results and
1007 output parameter values have been retrieved.
1009 @param autoCommit true enables auto-commit; false disables auto-commit.
1011 procedure TZAbstractConnection.SetAutoCommit(Value: Boolean);
1013 FAutoCommit := Value;
1017 Gets the current auto-commit state.
1018 @return the current state of auto-commit mode
1021 function TZAbstractConnection.GetAutoCommit: Boolean;
1023 Result := FAutoCommit;
1027 Makes all changes made since the previous
1028 commit/rollback permanent and releases any database locks
1029 currently held by the Connection. This method should be
1030 used only when auto-commit mode has been disabled.
1033 procedure TZAbstractConnection.Commit;
1035 RaiseUnsupportedException;
1038 procedure TZAbstractConnection.CommitPrepared(const transactionid: string);
1040 RaiseUnsupportedException;
1044 Drops all changes made since the previous
1045 commit/rollback and releases any database locks currently held
1046 by this Connection. This method should be used only when auto-
1047 commit has been disabled.
1050 procedure TZAbstractConnection.Rollback;
1052 RaiseUnsupportedException;
1055 procedure TZAbstractConnection.RollbackPrepared(const transactionid: string);
1057 RaiseUnsupportedException;
1061 Ping Current Connection's server, if client was disconnected,
1062 the connection is resumed.
1063 @return 0 if succesfull or error code if any error occurs
1065 function TZAbstractConnection.PingServer: Integer;
1068 RaiseUnsupportedException;
1072 Escape a string so it's acceptable for the Connection's server.
1073 @param value string that should be escaped
1074 @return Escaped string
1076 function TZAbstractConnection.EscapeString(Value : RawByteString) : RawByteString;
1078 Result := AnsiString(EncodeCString(String(Value)));
1082 Releases a Connection's database and JDBC resources
1083 immediately instead of waiting for
1084 them to be automatically released.
1086 <P><B>Note:</B> A Connection is automatically closed when it is
1087 garbage collected. Certain fatal errors also result in a closed
1091 procedure TZAbstractConnection.Close;
1093 if FDisposeCodePage then
1095 Dispose(ConSettings^.ClientCodePage);
1096 ConSettings^.ClientCodePage := nil;
1097 FDisposeCodePage := False;
1103 Tests to see if a Connection is closed.
1104 @return true if the connection is closed; false if it's still open
1106 function TZAbstractConnection.IsClosed: Boolean;
1112 Gets the parent ZDBC driver.
1113 @returns the parent ZDBC driver interface.
1115 function TZAbstractConnection.GetDriver: IZDriver;
1121 Gets the plain driver.
1122 @returns the plain driver interface.
1124 function TZAbstractConnection.GetIZPlainDriver: IZPlainDriver;
1126 result := FIZPlainDriver;
1130 Gets the metadata regarding this connection's database.
1131 A Connection's database is able to provide information
1132 describing its tables, its supported SQL grammar, its stored
1133 procedures, the capabilities of this connection, and so on. This
1134 information is made available through a DatabaseMetaData
1137 @return a DatabaseMetaData object for this Connection
1139 function TZAbstractConnection.GetMetadata: IZDatabaseMetadata;
1141 Result := FMetadata as IZDatabaseMetadata;
1145 Gets a connection parameters.
1146 @returns a list with connection parameters.
1148 function TZAbstractConnection.GetParameters: TStrings;
1154 Gets the client's full version number. Initially this should be 0.
1155 The format of the version resturned must be XYYYZZZ where
1159 @return this clients's full version number
1161 function TZAbstractConnection.GetClientVersion: Integer;
1167 Gets the host's full version number. Initially this should be 0.
1168 The format of the version returned must be XYYYZZZ where
1172 @return this server's full version number
1174 function TZAbstractConnection.GetHostVersion: Integer;
1179 function TZAbstractConnection.GetDescription: AnsiString;
1181 PlainDriver.GetDescription;
1184 {END ADDED by fduenas 15-06-2006}
1187 Puts this connection in read-only mode as a hint to enable
1188 database optimizations.
1190 <P><B>Note:</B> This method cannot be called while in the
1191 middle of a transaction.
1193 @param readOnly true enables read-only mode; false disables
1196 procedure TZAbstractConnection.SetReadOnly(ReadOnly: Boolean);
1198 FReadOnly := ReadOnly;
1202 Tests to see if the connection is in read-only mode.
1203 @return true if connection is read-only and false otherwise
1205 function TZAbstractConnection.IsReadOnly: Boolean;
1207 Result := FReadOnly;
1211 Sets a catalog name in order to select
1212 a subspace of this Connection's database in which to work.
1213 If the driver does not support catalogs, it will
1214 silently ignore this request.
1216 procedure TZAbstractConnection.SetCatalog(const Catalog: string);
1221 Returns the Connection's current catalog name.
1222 @return the current catalog name or null
1224 function TZAbstractConnection.GetCatalog: string;
1230 Attempts to change the transaction isolation level to the one given.
1231 The constants defined in the interface <code>Connection</code>
1232 are the possible transaction isolation levels.
1234 <P><B>Note:</B> This method cannot be called while
1235 in the middle of a transaction.
1237 @param level one of the TRANSACTION_* isolation values with the
1238 exception of TRANSACTION_NONE; some databases may not support other values
1239 @see DatabaseMetaData#supportsTransactionIsolationLevel
1241 procedure TZAbstractConnection.SetTransactionIsolation(
1242 Level: TZTransactIsolationLevel);
1244 FTransactIsolationLevel := Level;
1248 Gets this Connection's current transaction isolation level.
1249 @return the current TRANSACTION_* mode value
1251 function TZAbstractConnection.GetTransactionIsolation: TZTransactIsolationLevel;
1253 Result := FTransactIsolationLevel;
1257 Returns the first warning reported by calls on this Connection.
1258 <P><B>Note:</B> Subsequent warnings will be chained to this
1260 @return the first SQLWarning or null
1262 function TZAbstractConnection.GetWarnings: EZSQLWarning;
1268 Clears all warnings reported for this <code>Connection</code> object.
1269 After a call to this method, the method <code>getWarnings</code>
1270 returns null until a new warning is reported for this Connection.
1272 procedure TZAbstractConnection.ClearWarnings;
1276 function TZAbstractConnection.UseMetadata: boolean;
1278 result := FUseMetadata;
1281 procedure TZAbstractConnection.SetUseMetadata(Value: Boolean);
1283 FUseMetadata := Value;
1286 {$IFDEF ZEOS_TEST_ONLY}
1287 function TZAbstractConnection.GetTestMode: Byte;
1289 Result := FTestMode;
1292 procedure TZAbstractConnection.SetTestMode(Mode: Byte);
1300 Returns the BinaryString in a Tokenizer-detectable kind
1301 If the Tokenizer don't need to predetect it Result = BinaryString
1302 @param Value represents the Binary-String
1303 @param EscapeMarkSequence represents a Tokenizer detectable EscapeSequence (Len >= 3)
1304 @result the detectable Binary String
1306 function TZAbstractConnection.GetBinaryEscapeString(const Value: RawByteString): String;
1308 if GetAutoEncodeStrings then //Set detect-sequence only if Prepreparing should be done else it's not server-understandable.
1309 Result := Self.GetDriver.GetTokenizer.AnsiGetEscapeString(GetSQLHexString(PAnsiChar(Value), Length(Value)))
1311 Result := GetSQLHexString(PAnsiChar(Value), Length(Value));
1314 function TZAbstractConnection.GetBinaryEscapeString(const Value: TByteDynArray): String;
1316 if GetAutoEncodeStrings then //Set detect-sequence only if Prepreparing should be done else it's not server-understandable.
1317 Result := Self.GetDriver.GetTokenizer.AnsiGetEscapeString(GetSQLHexString(PAnsiChar(Value), Length(Value)))
1319 Result := GetSQLHexString(PAnsiChar(Value), Length(Value));
1322 function TZAbstractConnection.GetEscapeString(const Value: ZWideString): ZWideString;
1324 if GetAutoEncodeStrings then
1325 if StartsWith(Value, '''') and EndsWith(Value, '''') then
1326 Result := GetDriver.GetTokenizer.GetEscapeString(Value)
1329 Result := AnsiQuotedStr(Value, #39)
1331 Result := ZDbcUnicodeString(GetDriver.GetTokenizer.GetEscapeString(AnsiQuotedStr(ZPlainString(Value), #39)))
1334 if StartsWith(Value, '''') and EndsWith(Value, '''') then
1338 Result := AnsiQuotedStr(Value, #39);
1340 Result := ZDbcUnicodeString(AnsiQuotedStr(ZPlainString(Value), #39));
1344 function TZAbstractConnection.GetEscapeString(const Value: RawByteString): RawByteString;
1346 if GetAutoEncodeStrings then
1347 if StartsWith(Value, '''') and EndsWith(Value, '''') then
1348 Result := {$IFNDEF UNICODE}GetDriver.GetTokenizer.GetEscapeString{$ENDIF}(Value)
1350 {$IFDEF WITH_UNITANSISTRINGS}
1351 AnsiStrings.AnsiQuotedStr(Value, #39)
1353 Result := GetDriver.GetTokenizer.GetEscapeString(AnsiQuotedStr(ZDbcString(Value), #39))
1356 if StartsWith(Value, '''') and EndsWith(Value, '''') then
1359 Result := {$IFDEF WITH_UNITANSISTRINGS}AnsiStrings.{$ENDIF}AnsiQuotedStr(Value, #39);
1363 Result 100% Compiler-Compatible
1364 And sets it Result to ClientCodePage by calling the
1365 PlainDriver.GetClientCodePageInformations function
1367 @param ClientCharacterSet the CharacterSet which has to be checked
1368 @result PZCodePage see ZCompatible.pas
1370 function TZAbstractConnection.GetClientCodePageInformations: PZCodePage; //EgonHugeist
1372 Result := ConSettings.ClientCodePage
1375 procedure TZAbstractConnection.OnPropertiesChange(Sender: TObject);
1377 // do nothing in base class
1380 { TZAbstractNotification }
1383 Creates this object and assignes the main properties.
1384 @param Connection a database connection object.
1385 @param EventName a name of the SQL event.
1387 constructor TZAbstractNotification.Create(Connection: IZConnection;
1390 FConnection := Connection;
1391 FEventName := EventName;
1396 @return an event name for this notification.
1398 function TZAbstractNotification.GetEvent: string;
1400 Result := FEventName;
1404 Sets a listener to the specified event.
1406 procedure TZAbstractNotification.Listen;
1411 Removes a listener to the specified event.
1413 procedure TZAbstractNotification.Unlisten;
1418 Checks for any pending events.
1419 @return a string with incoming events??
1421 function TZAbstractNotification.CheckEvents: string;
1427 Sends a notification string.
1429 procedure TZAbstractNotification.DoNotify;
1434 Returns the <code>Connection</code> object
1435 that produced this <code>Statement</code> object.
1436 @return the connection that produced this statement
1438 function TZAbstractNotification.GetConnection: IZConnection;
1440 Result := FConnection;
1443 { TZAbstractSequence }
1446 Creates this sequence object.
1447 @param Connection an SQL connection interface.
1448 @param Name a name of the sequence generator.
1449 @param BlockSize a number of unique keys requested in one trip to server.
1451 constructor TZAbstractSequence.Create(Connection: IZConnection;
1452 Name: string; BlockSize: Integer);
1454 FConnection := Connection;
1456 FBlockSize := BlockSize;
1460 Returns the <code>Connection</code> object
1461 that produced this <code>Statement</code> object.
1462 @return the connection that produced this statement
1464 function TZAbstractSequence.GetConnection: IZConnection;
1466 Result := FConnection;
1470 Returns a name of the sequence generator.
1471 @return a name of this sequence generator.
1473 function TZAbstractSequence.GetName: string;
1479 Returns the assigned block size for this sequence.
1480 @return the assigned block size.
1482 function TZAbstractSequence.GetBlockSize: Integer;
1484 Result := FBlockSize;
1488 Gets the current unique key generated by this sequence.
1489 @param the last generated unique key.
1491 function TZAbstractSequence.GetCurrentValue: Int64;
1497 function TZAbstractSequence.GetCurrentValueSQL: String;
1499 result:='IMPLEMENT';
1504 Gets the next unique key generated by this sequence.
1505 @param the next generated unique key.
1507 function TZAbstractSequence.GetNextValue: Int64;
1513 function TZAbstractSequence.GetNextValueSQL: String;
1515 result:='IMPLEMENT';
1520 Sets the block size for this sequence.
1521 @param Value the block size.
1523 procedure TZAbstractSequence.SetBlockSize(const Value: Integer);
1525 FBlockSize := Value;
1529 Sets a name of the sequence generator.
1530 @param Value a name of this sequence generator.
1532 procedure TZAbstractSequence.SetName(const Value: string);