123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264# 1 "dblib.cppo.ml"(* File: dblib.ml
Copyright (C) 2010
Christophe Troestler <Christophe.Troestler@umons.ac.be>
WWW: http://math.umons.ac.be/an/software/
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or
later as published by the Free Software Foundation, with the special
exception on linking described in the file LICENSE.
This library 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. See the file
LICENSE for more details. *)openPrintftypeseverity=|INFO|USER|NONFATAL|CONVERSION|SERVER|TIME|PROGRAM|RESOURCE|COMM|FATAL|CONSISTENCYletpp_severity_=function|INFO->"INFO"|USER->"USER"|NONFATAL->"NONFATAL"|CONVERSION->"CONVERSION"|SERVER->"SERVER"|TIME->"TIME"|PROGRAM->"PROGRAM"|RESOURCE->"RESOURCE"|COMM->"COMM"|FATAL->"FATAL"|CONSISTENCY->"CONSISTENCY"exceptionErrorofseverity*stringleterr_handler(f:severity->int->string->unit)=Callback.register"Freetds.Dblib.err_handler"fletdefault_err_handlerseverity_errmsg=raise(Error(severity,msg))letmsg_handler(f:severity->int->string->unit)=Callback.register"Freetds.Dblib.msg_handler"fletdefault_msg_handlerseveritylinemsg=matchseveritywith|FATAL|CONSISTENCY->letmsg=sprintf"Error on line %d: %s"linemsginraise(Error(severity,msg))|INFO|USER|NONFATAL|CONVERSION|SERVER|TIME|PROGRAM|RESOURCE|COMM->()typeversion=V42|V46|V70|V71|V72|V73|V74externaldbinit:unit->unit="ocaml_freetds_dbinit"letinitversion=Callback.register_exception"Freetds.Dblib.Error"(Error(FATAL,"message"));letpp_error=function|Error(s,m)->Some(sprintf"Error(%a, %S)"pp_severitysm)|_->NoneinPrintexc.register_printerpp_error;err_handlerdefault_err_handler;msg_handlerdefault_msg_handler;dbinitversion(* One must call this function before trying to use db-lib in any
way. Allocates various internal structures and reads
locales.conf (if any) to determine the default date format. *)letis_initialized=reffalsetypedbprocessexternaldbopen:user:stringoption->password:stringoption->charset:stringoption->language:stringoption->application:stringoption->version:versionoption->server:string->dbprocess="ocaml_freetds_dbopen_bc""ocaml_freetds_dbopen"letconnect?user?password?charset?language?application?versionserver=ifnot!is_initializedtheninit();dbopen~user~password~charset~language~application~version~serverexternalclose:dbprocess->unit="ocaml_freetds_dbclose"externaldbuse:dbprocess->string->unit="ocaml_freetds_dbuse"letusedbname=trydbusedbnamewith_->letmsg=sprintf"Freetds.Dblib.use: unable to open the database %S"nameinraise(Error(PROGRAM,msg))externalname:dbprocess->string="ocaml_freetds_dbname"externaldbsqlexec:dbprocess->string->unit="ocaml_freetds_dbsqlexec"letsqlexecdbsql=trydbsqlexecdbsqlwith|Not_found->letmsg=sprintf"Freetds.Dblib.sqlexec: the SQL query %S is invalid. \
It may be due to a SQL syntax error, incorrect column \
or table names, or if the previous query results were \
not completely read,..."sqlinraise(Error(PROGRAM,msg))|Error(severity,msg)->(* The handler may raise exceptions. *)letmsg=sprintf"Freetds.Dblib.sqlexec: the SQL query %S generated \
the error %S"sqlmsginraise(Error(severity,msg))externalcancel:dbprocess->unit="ocaml_freetds_dbcancel"externalcanquery:dbprocess->unit="ocaml_freetds_dbcanquery"externalresults:dbprocess->bool="ocaml_freetds_dbresults"externalnumcols:dbprocess->int="ocaml_freetds_numcols"[@@noalloc](** Return number of regular columns in a result set. *)externalcolname:dbprocess->int->string="ocaml_freetds_dbcolname"(* See /usr/include/sybdb.h *)typecol_type=|SYBCHAR(* 0 *)|SYBVARCHAR(* 1 *)|SYBINTN(* 2 *)|SYBINT1(* 3 *)|SYBINT2(* 4 *)|SYBINT4(* 5 *)|SYBINT8(* 6 *)|SYBFLT8(* 7 *)|SYBFLTN(* 8 *)|SYBNUMERIC(* 9 *)|SYBDECIMAL(* 10 *)|SYBDATETIME(* 11 *)|SYBDATETIME4(* 12 *)|SYBDATETIMN(* 13 *)|SYBBIT(* 14 *)|SYBTEXT(* 15 *)|SYBIMAGE(* 16 *)|SYBMONEY4(* 17 *)|SYBMONEY(* 18 *)|SYBMONEYN(* 19 *)|SYBREAL(* 20 *)|SYBBINARY(* 21 *)|SYBVARBINARY(* 22 *)letstring_of_col_type=function|SYBCHAR->"CHAR"|SYBVARCHAR->"VARCHAR"|SYBINTN->"INT"|SYBINT1->"INT1"|SYBINT2->"INT2"|SYBINT4->"INT4"|SYBINT8->"INT8"|SYBFLT8->"FLOAT8"|SYBFLTN->"FLOAT"|SYBREAL->"REAL"|SYBBIT->"BIT"|SYBTEXT->"TEXT"|SYBIMAGE->"IMAGE"|SYBMONEY4->"MONEY4"|SYBMONEY->"MONEY"|SYBMONEYN->"MONEY"|SYBDATETIME->"DATETIME"|SYBDATETIME4->"DATETIME4"|SYBDATETIMN->"DATETIME"|SYBBINARY->"BINARY"|SYBVARBINARY->"VARBINARY"|SYBNUMERIC->"NUMERIC"|SYBDECIMAL->"DECIMAL";;externalcoltype:dbprocess->int->col_type="ocaml_freetds_dbcoltype"(* See /usr/include/sybdb.h, CHARBIND ... *)typedata=|NULL|STRINGofstring(* tag = 0 *)|TINYofint|SMALLofint|INTofint|INT32ofint32|INT64ofint64|FLOAToffloat(* tag = 6 *)|DATETIMEofint*int*int*int*int*int*int*int|MONEYoffloat|BITofbool|BINARYofstring(* tag = 10 *)|NUMERICofstring(* FIXME: do better *)|DECIMALofstring(* FIXME: do better *)letstring_of_data=function|NULL->"NULL"|STRINGs->sprintf"STRING(%S)"s|TINYi->sprintf"TINY(%i)"i|SMALLi->sprintf"SMALL(%i)"i|INTi->sprintf"INT(%i)"i|INT32i->sprintf"INT32(%li)"i|INT64i->sprintf"INT64(%Li)"i|FLOATf->sprintf"FLOAT(%f)"f|DATETIME(y,mo,day,h,m,s,_,_)->sprintf"DATETIME(%i/%i/%i %i:%02i:%02i)"ymodayhms|MONEYf->sprintf"MONEY(%f)"f|BITb->sprintf"BIT(%b)"b|BINARYs->sprintf"BINARY(%S)"s|NUMERICs->sprintf"NUMERIC(%S)"s|DECIMALs->sprintf"DECIMAL(%S)"sexternaldbnextrow:dbprocess->int="ocaml_freetds_dbnextrow"(* Return the ID of computed queries or REG_ROW (value extracted
by discover.ml) for a regular result.
Raises [Not_found] if NO_MORE_ROWS. *)typedata_ptrexternaldbdata:dbprocess->col:int->data_ptr="ocaml_freetds_dbdata"(* Note that [data_ptr] is a pointer to the data. *)externalis_null:data_ptr->bool="ocaml_freetds_is_null"[@@noalloc]externaldbdatlen:dbprocess->col:int->int="ocaml_freetds_dbdatlen"[@@noalloc]externalget_data:dbprocess->col:int->data_ptr->data="ocaml_freetds_get_data"(* Beware that [data_ptr] must be those associated with [col]. *)letnextrowdb=letstatus=dbnextrowdbinifstatus=# 231 "dblib.cppo.ml"-1# 231 "dblib.cppo.ml"then(letrow=ref[]inforc=numcolsdbdownto1doletdata_ptr=dbdatadb~col:cinletlen=dbdatlendb~col:ciniflen<0thenletmsg=sprintf"FreeTDS.Dblib.nextrow: column %d does not exist. \
Contact the OCaml FreeTDS developers."cinraise(Error(FATAL,msg))elseifis_nulldata_ptrtheniflen=0thenrow:=NULL::!rowelseletmsg=sprintf"Freetds.Dlib.nextrow: column %d has a length \
of %d but no data is returned."cleninraise(Error(FATAL,msg))elserow:=get_datadb~col:cdata_ptr::!rowdone;!row)else(* [status] = ID of computed row *)failwith"Computed rows are not handled at the moment. Please write \
to the developers of OCaml FreeTDS."externalcount:dbprocess->int="ocaml_freetds_dbcount"externalsettime:int->unit="ocaml_freetds_dbsettime"