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 {
00030 namespace plugin
00031 {
00032 
00036 class NullClient: public Client
00037 {
00038   typedef std::vector<char> Bytes;
00039   typedef std::queue <Bytes> Queue;
00040   Queue to_execute;
00041   bool is_dead;
00042   Bytes packet_buffer;
00043 
00044 public:
00045 
00046   NullClient() :
00047     is_dead(false)
00048   {
00049   }
00050 
00051   virtual int getFileDescriptor(void) { return -1; }
00052   virtual bool isConnected(void) { return true; }
00053   virtual bool isReading(void) { return false; }
00054   virtual bool isWriting(void) { return false; }
00055   virtual bool flush(void) { return false; }
00056   virtual void close(void) {}
00057   virtual bool authenticate(void) { return true; }
00058 
00059   virtual bool readCommand(char **packet, uint32_t *packet_length)
00060   {
00061     while(not to_execute.empty())
00062     {
00063       Queue::value_type next= to_execute.front();
00064       packet_buffer.resize(next.size());
00065       memcpy(&packet_buffer[0], &next[0], next.size());
00066 
00067       *packet= &packet_buffer[0];
00068 
00069       *packet_length= next.size();
00070 
00071       to_execute.pop();
00072 
00073       return true;
00074     }
00075 
00076     if (not is_dead)
00077     {
00078       packet_buffer.resize(1);
00079       *packet_length= 1;
00080       *packet= &packet_buffer[0];
00081       is_dead= true;
00082 
00083       return true;
00084     }
00085 
00086     *packet_length= 0;
00087     return false;
00088   }
00089 
00090   virtual void sendOK(void) {}
00091   virtual void sendEOF(void) {}
00092   virtual void sendError(const drizzled::error_t, const char*) {}
00093   virtual bool sendFields(List<Item>*) { return false; }
00094   virtual bool store(Field *) { return false; }
00095   virtual bool store(void) { return false; }
00096   virtual bool store(int32_t) { return false; }
00097   virtual bool store(uint32_t) { return false; }
00098   virtual bool store(int64_t) { return false; }
00099   virtual bool store(uint64_t) { return false; }
00100   virtual bool store(double, uint32_t, String*) { return false; }
00101   virtual bool store(const type::Time*) { return false; }
00102   virtual bool store(const char*) { return false; }
00103   virtual bool store(const char*, size_t) { return false; }
00104   virtual bool store(const std::string &) { return false; }
00105   virtual bool haveMoreData(void) { return false;}
00106   virtual bool haveError(void) { return false; }
00107   virtual bool wasAborted(void) { return false; }
00108 
00109   void pushSQL(const std::string &arg)
00110   {
00111     Bytes byte;
00112     typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
00113     Tokenizer tok(arg, boost::escaped_list_separator<char>("\\", ";", "\""));
00114 
00115     for (Tokenizer::iterator iter= tok.begin(); iter != tok.end(); ++iter)
00116     {
00117       byte.resize(iter->size() +1); // +1 for the COM_QUERY
00118       byte[0]= COM_QUERY;
00119       memcpy(&byte[1], iter->c_str(), iter->size());
00120       to_execute.push(byte);
00121     }
00122   }
00123 };
00124 
00125 } /* namespace plugin */
00126 } /* namespace drizzled */
00127