要操纵一个数据库你就得有一个这个数据库的句柄(又碰到这个难以理解的词了,不过确实还没得一个更好的词来替代它)。其实你跟本不需要去在乎这个词 叫什么,你只要搞清楚他是一个什么玩意儿。就如同鞋子为什么叫鞋子,仔细想想确实也难以理解,不过 清楚他的功能就OK了,不是吗?
句柄在很多地方我们见到过,最常见的就是文件的句柄,我们要操纵一个文件,我们就要取得一个文件的句柄。句柄是个什么东东呢?其实很简单,句柄是一 个东东的描述,他被定义为一个结构体,这个结构体可能会包含要描述的东东的具体信息,比如位置、大小、类型等等。我们有了这个描述信息我们就能去找到这个 东东,然后操纵它。
一句话:句柄是物体的描述结构体。
我们来看看sqlite的句柄是怎么定义的(不要被吓到了,代码直接跳过就好):
- structsqlite3{
- sqlite3_vfs*pVfs;/*OSInterface*/
- intnDb;/*Numberofbackendscurrentlyinuse*/
- Db*aDb;/*Allbackends*/
- intflags;/*Miscellaneousflags.Seebelow*/
- unsignedintopenFlags;/*Flagspassedtosqlite3_vfs.xOpen()*/
- interrCode;/*Mostrecenterrorcode(sqlITE_*)*/
- interrMask;/*&resultcodeswiththisbeforereturning*/
- u8autoCommit;/*Theauto-commitflag.*/
- u8temp_store;/*1:file2:memory0:default*/
- u8mallocFailed;/*Trueifwehaveseenamallocfailure*/
- u8dfltLockMode;/*Defaultlocking-modeforattacheddbs*/
- signedcharnextAutovac;/*AutovacsettingafterVACUUMif>=0*/
- u8suppressErr;/*Donotissueerrormessagesiftrue*/
- u8vtabOnConflict;/*Valuetoreturnfors3_vtab_on_conflict()*/
- intnextPagesize;/*PagesizeafterVACUUMif>0*/
- intnTable;/*Numberoftablesinthedatabase*/
- CollSeq*pDfltColl;/*Thedefaultcollatingsequence(BINARY)*/
- i64lastRowid;/*ROWIDofmostrecentinsert(seeabove)*/
- u32magic;/*Magicnumberfordetectlibrarymisuse*/
- intnChange;/*Valuereturnedbysqlite3_changes()*/
- intnTotalChange;/*Valuereturnedbysqlite3_total_changes()*/
- sqlite3_mutex*mutex;/*Connectionmutex*/
- intaLimit[sqlITE_N_LIMIT];/*Limits*/
- structsqlite3InitInfo{/*Informationusedduringinitialization*/
- intiDb;/*Whenbackisbeinginitialized*/
- intnewTnum;/*Rootpageoftablebeinginitialized*/
- u8busy;/*TRUEifcurrentlyinitializing*/
- u8orphanTrigger;/*LaststatementisorphanedTEMPtrigger*/
- }init;
- intnExtension;/*Numberofloadedextensions*/
- void**aExtension;/*Arrayofsharedlibraryhandles*/
- structVdbe*pVdbe;/*Listofactivevirtualmachines*/
- intactiveVdbeCnt;/*NumberofVDBEscurrentlyexecuting*/
- intwriteVdbeCnt;/*NumberofactiveVDBEsthatarewriting*/
- intvdbeExecCnt;/*NumberofnestedcallstoVdbeExec()*/
- void(*xTrace)(void*,constchar*);/*Tracefunction*/
- void*pTraceArg;/*Argumenttothetracefunction*/
- void(*xProfile)(void*,constchar*,u64);/*Profilingfunction*/
- void*pProfileArg;/*Argumenttoprofilefunction*/
- void*pCommitArg;/*ArgumenttoxCommitCallback()*/
- int(*xCommitCallback)(void*);/*Invokedateverycommit.*/
- void*pRollbackArg;/*ArgumenttoxRollbackCallback()*/
- void(*xRollbackCallback)(void*);/*Invokedateverycommit.*/
- void*pUpdateArg;
- void(*xUpdateCallback)(void*,int,sqlite_int64);
- #ifndefsqlITE_OMIT_WAL
- int(*xWalCallback)(void*,sqlite3*,int);
- void*pWalArg;
- #endif
- void(*xCollNeeded)(void*,inteTextRep,constchar*);
- void(*xCollNeeded16)(void*,constvoid*);
- void*pCollNeededArg;
- sqlite3_value*pErr;/*Mostrecenterrormessage*/
- char*zErrMsg;/*Mostrecenterrormessage(UTF-8encoded)*/
- char*zErrMsg16;/*Mostrecenterrormessage(UTF-16encoded)*/
- union{
- volatileintisInterrupted;/*Trueifsqlite3_interrupthasbeencalled*/
- doublenotUsed1;/*Spacer*/
- }u1;
- Lookasidelookaside;/*Lookasidemallocconfiguration*/
- #ifndefsqlITE_OMIT_AUTHORIZATION
- int(*xAuth)(void*,constchar*);
- /*Accessauthorizationfunction*/
- void*pAuthArg;/*1stargumenttotheaccessauthfunction*/
- #endif
- #ifndefsqlITE_OMIT_PROGRESS_CALLBACK
- int(*xProgress)(void*);/*Theprogresscallback*/
- void*pProgressArg;/*Argumenttotheprogresscallback*/
- intnProgressOps;/*Numberofopcodesforprogresscallback*/
- #endif
- #ifndefsqlITE_OMIT_VIRTUALTABLE
- HashaModule;/*populatedbysqlite3_create_module()*/
- VtabCtx*pVtabCtx;/*Contextforactivevtabconnect/create*/
- VTable**aVTrans;/*Virtualtableswithopentransactions*/
- intnVTrans;/*AllocatedsizeofaVTrans*/
- VTable*pDisconnect;/*Disconnecttheseinnextsqlite3_prepare()*/
- #endif
- FuncDefHashaFunc;/*Hashtableofconnectionfunctions*/
- HashaCollSeq;/*Allcollatingsequences*/
- BusyHandlerbusyHandler;/*Busycallback*/
- intbusyTimeout;/*Busyhandlertimeout,inmsec*/
- DbaDbStatic[2];/*Staticspaceforthe2defaultbackends*/
- Savepoint*pSavepoint;/*Listofactivesavepoints*/
- intnSavepoint;/*Numberofnon-transactionsavepoints*/
- intnStatement;/*Numberofnestedstatement-transactions*/
- u8isTransactionSavepoint;/*TrueiftheoutermostsavepointisaTS*/
- i64nDeferredCons;/*Netdeferredconstraintsthistransaction.*/
- int*pnBytesFreed;/*IfnotNULL,incrementthisinDbFree()*/
- #ifdefsqlITE_ENABLE_UNLOCK_NOTIFY
- /*ThefollowingvariablesareallprotectedbytheSTATIC_MASTER
- **mutex,notbysqlite3.mutex.Theyareusedbycodeinnotify.c.
- **
- **WhenX.pUnlockConnection==Y,thatmeansthatXiswaitingforYto
- **unlocksothatitcanproceed.
- **
- **WhenX.pBlockingConnection==Y,thatmeansthatsomethingthatXtried
- **triedtodorecentlyFailedwithansqlITE_LOCKEDerrorduetolocks
- **heldbyY.
- */
- sqlite3*pBlockingConnection;/*ConnectionthatcausedsqlITE_LOCKED*/
- sqlite3*pUnlockConnection;/*Connectiontowatchforunlock*/
- void*pUnlockArg;/*ArgumenttoxUnlockNotify*/
- void(*xUnlockNotify)(void**,int);/*Unlocknotifycallback*/
- sqlite3*pNextBlocked;/*Nextinlistofallblockedconnections*/
- #endif
- };
?