Drizzled Public API Documentation

cached.h
00001 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2011 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #pragma once
00022 
00023 #include <boost/lexical_cast.hpp>
00024 #include <drizzled/field.h>
00025 #include <drizzled/plugin/client/concurrent.h>
00026 #include <drizzled/sql/result_set.h>
00027 #include <iosfwd>
00028 
00029 namespace drizzled {
00030 namespace plugin {
00031 namespace client {
00032 
00033 class Cached : public Concurrent
00034 {
00035   uint32_t column;
00036   uint32_t max_column;
00037   sql::ResultSet *_result_set;
00038 
00039 public:
00040   Cached(sql::ResultSet &rs) :
00041     column(0),
00042     max_column(0),
00043     _result_set(&rs)
00044   {
00045   }
00046 
00047   virtual void sendFields(List<Item>& list)
00048   {
00049     List<Item>::iterator it(list.begin());
00050 
00051     column= 0;
00052     max_column= 0;
00053 
00054     while (Item* item= it++)
00055     {
00056       SendField field;
00057       item->make_field(&field);
00058       max_column++;
00059     }
00060     _result_set->setColumnCount(max_column);
00061     // Moved to checkRowBegin()
00062     //_result_set->createRow();
00063   }
00064 
00065   virtual void sendError(drizzled::error_t error_code, const char *error_message)
00066   {
00067     _result_set->pushException(sql::Exception(error_message, error_code));
00068   }
00069 
00070   virtual void checkRowBegin()
00071   {
00072     if (currentColumn() == 0)
00073     {
00074       _result_set->createRow();
00075     }
00076   }
00077 
00078 virtual void checkRowEnd()
00079   {
00080     column++;
00081   }
00082 
00083   using Client::store;
00084 
00085   virtual void store(Field *from)
00086   {
00087     if (from->is_null())
00088       return store();
00089 
00090     char buff[MAX_FIELD_WIDTH];
00091     String str(buff, sizeof(buff), &my_charset_bin);
00092     from->val_str_internal(&str);
00093 
00094     return store(str.ptr(), str.length());
00095   }
00096 
00097   virtual void store()
00098   {
00099     checkRowBegin();
00100     _result_set->setColumnNull(currentColumn());
00101     checkRowEnd();
00102   }
00103 
00104   virtual void store(int32_t from)
00105   {
00106     checkRowBegin();
00107     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00108     checkRowEnd();
00109   }
00110 
00111   virtual void store(uint32_t from)
00112   {
00113     checkRowBegin();
00114     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00115     checkRowEnd();
00116   }
00117 
00118   virtual void store(int64_t from)
00119   {
00120     checkRowBegin();
00121     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00122     checkRowEnd();
00123   }
00124 
00125   virtual void store(uint64_t from)
00126   {
00127     checkRowBegin();
00128     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00129     checkRowEnd();
00130   }
00131 
00132   virtual void store(double from, uint32_t decimals, String *buffer)
00133   {
00134     checkRowBegin();
00135     buffer->set_real(from, decimals, &my_charset_bin);
00136     return store(buffer->ptr(), buffer->length());
00137   }
00138 
00139   virtual void store(const char *from, size_t length)
00140   {
00141     checkRowBegin();
00142     _result_set->setColumn(currentColumn(), std::string(from, length));
00143     checkRowEnd();
00144   }
00145   
00146   inline uint32_t currentColumn() const
00147   {
00148     return column % max_column;
00149   }
00150 };
00151 
00152 } /* namespace client */
00153 } /* namespace plugin */
00154 } /* namespace drizzled */
00155