Drizzled Public API Documentation

null_client.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
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; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <drizzled/plugin/client.h>
00023 #include <boost/tokenizer.hpp>
00024 #include <vector>
00025 #include <queue>
00026 #include <string>
00027 
00028 namespace drizzled {
00029 namespace plugin {
00030 
00034 class NullClient: public Client
00035 {
00036   typedef std::vector<char> Bytes;
00037   typedef std::queue<Bytes> Queue;
00038   Queue to_execute;
00039   bool is_dead;
00040   Bytes packet_buffer;
00041 
00042 public:
00043 
00044   NullClient() :
00045     is_dead(false)
00046   {
00047   }
00048 
00049   virtual int getFileDescriptor(void) { return -1; }
00050   virtual bool isConnected(void) { return true; }
00051   virtual bool flush(void) { return false; }
00052   virtual void close(void) {}
00053   virtual bool authenticate(void) { return true; }
00054 
00055   virtual bool readCommand(char **packet, uint32_t& packet_length)
00056   {
00057     while (not to_execute.empty())
00058     {
00059       Queue::value_type next= to_execute.front();
00060       packet_buffer.assign(next.begin(), next.end());
00061       *packet= &packet_buffer[0];
00062       packet_length= next.size();
00063       to_execute.pop();
00064       return true;
00065     }
00066 
00067     if (not is_dead)
00068     {
00069       packet_buffer.resize(1);
00070       packet_length= 1;
00071       *packet= &packet_buffer[0];
00072       is_dead= true;
00073       return true;
00074     }
00075 
00076     packet_length= 0;
00077     return false;
00078   }
00079 
00080   virtual void sendOK() {}
00081   virtual void sendEOF() {}
00082   virtual void sendError(const drizzled::error_t, const char*) {}
00083   virtual void sendFields(List<Item>&) {}
00084   virtual void store(Field *) {}
00085   virtual void store(void) {}
00086   virtual void store(int32_t) {}
00087   virtual void store(uint32_t) {}
00088   virtual void store(int64_t) {}
00089   virtual void store(uint64_t) {}
00090   virtual void store(double, uint32_t, String*) {}
00091   virtual void store(const type::Time*) {}
00092   virtual void store(const char*) {}
00093   virtual void store(const char*, size_t) {}
00094   virtual void store(str_ref) {}
00095   virtual bool haveError() { return false; }
00096   virtual bool wasAborted() { return false; }
00097 
00098   void pushSQL(str_ref arg)
00099   {
00100     Bytes byte;
00101     typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
00102     Tokenizer tok(arg, boost::escaped_list_separator<char>("\\", ";", "\""));
00103 
00104     for (Tokenizer::iterator iter= tok.begin(); iter != tok.end(); ++iter)
00105     {
00106       byte.resize(iter->size() +1); // +1 for the COM_QUERY
00107       byte[0]= COM_QUERY;
00108       memcpy(&byte[1], iter->data(), iter->size());
00109       to_execute.push(byte);
00110     }
00111   }
00112 };
00113 
00114 } /* namespace plugin */
00115 } /* namespace drizzled */
00116