home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / DBIXS.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-11-13  |  18.0 KB  |  481 lines

  1. /* $Id: DBIXS.h,v 11.17 2003/11/13 13:04:38 timbo Exp $
  2.  *
  3.  * Copyright (c) 1994-2002  Tim Bunce  Ireland
  4.  *
  5.  * See COPYRIGHT section in DBI.pm for usage and distribution rights.
  6.  */
  7.  
  8. /* DBI Interface Definitions for DBD Modules */
  9.  
  10. #ifndef DBIXS_VERSION                /* prevent multiple inclusion */
  11.  
  12. #ifndef DBIS
  13. #define DBIS    dbis    /* default name for dbistate_t variable    */
  14. #endif
  15.  
  16. /* first pull in the standard Perl header files for extensions    */
  17. #define PERL_POLLUTE
  18. #include <EXTERN.h>
  19. #include <perl.h>
  20. #include <XSUB.h>
  21.  
  22. #ifdef debug        /* causes problems with DBIS->debug    */
  23. #undef debug
  24. #endif
  25.  
  26. #ifdef std             /* causes problems with STLport <tscheresky@micron.com> */
  27. #undef std
  28. #endif
  29.  
  30. /* Perl backwards compatibility definitions */
  31. #include "dbipport.h"
  32.  
  33. /* DBI SQL_* type definitions */
  34. #include "dbi_sql.h"
  35.  
  36.  
  37. /* The DBIXS_VERSION value will be incremented whenever new code is
  38.  * added to the interface (this file) or significant changes are made.
  39.  * It's primary goal is to allow newer drivers to compile against an
  40.  * older installed DBI. This is mainly an issue whilst the API grows
  41.  * and learns from the needs of various drivers.  See also the
  42.  * DBISTATE_VERSION macro below. You can think of DBIXS_VERSION as
  43.  * being a compile time check and DBISTATE_VERSION as a runtime check.
  44.  */
  45. #define DBIXS_VERSION 93
  46.  
  47. #ifdef NEED_DBIXS_VERSION
  48. #if NEED_DBIXS_VERSION > DBIXS_VERSION
  49. error You_need_to_upgrade_your_DBI_module_before_building_this_driver
  50. #endif
  51. #else
  52. #define NEED_DBIXS_VERSION DBIXS_VERSION
  53. #endif
  54.  
  55.  
  56. #define DBI_LOCK
  57. #define DBI_UNLOCK
  58.  
  59. #ifndef DBI_NO_THREADS
  60. #ifdef USE_ITHREADS
  61. #define DBI_USE_THREADS
  62. #endif /* USE_ITHREADS */
  63. #endif /* DBI_NO_THREADS */
  64.  
  65.  
  66. /* forward struct declarations                        */
  67.  
  68. typedef struct dbistate_st dbistate_t;
  69. /* implementor needs to define actual struct { dbih_??c_t com; ... }*/
  70. typedef struct imp_drh_st imp_drh_t;    /* driver            */
  71. typedef struct imp_dbh_st imp_dbh_t;    /* database            */
  72. typedef struct imp_sth_st imp_sth_t;    /* statement            */
  73. typedef struct imp_fdh_st imp_fdh_t;    /* field descriptor        */
  74. typedef struct imp_xxh_st imp_xxh_t;    /* any (defined below)        */
  75. #define DBI_imp_data_ imp_xxh_t        /* friendly for take_imp_data    */
  76.  
  77.  
  78.  
  79. /* --- DBI Handle Common Data Structure (all handles have one) ---    */
  80.  
  81. /* Handle types. Code currently assumes child = parent + 1.        */
  82. #define DBIt_DR        1
  83. #define DBIt_DB        2
  84. #define DBIt_ST        3
  85. #define DBIt_FD        4
  86.  
  87. /* component structures */
  88.  
  89. typedef struct dbih_com_std_st {
  90.     U32  flags;
  91.     int  call_depth;    /* used by DBI to track nested calls (int)    */
  92.     U16  type;        /* DBIt_DR, DBIt_DB, DBIt_ST            */
  93.     HV   *my_h;        /* copy of outer handle HV (not refcounted)    */
  94.     SV   *parent_h;    /* parent inner handle (ref to hv) (r.c.inc)    */
  95.     imp_xxh_t *parent_com;    /* parent com struct shortcut        */
  96.     PerlInterpreter * thr_user;  /* thread that owns the handle         */
  97.  
  98.     HV   *imp_stash;    /* who is the implementor for this handle    */
  99.     SV   *imp_data;    /* optional implementors data (for perl imp's)    */
  100.  
  101.     I32  kids;        /* count of db's for dr's, st's for db's etc    */
  102.     I32  active_kids;    /* kids which are currently DBIc_ACTIVE        */
  103.     U32 pad;        /* keep binary compat */
  104.     dbistate_t *dbistate;
  105. } dbih_com_std_t;
  106.  
  107. typedef struct dbih_com_attr_st {
  108.     /* These are copies of the Hash values (ref.cnt.inc'd)    */
  109.     /* Many of the hash values are themselves references    */
  110.     SV *TraceLevel;
  111.     SV *State;        /* Standard SQLSTATE, 5 char string    */
  112.     SV *Err;        /* Native engine error code        */
  113.     SV *Errstr;        /* Native engine error message        */
  114.     SV *spare;
  115.     U32  LongReadLen;    /* auto read length for long/blob types    */
  116.     SV *FetchHashKeyName;    /* for fetchrow_hashref        */
  117.     /* (NEW FIELDS?... DON'T FORGET TO UPDATE dbih_clearcom()!)    */
  118. } dbih_com_attr_t;
  119.  
  120.  
  121. struct dbih_com_st {    /* complete core structure (typedef'd above)    */
  122.     dbih_com_std_t    std;
  123.     dbih_com_attr_t    attr;
  124. };
  125.  
  126. /* This 'implementors' type the DBI defines by default as a way to    */
  127. /* refer to the imp_??h data of a handle without considering its type.    */
  128. struct imp_xxh_st { struct dbih_com_st com; };
  129.  
  130. /* Define handle-type specific structures for implementors to include    */
  131. /* at the start of their private structures.                */
  132.  
  133. typedef struct {        /* -- DRIVER --                */
  134.     dbih_com_std_t    std;
  135.     dbih_com_attr_t    attr;
  136.     HV          *cached_kids;    /* $drh->connect_cached(...)        */
  137. } dbih_drc_t;
  138.  
  139. typedef struct {        /* -- DATABASE --            */
  140.     dbih_com_std_t    std;    /* \__ standard structure        */
  141.     dbih_com_attr_t    attr;    /* /   plus... (nothing else right now)    */
  142.     HV          *cached_kids;    /* $dbh->prepare_cached(...)        */
  143. } dbih_dbc_t;
  144.  
  145. typedef struct {        /* -- STATEMENT --            */
  146.     dbih_com_std_t    std;    /* \__ standard structure        */
  147.     dbih_com_attr_t    attr;    /* /   plus ...                */
  148.  
  149.     int     num_params;    /* number of placeholders        */
  150.     int     num_fields;    /* NUM_OF_FIELDS, must be set        */
  151.     AV      *fields_svav;    /* special row buffer (inc bind_cols)    */
  152.     IV        row_count;    /* incremented by get_fbav()        */
  153.  
  154.     AV        *fields_fdav;    /* not used yet, may change */
  155.  
  156.     I32  spare1;
  157.     void *spare2;
  158. } dbih_stc_t;
  159.  
  160.  
  161. /* XXX THIS STRUCTURE SHOULD NOT BE USED */
  162. typedef struct {        /* -- FIELD DESCRIPTOR --        */
  163.     dbih_com_std_t    std;    /* standard structure (not fully setup)    */
  164.  
  165.     /* core attributes (from DescribeCol in ODBC)        */
  166.     char *col_name;        /* see dbih_make_fdsv        */
  167.     I16   col_name_len;
  168.     I16   col_sql_type;
  169.     I16   col_precision;
  170.     I16   col_scale;
  171.     I16   col_nullable;
  172.  
  173.     /* additional attributes (from ColAttributes in ODBC)    */
  174.     I32   col_length;
  175.     I32   col_disp_size;
  176.  
  177.     I32  spare1;
  178.     void *spare2;
  179. } dbih_fdc_t;
  180.  
  181.  
  182. #define _imp2com(p,f)          ((p)->com.f)
  183.  
  184. #define DBIc_FLAGS(imp)        _imp2com(imp, std.flags)
  185. #define DBIc_TYPE(imp)        _imp2com(imp, std.type)
  186. #define DBIc_CALL_DEPTH(imp)    _imp2com(imp, std.call_depth)
  187. #define DBIc_MY_H(imp)      _imp2com(imp, std.my_h)
  188. #define DBIc_PARENT_H(imp)      _imp2com(imp, std.parent_h)
  189. #define DBIc_PARENT_COM(imp)      _imp2com(imp, std.parent_com)
  190. #define DBIc_THR_COND(imp)      _imp2com(imp, std.thr_cond)
  191. #define DBIc_THR_USER(imp)      _imp2com(imp, std.thr_user)
  192. #define DBIc_THR_USER_NONE      (0xFFFF)
  193. #define DBIc_IMP_STASH(imp)      _imp2com(imp, std.imp_stash)
  194. #define DBIc_IMP_DATA(imp)      _imp2com(imp, std.imp_data)
  195. #define DBIc_DBISTATE(imp)      _imp2com(imp, std.dbistate)
  196. #define DBIc_LOGPIO(imp)      DBIc_DBISTATE(imp)->logfp
  197. #define DBIc_KIDS(imp)      _imp2com(imp, std.kids)
  198. #define DBIc_ACTIVE_KIDS(imp)      _imp2com(imp, std.active_kids)
  199. #define DBIc_LAST_METHOD(imp)      _imp2com(imp, std.last_method)
  200.  
  201. #define DBIc_DEBUG(imp)        (_imp2com(imp, attr.TraceLevel))
  202. #define DBIc_DEBUGIV(imp)    SvIV(DBIc_DEBUG(imp))
  203. #define DBIc_STATE(imp)        SvRV(_imp2com(imp, attr.State))
  204. #define DBIc_ERR(imp)        SvRV(_imp2com(imp, attr.Err))
  205. #define DBIc_ERRSTR(imp)    SvRV(_imp2com(imp, attr.Errstr))
  206. #define DBIc_LongReadLen(imp)      _imp2com(imp, attr.LongReadLen)
  207. #define DBIc_LongReadLen_init    80    /* may change */
  208. #define DBIc_FetchHashKeyName(imp) (_imp2com(imp, attr.FetchHashKeyName))
  209.  
  210. /* handle sub-type specific fields                        */
  211. /*    dbh    */
  212. #define DBIc_CACHED_KIDS(imp)      _imp2com(imp, cached_kids)
  213. /*    sth    */
  214. #define DBIc_NUM_FIELDS(imp)      _imp2com(imp, num_fields)
  215. #define DBIc_NUM_PARAMS(imp)      _imp2com(imp, num_params)
  216. #define DBIc_NUM_PARAMS_AT_EXECUTE    -9 /* see Driver.xst */
  217. #define DBIc_ROW_COUNT(imp)      _imp2com(imp, row_count)
  218. #define DBIc_FIELDS_AV(imp)      _imp2com(imp, fields_svav)
  219. #define DBIc_FDESC_AV(imp)      _imp2com(imp, fields_fdav)
  220. #define DBIc_FDESC(imp, i)      ((imp_fdh_t*)(void*)SvPVX(AvARRAY(DBIc_FDESC_AV(imp))[i]))
  221.  
  222. /* XXX --- DO NOT CHANGE THESE VALUES AS THEY ARE COMPILED INTO DRIVERS --- XXX */
  223. #define DBIcf_COMSET      0x000001    /* needs to be clear'd before free'd    */
  224. #define DBIcf_IMPSET      0x000002    /* has implementor data to be clear'd    */
  225. #define DBIcf_ACTIVE      0x000004    /* needs finish/disconnect before clear    */
  226. #define DBIcf_IADESTROY      0x000008    /* do DBIc_ACTIVE_off before DESTROY    */
  227. #define DBIcf_WARN        0x000010    /* warn about poor practice etc      */
  228. #define DBIcf_COMPAT        0x000020    /* compat/emulation mode (eg oraperl)    */
  229. #define DBIcf_ChopBlanks  0x000040    /* rtrim spaces from fetch char columns    */
  230. #define DBIcf_RaiseError  0x000080    /* throw exception (croak) on error    */
  231. #define DBIcf_PrintError  0x000100    /* warn() on error            */
  232. #define DBIcf_AutoCommit  0x000200    /* dbh only. used by drivers        */
  233. #define DBIcf_LongTruncOk 0x000400    /* truncation to LongReadLen is okay    */
  234. #define DBIcf_MultiThread 0x000800    /* allow multiple threads to enter    */
  235. /*    spare          0x001000                        */
  236. #define DBIcf_ShowErrorStatement  0x002000   /* include Statement in error    */
  237. #define DBIcf_BegunWork   0x004000    /* between begin_work & commit/rollback */
  238. #define DBIcf_HandleError 0x008000    /* has coderef in HandleError attribute */
  239. #define DBIcf_Profile     0x010000    /* profile activity on this handle      */
  240. #define DBIcf_TaintIn     0x020000    /* check inputs for taintedness */
  241. #define DBIcf_TaintOut    0x040000    /* taint outgoing data */
  242. /* new flags may require clone() to be updated */
  243.  
  244. #define DBIcf_INHERITMASK        /* what NOT to pass on to children */    \
  245.   (U32)( DBIcf_COMSET | DBIcf_IMPSET | DBIcf_ACTIVE | DBIcf_IADESTROY        \
  246.   /* These are for dbh only:    */                        \
  247.   | DBIcf_AutoCommit | DBIcf_BegunWork    )
  248.  
  249. /* general purpose bit setting and testing macros            */
  250. #define DBIbf_is( bitset,flag)        ((bitset) &   (flag))
  251. #define DBIbf_has(bitset,flag)        DBIbf_is(bitset, flag) /* alias for _is */
  252. #define DBIbf_on( bitset,flag)        ((bitset) |=  (flag))
  253. #define DBIbf_off(bitset,flag)        ((bitset) &= ~(flag))
  254. #define DBIbf_set(bitset,flag,on)    ((on) ? DBIbf_on(bitset, flag) : DBIbf_off(bitset,flag))
  255.  
  256. /* as above, but specifically for DBIc_FLAGS imp flags (except ACTIVE)    */
  257. #define DBIc_is(imp, flag)    DBIbf_is( DBIc_FLAGS(imp), flag)
  258. #define DBIc_has(imp,flag)    DBIc_is(imp, flag) /* alias for DBIc_is */
  259. #define DBIc_on(imp, flag)    DBIbf_on( DBIc_FLAGS(imp), flag)
  260. #define DBIc_off(imp,flag)    DBIbf_off(DBIc_FLAGS(imp), flag)
  261. #define DBIc_set(imp,flag,on)    DBIbf_set(DBIc_FLAGS(imp), flag, on)
  262.  
  263. #define DBIc_COMSET(imp)    DBIc_is(imp, DBIcf_COMSET)
  264. #define DBIc_COMSET_on(imp)    DBIc_on(imp, DBIcf_COMSET)
  265. #define DBIc_COMSET_off(imp)    DBIc_off(imp,DBIcf_COMSET)
  266.  
  267. #define DBIc_IMPSET(imp)    DBIc_is(imp, DBIcf_IMPSET)
  268. #define DBIc_IMPSET_on(imp)    DBIc_on(imp, DBIcf_IMPSET)
  269. #define DBIc_IMPSET_off(imp)    DBIc_off(imp,DBIcf_IMPSET)
  270.  
  271. #define DBIc_ACTIVE(imp)    (DBIc_FLAGS(imp) &   DBIcf_ACTIVE)
  272. #define DBIc_ACTIVE_on(imp)    /* adjust parent's active kid count */    \
  273.     do {                                \
  274.     imp_xxh_t *ph_com = DBIc_PARENT_COM(imp);            \
  275.     if (!DBIc_ACTIVE(imp) && ph_com && !dirty            \
  276.         && ++DBIc_ACTIVE_KIDS(ph_com) > DBIc_KIDS(ph_com))    \
  277.         croak("panic: DBI active kids (%d) > kids (%d)",        \
  278.         DBIc_ACTIVE_KIDS(ph_com), DBIc_KIDS(ph_com));        \
  279.     DBIc_FLAGS(imp) |=  DBIcf_ACTIVE;                \
  280.     } while(0)
  281. #define DBIc_ACTIVE_off(imp)    /* adjust parent's active kid count */    \
  282.     do {                                \
  283.     imp_xxh_t *ph_com = DBIc_PARENT_COM(imp);            \
  284.     if (DBIc_ACTIVE(imp) && ph_com && !dirty            \
  285.         && (--DBIc_ACTIVE_KIDS(ph_com) > DBIc_KIDS(ph_com)    \
  286.            || DBIc_ACTIVE_KIDS(ph_com) < 0) )            \
  287.         croak("panic: DBI active kids (%d) < 0 or > kids (%d)",    \
  288.         DBIc_ACTIVE_KIDS(ph_com), DBIc_KIDS(ph_com));        \
  289.     DBIc_FLAGS(imp) &= ~DBIcf_ACTIVE;                \
  290.     } while(0)
  291.  
  292. #define DBIc_IADESTROY(imp)    (DBIc_FLAGS(imp) &   DBIcf_IADESTROY)
  293. #define DBIc_IADESTROY_on(imp)    (DBIc_FLAGS(imp) |=  DBIcf_IADESTROY)
  294. #define DBIc_IADESTROY_off(imp)    (DBIc_FLAGS(imp) &= ~DBIcf_IADESTROY)
  295.  
  296. #define DBIc_WARN(imp)       (DBIc_FLAGS(imp) &   DBIcf_WARN)
  297. #define DBIc_WARN_on(imp)    (DBIc_FLAGS(imp) |=  DBIcf_WARN)
  298. #define DBIc_WARN_off(imp)    (DBIc_FLAGS(imp) &= ~DBIcf_WARN)
  299.  
  300. #define DBIc_COMPAT(imp)       (DBIc_FLAGS(imp) &   DBIcf_COMPAT)
  301. #define DBIc_COMPAT_on(imp)    (DBIc_FLAGS(imp) |=  DBIcf_COMPAT)
  302. #define DBIc_COMPAT_off(imp)    (DBIc_FLAGS(imp) &= ~DBIcf_COMPAT)
  303.  
  304.  
  305. #ifdef IN_DBI_XS        /* get Handle Common Data Structure    */
  306. #define DBIh_COM(h)             (dbih_getcom2(h, 0))
  307. #else
  308. #define DBIh_COM(h)             (DBIS->getcom(h))
  309. #define neatsvpv(sv,len)           (DBIS->neat_svpv(sv,len))
  310. #endif
  311.  
  312.  
  313. /* --- Implementors Private Data Support --- */
  314.  
  315. #define D_impdata(name,type,h)    type *name = (type*)(DBIh_COM(h))
  316. #define D_imp_drh(h) D_impdata(imp_drh, imp_drh_t, h)
  317. #define D_imp_dbh(h) D_impdata(imp_dbh, imp_dbh_t, h)
  318. #define D_imp_sth(h) D_impdata(imp_sth, imp_sth_t, h)
  319. #define D_imp_xxh(h) D_impdata(imp_xxh, imp_xxh_t, h)
  320.  
  321. #define D_imp_from_child(name,type,child)    \
  322.                 type *name = (type*)(DBIc_PARENT_COM(child))
  323. #define D_imp_drh_from_dbh D_imp_from_child(imp_drh, imp_drh_t, imp_dbh)
  324. #define D_imp_dbh_from_sth D_imp_from_child(imp_dbh, imp_dbh_t, imp_sth)
  325.  
  326. #define DBI_IMP_SIZE(n,s) sv_setiv(perl_get_sv((n), GV_ADDMULTI), (s)) /* XXX */
  327.  
  328.  
  329.  
  330. /* --- Event Support (VERY LIABLE TO CHANGE) --- */
  331.  
  332. /* #define DBIh_EVENTx(h,t,a1,a2) (DBIS->event((h), (t), (a1), (a2))) */
  333. #define DBIh_EVENTx(h,t,a1,a2)    /* deprecated */ &PL_sv_no
  334. #define DBIh_EVENT0(h,t)    DBIh_EVENTx((h), (t), &PL_sv_undef, &PL_sv_undef)
  335. #define DBIh_EVENT1(h,t, a1)    DBIh_EVENTx((h), (t), (a1),         &PL_sv_undef)
  336. #define DBIh_EVENT2(h,t, a1,a2)    DBIh_EVENTx((h), (t), (a1),         (a2))
  337.  
  338. #define ERROR_event    "ERROR"
  339. #define WARN_event    "WARN"
  340. #define MSG_event    "MESSAGE"
  341. #define DBEVENT_event    "DBEVENT"
  342. #define UNKNOWN_event    "UNKNOWN"
  343.  
  344.  
  345. /* --- Handy Macros --- */
  346.  
  347. #define DBIh_CLEAR_ERROR(imp_xxh) (void)( \
  348.     (void)SvOK_off(DBIc_ERR(imp_xxh)),        \
  349.     (void)SvOK_off(DBIc_ERRSTR(imp_xxh)),    \
  350.     (SvPOK(DBIc_STATE(imp_xxh)) ? SvCUR(DBIc_STATE(imp_xxh))=0 : 0)    \
  351.     )
  352.  
  353.  
  354. /* --- DBI State Structure --- */
  355.  
  356. struct dbistate_st {
  357.  
  358. #define DBISTATE_VERSION  94    /* Must change whenever dbistate_t does    */
  359.  
  360.     /* this must be the first member in structure            */
  361.     void (*check_version) _((char *name,
  362.         int dbis_cv, int dbis_cs, int need_dbixs_cv,
  363.         int drc_s, int dbc_s, int stc_s, int fdc_s));
  364.  
  365.     /* version and size are used to check for DBI/DBD version mis-match    */
  366.     U16 version;    /* version of this structure            */
  367.     U16 size;
  368.     U16 xs_version;    /* version of the overall DBIXS / DBD interface    */
  369.     U16 spare_pad;
  370.  
  371.     I32 debug;
  372.     PerlIO *logfp;
  373.  
  374.     /* pointers to DBI functions which the DBD's will want to use    */
  375.     char      * (*neat_svpv)    _((SV *sv, STRLEN maxlen));
  376.     imp_xxh_t * (*getcom)    _((SV *h));    /* see DBIh_COM macro    */
  377.     void        (*clearcom)    _((imp_xxh_t *imp_xxh));
  378.     SV        * (*event)    _((SV *h, char *name, SV*, SV*));
  379.     int         (*set_attr_k)    _((SV *h, SV *keysv, int dbikey, SV *valuesv));
  380.     SV        * (*get_attr_k)    _((SV *h, SV *keysv, int dbikey));
  381.     AV        * (*get_fbav)    _((imp_sth_t *imp_sth));
  382.     SV        * (*make_fdsv)    _((SV *sth, char *imp_class, STRLEN imp_size, char *col_name));
  383.     int         (*bind_as_num)    _((int sql_type, int p, int s, int *t, void *v));
  384.     int         (*hash)        _((char *string, long i));
  385.     SV        * (*preparse)    _((SV *sth, char *statement, IV ps_return, IV ps_accept, void *foo));
  386.  
  387.     SV *neatsvpvlen;        /* only show dbgpvlen chars when debugging pv's    */
  388.  
  389.     PerlInterpreter * thr_owner;    /* thread that owns this dbistate    */
  390.  
  391.     int         (*logmsg)    _((imp_xxh_t *imp_xxh, char *fmt, ...));
  392.     int         (*set_err)    _((imp_xxh_t *imp_xxh, char *fmt, ...));
  393.  
  394.     void *pad2[7];
  395. };
  396.  
  397. /* macros for backwards compatibility */
  398. #define set_attr(h, k, v)    set_attr_k(h, k, 0, v)
  399. #define get_attr(h, k)        get_attr_k(h, k, 0)
  400.  
  401. #define DBISTATE_PERLNAME "DBI::_dbistate"
  402. #define DBISTATE_ADDRSV   (perl_get_sv(DBISTATE_PERLNAME, 0x05))
  403. #define DBILOGFP    (DBIS->logfp)
  404. #ifdef IN_DBI_XS
  405. #define DBILOGMSG    (dbih_logmsg)
  406. #else
  407. #define DBILOGMSG    (DBIS->logmsg)
  408. #endif
  409.  
  410.  
  411. /* --- perl object (ActiveState) / multiplicity hooks and hoops --- */
  412. /* note that USE_ITHREADS implies MULTIPLICITY                      */
  413. #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
  414.  
  415. # define DBISTATE_DECLARE typedef int dummy_dbistate /* keep semicolon from feeling lonely */
  416. # define DBISTATE_ASSIGN(st)
  417. # define DBISTATE_INIT
  418. # undef DBIS
  419. # define DBIS (*(INT2PTR(dbistate_t**, &SvIVX(DBISTATE_ADDRSV))))
  420. /* 'dbis' is temp for bad drivers using 'dbis' instead of 'DBIS' */
  421. # define dbis (*(INT2PTR(dbistate_t**, &SvIVX(DBISTATE_ADDRSV))))
  422.  
  423. #else    /* plain and simple non perl object / multiplicity case */
  424.  
  425. # define DBISTATE_DECLARE    static dbistate_t *DBIS
  426. # define DBISTATE_ASSIGN(st)    (DBIS = (st))
  427. # define DBISTATE_INIT_DBIS    DBISTATE_ASSIGN(INT2PTR(dbistate_t*, SvIV(DBISTATE_ADDRSV)))
  428. # define DBISTATE_INIT {    /* typically use in BOOT: of XS file    */    \
  429.     DBISTATE_INIT_DBIS;    \
  430.     if (DBIS == NULL)    \
  431.     croak("Unable to get DBI state from %s at %p. DBI not loaded.", DBISTATE_PERLNAME, DBISTATE_ADDRSV); \
  432.     DBIS->check_version(__FILE__, DBISTATE_VERSION, sizeof(*DBIS), NEED_DBIXS_VERSION, \
  433.         sizeof(dbih_drc_t), sizeof(dbih_dbc_t), sizeof(dbih_stc_t), sizeof(dbih_fdc_t) \
  434.     ); \
  435. }
  436. #endif
  437.  
  438.  
  439. /* --- Assorted Utility Macros    --- */
  440.  
  441. #define DBD_ATTRIB_OK(attribs)    /* is this a usable attrib value */    \
  442.     (attribs && SvROK(attribs) && SvTYPE(SvRV(attribs))==SVt_PVHV)
  443.  
  444. /* If attribs value supplied then croak if it's not a hash ref.        */
  445. /* Also map undef to Null. Should always be called to pre-process the    */
  446. /* attribs value. One day we may add some extra magic in here.        */
  447. #define DBD_ATTRIBS_CHECK(func, h, attribs)    \
  448.     if ((attribs) && SvOK(attribs)) {        \
  449.     STRLEN lna1=0, lna2=0;            \
  450.     if (!SvROK(attribs) || SvTYPE(SvRV(attribs))!=SVt_PVHV)        \
  451.         croak("%s->%s(...): attribute parameter '%s' is not a hash ref",    \
  452.             SvPV(h,lna1), func, SvPV(attribs,lna2));        \
  453.     } else (attribs) = Nullsv
  454.  
  455. #define DBD_ATTRIB_GET_SVP(attribs, key,klen)            \
  456.     (DBD_ATTRIB_OK(attribs)                    \
  457.         ? hv_fetch((HV*)SvRV(attribs), key,klen, 0)        \
  458.         : (SV **)Nullsv)
  459.     
  460. #define DBD_ATTRIB_GET_IV(attribs, key,klen, svp, var)            \
  461.     if ((svp=DBD_ATTRIB_GET_SVP(attribs, key,klen)) != NULL)    \
  462.         var = SvIV(*svp)
  463.  
  464. #define DBD_ATTRIB_GET_BOOL(attribs, key,klen, svp, var)        \
  465.     if ((svp=DBD_ATTRIB_GET_SVP(attribs, key,klen)) != NULL)    \
  466.         var = SvTRUE(*svp)
  467.  
  468. #define DBD_ATTRIB_TRUE(attribs, key,klen, svp)                \
  469.     (  ((svp=DBD_ATTRIB_GET_SVP(attribs, key,klen)) != NULL)    \
  470.         ? SvTRUE(*svp) : 0 )
  471.  
  472. #define DBD_ATTRIB_GET_PV(attribs, key,klen, svp, dflt)            \
  473.     (((svp=DBD_ATTRIB_GET_SVP(attribs, key,klen)) != NULL)        \
  474.         ? SvPV_nolen(*svp) : (dflt))
  475.  
  476. #define DBD_ATTRIB_DELETE(attribs, key, klen)            \
  477.     hv_delete((HV*)attribs, key, len, G_DISCARD)
  478.  
  479. #endif /* DBIXS_VERSION */
  480. /* end of DBIXS.h */
  481.