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