Wolframe, 0.0.3

valueTupleSet.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 #ifndef _DATABASE_VIRTUAL_MACHINE_VALUE_TYPLE_SET_HPP_INCLUDED
36 #define _DATABASE_VIRTUAL_MACHINE_VALUE_TYPLE_SET_HPP_INCLUDED
37 #include "types/variant.hpp"
38 #include <string>
39 #include <vector>
40 #include <cstdlib>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/algorithm/string.hpp>
43 
44 namespace _Wolframe {
45 namespace db {
46 namespace vm {
47 
51 {
52 public:
56  explicit ValueTupleSet( const std::vector<std::string>& colnames_)
57  :m_colnames(colnames_){}
61 
62  friend class const_iterator;
63 
67  {
68  public:
70  :m_idx(0)
71  {
72  static const ValueTupleSet v;
73  m_ref = &v;
74  }
75  explicit const_iterator( const ValueTupleSet* ref_, std::size_t idx_=0)
76  :m_idx(idx_),m_ref(ref_){}
78  :m_idx(o.m_idx),m_ref(o.m_ref){}
80  {
81  if (m_ref)
82  {
83  m_idx += m_ref->m_colnames.size();
84  }
85  return *this;
86  }
88  {
89  const_iterator rt(*this);
90  operator++();
91  return rt;
92  }
93 
94  std::size_t index() const
95  {
96  return m_idx;
97  }
98 
99  bool operator==( const const_iterator& o) const
100  {
101  return (m_ref == o.m_ref && m_idx == o.m_idx);
102  }
103  bool operator!=( const const_iterator& o) const
104  {
105  return (m_ref != o.m_ref || m_idx != o.m_idx);
106  }
107 
108  const const_iterator* operator->() const {return this;}
109  const types::Variant& column( std::size_t i) const
110  {
111  if (!m_ref || i == 0 || i > m_ref->m_colnames.size() || m_idx + i - 1 >= m_ref->m_ar.size())
112  {
113  throw std::runtime_error("value tuple set column index out of range");
114  }
115  return m_ref->m_ar.at( m_idx + i - 1);
116  }
117 
118  private:
119  std::size_t m_idx; //< index of currently visited element
120  const ValueTupleSet* m_ref; //< reference to visited set of tuples
121  };
122 
125  {
126  return const_iterator( this);
127  }
130  {
131  return const_iterator( this, m_ar.size());
132  }
133 
135  std::size_t nofColumns() const
136  {
137  return m_colnames.size();
138  }
140  const std::string& columnName( std::size_t i) const
141  {
142  if (i == 0 || i > m_colnames.size()) throw std::runtime_error( "column index out of range");
143  return m_colnames.at(i-1);
144  }
145 
147  std::size_t columnIndex( const std::string& name) const
148  {
149  std::size_t ii=0;
150  for (; ii<m_colnames.size(); ++ii)
151  {
152  if (boost::algorithm::iequals( name, m_colnames.at(ii)))
153  {
154  return ii+1;
155  }
156  }
157  throw std::runtime_error("unknown value set column name");
158  }
159 
161  void push( const std::vector<types::VariantConst>& c)
162  {
163  if (c.size() != m_colnames.size()) throw std::runtime_error("pushed result with non matching number of columns");
164  for (std::vector<types::VariantConst>::const_iterator ci=c.begin(), ce=c.end(); ci != ce; ++ci)
165  {
166  m_ar.push_back( *ci);
167  }
168  }
169 
171  void push( const std::vector<types::Variant>& c)
172  {
173  if (c.size() != m_colnames.size()) throw std::runtime_error("pushed result with non matching number of columns");
174  for (std::vector<types::Variant>::const_iterator ci=c.begin(), ce=c.end(); ci != ce; ++ci)
175  {
176  m_ar.push_back( *ci);
177  }
178  }
179 
182  std::size_t size() const
183  {
184  return m_ar.size() / m_colnames.size();
185  }
186 
189  bool empty() const
190  {
191  return m_ar.empty();
192  }
193 
196  {
197  if (size() == 0) throw std::runtime_error( "constraint 'set non empty' failed");
198  }
199 
202  {
203  if (size() > 1) throw std::runtime_error( "constraint 'set unique' failed");
204  }
205 
207  void append( const ValueTupleSet& ts)
208  {
209  if (ts.m_colnames.size() != m_colnames.size()) throw std::runtime_error("joining incompatible lists");
210  m_ar.insert( m_ar.end(), ts.m_ar.begin(), ts.m_ar.end());
211  }
212 
213 private:
214  std::vector<std::string> m_colnames;
215  std::vector<types::Variant> m_ar;
216 };
217 
218 typedef boost::shared_ptr<ValueTupleSet> ValueTupleSetR;
219 
220 }}}//namespace
221 #endif
222 
bool operator==(const const_iterator &o) const
Definition: valueTupleSet.hpp:99
ValueTupleSet()
Default constructor.
Definition: valueTupleSet.hpp:54
bool empty() const
Evaluate if the tuple set is empty.
Definition: valueTupleSet.hpp:189
void push(const std::vector< types::Variant > &c)
Add one tuple to the set (checking its size)
Definition: valueTupleSet.hpp:171
const const_iterator * operator->() const
Definition: valueTupleSet.hpp:108
Iterator on this set of value tuples.
Definition: valueTupleSet.hpp:66
boost::shared_ptr< ValueTupleSet > ValueTupleSetR
Definition: valueTupleSet.hpp:218
std::vector< types::Variant > m_ar
Definition: valueTupleSet.hpp:215
const_iterator(const ValueTupleSet *ref_, std::size_t idx_=0)
Definition: valueTupleSet.hpp:75
const_iterator end() const
Get the end tuple set iterator.
Definition: valueTupleSet.hpp:129
std::size_t index() const
Definition: valueTupleSet.hpp:94
Forward declaration.
Definition: variant.hpp:65
const_iterator operator++(int)
Definition: valueTupleSet.hpp:87
Set of tuples (database results, transaction input, etc.)
Definition: valueTupleSet.hpp:50
void checkConstraintNonEmpty() const
Check the NONEMPTY constaint on this tuple set.
Definition: valueTupleSet.hpp:195
std::size_t columnIndex(const std::string &name) const
Get the index of a column by name numbered starting with 1.
Definition: valueTupleSet.hpp:147
Variant value type.
void push(const std::vector< types::VariantConst > &c)
Add one tuple to the set (checking its size)
Definition: valueTupleSet.hpp:161
std::vector< std::string > m_colnames
Definition: valueTupleSet.hpp:214
void append(const ValueTupleSet &ts)
Append a tuple set (checking number of columns to be equal in both sets)
Definition: valueTupleSet.hpp:207
const ValueTupleSet * m_ref
Definition: valueTupleSet.hpp:120
const types::Variant & column(std::size_t i) const
Definition: valueTupleSet.hpp:109
friend class const_iterator
Definition: valueTupleSet.hpp:62
const_iterator & operator++()
Definition: valueTupleSet.hpp:79
std::size_t nofColumns() const
Get the number of columns.
Definition: valueTupleSet.hpp:135
ValueTupleSet(const ValueTupleSet &o)
Copy constructor.
Definition: valueTupleSet.hpp:59
ValueTupleSet(const std::vector< std::string > &colnames_)
Constructor.
Definition: valueTupleSet.hpp:56
const std::string & columnName(std::size_t i) const
Get the name of a column numbered starting with 1.
Definition: valueTupleSet.hpp:140
const_iterator()
Definition: valueTupleSet.hpp:69
const_iterator(const const_iterator &o)
Definition: valueTupleSet.hpp:77
std::size_t size() const
Get the number of tuples in the tuple set.
Definition: valueTupleSet.hpp:182
const_iterator begin() const
Get the start tuple set iterator.
Definition: valueTupleSet.hpp:124
void checkConstraintUnique() const
Check the UNIQUE constaint on this tuple set.
Definition: valueTupleSet.hpp:201
std::size_t m_idx
Definition: valueTupleSet.hpp:119
bool operator!=(const const_iterator &o) const
Definition: valueTupleSet.hpp:103