~drizzle-trunk/drizzle/development

2131.12.1 by Brian Aker
Example for David.
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
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
21
#pragma once
2131.12.1 by Brian Aker
Example for David.
22
2239.1.10 by Olaf van der Spek
Refactor includes
23
#include <boost/lexical_cast.hpp>
2234.1.3 by Olaf van der Spek
Refactor includes
24
#include <drizzled/field.h>
2131.12.1 by Brian Aker
Example for David.
25
#include <drizzled/plugin/client/concurrent.h>
26
#include <drizzled/sql/result_set.h>
27
#include <iostream>
28
2234.1.3 by Olaf van der Spek
Refactor includes
29
namespace drizzled {
30
namespace plugin {
31
namespace client {
2131.12.1 by Brian Aker
Example for David.
32
33
class Cached : public Concurrent
34
{
35
  uint32_t column;
36
  uint32_t max_column;
2116.1.13 by David Shrewsbury
Initial work to access result set.
37
  sql::ResultSet *_result_set;
2131.12.1 by Brian Aker
Example for David.
38
39
public:
2116.1.17 by David Shrewsbury
Various fixes
40
  Cached(sql::ResultSet &rs) :
2131.12.1 by Brian Aker
Example for David.
41
    column(0),
2116.1.13 by David Shrewsbury
Initial work to access result set.
42
    max_column(0),
2116.1.17 by David Shrewsbury
Various fixes
43
    _result_set(&rs)
2131.12.1 by Brian Aker
Example for David.
44
  {
45
  }
46
47
  virtual bool sendFields(List<Item> *list)
48
  {
2116.1.49 by David Shrewsbury
Update Cached client to no longer use removed List_iterator_fast
49
    List<Item>::iterator it(list->begin());
2131.12.1 by Brian Aker
Example for David.
50
51
    column= 0;
52
    max_column= 0;
53
2234.1.3 by Olaf van der Spek
Refactor includes
54
    while (Item* item= it++)
2131.12.1 by Brian Aker
Example for David.
55
    {
56
      SendField field;
57
      item->make_field(&field);
58
      max_column++;
59
    }
2283.4.4 by Stewart Smith
allow sql result_set to change column count, so we construct the cached result set with the correct column count ofr the query as we ar executing it instead of having to magically know it from the start. This allows us to use the cached interface to, e.g. build JSON objects from arbitrary SQL query result sets.
60
    _result_set->setColumnCount(max_column);
2131.12.1 by Brian Aker
Example for David.
61
    _result_set->createRow();
62
63
    return false;
64
  }
65
2131.12.3 by David Shrewsbury
Update sendError signature
66
  virtual void sendError(drizzled::error_t error_code, const char *error_message)
2131.12.1 by Brian Aker
Example for David.
67
  {
2234.1.3 by Olaf van der Spek
Refactor includes
68
    _result_set->pushException(sql::Exception(error_message, error_code));
2131.12.1 by Brian Aker
Example for David.
69
  }
70
2234.1.3 by Olaf van der Spek
Refactor includes
71
  virtual void checkRowEnd()
2131.12.1 by Brian Aker
Example for David.
72
  {
73
    if (++column % max_column == 0)
74
    {
75
      _result_set->createRow();
76
    }
77
  }
78
79
  using Client::store;
80
2313.3.6 by Olaf van der Spek
Return void
81
  virtual void store(Field *from)
2131.12.1 by Brian Aker
Example for David.
82
  {
83
    if (from->is_null())
84
      return store();
85
86
    char buff[MAX_FIELD_WIDTH];
87
    String str(buff, sizeof(buff), &my_charset_bin);
88
    from->val_str_internal(&str);
89
90
    return store(str.ptr(), str.length());
91
  }
92
2313.3.6 by Olaf van der Spek
Return void
93
  virtual void store()
2131.12.1 by Brian Aker
Example for David.
94
  {
95
    _result_set->setColumnNull(currentColumn());
2313.3.6 by Olaf van der Spek
Return void
96
    checkRowEnd();
97
  }
98
99
  virtual void store(int32_t from)
100
  {
101
    _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
102
    checkRowEnd();
103
  }
104
105
  virtual void store(uint32_t from)
106
  {
107
    _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
108
    checkRowEnd();
109
  }
110
111
  virtual void store(int64_t from)
112
  {
113
    _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
114
    checkRowEnd();
115
  }
116
117
  virtual void store(uint64_t from)
118
  {
119
    _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
120
    checkRowEnd();
121
  }
122
123
  virtual void store(double from, uint32_t decimals, String *buffer)
2131.12.1 by Brian Aker
Example for David.
124
  {
125
    buffer->set_real(from, decimals, &my_charset_bin);
126
    return store(buffer->ptr(), buffer->length());
127
  }
128
2313.3.6 by Olaf van der Spek
Return void
129
  virtual void store(const char *from, size_t length)
2131.12.1 by Brian Aker
Example for David.
130
  {
131
    _result_set->setColumn(currentColumn(), std::string(from, length));
132
    checkRowEnd();
133
  }
134
  
135
  inline uint32_t currentColumn() const
136
  {
137
    return column % max_column;
138
  }
139
};
140
141
} /* namespace client */
142
} /* namespace plugin */
143
} /* namespace drizzled */
144