Wtx ~ Wt Extension Library
WtxLib
QueryModel.h
1 
2 #ifndef __WTX_QUERYMODEL_H___
3 #define __WTX_QUERYMODEL_H___
4 
5 #include <Wt/Dbo/QueryModel.h>
6 
7 #include <Wtx/TableViewDef.h>
8 
9 namespace Wtx {
10  namespace Dbo {
11 
12 template <class C>
14 : public Wt::Dbo::QueryModel<C>
15 {
16  typedef Wt::Dbo::QueryModel<C> Base;
17 
18  public:
19 
20  virtual int rowCount( const Wt::WModelIndex & parent = Wt::WModelIndex() ) const
21  {
22  if( m_countTable != "" && session() )
23  {
24  if( m_cachedRowCount == -1 )
25  {
26  /*
27  ** the following is the normal way to quickly count
28  ** rows in a table. It supports the addition of
29  ** a where clause that can affect the row count.
30  **
31  */
32 #ifndef DO_TRADITIONAL_COUNT
33  auto query =
34  Wt::WString( "SELECT COUNT(1) FROM \"{1}\" " )
35  .arg( m_countTable )
36  .toUTF8()
37  ;
38 #else
39  /*
40  ** the following is not a normal way to quickly count
41  ** rows in a table. While it can count the raw row
42  ** count, it does not support filters on the data
43  ** and it is also subject to count accuracy deterioration
44  ** if vacuum is not run periodically on the system.
45  **
46  ** https://wiki.postgresql.org/wiki/Count_estimate
47  **
48  */
49  auto query =
50  Wt::WString( "SELECT reltuples::BIGINT FROM pg_class WHERE relname = '{1}'" )
51  .arg( m_countTable )
52  .toUTF8()
53  ;
54 #endif
55 
56  if( m_countWhere != "" )
57  {
58  query += m_countWhere;
59  }
60 
61  Wt::Dbo::Transaction t(*session());
62  m_cachedRowCount = session()-> template query<int>(query);
63 
64  } // endif( m_cachedRowCount == -1 )
65 
66  return m_cachedRowCount;
67 
68  } // endif( m_countTable != "" && session() )
69 
70  return Wt::Dbo::QueryModel<C>::rowCount( parent );
71 
72  } // endvirtual int rowCount( const Wt::WModelIndex & parent = Wt::WModelIndex() ) const
73 
74  Wt::Dbo::Session * session() const
75  {
76  return m_session;
77  }
78 
79  void setSession( Wt::Dbo::Session * value )
80  {
81  m_session = value;
82  }
83 
84  void setCountTable( const std::string & table )
85  {
86  m_countTable = table;
87  }
88 
89  void setCountWhere( const std::string & where )
90  {
91  m_countWhere = where;
92  m_cachedRowCount = -1;
93  }
94 
95  void setViewDef( Wtx::TableViewDef * viewDef )
96  {
97  m_viewDef = viewDef;
98  }
99 
100  virtual Wt::WFlags<Wt::ItemFlag> flags( const Wt::WModelIndex & index ) const
101  {
102 // std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
103 
104  auto result = Base::flags(index);
105 
106  if( index.column() == 0 )
107  result |= Wt::ItemFlag::UserCheckable;
108 
109 // result |= Wt::ItemFlag::Editable;
110 
111  return result;
112  }
113 
114  virtual Wt::cpp17::any data( const Wt::WModelIndex& index, Wt::ItemDataRole role ) const
115  {
116 
117  if( m_viewDef )
118  {
119  if( m_viewDef-> m_def.checkBoxSelect )
120  {
121  if( index.column() == 0 && role == Wt::ItemDataRole::Checked )
122  {
123 // if( editSelection_-> find( this->resultRow(index.row()) ) != editSelection_->end() )
124 
125 // return true;
126 // else
127  return false;
128  }
129  }
130  }
131 
132  return Base::data( index, role );
133  }
134 
135  void refreshModelData()
136  {
137 // Base::reset();
138  Base::reload();
139  }
140 
141  Wtx::TableViewDef * m_viewDef = nullptr;
142  Wt::Dbo::Session * m_session = nullptr;
143  std::string m_countTable;
144  std::string m_countWhere;
145  mutable int m_cachedRowCount = -1;
146 
147 }; // endclass QueryModel
148 
149  } // endnamespace Dbo
150 } // endnamespace Wtx
151 
152 #endif // __WTX_QUERYMODEL_H___
153 
154 
Table View Definition.
Definition: TableViewDef.h:134
witty extension library
Definition: Activity.h:51