Drizzled Public API Documentation

cached.h
1 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2011 Brian Aker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #pragma once
22 
23 #include <boost/lexical_cast.hpp>
24 #include <drizzled/field.h>
25 #include <drizzled/plugin/client/concurrent.h>
26 #include <drizzled/sql/result_set.h>
27 #include <iosfwd>
28 
29 namespace drizzled {
30 namespace plugin {
31 namespace client {
32 
33 class Cached : public Concurrent
34 {
35  uint32_t column;
36  uint32_t max_column;
37  sql::ResultSet *_result_set;
38 
39 public:
40  Cached(sql::ResultSet &rs) :
41  column(0),
42  max_column(0),
43  _result_set(&rs)
44  {
45  }
46 
47  virtual void sendFields(List<Item>& list)
48  {
49  List<Item>::iterator it(list.begin());
50 
51  column= 0;
52  max_column= 0;
53 
54  while (Item* item= it++)
55  {
56  SendField field;
57  item->make_field(&field);
58  max_column++;
59  }
60  _result_set->setColumnCount(max_column);
61  // Moved to checkRowBegin()
62  //_result_set->createRow();
63  }
64 
65  virtual void sendError(drizzled::error_t error_code, const char *error_message)
66  {
67  _result_set->pushException(sql::Exception(error_message, error_code));
68  }
69 
70  virtual void checkRowBegin()
71  {
72  if (currentColumn() == 0)
73  {
74  _result_set->createRow();
75  }
76  }
77 
78 virtual void checkRowEnd()
79  {
80  column++;
81  }
82 
83  using Client::store;
84 
85  virtual void store(Field *from)
86  {
87  if (from->is_null())
88  return store();
89 
90  char buff[MAX_FIELD_WIDTH];
91  String str(buff, sizeof(buff), &my_charset_bin);
92  from->val_str_internal(&str);
93 
94  return store(str.ptr(), str.length());
95  }
96 
97  virtual void store()
98  {
99  checkRowBegin();
100  _result_set->setColumnNull(currentColumn());
101  checkRowEnd();
102  }
103 
104  virtual void store(int32_t from)
105  {
106  checkRowBegin();
107  _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
108  checkRowEnd();
109  }
110 
111  virtual void store(uint32_t from)
112  {
113  checkRowBegin();
114  _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
115  checkRowEnd();
116  }
117 
118  virtual void store(int64_t from)
119  {
120  checkRowBegin();
121  _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
122  checkRowEnd();
123  }
124 
125  virtual void store(uint64_t from)
126  {
127  checkRowBegin();
128  _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
129  checkRowEnd();
130  }
131 
132  virtual void store(double from, uint32_t decimals, String *buffer)
133  {
134  checkRowBegin();
135  buffer->set_real(from, decimals, &my_charset_bin);
136  return store(buffer->ptr(), buffer->length());
137  }
138 
139  virtual void store(const char *from, size_t length)
140  {
141  checkRowBegin();
142  _result_set->setColumn(currentColumn(), std::string(from, length));
143  checkRowEnd();
144  }
145 
146  inline uint32_t currentColumn() const
147  {
148  return column % max_column;
149  }
150 };
151 
152 } /* namespace client */
153 } /* namespace plugin */
154 } /* namespace drizzled */
155