home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / wsqlstmt.hpp < prev    next >
C/C++ Source or Header  |  1996-11-21  |  9KB  |  253 lines

  1. #ifndef _WSQLSTMT_HPP_INCLUDED
  2. #define _WSQLSTMT_HPP_INCLUDED
  3.  
  4. #ifndef _WSQLCONN_HPP_INCLUDED
  5. #include "wsqlconn.hpp"
  6. #endif
  7.  
  8. class WSQLCorrelation : public WObject
  9. {
  10.     WDeclareSubclass( WSQLCorrelation, WObject );
  11.     public:
  12.     WString     owner;
  13.     WString     name;
  14.     WString     correlation;
  15. };
  16.  
  17. class WSQLColumnExpr : public WString
  18. {
  19.     WDeclareSubclass( WSQLColumnExpr , WString);
  20.     public:
  21.     WSQLColumnExpr( WString name, WString as = WString( "" ) );
  22.     WBool       computed;
  23.     WString     as;
  24. };
  25.  
  26. class WSQLTableExpr : public WSQLCorrelation {
  27.     WDeclareSubclass( WSQLTableExpr, WSQLCorrelation );
  28.     public:
  29.     WVector< WSQLColumnExpr > columns;
  30. };
  31.  
  32. class WSQLWhereExpr : public WObject {
  33.     WDeclareSubclass( WSQLWhereExpr, WObject );
  34.     public:
  35.     WString     expr1;
  36.     WString     op;
  37.     WString     expr2;
  38.     WString     condition;
  39.     WBool       exclusive;
  40. };
  41.  
  42. class WSQLHaveExpr : public WSQLWhereExpr {
  43.     WDeclareSubclass( WSQLHaveExpr, WSQLWhereExpr );
  44. };
  45.  
  46. class WSQLGroupExpr : public WString {
  47.     WDeclareSubclass( WSQLGroupExpr, WString );
  48. };
  49.  
  50. class WSQLJoinExpr : public WObject {
  51.     WDeclareSubclass( WSQLJoinExpr, WObject );
  52.     public:
  53.     WString     type;
  54.     WString     table1;
  55.     WString     correlation1;
  56.     WString     table2;
  57.     WString     correlation2;
  58.     int         tab1;
  59.     int         tab2;
  60.     WVector< WString > how;             // array of join conditions
  61. };
  62.  
  63. class WSQLOrderExpr : public WObject {
  64.     WDeclareSubclass( WSQLOrderExpr, WObject );
  65.     public:
  66.     WString     expr;
  67.     int         ascending;
  68. };
  69.  
  70. extern template WVector< WSQLColumnExpr >;
  71. extern template WVector< WSQLTableExpr >;
  72. extern template WVector< WSQLWhereExpr >;
  73. extern template WVector< WSQLHaveExpr >;
  74. extern template WVector< WSQLJoinExpr >;
  75. extern template WVector< WSQLGroupExpr >;
  76. extern template WVector< WSQLOrderExpr >;
  77. extern template WVector< WSQLCorrelation >;
  78.  
  79. class WCMCLASS WSQLStatement : public WObject {
  80.     WDeclareSubclass( WSQLStatement, WObject );
  81.     struct WSQLToken {
  82.         int             blanks;
  83.         WString         text;
  84.     };
  85.     public:
  86.         WSQLStatement()
  87.             : col_expr( TRUE )
  88.             , tab_expr( TRUE )
  89.             , whr_expr( TRUE )
  90.             , hav_expr( TRUE )
  91.             , joins( TRUE )
  92.             , grp_expr( TRUE )
  93.             , ord_expr( TRUE )
  94.             , _quote_names( TRUE )
  95.             , _use_owner( TRUE )
  96.             , _auto_refresh( TRUE )
  97.             {}
  98.  
  99.         /***********************************************************
  100.          * Properties
  101.          ***********************************************************/
  102.  
  103.         WBool GetQuoteNames()              { return( _quote_names ); }
  104.         void  SetQuoteNames( WBool quote ) { _quote_names = quote; }
  105.  
  106.         WBool GetUseOwner()                { return( _use_owner ); }
  107.         void  SetUseOwner( WBool owner )   { _use_owner = owner; }
  108.  
  109.         WBool GetAutoRefresh()                { return( _auto_refresh ); }
  110.         void  SetAutoRefresh( WBool autoref ) { _auto_refresh = autoref; }
  111.  
  112.         WBool GetDistinct()                 { return( _distinct ); }
  113.         void  SetDistinct( WBool distinct ) { _distinct = distinct; }
  114.  
  115.         /***********************************************************
  116.          * Methods
  117.          ***********************************************************/
  118.  
  119.         WString Parse( WString statement , WSQLConnection *conn = NULL);
  120.         WString Compose();
  121.         
  122.         
  123.         int     NumColumns();
  124.         WString Column( int i );
  125.         void    AddColumn( WString const & );
  126.         void    DeleteColumns();
  127.  
  128.         int     NumComputed();
  129.         WString Computed( int i );
  130.         void    DeleteComputed();
  131.  
  132.  
  133.         WSQLTableExpr *Table( int i ) { return( tab_expr[i] ); }
  134.         int  NumTables()              { return( tab_expr.Count() ); }
  135.         void DeleteTables();
  136.         void AddTable( WString const &owner, WString const &name );
  137.  
  138.         WSQLWhereExpr *Where( int i ) { return( whr_expr[i] ); }
  139.         int NumWheres()               { return( whr_expr.Count() ); }
  140.         void DeleteWheres();
  141.         void AddWhere( WString const &expr, WString const &condition, WBool excl = FALSE );
  142.  
  143.         WSQLHaveExpr *Have( int i )   { return( hav_expr[i] ); }
  144.         int  NumHaves()               { return( hav_expr.Count() ); }
  145.         void DeleteHaves();
  146.         void AddHave( WString const &, WString const &condition );
  147.  
  148.         WSQLJoinExpr *Join( int i )   { return( joins[i] ); }
  149.         int  NumJoins()               { return( joins.Count() ); }
  150.         void DeleteJoins();
  151.         void AddJoin( WString const &tab1, WString const &tab2, WString const &type );
  152.  
  153.         WSQLGroupExpr *Group( int i ) { return( grp_expr[i] ); }
  154.         int  NumGroups()              { return( grp_expr.Count() ); }
  155.         void DeleteGroups();
  156.         void AddGroup( WString const & );
  157.  
  158.         WSQLOrderExpr *Order( int i ) { return( ord_expr[i] ); }
  159.         int  NumOrders() { return( ord_expr.Count() ); }
  160.         void DeleteOrders();
  161.         void AddOrder( WString const &, int );
  162.         
  163.         int     NumJoinConds( int i ) { return( joins[i]->how.Count() ); }
  164.         WString JoinCond( int i, int j ) { return( *(joins[i]->how[j]) ); }
  165.         void    DeleteJoinConds( int i );
  166.         void    AddJoinCond( int i, WString const &condition );
  167.  
  168.         void ParseDot( WString , WString &, WString & );
  169.  
  170.         WBool ParseJoinCond( WString const &str, WVector<WString> &list );
  171.         void Update();
  172.  
  173.     private:
  174.         WBool                           _distinct;
  175.         WVector< WSQLColumnExpr >       col_expr;
  176.         WVector< WSQLTableExpr >        tab_expr;
  177.         WVector< WSQLWhereExpr >        whr_expr;
  178.         WVector< WSQLHaveExpr >         hav_expr;
  179.         WVector< WSQLJoinExpr >         joins;
  180.         WVector< WSQLGroupExpr >        grp_expr;
  181.         WVector< WSQLOrderExpr >        ord_expr;
  182.         WVector< WSQLCorrelation >      correlations;
  183.  
  184.         // parser functions
  185.  
  186.         void save_parse( WString const &expr );
  187.         void restore_parse();
  188.         void parse_as( WString const &expr, WString &name, WString &as );
  189.         WBool get_quoted_token( WWidestChar quote );
  190.         void get_numeric_token( void );
  191.         void get_identifier( void );
  192.         void get_token( void );
  193.         WBool in_keyword_range( WString token, int kw_1, int kw_2 );
  194.         WString add_token( WString str );
  195.         WString parse_expr( WBool stop_on_comma, WBool stop_on_bool, int kw_1, int kw_2 );
  196.         WBool parse_select( void );
  197.         int add_table( WString expr );
  198.         WBool parse_from( void );
  199.         WBool parse_where( void );
  200.         WBool parse_group();
  201.         WBool parse_having();
  202.         WBool parse_order();
  203.         WBool is_operator( WString p, int *op_len );
  204.         void fix_table_exprs();
  205.         void fix_where_expr( WSQLWhereExpr *exp );
  206.         void fix_correlations();
  207.         void fix_tables();
  208.         void fix_where_exprs( WVector< WSQLWhereExpr > &exp );
  209.         void fix_have_exprs( WVector< WSQLHaveExpr > &exp );
  210.  
  211.         // parser data
  212.  
  213.         WString         _str;
  214.         WULong          _bufidx;
  215.         char            const *_bufptr;
  216.         WSQLToken       _token;
  217.         WString         _saveStr;
  218.         WULong          _saveBufidx;
  219.         char            const *_saveBufptr;
  220.         WSQLToken       _saveToken;
  221.         WString         _errormsg;
  222.  
  223.         // compose functions
  224.  
  225.         WString quote( WString const &str, WChar quote_char = '"' );
  226.         WString build_select();
  227.         WString build_from();
  228.         WString build_orderby();
  229.         WString build_where();
  230.         WString build_groupby();
  231.         WString build_having();
  232.         WString substitute_correlation_names( WString info );
  233.         WString Translate( WString x ) { return( x ); }
  234.         int get_table_index( WString info );
  235.         int get_column_index( WString expr );
  236.  
  237.         // compose data
  238.  
  239.         WBool           _quote_names;
  240.         WBool           _use_owner;
  241.         WBool           _auto_refresh;
  242.         WSQLConnection *_connection;
  243. };
  244.  
  245. extern WBool             ParseSQL( WString, WSQLStatement *, WString * );
  246. #if 0
  247. extern char             *ParseWhere( char * );
  248. extern void             ParseColName( char *, unsigned, char *, unsigned );
  249. extern void             FreeParse( WSQLStatement * );
  250. #endif
  251.  
  252. #endif
  253.