12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788 |
- {
- Copyright (c) 2004-2014 by Joost van der Sluis, FPC contributors
- SQL database & dataset
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit SQLDB;
- {$mode objfpc}
- {$H+}
- {$M+} // ### remove this!!!
- interface
- uses SysUtils, Classes, DB, bufdataset, sqlscript, sqltypes;
- type
- TSchemaType = sqltypes.TSchemaType;
- TStatementType = sqltypes.TStatementType;
- TDBEventType = sqltypes.TDBEventType;
- TDBEventTypes = sqltypes.TDBEventTypes;
- TQuoteChars = sqltypes.TQuoteChars;
- const
- StatementTokens : Array[TStatementType] of string = ('(unknown)', 'select',
- 'insert', 'update', 'delete',
- 'create', 'get', 'put', 'execute',
- 'start','commit','rollback', '?'
- );
- TSchemaObjectNames: array[TSchemaType] of String = ('???', 'table_name',
- '???', 'procedure_name', 'column_name', 'param_name',
- 'index_name', 'package_name', 'schema_name','sequence');
- SingleQuotes : TQuoteChars = ('''','''');
- DoubleQuotes : TQuoteChars = ('"','"');
- LogAllEvents = [detCustom, detPrepare, detExecute, detFetch, detCommit, detRollBack];
- LogAllEventsExtra = [detCustom, detPrepare, detExecute, detFetch, detCommit, detRollBack, detParamValue,detActualSQL];
- // Backwards compatibility alias constants.
-
- stNoSchema = sqltypes.stNoSchema;
- stTables = sqltypes.stTables;
- stSysTables = sqltypes.stSysTables;
- stProcedures = sqltypes.stProcedures;
- stColumns = sqltypes.stColumns;
- stProcedureParams = sqltypes.stProcedureParams;
- stIndexes = sqltypes.stIndexes;
- stPackages = sqltypes.stPackages;
- stSchemata = sqltypes.stSchemata;
- stSequences = sqltypes.stSequences;
- stUnknown = sqltypes.stUnknown;
- stSelect = sqltypes.stSelect;
- stInsert = sqltypes.stInsert;
- stUpdate = sqltypes.stUpdate;
- stDelete = sqltypes.stDelete;
- stDDL = sqltypes.stDDL;
- stGetSegment = sqltypes.stGetSegment;
- stPutSegment = sqltypes.stPutSegment;
- stExecProcedure = sqltypes.stExecProcedure;
- stStartTrans = sqltypes.stStartTrans;
- stCommit = sqltypes.stCommit;
- stRollback = sqltypes.stRollback;
- stSelectForUpd = sqltypes.stSelectForUpd;
- detCustom = sqltypes.detCustom;
- detPrepare = sqltypes.detPrepare;
- detExecute = sqltypes.detExecute;
- detFetch = sqltypes.detFetch;
- detCommit = sqltypes.detCommit;
- detRollBack = sqltypes.detRollBack;
- detParamValue = sqltypes.detParamValue;
- detActualSQL = sqltypes.detActualSQL;
- Type
- TRowsCount = LargeInt;
- TSQLStatementInfo = Record
- StatementType : TStatementType;
- TableName : String;
- Updateable : Boolean;
- WhereStartPos ,
- WhereStopPos : integer;
- end;
- TSQLConnection = class;
- TSQLTransaction = class;
- TCustomSQLQuery = class;
- TCustomSQLStatement = Class;
- TSQLQuery = class;
- TSQLScript = class;
- TSQLHandle = Class(TObject)
- end;
- { TSQLCursor }
- TSQLCursor = Class(TSQLHandle)
- public
- FPrepared : Boolean;
- FSelectable : Boolean;
- FInitFieldDef : Boolean;
- FStatementType : TStatementType;
- FSchemaType : TSchemaType;
- end;
- { ESQLDatabaseError}
- ESQLDatabaseError = class(EDatabaseError)
- public
- ErrorCode: integer;
- SQLState : string;
- constructor CreateFmt(const Fmt: string; const Args: array of const;
- Comp : TComponent; AErrorCode: integer; ASQLState: string); overload;
- end;
- { TSQLDBFieldDef }
- TSQLDBFieldDef = Class(TFieldDef)
- private
- FData: Pointer;
- Public
- Property SQLDBData : Pointer Read FData Write FData;
- end;
- { TSQLDBFieldDefs }
- TSQLDBFieldDefs = Class(TFieldDefs)
- Protected
- Class Function FieldDefClass : TFieldDefClass; override;
- end;
- { TSQLDBParam }
- TSQLDBParam = Class(TParam)
- private
- FFieldDef: TFieldDef;
- FData : Pointer;
- Public
- Property FieldDef : TFieldDef Read FFieldDef Write FFieldDef;
- Property SQLDBData : Pointer Read FData Write FData;
- end;
- { TSQLDBParams }
- TSQLDBParams = Class(TParams)
- Protected
- Class Function ParamClass : TParamClass; override;
- end;
- type
- { TServerIndexDefs }
- TServerIndexDefs = class(TIndexDefs)
- Private
- public
- constructor Create(ADataSet: TDataSet); override;
- procedure Update; override;
- end;
- type
- { TSQLConnection }
-
- TDBLogNotifyEvent = Procedure (Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String) of object;
- TConnOption = (sqSupportParams, sqSupportEmptyDatabaseName, sqEscapeSlash, sqEscapeRepeat, sqImplicitTransaction, sqLastInsertID, sqSupportReturning);
- TConnOptions= set of TConnOption;
- TSQLConnectionOption = (scoExplicitConnect, scoApplyUpdatesChecksRowsAffected);
- TSQLConnectionOptions = Set of TSQLConnectionOption;
- TConnInfoType=(citAll=-1, citServerType, citServerVersion, citServerVersionString, citClientName, citClientVersion);
- TSQLConnection = class (TDatabase)
- private
- FFieldNameQuoteChars : TQuoteChars;
- FOptions : TSQLConnectionOptions;
- FPassword : string;
- FTransaction : TSQLTransaction;
- FUserName : string;
- FHostName : string;
- FCharSet : string;
- FCodePage : TSystemCodePage;
- FRole : String;
- FStatements : TThreadList;
- FLogEvents: TDBEventTypes;
- FOnLog: TDBLogNotifyEvent;
- function GetPort: cardinal;
- procedure SetOptions(AValue: TSQLConnectionOptions);
- procedure SetPort(const AValue: cardinal);
- function AttemptCommit(trans : TSQLHandle) : boolean;
- function AttemptRollBack(trans : TSQLHandle) : boolean;
- protected
- FConnOptions : TConnOptions;
- FSQLFormatSettings : TFormatSettings;
- // Updating of DB records is moved out of TSQLQuery.
- // It is done here, so descendents can override it and implement DB-specific.
- // One day, this may be factored out to a TSQLResolver class.
- // The following allow construction of update queries. They can be adapted as needed by descendents to fit the DB engine.
- procedure AddFieldToUpdateWherePart(var sql_where: string; UpdateMode : TUpdateMode; F: TField); virtual;
- function ConstructInsertSQL(Query: TCustomSQLQuery; Var ReturningClause : Boolean): string; virtual;
- function ConstructUpdateSQL(Query: TCustomSQLQuery; Var ReturningClause : Boolean): string; virtual;
- function ConstructDeleteSQL(Query: TCustomSQLQuery): string; virtual;
- function ConstructRefreshSQL(Query: TCustomSQLQuery; UpdateKind : TUpdateKind): string; virtual;
- // factory function used to create custom statements
- function CreateCustomQuery(aOwner: TComponent): TCustomSQLQuery; virtual;
- function InitialiseUpdateStatement(Query: TCustomSQLQuery; var qry: TCustomSQLQuery): TCustomSQLQuery;
- procedure ApplyFieldUpdate(C : TSQLCursor; P: TSQLDBParam; F: TField; UseOldValue: Boolean); virtual;
- // This is the call that updates a record, it used to be in TSQLQuery.
- procedure ApplyRecUpdate(Query : TCustomSQLQuery; UpdateKind : TUpdateKind); virtual;
- function RefreshLastInsertID(Query : TCustomSQLQuery; Field : TField): Boolean; virtual;
- procedure GetDBInfo(const ASchemaType : TSchemaType; const ASchemaObjectName, AReturnField : string; AList: TStrings);
- function GetConnectionCharSet: string; virtual;
- procedure SetTransaction(Value : TSQLTransaction); virtual;
- procedure DoConnect; override;
- procedure DoInternalConnect; override;
- procedure DoInternalDisconnect; override;
- function GetAsString(Param: TParam): RawByteString;
- function GetAsSQLText(Field : TField) : string; overload; virtual;
- function GetAsSQLText(Param : TParam) : string; overload; virtual;
- function GetHandle : pointer; virtual;
- Function LogEvent(EventType : TDBEventType) : Boolean;
- Procedure LogParams(Const AParams : TParams); virtual;
- Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
- Procedure RegisterStatement(S : TCustomSQLStatement);
- Procedure UnRegisterStatement(S : TCustomSQLStatement);
- Function AllocateCursorHandle : TSQLCursor; virtual; abstract;
- Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); virtual; abstract;
- function StrToStatementType(s : string) : TStatementType; virtual;
- procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); virtual; abstract;
- procedure UnPrepareStatement(cursor : TSQLCursor); virtual; abstract;
- procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); virtual; abstract;
- function RowsAffected(cursor: TSQLCursor): TRowsCount; virtual;
- function Fetch(cursor : TSQLCursor) : boolean; virtual; abstract;
- procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TFieldDefs); virtual; abstract;
- function AddFieldDef(AFieldDefs: TFieldDefs; AFieldNo: Longint; const AName: string; ADataType: TFieldType; ASize, APrecision: Integer; AByteSize, ARequired, AReadOnly: Boolean): TFieldDef;
- function LoadField(cursor : TSQLCursor; FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; virtual; abstract;
- procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); virtual; abstract;
- procedure FreeFldBuffers(cursor : TSQLCursor); virtual;
- Function AllocateTransactionHandle : TSQLHandle; virtual; abstract;
- function GetTransactionHandle(trans : TSQLHandle): pointer; virtual; abstract;
- function Commit(trans : TSQLHandle) : boolean; virtual; abstract;
- function RollBack(trans : TSQLHandle) : boolean; virtual; abstract;
- function StartImplicitTransaction(trans : TSQLHandle; aParams : string) : boolean; virtual;
- function StartDBTransaction(trans : TSQLHandle; aParams : string) : boolean; virtual; abstract;
- procedure CommitRetaining(trans : TSQLHandle); virtual; abstract;
- procedure RollBackRetaining(trans : TSQLHandle); virtual; abstract;
- procedure UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string); virtual;
- function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; virtual;
- function GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string; virtual;
- Procedure MaybeConnect;
- Property Statements : TThreadList Read FStatements;
- property Port: cardinal read GetPort write SetPort;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure StartTransaction; override;
- procedure EndTransaction; override;
- procedure ExecuteDirect(SQL : String); overload; virtual;
- procedure ExecuteDirect(SQL : String; ATransaction : TSQLTransaction); overload; virtual;
- // Unified version
- function GetObjectNames(ASchemaType: TSchemaType; AList : TSqlObjectIdentifierList): Integer; virtual;
- // Older versions.
- procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); virtual;
- procedure GetProcedureNames(List : TStrings); virtual;
- procedure GetFieldNames(const TableName : string; List : TStrings); virtual;
- procedure GetSchemaNames(List: TStrings); virtual;
- procedure GetSequenceNames(List: TStrings); virtual;
- function GetConnectionInfo(InfoType:TConnInfoType): string; virtual;
- function GetStatementInfo(const ASQL: string): TSQLStatementInfo; virtual;
- procedure CreateDB; virtual;
- procedure DropDB; virtual;
- function GetNextValue(const SequenceName: string; IncrementBy: integer=1): Int64; virtual;
- property ConnOptions: TConnOptions read FConnOptions;
- property Handle: Pointer read GetHandle;
- property FieldNameQuoteChars: TQuoteChars read FFieldNameQuoteChars write FFieldNameQuoteChars;
- published
- property Password : string read FPassword write FPassword;
- property Transaction : TSQLTransaction read FTransaction write SetTransaction;
- property UserName : string read FUserName write FUserName;
- property CharSet : string read FCharSet write FCharSet;
- property HostName : string Read FHostName Write FHostName;
- Property OnLog : TDBLogNotifyEvent Read FOnLog Write FOnLog;
- Property LogEvents : TDBEventTypes Read FLogEvents Write FLogEvents Default LogAllEvents;
- Property Options : TSQLConnectionOptions Read FOptions Write SetOptions default [];
- Property Role : String read FRole write FRole;
- property Connected;
- property DatabaseName;
- property KeepConnection;
- property LoginPrompt;
- property Params;
- property OnLogin;
- end;
- { TSQLTransaction }
- TCommitRollbackAction = (caNone, caCommit, caCommitRetaining, caRollback,
- caRollbackRetaining);
- TSQLTransactionOption = (stoUseImplicit, stoExplicitStart);
- TSQLTransactionOptions = Set of TSQLTransactionOption;
- TSQLTransaction = class (TDBTransaction)
- private
- FOptions : TSQLTransactionOptions;
- FTrans : TSQLHandle;
- FAction : TCommitRollbackAction;
- FParams : TStringList;
- function GetSQLConnection: TSQLConnection;
- procedure SetOptions(AValue: TSQLTransactionOptions);
- procedure SetParams(const AValue: TStringList);
- procedure SetSQLConnection(AValue: TSQLConnection);
- protected
- Procedure MaybeStartTransaction;
- Function AllowClose(DS: TDBDataset): Boolean; override;
- function GetHandle : Pointer; virtual;
- Procedure SetDatabase (Value : TDatabase); override;
- Function LogEvent(EventType : TDBEventType) : Boolean;
- Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
- public
- constructor Create(AOwner : TComponent); override;
- destructor Destroy; override;
- procedure Commit; override;
- procedure CommitRetaining; override;
- procedure Rollback; override;
- procedure RollbackRetaining; override;
- procedure StartTransaction; override;
- procedure EndTransaction; override;
- property Handle: Pointer read GetHandle;
- Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
- published
- property Action : TCommitRollbackAction read FAction write FAction Default caRollBack;
- property Database;
- property Params : TStringList read FParams write SetParams;
- Property Options : TSQLTransactionOptions Read FOptions Write SetOptions default [];
- end;
- { TCustomSQLStatement }
- TCustomSQLStatement = Class(TComponent)
- Private
- FCursor : TSQLCursor;
- FDatabase: TSQLConnection;
- FParamCheck: Boolean;
- FParams: TParams;
- FSQL: TStrings;
- FOrigSQL : String;
- FServerSQL : String;
- FTransaction: TSQLTransaction;
- FParseSQL: Boolean;
- FDataLink : TDataLink;
- FRowsAffected : TRowsCount;
- procedure SetDatabase(AValue: TSQLConnection);
- procedure SetParams(AValue: TParams);
- procedure SetSQL(AValue: TStrings);
- procedure SetTransaction(AValue: TSQLTransaction);
- Function GetPrepared : Boolean;
- Protected
- Function CreateDataLink : TDataLink; virtual;
- procedure OnChangeSQL(Sender : TObject); virtual;
- function GetDataSource: TDataSource; Virtual;
- procedure SetDataSource(AValue: TDataSource); virtual;
- Procedure CopyParamsFromMaster(CopyBound : Boolean); virtual;
- procedure AllocateCursor;
- procedure DeAllocateCursor;
- Function GetSchemaType : TSchemaType; virtual;
- Function GetSchemaObjectName : String; virtual;
- Function GetSchemaPattern: String; virtual;
- Function IsSelectable : Boolean ; virtual;
- procedure GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo); virtual;
- Procedure DoExecute; virtual;
- procedure DoPrepare; virtual;
- procedure DoUnPrepare; virtual;
- Function CreateParams : TSQLDBParams; virtual;
- Function LogEvent(EventType : TDBEventType) : Boolean;
- Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- Property Cursor : TSQLCursor read FCursor;
- Property Database : TSQLConnection Read FDatabase Write SetDatabase;
- Property Transaction : TSQLTransaction Read FTransaction Write SetTransaction;
- Property SQL : TStrings Read FSQL Write SetSQL;
- Property Params : TParams Read FParams Write SetParams;
- Property DataSource : TDataSource Read GetDataSource Write SetDataSource;
- Property ParseSQL : Boolean Read FParseSQL Write FParseSQL;
- Property ParamCheck : Boolean Read FParamCheck Write FParamCheck default true;
- Public
- constructor Create(AOwner : TComponent); override;
- destructor Destroy; override;
- Procedure Prepare;
- Procedure Execute;
- Procedure Unprepare;
- function ParamByName(Const AParamName : String) : TParam;
- function RowsAffected: TRowsCount; virtual;
- Property Prepared : boolean read GetPrepared;
- end;
- TSQLStatement = Class(TCustomSQLStatement)
- Published
- Property Database;
- Property DataSource;
- Property ParamCheck;
- Property Params;
- Property ParseSQL;
- Property SQL;
- Property Transaction;
- end;
- { TSQLSequence }
- TSQLSequenceApplyEvent = (saeOnNewRecord, saeOnPost);
- TSQLSequence = class(TPersistent)
- private
- FQuery: TCustomSQLQuery;
- FFieldName: String;
- FSequenceName: String;
- FIncrementBy: Integer;
- FApplyEvent: TSQLSequenceApplyEvent;
- public
- constructor Create(AQuery: TCustomSQLQuery);
- procedure Assign(Source: TPersistent); override;
- procedure Apply;
- function GetNextValue: Int64;
- published
- property FieldName: String read FFieldName write FFieldName;
- property SequenceName: String read FSequenceName write FSequenceName;
- property IncrementBy: Integer read FIncrementBy write FIncrementBy default 1;
- property ApplyEvent: TSQLSequenceApplyEvent read FApplyEvent write FApplyEvent default saeOnNewRecord;
- end;
- { TCustomSQLQuery }
- TSQLQueryOption = (sqoKeepOpenOnCommit, sqoAutoApplyUpdates, sqoAutoCommit, sqoCancelUpdatesOnRefresh, sqoRefreshUsingSelect);
- TSQLQueryOptions = Set of TSQLQueryOption;
- TCustomSQLQuery = class (TCustomBufDataset)
- private
- FOptions : TSQLQueryOptions;
- FSchemaType : TSchemaType;
- FUpdateable : boolean;
- FTableName : string;
- FStatement : TCustomSQLStatement;
- FInsertSQL,
- FUpdateSQL,
- FDeleteSQL,
- FRefreshSQL : TStringList;
- FIsEOF : boolean;
- FLoadingFieldDefs : boolean;
- FUpdateMode : TUpdateMode;
- FusePrimaryKeyAsKey : Boolean;
- FWhereStartPos : integer;
- FWhereStopPos : integer;
- FServerFilterText : string;
- FServerFiltered : Boolean;
- FServerIndexDefs : TServerIndexDefs;
- // Used by SetSchemaType
- FSchemaObjectName : string;
- FSchemaPattern : string;
- FInsertQry,
- FUpdateQry,
- FDeleteQry : TCustomSQLQuery;
- FSequence : TSQLSequence;
- procedure FreeFldBuffers;
- function GetParamCheck: Boolean;
- function GetParams: TParams;
- function GetParseSQL: Boolean;
- function GetServerIndexDefs: TServerIndexDefs;
- function GetSQL: TStringList;
- function GetSQLConnection: TSQLConnection;
- function GetSQLTransaction: TSQLTransaction;
- function GetStatementType : TStatementType;
- Function NeedLastInsertID: TField;
- procedure SetOptions(AValue: TSQLQueryOptions);
- procedure SetParamCheck(AValue: Boolean);
- procedure SetSQLConnection(AValue: TSQLConnection);
- procedure SetSQLTransaction(AValue: TSQLTransaction);
- procedure SetInsertSQL(const AValue: TStringList);
- procedure SetUpdateSQL(const AValue: TStringList);
- procedure SetDeleteSQL(const AValue: TStringList);
- procedure SetRefreshSQL(const AValue: TStringList);
- procedure SetParams(AValue: TParams);
- procedure SetParseSQL(AValue : Boolean);
- procedure SetSQL(const AValue: TStringList);
- procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
- procedure SetUpdateMode(AValue : TUpdateMode);
- procedure OnChangeModifySQL(Sender : TObject);
- procedure Execute;
- procedure ApplyFilter;
- Function AddFilter(SQLstr : string) : string;
- protected
- function CreateSQLStatement(aOwner: TComponent): TCustomSQLStatement; virtual;
- Function CreateParams: TSQLDBParams; virtual;
- Function RefreshLastInsertID(Field: TField): Boolean; virtual;
- Function NeedRefreshRecord (UpdateKind: TUpdateKind): Boolean; virtual;
- Function RefreshRecord (UpdateKind: TUpdateKind) : Boolean; virtual;
- Procedure ApplyReturningResult(Q : TCustomSQLQuery; UpdateKind : TUpdateKind);
- Function Cursor : TSQLCursor;
- Function LogEvent(EventType : TDBEventType) : Boolean;
- Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
- // abstract & virtual methods of TBufDataset
- function Fetch : boolean; override;
- function LoadField(FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; override;
- procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField); override;
- procedure ApplyRecUpdate(UpdateKind : TUpdateKind); override;
- procedure SetPacketRecords(aValue : integer); override;
- // abstract & virtual methods of TDataset
- procedure UpdateServerIndexDefs; virtual;
- procedure SetDatabase(Value : TDatabase); override;
- Procedure SetTransaction(Value : TDBTransaction); override;
- procedure InternalAddRecord(Buffer: Pointer; AAppend: Boolean); override;
- procedure InternalClose; override;
- procedure InternalInitFieldDefs; override;
- procedure InternalOpen; override;
- Procedure InternalRefresh; override;
- function GetCanModify: Boolean; override;
- Function IsPrepared : Boolean; virtual;
- Procedure SetActive (Value : Boolean); override;
- procedure SetServerFiltered(Value: Boolean); virtual;
- procedure SetServerFilterText(const Value: string); virtual;
- Function GetDataSource : TDataSource; override;
- Procedure SetDataSource(AValue : TDataSource);
- procedure BeforeRefreshOpenCursor; override;
- procedure SetReadOnly(AValue : Boolean); override;
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- procedure DoOnNewRecord; override;
- procedure DoBeforePost; override;
- class function FieldDefsClass : TFieldDefsClass; override;
- // IProviderSupport methods
- function PSGetUpdateException(E: Exception; Prev: EUpdateError): EUpdateError; override;
- function PSGetTableName: string; override;
- Property TableName : String Read FTableName Write FTableName; // alternative: procedure DoGetTableName
- public
- constructor Create(AOwner : TComponent); override;
- destructor Destroy; override;
- procedure Prepare; virtual;
- procedure UnPrepare; virtual;
- procedure ExecSQL; virtual;
- procedure SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string); virtual;
- function RowsAffected: TRowsCount; virtual;
- function ParamByName(Const AParamName : String) : TParam;
- Property Prepared : boolean read IsPrepared;
- Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
- Property SQLTransaction: TSQLTransaction Read GetSQLTransaction Write SetSQLTransaction;
- // overriden TBufDataSet methods
- Procedure ApplyUpdates(MaxErrors: Integer); override; overload;
- // overriden TDataSet methods
- Procedure Post; override;
- Procedure Delete; override;
- protected
- // redeclared TDataSet properties
- property Active;
- property Filter;
- property Filtered;
- property BeforeOpen;
- property AfterOpen;
- property BeforeClose;
- property AfterClose;
- property BeforeInsert;
- property AfterInsert;
- property BeforeEdit;
- property AfterEdit;
- property BeforePost;
- property AfterPost;
- property BeforeCancel;
- property AfterCancel;
- property BeforeDelete;
- property AfterDelete;
- property BeforeRefresh;
- property AfterRefresh;
- property BeforeScroll;
- property AfterScroll;
- property OnCalcFields;
- property OnDeleteError;
- property OnEditError;
- property OnFilterRecord;
- property OnNewRecord;
- property OnPostError;
- property AutoCalcFields;
- // protected
- property Database;
- property Transaction;
- property SchemaType : TSchemaType read FSchemaType default stNoSchema;
- property SQL : TStringlist read GetSQL write SetSQL;
- property InsertSQL : TStringList read FInsertSQL write SetInsertSQL;
- property UpdateSQL : TStringList read FUpdateSQL write SetUpdateSQL;
- property DeleteSQL : TStringList read FDeleteSQL write SetDeleteSQL;
- property RefreshSQL : TStringList read FRefreshSQL write SetRefreshSQL;
- Property Options : TSQLQueryOptions Read FOptions Write SetOptions default [];
- property Params : TParams read GetParams Write SetParams;
- Property ParamCheck : Boolean Read GetParamCheck Write SetParamCheck default true;
- property ParseSQL : Boolean read GetParseSQL write SetParseSQL default true;
- property UpdateMode : TUpdateMode read FUpdateMode write SetUpdateMode default upWhereKeyOnly;
- property UsePrimaryKeyAsKey : boolean read FUsePrimaryKeyAsKey write SetUsePrimaryKeyAsKey default true;
- property StatementType : TStatementType read GetStatementType;
- Property DataSource : TDataSource Read GetDataSource Write SetDataSource;
- property Sequence: TSQLSequence read FSequence write FSequence;
- property ServerFilter: string read FServerFilterText write SetServerFilterText;
- property ServerFiltered: Boolean read FServerFiltered write SetServerFiltered default False;
- property ServerIndexDefs : TServerIndexDefs read GetServerIndexDefs;
- end;
- { TSQLQuery }
- TSQLQuery = Class(TCustomSQLQuery)
- public
- property SchemaType;
- Property StatementType;
- Published
- property MaxIndexesCount;
- // TDataset stuff
- property FieldDefs;
- Property Active;
- Property AutoCalcFields;
- Property Filter;
- Property Filtered;
- Property AfterCancel;
- Property AfterClose;
- Property AfterDelete;
- Property AfterEdit;
- Property AfterInsert;
- Property AfterOpen;
- Property AfterPost;
- Property AfterRefresh;
- Property AfterScroll;
- Property BeforeCancel;
- Property BeforeClose;
- Property BeforeDelete;
- Property BeforeEdit;
- Property BeforeInsert;
- Property BeforeOpen;
- Property BeforePost;
- Property BeforeRefresh;
- Property BeforeScroll;
- Property OnCalcFields;
- Property OnDeleteError;
- Property OnEditError;
- Property OnFilterRecord;
- Property OnNewRecord;
- Property OnPostError;
- // property SchemaInfo default stNoSchema;
- property Database;
- property Transaction;
- property ReadOnly;
- property SQL;
- property InsertSQL;
- property UpdateSQL;
- property DeleteSQL;
- property RefreshSQL;
- property IndexDefs;
- Property Options;
- property Params;
- Property ParamCheck;
- property ParseSQL;
- property UpdateMode;
- property UsePrimaryKeyAsKey;
- Property DataSource;
- property Sequence;
- property ServerFilter;
- property ServerFiltered;
- property ServerIndexDefs;
- end;
- { TSQLScript }
- TSQLScript = class (TCustomSQLscript)
- private
- FOnDirective: TSQLScriptDirectiveEvent;
- FQuery : TCustomSQLQuery;
- FDatabase : TDatabase;
- FTransaction : TDBTransaction;
- protected
- procedure ExecuteStatement (SQLStatement: TStrings; var StopExecution: Boolean); override;
- procedure ExecuteDirective (Directive, Argument: String; var StopExecution: Boolean); override;
- procedure ExecuteCommit(CommitRetaining: boolean=true); override;
- Procedure SetDatabase (Value : TDatabase); virtual;
- Procedure SetTransaction(Value : TDBTransaction); virtual;
- Procedure CheckDatabase;
- function CreateQuery: TCustomSQLQuery; virtual;
- public
- constructor Create(AOwner : TComponent); override;
- destructor Destroy; override;
- procedure Execute; override;
- procedure ExecuteScript;
- Property Aborted;
- Property Line;
- published
- Property DataBase : TDatabase Read FDatabase Write SetDatabase;
- Property Transaction : TDBTransaction Read FTransaction Write SetTransaction;
- property OnDirective: TSQLScriptDirectiveEvent read FOnDirective write FOnDirective;
- Property AutoCommit;
- Property UseDollarString;
- Property DollarStrings;
- property Directives;
- property Defines;
- property Script;
- property Terminator;
- property CommentsinSQL;
- property UseSetTerm;
- property UseCommit;
- property UseDefines;
- property OnException;
- end;
- { TSQLConnector }
- TSQLConnector = Class(TSQLConnection)
- private
- FProxy : TSQLConnection;
- FConnectorType: String;
- procedure SetConnectorType(const AValue: String);
- protected
- procedure SetTransaction(Value : TSQLTransaction);override;
- procedure DoInternalConnect; override;
- procedure DoInternalDisconnect; override;
- Procedure CheckProxy;
- Procedure CreateProxy; virtual;
- Procedure FreeProxy; virtual;
- function StrToStatementType(s : string) : TStatementType; override;
- function GetAsSQLText(Field : TField) : string; overload; override;
- function GetAsSQLText(Param : TParam) : string; overload; override;
- function GetHandle : pointer; override;
- Function AllocateCursorHandle : TSQLCursor; override;
- Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
- Function AllocateTransactionHandle : TSQLHandle; override;
- procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
- procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
- function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
- function Fetch(cursor : TSQLCursor) : boolean; override;
- procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
- procedure UnPrepareStatement(cursor : TSQLCursor); override;
- function LoadField(cursor : TSQLCursor; FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; override;
- procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
- procedure FreeFldBuffers(cursor : TSQLCursor); override;
- function GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string; override;
- function GetTransactionHandle(trans : TSQLHandle): pointer; override;
- function Commit(trans : TSQLHandle) : boolean; override;
- function RollBack(trans : TSQLHandle) : boolean; override;
- function StartDBTransaction(trans : TSQLHandle; aParams : string) : boolean; override;
- procedure CommitRetaining(trans : TSQLHandle); override;
- procedure RollBackRetaining(trans : TSQLHandle); override;
- procedure UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string); override;
- function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
- Property Proxy : TSQLConnection Read FProxy;
- Published
- Property ConnectorType : String Read FConnectorType Write SetConnectorType;
- end;
- TSQLConnectionClass = Class of TSQLConnection;
- { TConnectionDef }
- TLibraryLoadFunction = Function (Const S : AnsiString) : Integer;
- TLibraryUnLoadFunction = Procedure;
- TConnectionDef = Class(TPersistent)
- Class Function TypeName : String; virtual;
- Class Function ConnectionClass : TSQLConnectionClass; virtual;
- Class Function Description : String; virtual;
- Class Function DefaultLibraryName : String; virtual;
- Class Function LoadFunction : TLibraryLoadFunction; virtual;
- Class Function UnLoadFunction : TLibraryUnLoadFunction; virtual;
- Class Function LoadedLibraryName : string; virtual;
- Procedure ApplyParams(Params : TStrings; AConnection : TSQLConnection); virtual;
- end;
- TConnectionDefClass = class of TConnectionDef;
- Var
- GlobalDBLogHook : TDBLogNotifyEvent;
- Procedure RegisterConnection(Def : TConnectionDefClass);
- Procedure UnRegisterConnection(Def : TConnectionDefClass);
- Procedure UnRegisterConnection(ConnectionName : String);
- Function GetConnectionDef(ConnectorName : String) : TConnectionDef;
- Procedure GetConnectionList(List : TSTrings);
- const DefaultSQLFormatSettings : TFormatSettings = (
- CurrencyFormat: 1;
- NegCurrFormat: 5;
- ThousandSeparator: #0;
- DecimalSeparator: '.';
- CurrencyDecimals: 2;
- DateSeparator: '-';
- TimeSeparator: ':';
- ListSeparator: ' ';
- CurrencyString: '$';
- ShortDateFormat: 'yyyy-mm-dd';
- LongDateFormat: '';
- TimeAMString: '';
- TimePMString: '';
- ShortTimeFormat: 'hh:nn:ss';
- LongTimeFormat: 'hh:nn:ss.zzz';
- ShortMonthNames: ('','','','','','','','','','','','');
- LongMonthNames: ('','','','','','','','','','','','');
- ShortDayNames: ('','','','','','','');
- LongDayNames: ('','','','','','','');
- TwoDigitYearCenturyWindow: 50;
- );
- implementation
- uses dbconst, strutils;
- Const
- // Flags to check which fields must be refreshed.
- RefreshFlags : Array [ukModify..ukInsert] of TProviderFlag = (pfRefreshOnUpdate,pfRefreshOnInsert);
- function TimeIntervalToString(Time: TDateTime): string;
- var
- millisecond: word;
- second : word;
- minute : word;
- hour : word;
- begin
- DecodeTime(Time,hour,minute,second,millisecond);
- hour := hour + trunc(Time)*24;
- Result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
- end;
- { TSQLDBFieldDefs }
- class function TSQLDBFieldDefs.FieldDefClass: TFieldDefClass;
- begin
- Result:=TSQLDBFieldDef;
- end;
- { TSQLDBParams }
- class function TSQLDBParams.ParamClass: TParamClass;
- begin
- Result:=TSQLDBParam;
- end;
- { ESQLDatabaseError }
- constructor ESQLDatabaseError.CreateFmt(const Fmt: string; const Args: array of const;
- Comp: TComponent; AErrorCode: integer; ASQLState: string);
- const CompNameFmt='%s : %s';
- var Msg: string;
- begin
- if not assigned(Comp) then
- Msg := Fmt
- else if Comp.Name = '' then
- Msg := Format(CompNameFmt, [Comp.ClassName,Fmt])
- else
- Msg := Format(CompNameFmt, [Comp.Name,Fmt]);
- if Length(Args) = 0 then
- inherited Create(Msg)
- else
- inherited CreateFmt(Msg, Args);
- ErrorCode := AErrorCode;
- SQLState := ASQLState;
- end;
- { TCustomSQLStatement }
- procedure TCustomSQLStatement.OnChangeSQL(Sender: TObject);
- var
- ConnOptions : TConnOptions;
- NewParams: TSQLDBParams;
- begin
- UnPrepare;
- if not ParamCheck then
- exit;
- if assigned(DataBase) then
- ConnOptions:=DataBase.ConnOptions
- else
- ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
- NewParams := CreateParams;
- try
- NewParams.ParseSQL(FSQL.Text, True, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions, psInterbase);
- NewParams.AssignValues(FParams);
- FParams.Assign(NewParams);
- finally
- NewParams.Free;
- end;
- end;
- procedure TCustomSQLStatement.SetDatabase(AValue: TSQLConnection);
- begin
- if FDatabase=AValue then Exit;
- UnPrepare;
- If Assigned(FDatabase) then
- begin
- FDatabase.UnregisterStatement(Self);
- FDatabase.RemoveFreeNotification(Self);
- end;
- FDatabase:=AValue;
- If Assigned(FDatabase) then
- begin
- FDatabase.FreeNotification(Self);
- FDatabase.RegisterStatement(Self);
- if Assigned(Database.Transaction) and (not Assigned(Transaction) or (Transaction.DataBase <> Database)) then
- Transaction := Database.Transaction;
- OnChangeSQL(Self);
- end;
- end;
- procedure TCustomSQLStatement.SetTransaction(AValue: TSQLTransaction);
- begin
- if FTransaction=AValue then Exit;
- UnPrepare;
- if Assigned(FTransaction) then
- FTransaction.RemoveFreeNotification(Self);
- FTransaction:=AValue;
- if Assigned(FTransaction) then
- begin
- FTransaction.FreeNotification(Self);
- if Assigned(Transaction.DataBase) and (Database <> Transaction.DataBase) then
- Database := Transaction.DataBase as TSQLConnection;
- end;
- end;
- procedure TCustomSQLStatement.SetDataSource(AValue: TDataSource);
- begin
- if GetDataSource=AValue then Exit;
- if (FDataLink=Nil) then
- FDataLink:=CreateDataLink;
- FDataLink.DataSource:=AValue;
- end;
- Procedure TCustomSQLStatement.CopyParamsFromMaster(CopyBound: Boolean);
- begin
- if Assigned(DataSource) and Assigned(DataSource.Dataset) then
- FParams.CopyParamValuesFromDataset(DataSource.Dataset,CopyBound);
- end;
- procedure TCustomSQLStatement.SetParams(AValue: TParams);
- begin
- if FParams=AValue then Exit;
- FParams.Assign(AValue);
- end;
- procedure TCustomSQLStatement.SetSQL(AValue: TStrings);
- begin
- if FSQL=AValue then Exit;
- FSQL.Assign(AValue);
- end;
- Procedure TCustomSQLStatement.DoExecute;
- begin
- FRowsAffected:=-1;
- If (FParams.Count>0) and Assigned(DataSource) then
- CopyParamsFromMaster(False);
- If LogEvent(detExecute) then
- Log(detExecute,FServerSQL);
- Database.Execute(FCursor,Transaction, FParams);
- end;
- Function TCustomSQLStatement.GetPrepared: Boolean;
- begin
- Result := Assigned(FCursor) and FCursor.FPrepared;
- end;
- Function TCustomSQLStatement.CreateDataLink: TDataLink;
- begin
- Result:=TDataLink.Create;
- end;
- Function TCustomSQLStatement.CreateParams: TSQLDBParams;
- begin
- Result:=TSQLDBParams.Create(Nil);
- end;
- Function TCustomSQLStatement.LogEvent(EventType: TDBEventType): Boolean;
- begin
- Result:=Assigned(Database) and Database.LogEvent(EventType);
- end;
- Procedure TCustomSQLStatement.Log(EventType: TDBEventType; Const Msg: String);
- Var
- M : String;
- begin
- If LogEvent(EventType) then
- begin
- If (Name<>'') then
- M:=Name
- else
- M:=ClassName;
- Database.Log(EventType,M+' : '+Msg);
- end;
- end;
- procedure TCustomSQLStatement.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- inherited Notification(AComponent, Operation);
- if (operation=opRemove) then
- If (AComponent=FTransaction) then
- FTransaction:=Nil
- else if (AComponent=FDatabase) then
- begin
- UnPrepare;
- FDatabase:=Nil;
- end;
- end;
- constructor TCustomSQLStatement.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FSQL:=TStringList.Create;
- TStringList(FSQL).OnChange:=@OnChangeSQL;
- FParams:=CreateParams;
- FParamCheck:=True;
- FParseSQL:=True;
- FRowsAffected:=-1;
- end;
- destructor TCustomSQLStatement.Destroy;
- begin
- UnPrepare;
- Transaction:=Nil;
- Database:=Nil;
- DataSource:=Nil;
- FreeAndNil(FDataLink);
- FreeAndNil(FParams);
- FreeAndNil(FSQL);
- inherited Destroy;
- end;
- Function TCustomSQLStatement.GetSchemaType: TSchemaType;
- begin
- Result:=stNoSchema
- end;
- Function TCustomSQLStatement.GetSchemaObjectName: String;
- begin
- Result:='';
- end;
- Function TCustomSQLStatement.GetSchemaPattern: String;
- begin
- Result:='';
- end;
- Function TCustomSQLStatement.IsSelectable: Boolean;
- begin
- Result:=False;
- end;
- procedure TCustomSQLStatement.GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo);
- begin
- Info:=Database.GetStatementInfo(ASQL);
- end;
- procedure TCustomSQLStatement.AllocateCursor;
- begin
- if not assigned(FCursor) then
- // Do this as late as possible.
- FCursor:=Database.AllocateCursorHandle;
- end;
- procedure TCustomSQLStatement.DeAllocateCursor;
- begin
- if Assigned(FCursor) and Assigned(Database) then
- DataBase.DeAllocateCursorHandle(FCursor);
- end;
- procedure TCustomSQLStatement.DoPrepare;
- var
- StmInfo: TSQLStatementInfo;
- begin
- if GetSchemaType=stNoSchema then
- FOrigSQL := TrimRight(FSQL.Text)
- else
- FOrigSQL := Database.GetSchemaInfoSQL(GetSchemaType, GetSchemaObjectName, GetSchemaPattern);
- if (FOrigSQL='') then
- DatabaseError(SErrNoStatement);
- FServerSQL:=FOrigSQL;
- GetStatementInfo(FServerSQL,StmInfo);
- AllocateCursor;
- FCursor.FSelectable:=True; // let PrepareStatement and/or Execute alter it
- FCursor.FStatementType:=StmInfo.StatementType;
- FCursor.FSchemaType:=GetSchemaType;
- If LogEvent(detPrepare) then
- Log(detPrepare,FServerSQL);
- Database.PrepareStatement(FCursor,Transaction,FServerSQL,FParams);
- end;
- Procedure TCustomSQLStatement.Prepare;
- begin
- if Prepared then exit;
- if not assigned(Database) then
- DatabaseError(SErrDatabasenAssigned);
- if not assigned(Transaction) then
- DatabaseError(SErrTransactionnSet);
- Database.MaybeConnect;
- if not Transaction.Active then
- Transaction.MaybeStartTransaction;
- try
- DoPrepare;
- except
- DeAllocateCursor;
- Raise;
- end;
- end;
- Procedure TCustomSQLStatement.Execute;
- begin
- Prepare;
- DoExecute;
- end;
- procedure TCustomSQLStatement.DoUnPrepare;
- begin
- If Assigned(FCursor) then
- If Assigned(Database) then
- begin
- DataBase.UnPrepareStatement(FCursor);
- DeAllocateCursor;
- end
- else // this should never happen. It means a cursor handle leaks in the DB itself.
- FreeAndNil(FCursor);
- end;
- function TCustomSQLStatement.GetDataSource: TDataSource;
- begin
- if Assigned(FDataLink) then
- Result:=FDataLink.DataSource
- else
- Result:=Nil;
- end;
- Procedure TCustomSQLStatement.Unprepare;
- begin
- // Some SQLConnections does not support statement [un]preparation, but they have allocated local cursor(s)
- // so let them do cleanup f.e. cancel pending queries and/or free resultset
- // and also do UnRegisterStatement!
- if assigned(FCursor) then
- DoUnprepare;
- end;
- function TCustomSQLStatement.ParamByName(Const AParamName: String): TParam;
- begin
- Result:=FParams.ParamByName(AParamName);
- end;
- function TCustomSQLStatement.RowsAffected: TRowsCount;
- begin
- if FRowsAffected=-1 then
- begin
- if Assigned(Database) then
- FRowsAffected:=Database.RowsAffected(FCursor);
- end;
- Result:=FRowsAffected;
- end;
- { TSQLConnection }
- constructor TSQLConnection.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FSQLFormatSettings:=DefaultSQLFormatSettings;
- FFieldNameQuoteChars:=DoubleQuotes;
- FLogEvents:=LogAllEvents; //match Property LogEvents...Default LogAllEvents
- FStatements:=TThreadList.Create;
- FStatements.Duplicates:=dupIgnore;
- end;
- destructor TSQLConnection.Destroy;
- begin
- Connected:=False; // needed because we want to de-allocate statements
- FreeAndNil(FStatements);
- inherited Destroy;
- end;
- function TSQLConnection.StrToStatementType(s : string) : TStatementType;
- var T : TStatementType;
- begin
- S:=Lowercase(s);
- for T:=stSelect to stRollback do
- if (S=StatementTokens[T]) then
- Exit(T);
- Result:=stUnknown;
- end;
- procedure TSQLConnection.SetTransaction(Value : TSQLTransaction);
- begin
- if FTransaction<>value then
- begin
- if Assigned(FTransaction) and FTransaction.Active then
- DatabaseError(SErrAssTransaction);
- if Assigned(Value) then
- Value.Database := Self;
- FTransaction := Value;
- If Assigned(FTransaction) and (FTransaction.Database=Nil) then
- FTransaction.Database:=Self;
- end;
- end;
- procedure TSQLConnection.UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string);
- begin
- // Empty abstract
- end;
- procedure TSQLConnection.DoConnect;
- var ConnectionCharSet: string;
- begin
- inherited;
- // map connection CharSet to corresponding local CodePage
- // do not set FCodePage to CP_ACP if FCodePage = DefaultSystemCodePage
- // aliases listed here are commonly used, but not recognized by CodePageNameToCodePage()
- ConnectionCharSet := LowerCase(GetConnectionCharSet);
- case ConnectionCharSet of
- 'utf8','utf-8','utf8mb4':
- FCodePage := CP_UTF8;
- 'win1250','cp1250':
- FCodePage := 1250;
- 'win1251','cp1251':
- FCodePage := 1251;
- 'win1252','cp1252','latin1','iso8859_1':
- FCodePage := 1252;
- else
- begin
- FCodePage := CodePageNameToCodePage(ConnectionCharSet);
- if FCodePage = CP_NONE then
- FCodePage := CP_ACP;
- end;
- end;
- end;
- procedure TSQLConnection.DoInternalConnect;
- begin
- if (DatabaseName = '') and not(sqSupportEmptyDatabaseName in FConnOptions) then
- DatabaseError(SErrNoDatabaseName,Self);
- end;
- procedure TSQLConnection.DoInternalDisconnect;
- Var
- I : integer;
- L : TList;
- begin
- L:=FStatements.LockList;
- try
- For I:=0 to L.Count-1 do
- TCustomSQLStatement(L[i]).Unprepare;
- L.Clear;
- finally
- FStatements.UnlockList;
- end;
- end;
- procedure TSQLConnection.StartTransaction;
- begin
- if not assigned(Transaction) then
- DatabaseError(SErrConnTransactionnSet)
- else
- Transaction.StartTransaction;
- end;
- procedure TSQLConnection.EndTransaction;
- begin
- if not assigned(Transaction) then
- DatabaseError(SErrConnTransactionnSet)
- else
- Transaction.EndTransaction;
- end;
- procedure TSQLConnection.ExecuteDirect(SQL: String);
- begin
- ExecuteDirect(SQL,FTransaction);
- end;
- procedure TSQLConnection.ExecuteDirect(SQL: String;
- ATransaction: TSQLTransaction);
- var Cursor : TSQLCursor;
- begin
- if not assigned(ATransaction) then
- DatabaseError(SErrTransactionnSet);
- if not Connected then Open;
- if not ATransaction.Active then
- ATransaction.MaybeStartTransaction;
- SQL := TrimRight(SQL);
- if SQL = '' then
- DatabaseError(SErrNoStatement);
- try
- Cursor := AllocateCursorHandle;
- Cursor.FStatementType := stUnknown;
- If LogEvent(detPrepare) then
- Log(detPrepare,SQL);
- PrepareStatement(Cursor,ATransaction,SQL,Nil);
- try
- If LogEvent(detExecute) then
- Log(detExecute,SQL);
- Execute(Cursor,ATransaction, Nil);
- finally
- UnPrepareStatement(Cursor);
- end;
- finally;
- DeAllocateCursorHandle(Cursor);
- end;
- end;
- function TSQLConnection.GetPort: cardinal;
- begin
- result := StrToIntDef(Params.Values['Port'],0);
- end;
- procedure TSQLConnection.SetOptions(AValue: TSQLConnectionOptions);
- begin
- if FOptions=AValue then Exit;
- FOptions:=AValue;
- end;
- procedure TSQLConnection.SetPort(const AValue: cardinal);
- begin
- if AValue<>0 then
- Params.Values['Port']:=IntToStr(AValue)
- else with params do if IndexOfName('Port') > -1 then
- Delete(IndexOfName('Port'));
- end;
- function TSQLConnection.AttemptCommit(trans: TSQLHandle): boolean;
- begin
- try
- Result:=Commit(trans);
- except
- if ForcedClose then
- Result:=True
- else
- Raise;
- end;
- end;
- function TSQLConnection.AttemptRollBack(trans: TSQLHandle): boolean;
- begin
- try
- Result:=Rollback(trans);
- except
- if ForcedClose then
- Result:=True
- else
- Raise;
- end;
- end;
- procedure TSQLConnection.GetDBInfo(const ASchemaType : TSchemaType; const ASchemaObjectName, AReturnField : string; AList: TStrings);
- var qry : TCustomSQLQuery;
- begin
- if not assigned(Transaction) then
- DatabaseError(SErrConnTransactionnSet);
- qry := TCustomSQLQuery.Create(nil);
- try
- qry.transaction := Transaction;
- qry.database := Self;
- with qry do
- begin
- ParseSQL := False;
- SetSchemaInfo(ASchemaType,ASchemaObjectName,'');
- open;
- AList.Clear;
- while not eof do
- begin
- AList.Append(trim(fieldbyname(AReturnField).asstring));
- Next;
- end;
- end;
- finally
- qry.free;
- end;
- end;
- function TSQLConnection.GetConnectionCharSet: string;
- begin
- // default implementation returns user supplied FCharSet
- // (can be overriden by descendants, which are able retrieve current connection charset using client API)
- Result := LowerCase(FCharSet);
- end;
- function TSQLConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
- begin
- Result := -1;
- end;
- function TSQLConnection.AddFieldDef(AFieldDefs: TFieldDefs; AFieldNo: Longint;
- const AName: string; ADataType: TFieldType; ASize, APrecision: Integer;
- AByteSize, ARequired, AReadOnly: Boolean): TFieldDef;
- var
- ACodePage: TSystemCodePage;
- begin
- // helper function used by descendants
- if ADataType in [ftString, ftFixedChar, ftMemo] then
- begin
- ACodePage := FCodePage;
- // if ASize of character data is passed as "byte length",
- // translate it to "character length" as expected by TFieldDef
- if AByteSize and (ACodePage = CP_UTF8) then
- ASize := ASize div 4;
- end
- else
- ACodePage := 0;
- Result := AFieldDefs.Add(AName, ADataType, ASize, APrecision, ARequired, AReadOnly, AFieldNo, ACodePage);
- end;
- procedure TSQLConnection.GetTableNames(List: TStrings; SystemTables: Boolean);
- begin
- if not SystemTables then
- GetDBInfo(stTables,'','table_name',List)
- else
- GetDBInfo(stSysTables,'','table_name',List);
- end;
- procedure TSQLConnection.GetProcedureNames(List: TStrings);
- begin
- GetDBInfo(stProcedures,'','procedure_name',List);
- end;
- procedure TSQLConnection.GetFieldNames(const TableName: string; List: TStrings);
- begin
- GetDBInfo(stColumns,TableName,'column_name',List);
- end;
- procedure TSQLConnection.GetSchemaNames(List: TStrings);
- begin
- GetDBInfo(stSchemata,'','SCHEMA_NAME',List);
- end;
- procedure TSQLConnection.GetSequenceNames(List: TStrings);
- begin
- GetDBInfo(stSequences,'','SEQUENCE_NAME',List);
- end;
- {
- See if we can integrate/merge this with GetDBInfo. They are virtually identical
- }
- Function TSQLConnection.GetObjectNames(ASchemaType: TSchemaType; AList : TSqlObjectIdentifierList) : Integer;
- var
- qry : TCustomSQLQuery;
- vSchemaName, vObjectName: String;
- f: TField;
- begin
- Result:=0;
- if not assigned(Transaction) then
- DatabaseError(SErrConnTransactionnSet);
- qry := TCustomSQLQuery.Create(nil);
- try
- qry.transaction := Transaction;
- qry.database := Self;
- with qry do
- begin
- ParseSQL := False;
- SetSchemaInfo(ASchemaType,TSchemaObjectNames[ASchemaType],'');
- open;
- f:=FindField(TSchemaObjectNames[stSchemata]);
- while not eof do
- begin
- vSchemaName:='';
- if Assigned(f) then
- vSchemaName:=f.AsString;
- vObjectName:=FieldByName(FSchemaObjectName).AsString;
- AList.AddIdentifier(vObjectName, vSchemaName);
- Next;
- Inc(Result);
- end;
- end;
- finally
- qry.free;
- end;
- end;
- function TSQLConnection.GetConnectionInfo(InfoType: TConnInfoType): string;
- var i: TConnInfoType;
- begin
- Result:='';
- if InfoType = citAll then
- for i:=citServerType to citClientVersion do
- begin
- if Result<>'' then Result:=Result+',';
- Result:=Result+'"'+GetConnectionInfo(i)+'"';
- end;
- end;
- function TSQLConnection.GetStatementInfo(const ASQL: string): TSQLStatementInfo;
- type
- TParsePart = (ppStart,ppWith,ppSelect,ppTableName,ppFrom,ppWhere,ppGroup,ppOrder,ppBogus);
- TPhraseSeparator = (sepNone, sepWhiteSpace, sepComma, sepComment, sepParentheses, sepDoubleQuote, sepEnd);
- TKeyword = (kwWITH, kwSELECT, kwINSERT, kwUPDATE, kwDELETE, kwFROM, kwJOIN, kwWHERE, kwGROUP, kwORDER, kwUNION, kwROWS, kwLIMIT, kwUnknown);
- const
- KeywordNames: array[TKeyword] of string =
- ('WITH', 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'FROM', 'JOIN', 'WHERE', 'GROUP', 'ORDER', 'UNION', 'ROWS', 'LIMIT', '');
- var
- PSQL, CurrentP, SavedP,
- PhraseP, PStatementPart : pchar;
- S : string;
- ParsePart : TParsePart;
- BracketCount : Integer;
- Separator : TPhraseSeparator;
- Keyword, K : TKeyword;
- begin
- PSQL:=PChar(ASQL);
- ParsePart := ppStart;
- CurrentP := PSQL-1;
- PhraseP := PSQL;
- Result.TableName := '';
- Result.Updateable := False;
- Result.WhereStartPos := 0;
- Result.WhereStopPos := 0;
- repeat
- inc(CurrentP);
- SavedP := CurrentP;
- case CurrentP^ of
- ' ', #9..#13:
- Separator := sepWhiteSpace;
- ',':
- Separator := sepComma;
- #0, ';':
- Separator := sepEnd;
- '(':
- begin
- Separator := sepParentheses;
- // skip everything between brackets, since it could be a sub-select, and
- // further nothing between brackets could be interesting for the parser.
- BracketCount := 1;
- repeat
- inc(CurrentP);
- if CurrentP^ = '(' then inc(BracketCount)
- else if CurrentP^ = ')' then dec(BracketCount);
- until (CurrentP^ = #0) or (BracketCount = 0);
- if CurrentP^ <> #0 then inc(CurrentP);
- end;
- '"','`':
- if SkipComments(CurrentP, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions) then
- Separator := sepDoubleQuote;
- else
- if SkipComments(CurrentP, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions) then
- Separator := sepComment
- else
- Separator := sepNone;
- end;
- if Separator <> sepNone then
- begin
- if (CurrentP > SavedP) and (SavedP > PhraseP) then
- CurrentP := SavedP; // there is something before comment or left parenthesis or double quote
- if (Separator in [sepWhitespace,sepComment]) and (SavedP = PhraseP) then
- PhraseP := CurrentP; // skip comments (but not parentheses) and white spaces
- if (CurrentP-PhraseP > 0) or (Separator = sepEnd) then
- begin
- SetString(s, PhraseP, CurrentP-PhraseP);
- Keyword := kwUnknown;
- for K in TKeyword do
- if SameText(s, KeywordNames[K]) then
- begin
- Keyword := K;
- break;
- end;
- case ParsePart of
- ppStart : begin
- Result.StatementType := StrToStatementType(s);
- case Keyword of
- kwWITH : ParsePart := ppWith;
- kwSELECT: ParsePart := ppSelect;
- else break;
- end;
- end;
- ppWith : begin
- // WITH [RECURSIVE] CTE_name [ ( column_names ) ] AS ( CTE_query_definition ) [, ...]
- // { SELECT | INSERT | UPDATE | DELETE } ...
- case Keyword of
- kwSELECT: Result.StatementType := stSelect;
- kwINSERT: Result.StatementType := stInsert;
- kwUPDATE: Result.StatementType := stUpdate;
- kwDELETE: Result.StatementType := stDelete;
- end;
- if Result.StatementType <> stUnknown then break;
- end;
- ppSelect : begin
- if Keyword = kwFROM then
- ParsePart := ppTableName;
- end;
- ppTableName:
- begin
- // Meta-data requests are never updateable
- // and select statements from more than one table
- // and/or derived tables are also not updateable
- if Separator in [sepWhitespace, sepComment, sepDoubleQuote, sepEnd] then
- begin
- Result.TableName := Result.TableName + s;
- Result.Updateable := True;
- end;
- // compound delimited classifier like: "schema name"."table name"
- if not (CurrentP^ in ['.','"']) then
- ParsePart := ppFrom;
- end;
- ppFrom : begin
- if (Keyword in [kwWHERE, kwGROUP, kwORDER, kwLIMIT, kwROWS]) or
- (Separator = sepEnd) then
- begin
- case Keyword of
- kwWHERE: ParsePart := ppWhere;
- kwGROUP: ParsePart := ppGroup;
- kwORDER: ParsePart := ppOrder;
- else ParsePart := ppBogus;
- end;
- Result.WhereStartPos := PhraseP-PSQL+1;
- PStatementPart := CurrentP;
- end
- else
- // joined table or user_defined_function (...)
- if (Keyword = kwJOIN) or (Separator in [sepComma, sepParentheses]) then
- begin
- Result.TableName := '';
- Result.Updateable := False;
- end;
- end;
- ppWhere : begin
- if (Keyword in [kwGROUP, kwORDER, kwLIMIT, kwROWS]) or
- (Separator = sepEnd) then
- begin
- ParsePart := ppBogus;
- Result.WhereStartPos := PStatementPart-PSQL;
- if (Separator = sepEnd) then
- Result.WhereStopPos := CurrentP-PSQL+1
- else
- Result.WhereStopPos := PhraseP-PSQL+1;
- end
- else if (Keyword = kwUNION) then
- begin
- ParsePart := ppBogus;
- Result.Updateable := False;
- end;
- end;
- end; {case}
- end;
- if Separator in [sepComment, sepParentheses, sepDoubleQuote] then
- dec(CurrentP);
- PhraseP := CurrentP+1;
- end
- until CurrentP^=#0;
- end;
- function TSQLConnection.GetAsString(Param: TParam): RawByteString;
- begin
- // converts parameter value to connection charset
- if FCodePage = CP_UTF8 then
- Result := Param.AsUTF8String
- else if (FCodePage = DefaultSystemCodePage) or
- (FCodePage = CP_ACP) or (FCodePage = CP_NONE) then
- Result := Param.AsAnsiString
- else
- begin
- Result := Param.AsAnsiString;
- SetCodePage(Result, FCodePage, True);
- end;
- end;
- function TSQLConnection.GetAsSQLText(Field : TField) : string;
- begin
- if (not assigned(Field)) or Field.IsNull then Result := 'Null'
- else case Field.DataType of
- ftString : Result := QuotedStr(Field.AsString);
- ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd',Field.AsDateTime,FSQLFormatSettings) + '''';
- ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Field.AsDateTime,FSQLFormatSettings) + '''';
- ftTime : Result := '''' + TimeIntervalToString(Field.AsDateTime) + '''';
- else
- Result := Field.AsString;
- end; {case}
- end;
- function TSQLConnection.GetAsSQLText(Param: TParam) : string;
- begin
- if (not assigned(Param)) or Param.IsNull then Result := 'Null'
- else case Param.DataType of
- ftGuid,
- ftMemo,
- ftFixedChar,
- ftString : Result := QuotedStr(GetAsString(Param));
- ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd', Param.AsDateTime, FSQLFormatSettings) + '''';
- ftTime : Result := '''' + TimeIntervalToString(Param.AsDateTime) + '''';
- ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Param.AsDateTime, FSQLFormatSettings) + '''';
- ftCurrency,
- ftBcd : Result := CurrToStr(Param.AsCurrency, FSQLFormatSettings);
- ftFloat : Result := FloatToStr(Param.AsFloat, FSQLFormatSettings);
- ftFMTBcd : Result := StringReplace(Param.AsString, DefaultFormatSettings.DecimalSeparator, FSQLFormatSettings.DecimalSeparator, []);
- else
- Result := Param.AsString;
- end; {case}
- end;
- function TSQLConnection.GetHandle: pointer;
- begin
- Result := nil;
- end;
- function TSQLConnection.LogEvent(EventType: TDBEventType): Boolean;
- begin
- Result:=(Assigned(FOnLog) or Assigned(GlobalDBLogHook)) and (EventType in LogEvents);
- end;
- procedure TSQLConnection.LogParams(const AParams: TParams);
- Var
- S : String;
- P : TParam;
- begin
- if not LogEvent(detParamValue) or not Assigned(AParams) then
- Exit;
- For P in AParams do
- begin
- if P.IsNull then
- S:='<NULL>'
- else if (P.DataType in ftBlobTypes) and not (P.DataType in [ftMemo, ftFmtMemo,ftWideMemo]) then
- S:='<BLOB>'
- else
- S:=P.AsString;
- Log(detParamValue,Format(SLogParamValue,[P.Name,S]));
- end;
- end;
- procedure TSQLConnection.Log(EventType: TDBEventType; const Msg: String);
- Var
- M : String;
- begin
- If LogEvent(EventType) then
- begin
- If Assigned(FonLog) then
- FOnLog(Self,EventType,Msg);
- If Assigned(GlobalDBLogHook) then
- begin
- If (Name<>'') then
- M:=Name+' : '+Msg
- else
- M:=ClassName+' : '+Msg;
- GlobalDBLogHook(Self,EventType,M);
- end;
- end;
- end;
- procedure TSQLConnection.RegisterStatement(S: TCustomSQLStatement);
- begin
- FStatements.Add(S);
- end;
- procedure TSQLConnection.UnRegisterStatement(S: TCustomSQLStatement);
- begin
- if Assigned(FStatements) then // Can be nil, when we are destroying and datasets are uncoupled.
- FStatements.Remove(S);
- end;
- function TSQLConnection.CreateCustomQuery(aOwner : TComponent) : TCustomSQLQuery;
- begin
- Result:=TCustomSQLQuery.Create(AOwner);
- end;
- function TSQLConnection.InitialiseUpdateStatement(Query : TCustomSQLQuery; var qry : TCustomSQLQuery): TCustomSQLQuery;
- begin
- if not assigned(qry) then
- begin
- qry := TCustomSQLQuery(TComponentClass(Query.ClassType).Create(Nil));
- qry.ParseSQL := False;
- qry.DataBase := Self;
- qry.Transaction := Query.SQLTransaction;
- qry.Unidirectional:=True;
- qry.UsePrimaryKeyAsKey:=False;
- qry.PacketRecords:=1;
- end;
- Result:=qry;
- end;
- procedure TSQLConnection.AddFieldToUpdateWherePart(var sql_where : string;UpdateMode : TUpdateMode; F : TField);
- begin
- if (pfInKey in F.ProviderFlags)
- or ((UpdateMode = upWhereAll) and (pfInWhere in F.ProviderFlags))
- or ((UpdateMode = UpWhereChanged) and (pfInWhere in F.ProviderFlags) and (F.Value <> F.OldValue)) then
- begin
- if (sql_where<>'') then
- sql_where:=sql_where + ' and ';
- sql_where:= sql_where + '(' + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1];
- if F.OldValue = NULL then
- sql_where := sql_where + ' is null '
- else
- sql_where := sql_where +'= :"' + 'OLD_' + F.FieldName + '"';
- sql_where:=sql_where+') ';
- end;
- end;
- function TSQLConnection.ConstructInsertSQL(Query: TCustomSQLQuery;
- var ReturningClause: Boolean): string;
- var x : integer;
- sql_fields : string;
- sql_values : string;
- returning_fields : String;
- F : TField;
- begin
- sql_fields := '';
- sql_values := '';
- returning_fields := '';
- for x := 0 to Query.Fields.Count -1 do
- begin
- F:=Query.Fields[x];
- if (not F.IsNull) and (pfInUpdate in F.ProviderFlags) and (not F.ReadOnly) then
- begin
- sql_fields := sql_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
- sql_values := sql_values + ':"' + F.FieldName + '",';
- end;
- if ReturningClause and (pfRefreshOnInsert in F.ProviderFlags) then
- returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
- end;
- if length(sql_fields) = 0 then
- DatabaseErrorFmt(sNoUpdateFields,['insert'],self);
- setlength(sql_fields,length(sql_fields)-1);
- setlength(sql_values,length(sql_values)-1);
- result := 'insert into ' + Query.FTableName + ' (' + sql_fields + ') values (' + sql_values + ')';
- if ReturningClause then
- begin
- ReturningClause:=length(returning_fields) <> 0 ;
- if ReturningClause then
- begin
- setlength(returning_fields,length(returning_fields)-1);
- Result := Result + ' returning ' + returning_fields;
- end;
- end;
- end;
- function TSQLConnection.ConstructUpdateSQL(Query: TCustomSQLQuery;
- var ReturningClause: Boolean): string;
- var x : integer;
- F : TField;
- sql_set : string;
- sql_where : string;
- returning_fields : String;
- begin
- sql_set := '';
- sql_where := '';
- returning_fields := '';
- for x := 0 to Query.Fields.Count -1 do
- begin
- F:=Query.Fields[x];
- AddFieldToUpdateWherePart(sql_where,Query.UpdateMode,F);
- if (pfInUpdate in F.ProviderFlags) and (not F.ReadOnly) then
- sql_set := sql_set +FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] +'=:"' + F.FieldName + '",';
- if ReturningClause and (pfRefreshOnUpdate in F.ProviderFlags) then
- returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
- end;
- if length(sql_set) = 0 then DatabaseErrorFmt(sNoUpdateFields,['update'],self);
- setlength(sql_set,length(sql_set)-1);
- if length(sql_where) = 0 then DatabaseErrorFmt(sNoWhereFields,['update'],self);
- result := 'update ' + Query.FTableName + ' set ' + sql_set + ' where ' + sql_where;
- if ReturningClause then
- begin
- ReturningClause:=length(returning_fields) <> 0 ;
- if ReturningClause then
- begin
- setlength(returning_fields,length(returning_fields)-1);
- Result := Result + ' returning ' + returning_fields;
- end;
- end;
- end;
- function TSQLConnection.ConstructDeleteSQL(Query : TCustomSQLQuery) : string;
- var
- x : integer;
- sql_where : string;
- begin
- sql_where := '';
- for x := 0 to Query.Fields.Count -1 do
- AddFieldToUpdateWherePart(sql_where,Query.UpdateMode, Query.Fields[x]);
- if length(sql_where) = 0 then
- DatabaseErrorFmt(sNoWhereFields,['delete'],self);
- result := 'delete from ' + Query.FTableName + ' where ' + sql_where;
- end;
- function TSQLConnection.ConstructRefreshSQL(Query: TCustomSQLQuery; UpdateKind: TUpdateKind): string;
- Var
- F : TField;
- PF : TProviderFlag;
- Where : String;
- begin
- Result:=Trim(Query.RefreshSQL.Text);
- if (Result='') then
- begin
- Where:='';
- PF:=RefreshFlags[UpdateKind];
- For F in Query.Fields do
- begin
- if PF in F.ProviderFlags then
- begin
- if (Result<>'') then
- Result:=Result+', ';
- if (F.Origin<>'') and (F.Origin<>F.FieldName) then
- Result:=Result+F.Origin+' AS '+F.FieldName
- else
- Result:=Result+FieldNameQuoteChars[0]+F.FieldName+FieldNameQuoteChars[1]
- end;
- if pfInkey in F.ProviderFlags then
- begin
- if (Where<>'') then
- Where:=Where+' AND ';
- Where:=Where+'('+FieldNameQuoteChars[0]+F.FieldName+FieldNameQuoteChars[1]+' = :'+F.FieldName+')';
- end;
- end;
- if (Where='') then
- DatabaseError(SErrNoKeyFieldForRefreshClause,Query);
- Result:='SELECT '+Result+' FROM '+Query.FTableName+' WHERE '+Where;
- end;
- end;
- procedure TSQLConnection.ApplyFieldUpdate(C : TSQLCursor; P : TSQLDBParam; F : TField; UseOldValue : Boolean);
- begin
- if UseOldValue then
- P.AssignFieldValue(F,F.OldValue)
- else
- P.AssignFieldValue(F,F.Value);
- P.FFieldDef:=F.FieldDef;
- end;
- procedure TSQLConnection.ApplyRecUpdate(Query: TCustomSQLQuery; UpdateKind: TUpdateKind);
- var
- qry : TCustomSQLQuery;
- s : string;
- x : integer;
- Fld : TField;
- Par, P : TParam;
- UseOldValue, HasReturningClause : Boolean;
- begin
- qry:=Nil;
- HasReturningClause:=(sqSupportReturning in ConnOptions) and not (sqoRefreshUsingSelect in Query.Options) and (Trim(Query.RefreshSQL.Text)='');
- case UpdateKind of
- ukInsert : begin
- s := Trim(Query.FInsertSQL.Text);
- if s = '' then
- s := ConstructInsertSQL(Query, HasReturningClause)
- else
- HasReturningClause := False;
- qry := InitialiseUpdateStatement(Query, Query.FInsertQry);
- end;
- ukModify : begin
- s := Trim(Query.FUpdateSQL.Text);
- if s = '' then begin
- //if not assigned(Query.FUpdateQry) or (Query.UpdateMode<>upWhereKeyOnly) then // first time or dynamic where part
- s := ConstructUpdateSQL(Query, HasReturningClause);
- end
- else
- HasReturningClause := False;
- qry := InitialiseUpdateStatement(Query, Query.FUpdateQry);
- end;
- ukDelete : begin
- s := Trim(Query.FDeleteSQL.Text);
- if (s='') and (not assigned(Query.FDeleteQry) or (Query.UpdateMode<>upWhereKeyOnly)) then
- s := ConstructDeleteSQL(Query);
- HasReturningClause := False;
- qry := InitialiseUpdateStatement(Query, Query.FDeleteQry);
- end;
- end;
- if (s<>'') and (qry.SQL.Text<>s) then
- qry.SQL.Text:=s; //assign only when changed, to avoid UnPrepare/Prepare
- Assert(qry.SQL.Text<>'');
- for x:=0 to Qry.Params.Count-1 do
- begin
- P:=Qry.Params[x];
- S:=P.Name;
- UseOldValue:=SameText(Copy(S,1,4),'OLD_');
- if UseOldValue then
- begin
- Delete(S,1,4);
- Fld:=Query.FieldByName(S);
- end
- else
- Fld:=Query.FindField(S);
- if Assigned(Fld) then
- ApplyFieldUpdate(Query.Cursor, P as TSQLDBParam, Fld, UseOldValue)
- else
- begin
- // if does not exists field with given name, try look for param
- Par:=Query.Params.FindParam(S);
- if Assigned(Par) then
- P.Assign(Par)
- else
- DatabaseErrorFmt(SFieldNotFound,[S],Query); // same error as raised by FieldByName()
- end;
- end;
- if HasReturningClause then
- begin
- Qry.Close;
- Qry.Open
- end
- else
- Qry.Execute;
- if (scoApplyUpdatesChecksRowsAffected in Options) and (Qry.RowsAffected<>1) then
- begin
- Qry.Close;
- DatabaseErrorFmt(SErrFailedToUpdateRecord, [Qry.RowsAffected], Query);
- end;
- if HasReturningClause then
- Query.ApplyReturningResult(Qry,UpdateKind);
- end;
- function TSQLConnection.RefreshLastInsertID(Query: TCustomSQLQuery; Field: TField): Boolean;
- begin
- Result:=False;
- end;
- procedure TSQLConnection.FreeFldBuffers(cursor: TSQLCursor);
- begin
- // empty
- end;
- function TSQLConnection.StartImplicitTransaction(trans: TSQLHandle; aParams: string): boolean;
- begin
- Result:=False;
- end;
- function TSQLConnection.GetSchemaInfoSQL( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
- begin
- case SchemaType of
- stTables : Result := 'SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=''BASE TABLE''';
- stColumns : Result := 'SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='+QuotedStr(SchemaObjectName);
- stProcedures: Result := 'SELECT *, ROUTINE_NAME AS PROCEDURE_NAME FROM INFORMATION_SCHEMA.ROUTINES';
- stSchemata : Result := 'SELECT * FROM INFORMATION_SCHEMA.SCHEMATA';
- stSequences : Result := 'SELECT * FROM INFORMATION_SCHEMA.SEQUENCES';
- else DatabaseError(SMetadataUnavailable);
- end;
- end;
- function TSQLConnection.GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string;
- begin
- Result := 'SELECT NEXT VALUE FOR ' + SequenceName;
- end;
- function TSQLConnection.GetNextValue(const SequenceName: string; IncrementBy: integer): Int64;
- var
- Q: TCustomSQLQuery;
- begin
- Result := 0;
- Q := TCustomSQLQuery.Create(nil);
- try
- Q.DataBase := Self;
- Q.Transaction := Transaction;
- Q.SQL.Text := GetNextValueSQL(SequenceName, IncrementBy);
- Q.Open;
- if not Q.Eof then
- Result := Q.Fields[0].AsLargeInt;
- Q.Close;
- finally
- FreeAndNil(Q);
- end;
- end;
- procedure TSQLConnection.MaybeConnect;
- begin
- If Not Connected then
- begin
- If (scoExplicitConnect in Options) then
- DatabaseErrorFmt(SErrImplicitConnect,[Name]);
- Connected:=True;
- end;
- end;
- procedure TSQLConnection.CreateDB;
- begin
- DatabaseError(SNotSupported);
- end;
- procedure TSQLConnection.DropDB;
- begin
- DatabaseError(SNotSupported);
- end;
- { TSQLTransaction }
- constructor TSQLTransaction.Create(AOwner : TComponent);
- begin
- inherited Create(AOwner);
- FParams := TStringList.Create;
- Action := caRollBack;
- end;
- destructor TSQLTransaction.Destroy;
- begin
- EndTransaction;
- FreeAndNil(FTrans);
- FreeAndNil(FParams);
- inherited Destroy;
- end;
- procedure TSQLTransaction.EndTransaction;
- begin
- Case Action of
- caCommit, caCommitRetaining :
- Commit;
- caNone,
- caRollback, caRollbackRetaining :
- if not (stoUseImplicit in Options) then
- RollBack
- else
- CloseTrans;
- end;
- end;
- procedure TSQLTransaction.SetParams(const AValue: TStringList);
- begin
- FParams.Assign(AValue);
- end;
- function TSQLTransaction.GetSQLConnection: TSQLConnection;
- begin
- Result:=Database as TSQLConnection;
- end;
- procedure TSQLTransaction.SetOptions(AValue: TSQLTransactionOptions);
- begin
- if FOptions=AValue then Exit;
- if (stoUseImplicit in Avalue) and Assigned(SQLConnection) And Not (sqImplicitTransaction in SQLConnection.ConnOptions) then
- DatabaseErrorFmt(SErrNoImplicitTransaction, [SQLConnection.ClassName]);
- FOptions:=AValue;
- end;
- procedure TSQLTransaction.SetSQLConnection(AValue: TSQLConnection);
- begin
- Database:=AValue;
- end;
- Procedure TSQLTransaction.MaybeStartTransaction;
- begin
- if not Active then
- begin
- if (stoExplicitStart in Options) then
- DatabaseErrorFmt(SErrImplictTransactionStart, [Database.Name,Name]);
- StartTransaction;
- end;
- end;
- function TSQLTransaction.GetHandle: Pointer;
- begin
- Result := SQLConnection.GetTransactionHandle(FTrans);
- end;
- Function TSQLTransaction.AllowClose(DS: TDBDataset): Boolean;
- begin
- if (DS is TSQLQuery) then
- Result:=not (sqoKeepOpenOnCommit in TSQLQuery(DS).Options)
- else
- Result:=Inherited AllowClose(DS);
- end;
- procedure TSQLTransaction.Commit;
- begin
- if Active then
- begin
- CloseDataSets;
- If LogEvent(detCommit) then
- Log(detCommit,SCommitting);
- // FTrans=Nil for the case of forced close.
- if (FTrans=Nil) or (stoUseImplicit in Options) or SQLConnection.AttemptCommit(FTrans) then
- begin
- CloseTrans;
- FreeAndNil(FTrans);
- end;
- end;
- end;
- procedure TSQLTransaction.CommitRetaining;
- begin
- if Active then
- begin
- If LogEvent(detCommit) then
- Log(detCommit,SCommitRetaining);
- SQLConnection.CommitRetaining(FTrans);
- end;
- end;
- procedure TSQLTransaction.Rollback;
- begin
- if Active then
- begin
- if (stoUseImplicit in Options) then
- DatabaseError(SErrImplicitNoRollBack);
- CloseDataSets;
- If LogEvent(detRollback) then
- Log(detRollback,SRollingBack);
- // FTrans=Nil for the case of forced close.
- if (FTrans=Nil) or SQLConnection.AttemptRollBack(FTrans) then
- begin
- CloseTrans;
- FreeAndNil(FTrans);
- end;
- end;
- end;
- procedure TSQLTransaction.RollbackRetaining;
- begin
- if Active then
- begin
- if (stoUseImplicit in Options) then
- DatabaseError(SErrImplicitNoRollBack);
- If LogEvent(detRollback) then
- Log(detRollback,SRollBackRetaining);
- SQLConnection.RollBackRetaining(FTrans);
- end;
- end;
- procedure TSQLTransaction.StartTransaction;
- var db : TSQLConnection;
- begin
- if Active then
- DatabaseError(SErrTransAlreadyActive);
- db := SQLConnection;
- if Db = nil then
- DatabaseError(SErrDatabasenAssigned);
- Db.MaybeConnect;
- if not assigned(FTrans) then FTrans := Db.AllocateTransactionHandle;
- if (stoUseImplicit in Options) then
- begin
- if Db.StartImplicitTransaction(FTrans,FParams.CommaText) then
- OpenTrans
- end
- else
- begin
- if Db.StartDBTransaction(FTrans,FParams.CommaText) then
- OpenTrans
- end;
- end;
- Procedure TSQLTransaction.SetDatabase(Value: TDatabase);
- begin
- If Value<>Database then
- begin
- if Assigned(Value) and not (Value is TSQLConnection) then
- DatabaseErrorFmt(SErrNotASQLConnection, [Value.Name], Self);
- CheckInactive;
- if (stoUseImplicit in Options) and Assigned(Value) and Not (sqImplicitTransaction in TSQLConnection(Value).ConnOptions) then
- DatabaseErrorFmt(SErrNoImplicitTransaction, [Value.ClassName]);
- If Assigned(Database) then
- if SQLConnection.Transaction = Self then SQLConnection.Transaction := nil;
- inherited;
- If Assigned(Database) and not (csLoading in ComponentState) then
- If SQLConnection.Transaction = Nil then SQLConnection.Transaction := Self;
- end;
- end;
- Function TSQLTransaction.LogEvent(EventType: TDBEventType): Boolean;
- begin
- Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
- end;
- Procedure TSQLTransaction.Log(EventType: TDBEventType; Const Msg: String);
- Var
- M : String;
- begin
- If LogEvent(EventType) then
- begin
- If (Name<>'') then
- M:=Name+' : '+Msg
- else
- M:=Msg;
- SQLConnection.Log(EventType,M);
- end;
- end;
- { TSQLSequence }
- constructor TSQLSequence.Create(AQuery: TCustomSQLQuery);
- begin
- inherited Create;
- FQuery := AQuery;
- FApplyEvent := saeOnNewRecord;
- FIncrementBy := 1;
- end;
- procedure TSQLSequence.Assign(Source: TPersistent);
- var SourceSequence: TSQLSequence;
- begin
- if Source is TSQLSequence then
- begin
- SourceSequence := TSQLSequence(Source);
- FFieldName := SourceSequence.FieldName;
- FSequenceName := SourceSequence.SequenceName;
- FIncrementBy := SourceSequence.IncrementBy;
- FApplyEvent := SourceSequence.ApplyEvent;
- end
- else
- inherited;
- end;
- procedure TSQLSequence.Apply;
- var Field: TField;
- begin
- if Assigned(FQuery) and (FSequenceName<>'') and (FFieldName<>'') then
- begin
- Field := FQuery.FindField(FFieldName);
- if Assigned(Field) and Field.IsNull then
- Field.AsLargeInt := GetNextValue;
- end;
- end;
- function TSQLSequence.GetNextValue: Int64;
- begin
- if (FQuery=Nil) or (FQuery.SQLConnection=Nil) then
- DatabaseError(SErrDatabasenAssigned);
- Result := FQuery.SQLConnection.GetNextValue(FSequenceName, FIncrementBy);
- end;
- Type
- { TQuerySQLStatement }
- TQuerySQLStatement = Class(TCustomSQLStatement)
- protected
- FQuery : TCustomSQLQuery;
- function CreateParams: TSQLDBParams; override;
- Function CreateDataLink : TDataLink; override;
- Function GetSchemaType : TSchemaType; override;
- Function GetSchemaObjectName : String; override;
- Function GetSchemaPattern: String; override;
- procedure GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo); override;
- procedure OnChangeSQL(Sender : TObject); override;
- public
- constructor Create(AOwner: TComponent); override;
- end;
- { TQuerySQLStatement }
- constructor TQuerySQLStatement.Create(AOwner: TComponent);
- begin
- FQuery:=TCustomSQLQuery(AOwner);
- inherited Create(AOwner);
- end;
- function TQuerySQLStatement.CreateDataLink: TDataLink;
- begin
- Result:=TMasterParamsDataLink.Create(FQuery);
- end;
- function TQuerySQLStatement.CreateParams: TSQLDBParams;
- begin
- Result:=FQuery.CreateParams;
- end;
- function TQuerySQLStatement.GetSchemaType: TSchemaType;
- begin
- if Assigned(FQuery) then
- Result:=FQuery.FSchemaType
- else
- Result:=stNoSchema;
- end;
- function TQuerySQLStatement.GetSchemaObjectName: String;
- begin
- if Assigned(FQuery) then
- Result:=FQuery.FSchemaObjectname
- else
- Result:=inherited GetSchemaObjectName;
- end;
- function TQuerySQLStatement.GetSchemaPattern: String;
- begin
- if Assigned(FQuery) then
- Result:=FQuery.FSchemaPattern
- else
- Result:=inherited GetSchemaPattern;
- end;
- procedure TQuerySQLStatement.GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo);
- begin
- inherited GetStatementInfo(ASQL, Info);
- If Assigned(FQuery) then
- // Note: practical side effect of switch off ParseSQL is that UpdateServerIndexDefs is bypassed
- // which is used as performance tuning option
- if (FQuery.FSchemaType = stNoSchema) and FParseSQL then
- begin
- FQuery.FUpdateable:=Info.Updateable;
- FQuery.FTableName:=Info.TableName;
- FQuery.FWhereStartPos:=Info.WhereStartPos;
- FQuery.FWhereStopPos:=Info.WhereStopPos;
- if FQuery.ServerFiltered then
- ASQL:=FQuery.AddFilter(ASQL);
- end
- else
- begin
- FQuery.FUpdateable:=false;
- FQuery.FTableName:='';
- FQuery.FWhereStartPos:=0;
- FQuery.FWhereStopPos:=0;
- end;
- end;
- procedure TQuerySQLStatement.OnChangeSQL(Sender: TObject);
- begin
- UnPrepare;
- inherited OnChangeSQL(Sender);
- If ParamCheck and Assigned(FDataLink) then
- (FDataLink as TMasterParamsDataLink).RefreshParamNames;
- FQuery.ServerIndexDefs.Updated:=false;
- end;
- { TCustomSQLQuery }
- Function TCustomSQLQuery.CreateSQLStatement(aOwner : TComponent) : TCustomSQLStatement;
- begin
- Result:=TQuerySQLStatement.Create(Self);
- end;
- constructor TCustomSQLQuery.Create(AOwner : TComponent);
- begin
- inherited Create(AOwner);
- FStatement:=CreateSQLStatement(Self);
- FInsertSQL := TStringList.Create;
- FInsertSQL.OnChange := @OnChangeModifySQL;
- FUpdateSQL := TStringList.Create;
- FUpdateSQL.OnChange := @OnChangeModifySQL;
- FDeleteSQL := TStringList.Create;
- FDeleteSQL.OnChange := @OnChangeModifySQL;
- FRefreshSQL := TStringList.Create;
- FRefreshSQL.OnChange := @OnChangeModifySQL;
- FSequence := TSQLSequence.Create(Self);
- FServerIndexDefs := TServerIndexDefs.Create(Self);
- FServerFiltered := False;
- FServerFilterText := '';
- FSchemaType:=stNoSchema;
- FSchemaObjectName:='';
- FSchemaPattern:='';
- // Delphi has upWhereAll as default, but since strings and oldvalue's don't work yet
- // (variants) set it to upWhereKeyOnly
- FUpdateMode := upWhereKeyOnly;
- FUsePrimaryKeyAsKey := True;
- end;
- destructor TCustomSQLQuery.Destroy;
- begin
- if Active then Close;
- UnPrepare;
- FreeAndNil(FStatement);
- FreeAndNil(FInsertSQL);
- FreeAndNil(FUpdateSQL);
- FreeAndNil(FDeleteSQL);
- FreeAndNil(FRefreshSQL);
- FreeAndNil(FSequence);
- FreeAndNil(FServerIndexDefs);
- inherited Destroy;
- end;
- function TCustomSQLQuery.ParamByName(const AParamName: String): TParam;
- begin
- Result:=Params.ParamByName(AParamName);
- end;
- procedure TCustomSQLQuery.OnChangeModifySQL(Sender : TObject);
- begin
- CheckInactive;
- end;
- procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
- var DB : TSQLConnection;
- begin
- if Database = Value then Exit;
- if Assigned(Value) and not (Value is TSQLConnection) then
- DatabaseErrorFmt(SErrNotASQLConnection, [Value.Name], Self);
- UnPrepare;
- DB := TSQLConnection(Value);
- If Assigned(FStatement) then
- FStatement.Database := DB;
- inherited;
- if Assigned(DB) and Assigned(DB.Transaction) and (not Assigned(Transaction) or (Transaction.DataBase<>Database)) then
- Transaction := DB.Transaction;
- end;
- procedure TCustomSQLQuery.SetTransaction(Value: TDBTransaction);
- begin
- if Transaction = Value then Exit;
- UnPrepare;
- inherited;
- If Assigned(FStatement) then
- FStatement.Transaction := TSQLTransaction(Value);
- If Assigned(Transaction) and Assigned(Transaction.DataBase) and (Database<>Transaction.DataBase) then
- Database := Transaction.Database;
- end;
- function TCustomSQLQuery.IsPrepared: Boolean;
- begin
- if Assigned(Fstatement) then
- Result := FStatement.Prepared
- else
- Result := False;
- end;
- function TCustomSQLQuery.AddFilter(SQLstr: string): string;
- begin
- if (FWhereStartPos > 0) and (FWhereStopPos > 0) then
- begin
- system.insert('(',SQLstr,FWhereStartPos+1);
- system.insert(')',SQLstr,FWhereStopPos+1);
- end;
- if FWhereStartPos = 0 then
- SQLstr := SQLstr + ' where (' + ServerFilter + ')'
- else if FWhereStopPos > 0 then
- system.insert(' and ('+ServerFilter+') ',SQLstr,FWhereStopPos+2)
- else
- system.insert(' where ('+ServerFilter+') ',SQLstr,FWhereStartPos);
- Result := SQLstr;
- end;
- function TCustomSQLQuery.NeedRefreshRecord(UpdateKind: TUpdateKind): Boolean;
- Var
- PF : TProviderFlag;
- I : Integer;
- DoReturning : Boolean;
- begin
- Result:=(Trim(FRefreshSQL.Text)<>'');
- DoReturning:=(sqSupportReturning in SQLConnection.ConnOptions) and not (sqoRefreshUsingSelect in Options);
- if Not (Result or DoReturning) then
- begin
- PF:=RefreshFlags[UpdateKind];
- I:=0;
- While (Not Result) and (I<Fields.Count) do
- begin
- Result:=PF in Fields[i].ProviderFlags;
- Inc(I);
- end;
- end;
- end;
- function TCustomSQLQuery.RefreshRecord(UpdateKind: TUpdateKind): Boolean;
- Var
- Q : TCustomSQLQuery;
- P : TParam;
- F,FD : TField;
- N : String;
- begin
- Result:=False;
- Q:=TCustomSQLQuery.Create(Nil);
- try
- Q.Database:=Self.Database;
- Q.Transaction:=Self.Transaction;
- Q.SQL.Text:=SQLConnection.ConstructRefreshSQL(Self,UpdateKind);
- For P in Q.Params do
- begin
- N:=P.Name;
- If CompareText(Copy(N,1,4),'OLD_')=0 then
- system.Delete(N,1,4);
- F:=Fields.FindField(N);
- if Assigned(F) then
- P.AssignField(F);
- end;
- Q.Open;
- try
- if (Q.EOF and Q.BOF) then
- DatabaseError(SErrRefreshEmptyResult,Self)
- else
- begin
- if Q.RecordCount<>1 then
- DatabaseErrorFmt(SErrRefreshNotSingleton,[Q.RecordCount],Self);
- For F in Q.Fields do
- begin
- FD:=Fields.FindField(F.FieldName);
- if Assigned(FD) then
- begin
- FD.Assign(F);
- Result:=True; // We could check if the new value differs from the old, but we won't.
- end;
- end;
- end
- finally
- Q.Close;
- end;
- finally
- Q.Free;
- end;
- end;
- procedure TCustomSQLQuery.ApplyReturningResult(Q: TCustomSQLQuery; UpdateKind : TUpdateKind);
- Var
- S : TDataSetState;
- refreshFlag : TProviderFlag;
- F : TField;
- begin
- RefreshFlag:=RefreshFlags[UpdateKind];
- S:=SetTempState(dsRefreshFields);
- try
- For F in Fields do
- if RefreshFlag in F.ProviderFlags then
- F.Assign(Q.FieldByName(F.FieldName));
- finally
- RestoreState(S);
- end;
- end;
- procedure TCustomSQLQuery.ApplyFilter;
- begin
- FStatement.Unprepare;
- FStatement.DoPrepare;
- FStatement.DoExecute;
- InternalRefresh;
- First;
- end;
- procedure TCustomSQLQuery.SetActive(Value: Boolean);
- begin
- inherited SetActive(Value);
- // The query is UnPrepared, so that if a transaction closes all datasets
- // they also get unprepared
- if not Value and IsPrepared then UnPrepare;
- end;
- procedure TCustomSQLQuery.SetServerFiltered(Value: Boolean);
- begin
- if Value and not ParseSQL then
- DatabaseErrorFmt(SNoParseSQL,['Filtering ']);
- if (ServerFiltered <> Value) then
- begin
- FServerFiltered := Value;
- if Active then ApplyFilter;
- end;
- end;
- procedure TCustomSQLQuery.SetServerFilterText(const Value: string);
- begin
- if Value <> ServerFilter then
- begin
- FServerFilterText := Value;
- if Active then ApplyFilter;
- end;
- end;
- procedure TCustomSQLQuery.Prepare;
- begin
- FStatement.Prepare;
- if Assigned(FStatement.FCursor) then
- with FStatement.FCursor do
- FInitFieldDef := FSelectable;
- end;
- procedure TCustomSQLQuery.UnPrepare;
- begin
- CheckInactive;
- If Assigned(FStatement) then
- FStatement.Unprepare;
- end;
- procedure TCustomSQLQuery.FreeFldBuffers;
- begin
- if assigned(Cursor) then
- SQLConnection.FreeFldBuffers(Cursor);
- end;
- function TCustomSQLQuery.GetParamCheck: Boolean;
- begin
- Result:=FStatement.ParamCheck;
- end;
- function TCustomSQLQuery.GetParams: TParams;
- begin
- Result:=FStatement.Params;
- end;
- function TCustomSQLQuery.GetParseSQL: Boolean;
- begin
- Result:=FStatement.ParseSQL;
- end;
- function TCustomSQLQuery.GetServerIndexDefs: TServerIndexDefs;
- begin
- Result := FServerIndexDefs;
- end;
- function TCustomSQLQuery.GetSQL: TStringList;
- begin
- Result:=TStringList(Fstatement.SQL);
- end;
- function TCustomSQLQuery.GetSQLConnection: TSQLConnection;
- begin
- Result:=Database as TSQLConnection;
- end;
- function TCustomSQLQuery.GetSQLTransaction: TSQLTransaction;
- begin
- Result:=Transaction as TSQLTransaction;
- end;
- function TCustomSQLQuery.Cursor: TSQLCursor;
- begin
- Result:=FStatement.Cursor;
- end;
- function TCustomSQLQuery.Fetch : boolean;
- begin
- if Not Assigned(Cursor) then
- Exit;
- if not Cursor.FSelectable then
- Exit;
- If LogEvent(detFetch) then
- Log(detFetch,FStatement.FServerSQL);
- if not FIsEof then FIsEOF := not SQLConnection.Fetch(Cursor);
- Result := not FIsEOF;
- end;
- procedure TCustomSQLQuery.Execute;
- begin
- FStatement.Execute;
- end;
- function TCustomSQLQuery.RowsAffected: TRowsCount;
- begin
- Result:=FStatement.RowsAffected;
- end;
- function TCustomSQLQuery.LoadField(FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean;
- begin
- Result := SQLConnection.LoadField(Cursor, FieldDef, buffer, CreateBlob);
- // disable deferred blob loading for "disconnected" datasets
- if Result and (FieldDef.DataType in ftBlobTypes) and (sqoKeepOpenOnCommit in Options) then
- CreateBlob:=True
- end;
- procedure TCustomSQLQuery.LoadBlobIntoBuffer(FieldDef: TFieldDef;
- ABlobBuf: PBufBlobField);
- begin
- SQLConnection.LoadBlobIntoBuffer(FieldDef, ABlobBuf, Cursor,SQLTransaction);
- end;
- procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
- begin
- // not implemented - sql dataset
- end;
- procedure TCustomSQLQuery.InternalClose;
- begin
- if assigned(Cursor) then
- begin
- if Cursor.FSelectable then
- FreeFldBuffers;
- // Some SQLConnections does not support statement [un]preparation,
- // so let them do cleanup f.e. cancel pending queries and/or free resultset
- if not Prepared then FStatement.DoUnprepare;
- end;
- if DefaultFields then
- DestroyFields;
- FIsEOF := False;
- if assigned(FUpdateQry) then FreeAndNil(FUpdateQry);
- if assigned(FInsertQry) then FreeAndNil(FInsertQry);
- if assigned(FDeleteQry) then FreeAndNil(FDeleteQry);
- // FRecordSize := 0;
- inherited InternalClose;
- end;
- procedure TCustomSQLQuery.InternalInitFieldDefs;
- begin
- if FLoadingFieldDefs then
- Exit;
- FLoadingFieldDefs := True;
- try
- FieldDefs.Clear;
- Prepare;
- SQLConnection.AddFieldDefs(Cursor,FieldDefs);
- finally
- FLoadingFieldDefs := False;
- if assigned(Cursor) then Cursor.FInitFieldDef := False;
- end;
- end;
- procedure TCustomSQLQuery.InternalOpen;
- var counter, fieldc : integer;
- F : TField;
- IndexFields : TStrings;
- begin
- if IsReadFromPacket then
- begin
- // When we read from file there is no need for Cursor, also note that Database may not be assigned
- //FStatement.AllocateCursor;
- //Cursor.FSelectable:=True;
- //Cursor.FStatementType:=stSelect;
- FUpdateable:=True;
- end
- else
- begin
- Prepare;
- if not Cursor.FSelectable then
- DatabaseError(SErrNoSelectStatement,Self);
- // Call UpdateServerIndexDefs before Execute, to avoid problems with connections
- // which do not allow processing multiple recordsets at a time. (Microsoft
- // calls this MARS, see bug 13241)
- if DefaultFields and FUpdateable and FusePrimaryKeyAsKey and (not IsUniDirectional) then
- UpdateServerIndexDefs;
- Execute;
- if not Cursor.FSelectable then
- DatabaseError(SErrNoSelectStatement,Self);
- // InternalInitFieldDef is only called after a prepare. i.e. not twice if
- // a dataset is opened - closed - opened.
- if Cursor.FInitFieldDef then InternalInitFieldDefs;
- if DefaultFields then
- begin
- CreateFields;
- if FUpdateable and FusePrimaryKeyAsKey and (not IsUniDirectional) then
- for counter := 0 to ServerIndexDefs.Count-1 do
- if ixPrimary in ServerIndexDefs[counter].Options then
- begin
- IndexFields := TStringList.Create;
- ExtractStrings([';'],[' '],pchar(ServerIndexDefs[counter].Fields),IndexFields);
- for fieldc := 0 to IndexFields.Count-1 do
- begin
- F := FindField(IndexFields[fieldc]);
- if F <> nil then
- F.ProviderFlags := F.ProviderFlags + [pfInKey];
- end;
- IndexFields.Free;
- end;
- end;
- end;
- BindFields(True);
- if not ReadOnly and not FUpdateable and (FSchemaType=stNoSchema) then
- begin
- if (trim(FDeleteSQL.Text) <> '') or (trim(FUpdateSQL.Text) <> '') or
- (trim(FInsertSQL.Text) <> '') then FUpdateable := True;
- end;
- inherited InternalOpen;
- end;
- procedure TCustomSQLQuery.InternalRefresh;
- begin
- if (ChangeCount>0) and (sqoCancelUpdatesOnRefresh in Options) then
- CancelUpdates;
- inherited InternalRefresh;
- end;
- // public part
- procedure TCustomSQLQuery.ExecSQL;
- begin
- try
- Prepare;
- Execute;
- If sqoAutoCommit in Options then
- begin
- // Retrieve rows affected
- FStatement.RowsAffected;
- SQLTransaction.Commit;
- end;
- finally
- // Cursor has to be assigned, or else the prepare went wrong before PrepareStatment was
- // called, so UnPrepareStatement shoudn't be called either
- // Don't deallocate cursor; f.e. RowsAffected is requested later
- if not Prepared and (assigned(Database)) and (assigned(Cursor)) then SQLConnection.UnPrepareStatement(Cursor);
- end;
- end;
- procedure TCustomSQLQuery.ApplyUpdates(MaxErrors: Integer);
- begin
- inherited ApplyUpdates(MaxErrors);
- If sqoAutoCommit in Options then
- begin
- // Retrieve rows affected for last update.
- FStatement.RowsAffected;
- SQLTransaction.Commit;
- end;
- end;
- procedure TCustomSQLQuery.Post;
- begin
- inherited Post;
- If (sqoAutoApplyUpdates in Options) then
- ApplyUpdates;
- end;
- procedure TCustomSQLQuery.Delete;
- begin
- inherited Delete;
- If (sqoAutoApplyUpdates in Options) then
- ApplyUpdates;
- end;
- procedure TCustomSQLQuery.SetReadOnly(AValue : Boolean);
- begin
- CheckInactive;
- inherited SetReadOnly(AValue);
- end;
- procedure TCustomSQLQuery.SetParseSQL(AValue : Boolean);
- begin
- CheckInactive;
- FStatement.ParseSQL:=AValue;
- if not AValue then
- FServerFiltered := False;
- end;
- procedure TCustomSQLQuery.SetSQL(const AValue: TStringList);
- begin
- FStatement.SQL.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetUsePrimaryKeyAsKey(AValue : Boolean);
- begin
- if not Active then FusePrimaryKeyAsKey := AValue
- else
- begin
- // Just temporary, this should be possible in the future
- DatabaseError(SActiveDataset);
- end;
- end;
- procedure TCustomSQLQuery.UpdateServerIndexDefs;
- begin
- FServerIndexDefs.Clear;
- if assigned(DataBase) and (FTableName<>'') then
- SQLConnection.UpdateIndexDefs(ServerIndexDefs,FTableName);
- end;
- function TCustomSQLQuery.NeedLastInsertID: TField;
- Var
- I : Integer;
- begin
- Result:=Nil;
- if sqLastInsertID in SQLConnection.ConnOptions then
- begin
- I:=0;
- While (Result=Nil) and (I<Fields.Count) do
- begin
- Result:=Fields[i];
- if (Result.DataType<>ftAutoInc) or not Result.IsNull then
- Result:=Nil;
- Inc(I);
- end;
- end
- end;
- function TCustomSQLQuery.RefreshLastInsertID(Field: TField): Boolean;
- begin
- Result:=SQLConnection.RefreshLastInsertID(Self, Field);
- end;
- procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind: TUpdateKind);
- Var
- DoRefresh : Boolean;
- LastIDField : TField;
- S : TDataSetState;
- begin
- // Moved to connection: the SQLConnection always has more information about types etc.
- // than SQLQuery itself.
- SQLConnection.ApplyRecUpdate(Self,UpdateKind);
- if UpdateKind=ukInsert then
- LastIDField:=NeedLastInsertID
- else
- LastIDField:=nil;
- DoRefresh:=(UpdateKind in [ukModify,ukInsert]) and NeedRefreshRecord(UpdateKind);
- if assigned(LastIDField) or DoRefresh then
- begin
- // updates fields directly in record buffer of TBufDataSet
- // TDataSet buffers are resynchronized at end of ApplyUpdates process
- S:=SetTempState(dsRefreshFields);
- try
- if assigned(LastIDField) then
- RefreshLastInsertID(LastIDField);
- if DoRefresh then
- RefreshRecord(UpdateKind);
- finally
- RestoreState(S);
- end;
- end;
- end;
- procedure TCustomSQLQuery.SetPacketRecords(aValue: integer);
- begin
- if (AValue=PacketRecords) then exit;
- if (AValue<>-1) and (sqoKeepOpenOnCommit in Options) then
- DatabaseError(SErrDisconnectedPacketRecords);
- Inherited SetPacketRecords(aValue);
- end;
- function TCustomSQLQuery.GetCanModify: Boolean;
- begin
- // the test for assigned(Cursor) is needed for the case that the dataset isn't opened
- if assigned(Cursor) and (Cursor.FStatementType = stSelect) then
- Result:= FUpdateable and (not ReadOnly) and (not IsUniDirectional)
- else
- Result := False;
- end;
- procedure TCustomSQLQuery.SetUpdateMode(AValue : TUpdateMode);
- begin
- FUpdateMode := AValue;
- end;
- procedure TCustomSQLQuery.SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string);
- begin
- FSchemaType:=ASchemaType;
- FSchemaObjectName:=ASchemaObjectName;
- FSchemaPattern:=ASchemaPattern;
- end;
- procedure TCustomSQLQuery.BeforeRefreshOpenCursor;
- begin
- // This is only necessary because TIBConnection can not re-open a
- // prepared cursor. In fact this is wrong, but has never led to
- // problems because in SetActive(false) queries are always
- // unprepared. (which is also wrong, but has to be fixed later)
- if IsPrepared then with SQLConnection do
- UnPrepareStatement(Cursor);
- end;
- function TCustomSQLQuery.CreateParams: TSQLDBParams;
- begin
- Result:=TSQLDBParams.Create(Nil);
- end;
- function TCustomSQLQuery.LogEvent(EventType: TDBEventType): Boolean;
- begin
- Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
- end;
- procedure TCustomSQLQuery.Log(EventType: TDBEventType; const Msg: String);
- Var
- M : String;
- begin
- If LogEvent(EventType) then
- begin
- M:=Msg;
- If (Name<>'') then
- M:=Name+' : '+M;
- SQLConnection.Log(EventType,M);
- end;
- end;
- class function TCustomSQLQuery.FieldDefsClass: TFieldDefsClass;
- begin
- Result:=TSQLDBFieldDefs;
- end;
- function TCustomSQLQuery.GetStatementType : TStatementType;
- begin
- if Assigned(Cursor) then
- Result:=Cursor.FStatementType
- else
- Result:=stUnknown;
- end;
- procedure TCustomSQLQuery.SetParamCheck(AValue: Boolean);
- begin
- FStatement.ParamCheck:=AValue;
- end;
- procedure TCustomSQLQuery.SetOptions(AValue: TSQLQueryOptions);
- begin
- if FOptions=AValue then Exit;
- CheckInactive;
- FOptions:=AValue;
- if sqoKeepOpenOnCommit in FOptions then
- PacketRecords:=-1;
- end;
- procedure TCustomSQLQuery.SetSQLConnection(AValue: TSQLConnection);
- begin
- Database:=AValue;
- end;
- procedure TCustomSQLQuery.SetSQLTransaction(AValue: TSQLTransaction);
- begin
- Transaction:=AValue;
- end;
- procedure TCustomSQLQuery.SetInsertSQL(const AValue: TStringList);
- begin
- FInsertSQL.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetUpdateSQL(const AValue: TStringList);
- begin
- FUpdateSQL.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetDeleteSQL(const AValue: TStringList);
- begin
- FDeleteSQL.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetRefreshSQL(const AValue: TStringList);
- begin
- FRefreshSQL.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetParams(AValue: TParams);
- begin
- FStatement.Params.Assign(AValue);
- end;
- procedure TCustomSQLQuery.SetDataSource(AValue: TDataSource);
- Var
- DS : TDataSource;
- begin
- DS:=DataSource;
- If (AValue<>DS) then
- begin
- If Assigned(AValue) and (AValue.Dataset=Self) then
- DatabaseError(SErrCircularDataSourceReferenceNotAllowed,Self);
- If Assigned(DS) then
- DS.RemoveFreeNotification(Self);
- FStatement.DataSource:=AValue;
- end;
- end;
- function TCustomSQLQuery.GetDataSource: TDataSource;
- begin
- If Assigned(FStatement) then
- Result:=FStatement.DataSource
- else
- Result:=Nil;
- end;
- procedure TCustomSQLQuery.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- Inherited;
- If (Operation=opRemove) and (AComponent=DataSource) then
- DataSource:=Nil;
- end;
- procedure TCustomSQLQuery.DoOnNewRecord;
- begin
- inherited;
- if FSequence.ApplyEvent = saeOnNewRecord then
- FSequence.Apply;
- end;
- procedure TCustomSQLQuery.DoBeforePost;
- begin
- if (State = dsInsert) and (FSequence.ApplyEvent = saeOnPost) then
- FSequence.Apply;
- inherited;
- end;
- function TCustomSQLQuery.PSGetUpdateException(E: Exception; Prev: EUpdateError): EUpdateError;
- var
- PrevErrorCode, ErrorCode: Integer;
- begin
- if Assigned(Prev) then
- PrevErrorCode := Prev.ErrorCode
- else
- PrevErrorCode := 0;
- if E is ESQLDatabaseError then
- ErrorCode := ESQLDatabaseError(E).ErrorCode
- else
- ErrorCode := 0;
- Result := EUpdateError.Create(SOnUpdateError, E.Message, ErrorCode, PrevErrorCode, E);
- end;
- function TCustomSQLQuery.PSGetTableName: string;
- begin
- Result := FTableName;
- end;
- { TSQLScript }
- procedure TSQLScript.ExecuteStatement(SQLStatement: TStrings;
- var StopExecution: Boolean);
- begin
- fquery.SQL.assign(SQLStatement);
- fquery.ExecSQL;
- end;
- procedure TSQLScript.ExecuteDirective(Directive, Argument: String;
- var StopExecution: Boolean);
- begin
- if assigned (FOnDirective) then
- FOnDirective (Self, Directive, Argument, StopExecution);
- end;
- procedure TSQLScript.ExecuteCommit(CommitRetaining: boolean=true);
- begin
- if FTransaction is TSQLTransaction then
- if CommitRetaining then
- TSQLTransaction(FTransaction).CommitRetaining
- else
- begin
- TSQLTransaction(FTransaction).Commit;
- TSQLTransaction(FTransaction).StartTransaction;
- end
- else
- begin
- FTransaction.Active := false;
- FTransaction.Active := true;
- end;
- end;
- procedure TSQLScript.SetDatabase(Value: TDatabase);
- begin
- FDatabase := Value;
- end;
- procedure TSQLScript.SetTransaction(Value: TDBTransaction);
- begin
- FTransaction := Value;
- end;
- procedure TSQLScript.CheckDatabase;
- begin
- If (FDatabase=Nil) then
- DatabaseError(SErrNoDatabaseAvailable,Self)
- end;
- function TSQLScript.CreateQuery: TCustomSQLQuery;
- begin
- Result := TCustomSQLQuery.Create(nil);
- Result.ParamCheck := false; // Do not parse for parameters; breaks use of e.g. select bla into :bla in Firebird procedures
- end;
- constructor TSQLScript.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FQuery := CreateQuery;
- end;
- destructor TSQLScript.Destroy;
- begin
- FQuery.Free;
- inherited Destroy;
- end;
- procedure TSQLScript.Execute;
- begin
- FQuery.DataBase := FDatabase;
- FQuery.Transaction := FTransaction;
- inherited Execute;
- end;
- procedure TSQLScript.ExecuteScript;
- begin
- Execute;
- end;
- { Connection definitions }
- Var
- ConnDefs : TStringList;
- Procedure CheckDefs;
- begin
- If (ConnDefs=Nil) then
- begin
- ConnDefs:=TStringList.Create;
- ConnDefs.Sorted:=True;
- ConnDefs.Duplicates:=dupError;
- end;
- end;
- Procedure DoneDefs;
- Var
- I : Integer;
- begin
- If Assigned(ConnDefs) then
- begin
- For I:=ConnDefs.Count-1 downto 0 do
- begin
- ConnDefs.Objects[i].Free;
- ConnDefs.Delete(I);
- end;
- FreeAndNil(ConnDefs);
- end;
- end;
- Function GetConnectionDef(ConnectorName : String) : TConnectionDef;
- Var
- I : Integer;
- begin
- CheckDefs;
- I:=ConnDefs.IndexOf(ConnectorName);
- If (I<>-1) then
- Result:=TConnectionDef(ConnDefs.Objects[i])
- else
- Result:=Nil;
- end;
- procedure RegisterConnection(Def: TConnectionDefClass);
- Var
- I : Integer;
- begin
- CheckDefs;
- I:=ConnDefs.IndexOf(Def.TypeName);
- If (I=-1) then
- ConnDefs.AddObject(Def.TypeName,Def.Create)
- else
- begin
- ConnDefs.Objects[I].Free;
- ConnDefs.Objects[I]:=Def.Create;
- end;
- end;
- procedure UnRegisterConnection(Def: TConnectionDefClass);
- begin
- UnRegisterConnection(Def.TypeName);
- end;
- procedure UnRegisterConnection(ConnectionName: String);
- Var
- I : Integer;
- begin
- if (ConnDefs<>Nil) then
- begin
- I:=ConnDefs.IndexOf(ConnectionName);
- If (I<>-1) then
- begin
- ConnDefs.Objects[I].Free;
- ConnDefs.Delete(I);
- end;
- end;
- end;
- procedure GetConnectionList(List: TSTrings);
- begin
- CheckDefs;
- List.Text:=ConnDefs.Text;
- end;
- { TSQLConnector }
- procedure TSQLConnector.SetConnectorType(const AValue: String);
- begin
- if FConnectorType<>AValue then
- begin
- CheckDisconnected;
- If Assigned(FProxy) then
- FreeProxy;
- FConnectorType:=AValue;
- CreateProxy;
- end;
- end;
- procedure TSQLConnector.SetTransaction(Value: TSQLTransaction);
- begin
- inherited SetTransaction(Value);
- If Assigned(FProxy) and (FProxy.Transaction<>Value) then
- FProxy.FTransaction:=Value;
- end;
- procedure TSQLConnector.DoInternalConnect;
- Var
- D : TConnectionDef;
- begin
- inherited DoInternalConnect;
- CheckProxy;
- FProxy.CharSet:=Self.CharSet;
- FProxy.DatabaseName:=Self.DatabaseName;
- FProxy.HostName:=Self.HostName;
- FProxy.LogEvents:=Self.LogEvents;
- FProxy.Password:=Self.Password;
- FProxy.Role:=Self.Role;
- FProxy.UserName:=Self.UserName;
- FProxy.FTransaction:=Self.Transaction;
- FProxy.LogEvents:=Self.LogEvents;
- FProxy.OnLog:=Self.OnLog;
- FProxy.Options:=Self.Options;
- D:=GetConnectionDef(ConnectorType);
- D.ApplyParams(Params,FProxy);
- FProxy.Connected:=True;
- end;
- procedure TSQLConnector.DoInternalDisconnect;
- begin
- FProxy.Connected:=False;
- inherited DoInternalDisconnect;
- end;
- procedure TSQLConnector.CheckProxy;
- begin
- If (FProxy=Nil) then
- CreateProxy;
- end;
- procedure TSQLConnector.CreateProxy;
- Var
- D : TConnectionDef;
- begin
- D:=GetConnectionDef(ConnectorType);
- If (D=Nil) then
- DatabaseErrorFmt(SErrUnknownConnectorType,[ConnectorType],Self);
- FProxy:=D.ConnectionClass.Create(Self);
- FFieldNameQuoteChars := FProxy.FieldNameQuoteChars;
- FConnOptions := FProxy.ConnOptions;
- end;
- procedure TSQLConnector.FreeProxy;
- begin
- FProxy.Connected:=False;
- FreeAndNil(FProxy);
- end;
- function TSQLConnector.StrToStatementType(s: string): TStatementType;
- begin
- CheckProxy;
- Result:=FProxy.StrToStatementType(s);
- end;
- function TSQLConnector.GetAsSQLText(Field: TField): string;
- begin
- CheckProxy;
- Result:=FProxy.GetAsSQLText(Field);
- end;
- function TSQLConnector.GetAsSQLText(Param: TParam): string;
- begin
- CheckProxy;
- Result:=FProxy.GetAsSQLText(Param);
- end;
- function TSQLConnector.GetHandle: pointer;
- begin
- CheckProxy;
- Result:=FProxy.GetHandle;
- end;
- function TSQLConnector.AllocateCursorHandle: TSQLCursor;
- begin
- CheckProxy;
- Result:=FProxy.AllocateCursorHandle;
- end;
- procedure TSQLConnector.DeAllocateCursorHandle(var cursor: TSQLCursor);
- begin
- CheckProxy;
- FProxy.DeAllocateCursorHandle(cursor);
- end;
- function TSQLConnector.AllocateTransactionHandle: TSQLHandle;
- begin
- CheckProxy;
- Result:=FProxy.AllocateTransactionHandle;
- end;
- procedure TSQLConnector.PrepareStatement(cursor: TSQLCursor;
- ATransaction: TSQLTransaction; buf: string; AParams: TParams);
- begin
- CheckProxy;
- FProxy.PrepareStatement(cursor, ATransaction, buf, AParams);
- end;
- procedure TSQLConnector.Execute(cursor: TSQLCursor;
- atransaction: tSQLtransaction; AParams: TParams);
- begin
- CheckProxy;
- FProxy.Execute(cursor, atransaction, AParams);
- end;
- function TSQLConnector.Fetch(cursor: TSQLCursor): boolean;
- begin
- CheckProxy;
- Result:=FProxy.Fetch(cursor);
- end;
- procedure TSQLConnector.AddFieldDefs(cursor: TSQLCursor; FieldDefs: TfieldDefs
- );
- begin
- CheckProxy;
- FProxy.AddFieldDefs(cursor, FieldDefs);
- end;
- procedure TSQLConnector.UnPrepareStatement(cursor: TSQLCursor);
- begin
- CheckProxy;
- FProxy.UnPrepareStatement(cursor);
- end;
- procedure TSQLConnector.FreeFldBuffers(cursor: TSQLCursor);
- begin
- CheckProxy;
- FProxy.FreeFldBuffers(cursor);
- end;
- function TSQLConnector.GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string;
- begin
- Result:=Proxy.GetNextValueSQL(SequenceName, IncrementBy);
- end;
- function TSQLConnector.LoadField(cursor: TSQLCursor; FieldDef: TFieldDef;
- buffer: pointer; out CreateBlob: boolean): boolean;
- begin
- CheckProxy;
- Result:=FProxy.LoadField(cursor, FieldDef, buffer, CreateBlob);
- end;
- procedure TSQLConnector.LoadBlobIntoBuffer(FieldDef: TFieldDef;
- ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction: TSQLTransaction);
- begin
- CheckProxy;
- FProxy.LoadBlobIntoBuffer(FieldDef, ABlobBuf, cursor, ATransaction);
- end;
- function TSQLConnector.RowsAffected(cursor: TSQLCursor): TRowsCount;
- begin
- CheckProxy;
- Result := FProxy.RowsAffected(cursor);
- end;
- function TSQLConnector.GetTransactionHandle(trans: TSQLHandle): pointer;
- begin
- CheckProxy;
- Result:=FProxy.GetTransactionHandle(trans);
- end;
- function TSQLConnector.Commit(trans: TSQLHandle): boolean;
- begin
- CheckProxy;
- Result:=FProxy.Commit(trans);
- end;
- function TSQLConnector.RollBack(trans: TSQLHandle): boolean;
- begin
- CheckProxy;
- Result:=FProxy.RollBack(trans);
- end;
- function TSQLConnector.StartDBTransaction(trans: TSQLHandle; aParams: string): boolean;
- begin
- CheckProxy;
- Result:=FProxy.StartDBTransaction(trans, aParams);
- end;
- procedure TSQLConnector.CommitRetaining(trans: TSQLHandle);
- begin
- CheckProxy;
- FProxy.CommitRetaining(trans);
- end;
- procedure TSQLConnector.RollBackRetaining(trans: TSQLHandle);
- begin
- CheckProxy;
- FProxy.RollBackRetaining(trans);
- end;
- procedure TSQLConnector.UpdateIndexDefs(IndexDefs: TIndexDefs;
- TableName: string);
- begin
- CheckProxy;
- FProxy.UpdateIndexDefs(IndexDefs, TableName);
- end;
- function TSQLConnector.GetSchemaInfoSQL(SchemaType: TSchemaType;
- SchemaObjectName, SchemaPattern: string): string;
- begin
- CheckProxy;
- Result:=FProxy.GetSchemaInfoSQL(SchemaType, SchemaObjectName, SchemaPattern);
- end;
- { TConnectionDef }
- class function TConnectionDef.TypeName: String;
- begin
- Result:='';
- end;
- class function TConnectionDef.ConnectionClass: TSQLConnectionClass;
- begin
- Result:=Nil;
- end;
- class function TConnectionDef.Description: String;
- begin
- Result:='';
- end;
- class function TConnectionDef.DefaultLibraryName: String;
- begin
- Result:='';
- end;
- class function TConnectionDef.LoadFunction: TLibraryLoadFunction;
- begin
- Result:=Nil;
- end;
- class function TConnectionDef.UnLoadFunction: TLibraryUnLoadFunction;
- begin
- Result:=Nil;
- end;
- class function TConnectionDef.LoadedLibraryName: string;
- begin
- Result:='';
- end;
- procedure TConnectionDef.ApplyParams(Params: TStrings;
- AConnection: TSQLConnection);
- begin
- AConnection.Params.Assign(Params);
- end;
- { TServerIndexDefs }
- constructor TServerIndexDefs.create(ADataset: TDataset);
- begin
- if not (ADataset is TCustomSQLQuery) then
- DatabaseErrorFmt(SErrNotASQLQuery,[ADataset.Name]);
- inherited create(ADataset);
- end;
- procedure TServerIndexDefs.Update;
- begin
- if (not updated) and assigned(Dataset) then
- begin
- TCustomSQLQuery(Dataset).UpdateServerIndexDefs;
- updated := True;
- end;
- end;
- Initialization
- Finalization
- DoneDefs;
- end.
|