home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / object10 / oset.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  7.1 KB  |  270 lines

  1. /* Copyright (c) Oracle Corporation 1994.  All Rights Reserved */
  2.  
  3. /*
  4.     This source code is provided as a debugging aid for developers
  5.     who have purchased Oracle Objects for OLE    .  Please see the
  6.     online help for documentation of these classes.
  7. */
  8.  
  9. /*
  10.     Oracle Objects for OLE     C++ Classes
  11.     
  12.     This file implements the OFieldCollection, OConnectionCollection and
  13.     OSessionCollection classes
  14.                            
  15.     CREATED    ********   11/22/94
  16.     RWOOLARD    MODIFIED    03/20/95
  17.                 bug#
  18. */
  19.  
  20. #include "windows.h"
  21. #include <ole2.h>
  22. #include <olenls.h>       
  23. #include <dispatch.h>  
  24.  
  25. #ifndef ORACL_ORACLE
  26. #include "oracl.h"
  27. #endif
  28.  
  29. #ifndef ORAOBJI_ORACLE
  30. #include "oraobji.h"
  31. #endif
  32.  
  33. #ifndef _OracleInProcServer_H_
  34. #include <oratlb.h>
  35. #endif
  36.  
  37. static const IID IID_IOraFields =
  38. {0xedbe6700, 0x0562, 0x101b, { 0xad, 0xf2, 0x04, 0x02, 0x1c, 0x00, 0x70, 0x02 } };
  39. static const IID IID_IOraCollection =
  40. {0xdadfdec0, 0x134a, 0x101b, { 0x91, 0x9e, 0x04, 0x02, 0x1c, 0x00, 0x70, 0x02 } };
  41. static const IID IID_IOraConnection =
  42. {0xbea85c00, 0x134a, 0x101b, { 0x91, 0x9e, 0x04, 0x02, 0x1c, 0x00, 0x70, 0x02 } };   
  43. static const IID IID_IOraSession =
  44. {0xa16bec60, 0x134a, 0x101b, { 0x91, 0x9e, 0x04, 0x02, 0x1c, 0x00, 0x70, 0x02 } };
  45. static const IID IID_IOraField =
  46. {0x145f9fa0, 0x0563, 0x101b, { 0xad, 0xf2, 0x04, 0x02, 0x1c, 0x00, 0x70, 0x02 } };
  47.  
  48. // Set type defines (internal use only)
  49. #define SET_TYPE_NONE 0
  50. #define SET_TYPE_SESSION 1
  51. #define SET_TYPE_CONNECTION 2
  52. #define SET_TYPE_FIELD 3
  53.  
  54. // ----- OOracleCollection -----------------------------------------------
  55.  
  56. OOracleCollection::OOracleCollection()
  57. {
  58.     m_settype = SET_TYPE_NONE;
  59. }
  60.  
  61. OOracleCollection::OOracleCollection(const OOracleCollection &other)
  62. {
  63.     m_settype = SET_TYPE_NONE;
  64.     Copy(other);
  65. }
  66.  
  67. OOracleCollection::~OOracleCollection(void) 
  68. {
  69.     Cleanup();
  70. }
  71.  
  72. // overloaded assignment operator
  73. OOracleCollection &OOracleCollection::operator=(const OOracleCollection &other)
  74. {
  75.     if (&other == this)
  76.         return(*this); // self assignment - do nothing
  77.     
  78.     // clear out our old state
  79.     if (OSUCCESS == Cleanup())
  80.     {
  81.         Copy(other); // call copy constructor
  82.     }
  83.     // if the cleanup failed (possible but unlikely) we don't do the copy
  84.     //    and as a result, we pass on the unmodified (or partly cleaned!) object
  85.     
  86.     return(*this);
  87.  
  88. oresult OOracleCollection::Copy(const OOracleCollection &other)
  89. {
  90.     m_settype = other.m_settype;
  91.     
  92.     return(OOracleObject::Copy(other));
  93. }
  94.  
  95. oresult OOracleCollection::Close(void)
  96. {
  97.     return(Cleanup());
  98. }
  99.  
  100. oresult OOracleCollection::Cleanup(void)
  101. {
  102.     m_settype = SET_TYPE_NONE; 
  103.  
  104.     return(OOracleObject::Cleanup());        
  105. }
  106.  
  107. oresult OOracleCollection::OpenSetHelper(void *connect, void *otheri, unsigned char stype)
  108. {
  109.     if (!connect)
  110.     { // couldn't create set - error is on other object
  111.         SetOtherError(otheri);
  112.         return(OFAILURE);
  113.     }
  114.     
  115.     Cleanup();  // drop previous state
  116.     
  117.     HRESULT hc;
  118.     void *tempi;
  119.     if (stype == SET_TYPE_FIELD)
  120.         hc = ((IDispatch *) connect)->QueryInterface(IID_IOraFields, &tempi);
  121.     else    
  122.         hc = ((IDispatch *) connect)->QueryInterface(IID_IOraCollection, &tempi);
  123.     ((IDispatch *) connect)->Release();
  124.     if (FAILED(hc))
  125.     { // couldn't get the interface
  126.         SetInternalError(OERROR_NOINTER);
  127.         return(OFAILURE);
  128.     }
  129.     
  130.     // set object interface (only fields support error interface)
  131.     oresult ores = SetObjectInterface(tempi, stype == SET_TYPE_FIELD);
  132.     
  133.     if (ores == OSUCCESS)
  134.     { // we successfully created the object
  135.         m_settype = stype;    
  136.     }
  137.     
  138.     return(ores);
  139.  
  140. long OOracleCollection::GetCount(void) const
  141. {
  142.     if (ActionStart() != OSUCCESS)
  143.         return(0); // error
  144.     
  145.     long nitems;
  146.     if (m_settype == SET_TYPE_FIELD)
  147.         nitems = ((_IOraFields *) Internal())->get_Count();
  148.     else
  149.         nitems = ((_IOraCollection *) Internal())->get_Count();
  150.     
  151.     return(nitems);
  152. }
  153.  
  154. void *OOracleCollection::GetItem(unsigned char stype, int index) const
  155. {
  156.     void *itemP;
  157.     
  158.     if (stype != m_settype || !IsOpen())
  159.     {
  160.         SetInternalError(OERROR_NOINTER);
  161.         return(0);
  162.     }
  163.     
  164.     // get the dispatch pointer
  165.     VARIANT vres;
  166.     VariantInit(&vres); 
  167.  
  168.     vres.vt = VT_I2;
  169.     vres.iVal = (short) index;          
  170.     IDispatch *connect;
  171.     
  172.     if (m_settype == SET_TYPE_FIELD)
  173.         connect = ((_IOraFields *) Internal())->get_Item(vres); 
  174.     else
  175.         connect = ((_IOraCollection *) Internal())->get_Item(vres); 
  176.     
  177.     if (!connect)
  178.     {  // we know what the error is (caller will refer to us)
  179.         return(0);
  180.     }
  181.     
  182.     // now get the actual interface
  183.     
  184.     HRESULT hc;
  185.     
  186.     if (m_settype == SET_TYPE_CONNECTION)
  187.         hc = connect->QueryInterface(IID_IOraConnection, &itemP);
  188.     else if (m_settype == SET_TYPE_SESSION)
  189.         hc = connect->QueryInterface(IID_IOraSession, &itemP);
  190.     else if (m_settype == SET_TYPE_FIELD)
  191.         hc = connect->QueryInterface(IID_IOraField, &itemP);     
  192.         
  193.     connect->Release();
  194.     if (FAILED(hc))
  195.     { // couldn't get the interface
  196.         SetInternalError(OERROR_NOINTER);
  197.         return(0);
  198.         // now caller will refer to our error interface - will that know what the error is?
  199.     }
  200.     
  201.     return(itemP);
  202. }
  203.  
  204. // ----- OFieldCollection -----------------------------------------------
  205.  
  206. oresult OFieldCollection::OpenHelper(void *connect, void *otheri)
  207. {
  208.     return(OpenSetHelper(connect, otheri, SET_TYPE_FIELD));
  209. }
  210.  
  211. OField OFieldCollection::GetField(int index) const
  212. {
  213.     OField of;
  214.     
  215.     if (ActionGetStart(&of) != OSUCCESS)
  216.         return(of); // returning unopened object - indicates error
  217.     
  218.     // get new field from collection
  219.     void *tempi = GetItem(SET_TYPE_FIELD, index);    
  220.     
  221.     of.OpenHelper(tempi, Internal());
  222.     return(of);   
  223. }
  224.  
  225. // ----- OConnectionCollection -----------------------------------------------
  226.  
  227. oresult OConnectionCollection::OpenHelper(void *connect, void *otheri)
  228. {
  229.     return(OpenSetHelper(connect, otheri, SET_TYPE_CONNECTION));
  230. }
  231.  
  232. OConnection OConnectionCollection::GetConnection(int index) const
  233. {                                             
  234.     OConnection conn;
  235.     
  236.     if (ActionGetStart(&conn) != OSUCCESS)
  237.         return(conn); // returning unopened object - indicates error
  238.     
  239.     // get new session from collection
  240.     void *tempi = GetItem(SET_TYPE_CONNECTION, index);
  241.     
  242.     conn.OpenHelper(tempi, Internal());
  243.     
  244.     return(conn);
  245. }
  246.  
  247. // ----- OSessionCollection -----------------------------------------------
  248.  
  249. oresult OSessionCollection::OpenHelper(void *connect, void *otheri)
  250. {
  251.     return(OpenSetHelper(connect, otheri, SET_TYPE_SESSION));
  252. }
  253.  
  254. OSession OSessionCollection::GetSession(int index) const
  255. {
  256.     OSession sess;
  257.     
  258.     if (ActionGetStart(&sess) != OSUCCESS)
  259.         return(sess); // returning unopened object - indicates error
  260.     
  261.     // get new session from collection
  262.     void *tempi = GetItem(SET_TYPE_SESSION, index);
  263.     sess.OpenHelper(tempi, Internal());
  264.     
  265.     return(sess);
  266. }
  267.  
  268.