Drizzled Public API Documentation

processlist.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 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; 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 #include <config.h>
00022 
00023 #include <plugin/session_dictionary/dictionary.h>
00024 
00025 #include <netdb.h>
00026 
00027 #include <drizzled/pthread_globals.h>
00028 #include <drizzled/plugin/client.h>
00029 #include <drizzled/plugin/authorization.h>
00030 #include <drizzled/internal/my_sys.h>
00031 #include <drizzled/internal/thread_var.h>
00032 
00033 #include <set>
00034 
00035 using namespace std;
00036 using namespace drizzled;
00037 
00038 ProcesslistTool::ProcesslistTool() :
00039   plugin::TableFunction("DATA_DICTIONARY", "PROCESSLIST")
00040 {
00041   add_field("ID", plugin::TableFunction::NUMBER, 0, false);
00042   add_field("USERNAME", 16);
00043   add_field("HOST", NI_MAXHOST);
00044   add_field("DB", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, true);
00045   add_field("COMMAND", 16);
00046   add_field("TIME", plugin::TableFunction::SIZE, 0, false);
00047   add_field("STATE", plugin::TableFunction::STRING, 256, true);
00048   add_field("INFO", plugin::TableFunction::STRING, PROCESS_LIST_WIDTH, true);
00049   add_field("HAS_GLOBAL_LOCK", plugin::TableFunction::BOOLEAN, 0, false);
00050 }
00051 
00052 ProcesslistTool::Generator::Generator(Field **arg) :
00053   plugin::TableFunction::Generator(arg),
00054   session_generator(*getSession().user())
00055 {
00056 }
00057 
00058 bool ProcesslistTool::Generator::populate()
00059 {
00060   drizzled::Session::pointer tmp;
00061 
00062   while ((tmp= session_generator))
00063   {
00064     drizzled::session::State::const_shared_ptr state(tmp->state());
00065     identifier::User::const_shared_ptr tmp_sctx= tmp->user();
00066 
00067     /* ID */
00068     push((int64_t) tmp->thread_id);
00069 
00070     /* USER */
00071     if (not tmp_sctx->username().empty())
00072       push(tmp_sctx->username());
00073     else
00074       push(_("no user"));
00075 
00076     /* HOST */
00077     push(tmp_sctx->address());
00078 
00079     /* DB */
00080     drizzled::util::string::const_shared_ptr schema(tmp->schema());
00081     if (schema and not schema->empty())
00082     {
00083       push(*schema);
00084     }
00085     else
00086     {
00087       push();
00088     }
00089 
00090     /* COMMAND */
00091     const char *val= tmp->getKilled() == Session::KILL_CONNECTION ? "Killed" : NULL;
00092     if (val)
00093     {
00094       push(val);
00095     }
00096     else
00097     {
00098       push(getCommandName(tmp->command));
00099     }
00100 
00101     /* type::Time */
00102     boost::posix_time::time_duration duration_result;
00103     getSession().getTimeDifference(duration_result, getSession().start_timer());
00104     duration_result.is_negative() ? push(static_cast<uint64_t>(0)) : push(static_cast<uint64_t>(duration_result.total_seconds()));
00105 
00106     /* STATE */
00107     const char *step= tmp->get_proc_info();
00108     step ? push(step): push();
00109 
00110     /* INFO */
00111     if (state)
00112     {
00113       size_t length;
00114       const char *tmp_ptr= state->query(length);
00115       push(tmp_ptr, length);
00116     }
00117     else
00118     {
00119       push();
00120     }
00121 
00122     /* HAS_GLOBAL_LOCK */
00123     bool has_global_lock= tmp->isGlobalReadLock();
00124     push(has_global_lock);
00125 
00126     return true;
00127   }
00128 
00129   return false;
00130 }