{-# CFILES hdbc-sqlite3-helper.c #-}-- above line for hugsmoduleDatabase.HDBC.Sqlite3.Connection(connectSqlite3,connectSqlite3Raw,Impl.Connection())whereimportDatabase.HDBC.TypesimportDatabase.HDBCimportDatabase.HDBC.DriverUtilsimportqualifiedDatabase.HDBC.Sqlite3.ConnectionImplasImplimportDatabase.HDBC.Sqlite3.TypesimportDatabase.HDBC.Sqlite3.StatementimportForeign.C.TypesimportForeign.C.StringimportForeign.MarshalimportForeign.StorableimportDatabase.HDBC.Sqlite3.UtilsimportForeign.ForeignPtrimportForeign.PtrimportControl.Concurrent.MVarimportqualifiedData.ByteStringasBimportqualifiedData.ByteString.UTF8asBUTF8importqualifiedData.Char{- | Connect to an Sqlite version 3 database. The only parameter needed is
the filename of the database to connect to.
All database accessor functions are provided in the main HDBC module. -}connectSqlite3::FilePath->IOImpl.ConnectionconnectSqlite3=genericConnect(B.useAsCString.BUTF8.fromString){- | Connects to a Sqlite v3 database as with 'connectSqlite3', but
instead of converting the supplied 'FilePath' to a C String by performing
a conversion to Unicode, instead converts it by simply dropping all bits past
the eighth. This may be useful in rare situations
if your application or filesystemare not running in Unicode space. -}connectSqlite3Raw::FilePath->IOImpl.ConnectionconnectSqlite3Raw=genericConnectwithCStringgenericConnect::(String->(CString->IOImpl.Connection)->IOImpl.Connection)->FilePath->IOImpl.ConnectiongenericConnectstrAsCStrFuncfp=strAsCStrFuncfp(\cs->alloca(\(p::Ptr(PtrCSqlite3))->dores<-sqlite3_opencspo<-peekpfptr<-newForeignPtrsqlite3_closeptronewconn<-mkConnfpfptrcheckError("connectSqlite3 "++fp)fptrresreturnnewconn))mkConn::FilePath->Sqlite3->IOImpl.ConnectionmkConnfpobj=dochildren<-newMVar[]begin_transactionobjchildrenver<-(sqlite3_libversion>>=peekCString)return$Impl.Connection{Impl.disconnect=fdisconnectobjchildren,Impl.commit=fcommitobjchildren,Impl.rollback=frollbackobjchildren,Impl.run=frunobjchildren,Impl.runRaw=frunRawobjchildren,Impl.prepare=newSthobjchildrenTrue,Impl.clone=connectSqlite3fp,Impl.hdbcDriverName="sqlite3",Impl.hdbcClientVer=ver,Impl.proxiedClientName="sqlite3",Impl.proxiedClientVer=ver,Impl.dbTransactionSupport=True,Impl.dbServerVer=ver,Impl.getTables=fgettablesobjchildren,Impl.describeTable=fdescribeTableobjchildren,Impl.setBusyTimeout=fsetbusyobj}fgettablesomchildren=dosth<-newSthomchildrenTrue"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"executesth[]res1<-fetchAllRows'sthletres=mapfromSql$concatres1return$seq(lengthres)resfdescribeTableomchildrenname=dosth<-newSthomchildrenTrue$"PRAGMA table_info("++name++")"executesth[]res1<-fetchAllRows'sthreturn$mapdescribeColres1wheredescribeCol(_:name:typ:notnull:df:pk:_)=(fromSqlname,describeTypetypnotnulldfpk)describeTypenamenotnulldfpk=SqlColDesc(typeIdname)NothingNothingNothing(nullablenotnull)nullableSqlNull=Nothingnullable(SqlString"0")=JustTruenullable(SqlString"1")=JustFalsenullable_=NothingtypeIdSqlNull=SqlUnknownT"Any"typeId(SqlStringt)=typeId'ttypeId(SqlByteStringt)=typeId'$BUTF8.toStringttypeId_=SqlUnknownT"Unknown"typeId't=casemapData.Char.toLowertof('i':'n':'t':_)->SqlIntegerT"text"->SqlVarCharT"real"->SqlRealT"blob"->SqlVarBinaryT""->SqlUnknownT"Any"other->SqlUnknownTotherfsetbusyoms=withRawSqlite3o$\ppdb->sqlite3_busy_timeoutppdbms---------------------------------------------------- Guts here--------------------------------------------------begin_transaction::Sqlite3->ChildList->IO()begin_transactionochildren=frunochildren"BEGIN"[]>>return()frunomchildrenqueryargs=dosth<-newSthomchildrenFalsequeryres<-executesthargsfinishsthreturnresfrunRaw::Sqlite3->ChildList->String->IO()frunRawomchildrenquery=dosth<-newSthomchildrenFalsequeryexecuteRawsthfinishsthfcommitochildren=dofrunochildren"COMMIT"[]begin_transactionochildrenfrollbackochildren=dofrunochildren"ROLLBACK"[]begin_transactionochildrenfdisconnect::Sqlite3->ChildList->IO()fdisconnectomchildren=withRawSqlite3o$\p->docloseAllChildrenmchildrenr<-sqlite3_closepcheckError"disconnect"orforeignimportccallunsafe"hdbc-sqlite3-helper.h sqlite3_open2"sqlite3_open::CString->(Ptr(PtrCSqlite3))->IOCIntforeignimportccallunsafe"hdbc-sqlite3-helper.h &sqlite3_close_finalizer"sqlite3_closeptr::FunPtr((PtrCSqlite3)->IO())foreignimportccallunsafe"hdbc-sqlite3-helper.h sqlite3_close_app"sqlite3_close::PtrCSqlite3->IOCIntforeignimportccallunsafe"hdbc-sqlite3-helper.h sqlite3_busy_timeout2"sqlite3_busy_timeout::PtrCSqlite3->CInt->IO()foreignimportccallunsafe"sqlite3.h sqlite3_libversion"sqlite3_libversion::IOCString