home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WDATAOBJ.HPP < prev    next >
C/C++ Source or Header  |  1996-09-20  |  14KB  |  491 lines

  1.  
  2. #ifndef _WDATAOBJ_HPP_INCLUDED
  3. #define _WDATAOBJ_HPP_INCLUDED
  4.  
  5. //-----------------------------------------------------------------------
  6. //
  7. // Interfaces used to implement the binding of objects to data sources.
  8. //
  9. // These interfaces are meant to be embedded in other objects.  All
  10. // methods are virtual so that specializations can be created.
  11. //
  12. //-----------------------------------------------------------------------
  13.  
  14. #ifndef _WOBJECT_HPP_INCLUDED
  15. #include "wobject.hpp"
  16. #endif
  17. #ifndef _WEVENT_HPP_INCLUDED
  18. #include "wevent.hpp"
  19. #endif
  20. #ifndef _WBUFFER_HPP_INCLUDED
  21. #include "wbuffer.hpp"
  22. #endif
  23. #ifndef _WSTRING_HPP_INCLUDED
  24. #include "wstring.hpp"
  25. #endif
  26.  
  27. // Since we use the same types as ODBC, we include the header file,
  28. // even if these classes are not ODBC-specific.  Note that if needed,
  29. // the windows header file must be included before it.
  30.  
  31. #ifndef _WODBC_HPP_INCLUDED
  32. #include "wodbc.hpp"
  33. #endif
  34.  
  35. // Errors
  36.  
  37. #define WDV_NO_ERROR          0
  38. #define WDV_ERROR_OUTOFMEMORY 1
  39. #define WDV_ERROR_TRUNCATION  2
  40. #define WDV_ERROR_NOVALUE     3
  41. #define WDV_ERROR_CONVERSION  4
  42.  
  43. class WDataColumn;
  44. class WDataColumnReference;
  45. class WDataSource;
  46. class WDataTarget;
  47. class WDataValueReference;
  48.  
  49. class WDataOpenEventData;
  50. class WDataCloseEventData;
  51. class WDataAvailableEventData;
  52. class WDataRequestEventData;
  53.  
  54. // Define the two kinds of types.  WColumnDataType represents the
  55. // SQL_* data types, whereas WNativeDataType represents the SQL_C_*
  56. // data types.  We define separate types so that it is obvious
  57. // which is meant to be used.
  58.  
  59. typedef int WColumnDataType;
  60. typedef int WNativeDataType;
  61.  
  62. //
  63. // WDataValue -- Represents the value of an individual cell in
  64. //               the data source.
  65. //
  66.  
  67. class WCMCLASS WDataValue : public WObject {
  68.  
  69.     friend class WDataValueReference;
  70.  
  71.     public:
  72.  
  73.         WDataValue();
  74.         WDataValue( const WDataValue & val );
  75.         WDataValue( const WString & str );
  76.         WDataValue( const WBuffer & buf );
  77.         WDataValue( SDOUBLE s );
  78.         WDataValue( SDWORD s );
  79.         WDataValue( const WChar *str );
  80.         WDataValue( WDataValueReference *ref, WBool bumpRef );
  81.  
  82.         virtual ~WDataValue();
  83.  
  84.         WDataValue & operator=( const WDataValue & val );
  85.  
  86.         /***************************************************************
  87.          * Methods
  88.          ***************************************************************/
  89.  
  90.         // Clear
  91.         //
  92.         //    Remove any value.
  93.  
  94.         WBool Clear();
  95.  
  96.         // CopyInto
  97.  
  98.         WBool CopyInto( const WDataValue & val );
  99.  
  100.         // CopyOnWrite
  101.  
  102.         void CopyOnWrite( WBool force );
  103.  
  104.         // Create
  105.  
  106.         WBool Create();
  107.         WBool Create( SDWORD s );
  108.         WBool Create( SDOUBLE s );
  109.         WBool Create( const WString & str );
  110.         WBool Create( const WBuffer & buf );
  111.         WBool Create( const WDataValue & val );
  112.         WBool Create( const WChar *str );
  113.         WBool Create( WDataValueReference *ref, WBool bumpRef );
  114.  
  115.         /***************************************************************
  116.          * Properties
  117.          ***************************************************************/
  118.  
  119.         // ColumnType
  120.         //
  121.         //    Returns the column type of the value.  The column type
  122.         //    is used to determine how comparisons are done.
  123.  
  124.         WColumnDataType GetColumnType() const;
  125.         WBool           SetColumnType( WColumnDataType t );
  126.  
  127.         // Data
  128.         //
  129.         //    Returns a pointer to the raw data inside the value.  The
  130.         //    actual pointer type depends on what GetType() returns.
  131.  
  132.         const void *GetData() const;
  133.  
  134.         // ErrorCode
  135.  
  136.         WDWord GetErrorCode() const;
  137.         void   SetErrorCode( WDWord errCode );
  138.  
  139.         // Length
  140.         //
  141.         //    The length of the data inside the buffer.
  142.  
  143.         WULong GetLength() const;
  144.         WBool  SetLength( WULong length );
  145.  
  146.         // Null
  147.         //
  148.         //    Returns TRUE if the value is a null value.
  149.  
  150.         WBool GetNull() const { return !_ref; }
  151.  
  152.         // Size
  153.  
  154.         WULong GetSize() const;
  155.         WBool  SetSize( WULong size );
  156.  
  157.         // Type
  158.         //
  159.         //    The current type of the value.  Setting the type will
  160.         //    do a conversion if possible.
  161.  
  162.         WNativeDataType GetType() const;
  163.         WBool           SetType( WNativeDataType type );
  164.  
  165.         // Value
  166.         //
  167.         //    Covers for some of the more specific conversion methods below.
  168.         //    The concept of a 'value' property is convenient, but
  169.         //    because of C++ overloading rules there is a certain
  170.         //    loss of accuracy and possibility for ambiguity in choosing
  171.         //    which function to use.
  172.  
  173.         WBool GetValue( WString & str ) const;
  174.         WBool GetValue( SWORD & s ) const;
  175.         WBool GetValue( UWORD & u ) const;
  176.         WBool GetValue( SDWORD & s ) const;
  177.         WBool GetValue( UDWORD & u ) const;
  178.         WBool GetValue( SFLOAT & f ) const;
  179.         WBool GetValue( SDOUBLE & d ) const;
  180.         WBool GetValue( SCHAR & c ) const;
  181.         WBool GetValue( UCHAR & c ) const;
  182.         WBool GetValue( WBuffer & buf ) const;
  183.         WBool GetValue( DATE_STRUCT & d ) const;
  184.         WBool GetValue( TIME_STRUCT & t ) const;
  185.         WBool GetValue( TIMESTAMP_STRUCT & t ) const;
  186.  
  187.         WBool SetValue( const WString s );
  188.         WBool SetValue( SWORD s );
  189.         WBool SetValue( UWORD u );
  190.         WBool SetValue( SDWORD s );
  191.         WBool SetValue( UDWORD u );
  192.         WBool SetValue( SFLOAT f );
  193.         WBool SetValue( SDOUBLE d );
  194.         WBool SetValue( SCHAR c );
  195.         WBool SetValue( UCHAR c );
  196.         WBool SetValue( const WBuffer & b );
  197.         WBool SetValue( const DATE_STRUCT & d );
  198.         WBool SetValue( const TIME_STRUCT & t );
  199.         WBool SetValue( const TIMESTAMP_STRUCT & t );
  200.  
  201.         WString GetCHAR() const;
  202.         WBool   GetCHAR( WString & string ) const;
  203.         WBool   SetCHAR( const WString & string );
  204.  
  205.         SWORD   GetSSHORT() const;
  206.         WBool   GetSSHORT( SWORD & s ) const;
  207.         WBool   SetSSHORT( SWORD s );
  208.  
  209.         UWORD   GetUSHORT() const;
  210.         WBool   GetUSHORT( UWORD & u ) const;
  211.         WBool   SetUSHORT( UWORD u );
  212.  
  213.         SDWORD  GetSLONG() const;
  214.         WBool   GetSLONG( SDWORD & s ) const;
  215.         WBool   SetSLONG( SDWORD s );
  216.  
  217.         UDWORD  GetULONG() const;
  218.         WBool   GetULONG( UDWORD & u ) const;
  219.         WBool   SetULONG( UDWORD u );
  220.  
  221.         SFLOAT  WCMRETURNSFLOAT GetFLOAT() const;
  222.         WBool   GetFLOAT( SFLOAT & f ) const;
  223.         WBool   SetFLOAT( SFLOAT f );
  224.  
  225.         SDOUBLE WCMRETURNSFLOAT GetDOUBLE() const;
  226.         WBool   GetDOUBLE( SDOUBLE & d ) const;
  227.         WBool   SetDOUBLE( SDOUBLE d );
  228.  
  229.         UCHAR   GetBIT() const;
  230.         WBool   GetBIT( UCHAR & c ) const;
  231.         WBool   SetBIT( UCHAR c );
  232.  
  233.         SCHAR   GetSTINYINT() const;
  234.         WBool   GetSTINYINT( SCHAR & c ) const;
  235.         WBool   SetSTINYINT( SCHAR c );
  236.  
  237.         UCHAR   GetUTINYINT() const;
  238.         WBool   GetUTINYINT( UCHAR & c ) const;
  239.         WBool   SetUTINYINT( UCHAR c );
  240.  
  241.         WBuffer GetBINARY() const;
  242.         WBool   GetBINARY( WBuffer & buf ) const;
  243.         WBool   SetBINARY( const WBuffer & buf );
  244.  
  245.         BOOKMARK GetBOOKMARK() const;
  246.         WBool    GetBOOKMARK( BOOKMARK & mark ) const;
  247.         WBool    SetBOOKMARK( BOOKMARK mark );
  248.  
  249.         DATE_STRUCT GetDATE() const;
  250.         WBool       GetDATE( DATE_STRUCT & d ) const;
  251.         WBool       SetDATE( const DATE_STRUCT & d );
  252.  
  253.         TIME_STRUCT GetTIME() const;
  254.         WBool       GetTIME( TIME_STRUCT & t ) const;
  255.         WBool       SetTIME( const TIME_STRUCT & t );
  256.  
  257.         TIMESTAMP_STRUCT GetTIMESTAMP() const;
  258.         WBool            GetTIMESTAMP( TIMESTAMP_STRUCT & t ) const;
  259.         WBool            SetTIMESTAMP( const TIMESTAMP_STRUCT & t );
  260.  
  261.         /***************************************************************
  262.          * Static Properties
  263.          ***************************************************************/
  264.  
  265.         static const WDataValue & GetNullValue();
  266.  
  267.         /***************************************************************
  268.          * Operators
  269.          ***************************************************************/
  270.  
  271.         operator SDWORD() const;
  272.         operator UDWORD() const;
  273.         operator SDOUBLE() const;
  274.  
  275.         int operator==( const WDataValue & val ) const;
  276.         int operator!=( const WDataValue & val ) const;
  277.         int operator>( const WDataValue & val ) const;
  278.         int operator<( const WDataValue & val ) const;
  279.         int operator>=( const WDataValue & val ) const;
  280.         int operator<=( const WDataValue & val ) const;
  281.  
  282.         /***************************************************************
  283.          * Data Members
  284.          ***************************************************************/
  285.  
  286.     protected:
  287.  
  288.         WDataValueReference *_ref;
  289.         WDWord               _errorCode;
  290.         wllist_link          _link;
  291. };
  292.  
  293. //
  294. // WDataColumn -- Defines the name, type, etc. of a particular column.
  295. //                Used mostly by the data source.  Reference-counted
  296. //                so that we can safely and easily pass it around and
  297. //                also makes it easier to trap accesses to invalid
  298. //                columns (such as after a query has changed).
  299. //
  300.  
  301. class WCMCLASS WDataColumn : public WObject {
  302.  
  303.     friend class WDataColumnReference;
  304.  
  305.     public:
  306.  
  307.         WDataColumn();
  308.         WDataColumn( const WDataColumn & col );
  309.  
  310.         virtual ~WDataColumn();
  311.  
  312.         WDataColumn & operator=( const WDataColumn & col );
  313.  
  314.         /*********************************************************
  315.          * Properties
  316.          *********************************************************/
  317.  
  318.         // Bound
  319.         //
  320.         //    Returns TRUE if the column has been successfully bound.
  321.  
  322.         WBool GetBound() const;
  323.  
  324.         // CaseSensitive
  325.  
  326.         WBool GetCaseSensitive() const;
  327.  
  328.         // DataSource
  329.         //
  330.         //    The data source that created this column.
  331.  
  332.         WDataSource *GetDataSource() const;
  333.  
  334.         // DisplaySize
  335.         //
  336.         //    The display size for the column.
  337.  
  338.         WDWord GetDisplaySize() const;
  339.  
  340.         // Index
  341.         //
  342.         //    The number of the column from 1 to n.  Returns a
  343.         //    negative value if the column is no longer valid.
  344.  
  345.         WShort GetIndex() const;
  346.  
  347.         // Label
  348.         //
  349.         //    The column label.
  350.  
  351.         WString GetLabel() const;
  352.  
  353.         // Length
  354.         //
  355.         //    The column length.
  356.  
  357.         WDWord GetLength() const;
  358.  
  359.         // Name
  360.         //
  361.         //    The column name.
  362.  
  363.         WString GetName() const;
  364.  
  365.         // NativeType
  366.         //
  367.         //    Returns the default native type that best represents the
  368.         //    actual database type.
  369.  
  370.         WNativeDataType GetNativeType() const;
  371.  
  372.         // Null
  373.         //
  374.         //    Returns TRUE if the column is null (does not exist).
  375.  
  376.         WBool GetNull() const { return !_ref; }
  377.  
  378.         // Nullable
  379.         //
  380.         //    If column accepts NULLs, returns SQL_NULLABLE, otherwise
  381.         //    returns either SQL_NO_NULLS or SQL_NULLABLE_UNKNOWN.
  382.  
  383.         WShort GetNullable() const;
  384.  
  385.         // Owner
  386.         //
  387.         //    The name of the column's table's owner, if known.
  388.  
  389.         WString GetOwner() const;
  390.  
  391.         // Precision
  392.         //
  393.         //    The column precision.
  394.  
  395.         WDWord GetPrecision() const;
  396.  
  397.         // Qualifier
  398.         //
  399.         //    The name of the column's table's qualifier, if known.
  400.         //    See the transaction object as well.
  401.  
  402.         WString GetQualifier() const;
  403.  
  404.         // ReadOnly
  405.         //
  406.         //    Returns TRUE if GetUpdatable() returns SQL_ATTR_READONLY.
  407.  
  408.         WBool GetReadOnly() const;
  409.  
  410.         // Scale
  411.         //
  412.         //    The column scale.
  413.  
  414.         WShort GetScale() const;
  415.  
  416.         // Table
  417.         //
  418.         //    The name of the column's table, if applicable.
  419.  
  420.         WString GetTable() const;
  421.  
  422.         // Type
  423.         //
  424.         //    The column type.  Types are expressed in SQL terms.
  425.  
  426.         WColumnDataType GetType() const;
  427.  
  428.         // Unsigned
  429.         //
  430.         //    Whether or not the column is unsigned.
  431.  
  432.         WBool GetUnsigned() const;
  433.  
  434.         // Updatable
  435.         //
  436.         //    If column is updatable, returns SQL_ATTR_WRITE, otherwise
  437.         //    returns SQL_ATTR_READONLY or SQL_ATTR_READWRITE_UNKNOWN.
  438.  
  439.         WShort GetUpdatable() const;
  440.  
  441.         // Value
  442.         //
  443.         //    Set or get the value of the column in the current row.
  444.  
  445.         WDataValue GetValue() const;
  446.         WBool      SetValue( const WDataValue & value );
  447.  
  448.         /*********************************************************
  449.          * Methods
  450.          *********************************************************/
  451.  
  452.         // Clear
  453.         //
  454.         //     Prepare for re-use. Does not clear the actual column!
  455.  
  456.         WBool Clear();
  457.  
  458.         // Create
  459.         //
  460.  
  461.         WBool Create();
  462.         WBool Create( WDataColumnReference *ref, WBool bumpRef );
  463.         WBool Create( const WDataColumn & col );
  464.  
  465.         // QueryNumericAttribute
  466.         //
  467.         //     Return the value of a numeric attribute.
  468.  
  469.         WLong   QueryNumericAttribute( WUShort attribute ) const;
  470.  
  471.         // QueryStringAttribute
  472.  
  473.         WString QueryStringAttribute( WUShort attribute ) const;
  474.  
  475.         /***************************************************************
  476.          * Static Properties
  477.          ***************************************************************/
  478.  
  479.         static const WDataColumn & GetNullColumn();
  480.  
  481.     protected:
  482.  
  483.         /***************************************************************
  484.          * Data Members
  485.          ***************************************************************/
  486.  
  487.         WDataColumnReference *_ref;
  488. };
  489.  
  490. #endif
  491.