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 <drizzled/field.h>
00024 #include <drizzled/plugin/client/concurrent.h>
00025 #include <drizzled/sql/result_set.h>
00026 #include <iostream>
00027 
00028 namespace drizzled {
00029 namespace plugin {
00030 namespace client {
00031 
00032 class Cached : public Concurrent
00033 {
00034   uint32_t column;
00035   uint32_t max_column;
00036   sql::ResultSet *_result_set;
00037 
00038 public:
00039   Cached(sql::ResultSet &rs) :
00040     column(0),
00041     max_column(0),
00042     _result_set(&rs)
00043   {
00044   }
00045 
00046   virtual bool sendFields(List<Item> *list)
00047   {
00048     List<Item>::iterator it(list->begin());
00049 
00050     column= 0;
00051     max_column= 0;
00052 
00053     while (Item* item= it++)
00054     {
00055       SendField field;
00056       item->make_field(&field);
00057       max_column++;
00058     }
00059     _result_set->createRow();
00060 
00061     return false;
00062   }
00063 
00064   virtual void sendError(drizzled::error_t error_code, const char *error_message)
00065   {
00066     _result_set->pushException(sql::Exception(error_message, error_code));
00067   }
00068 
00069   virtual void checkRowEnd()
00070   {
00071     if (++column % max_column == 0)
00072     {
00073       _result_set->createRow();
00074     }
00075   }
00076 
00077   using Client::store;
00078 
00079   virtual bool store(Field *from)
00080   {
00081     if (from->is_null())
00082       return store();
00083 
00084     char buff[MAX_FIELD_WIDTH];
00085     String str(buff, sizeof(buff), &my_charset_bin);
00086     from->val_str_internal(&str);
00087 
00088     return store(str.ptr(), str.length());
00089   }
00090 
00091   virtual bool store()
00092   {
00093     _result_set->setColumnNull(currentColumn());
00094 
00095     checkRowEnd();
00096 
00097     return false;
00098   }
00099 
00100   virtual bool store(int32_t from)
00101   {
00102     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00103     checkRowEnd();
00104 
00105     return false;
00106   }
00107 
00108   virtual bool store(uint32_t from)
00109   {
00110     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00111     checkRowEnd();
00112 
00113     return false;
00114   }
00115 
00116   virtual bool store(int64_t from)
00117   {
00118     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00119     checkRowEnd();
00120 
00121     return false;
00122   }
00123 
00124   virtual bool store(uint64_t from)
00125   {
00126     _result_set->setColumn(currentColumn(), boost::lexical_cast<std::string>(from));
00127     checkRowEnd();
00128 
00129     return false;
00130   }
00131 
00132   virtual bool store(double from, uint32_t decimals, String *buffer)
00133   {
00134     buffer->set_real(from, decimals, &my_charset_bin);
00135     return store(buffer->ptr(), buffer->length());
00136   }
00137 
00138   virtual bool store(const char *from, size_t length)
00139   {
00140     _result_set->setColumn(currentColumn(), std::string(from, length));
00141     checkRowEnd();
00142 
00143     return false;
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