Drizzled Public API Documentation

sessions.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2011 Sun Microsystems, Inc.
00005  *  Copyright (C) 2009 Sun Microsystems, Inc.
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <config.h>
00023 
00024 #include <plugin/session_dictionary/dictionary.h>
00025 
00026 #include <netdb.h>
00027 
00028 #include <drizzled/internal/my_sys.h>
00029 #include <drizzled/internal/thread_var.h>
00030 #include <drizzled/plugin/authorization.h>
00031 #include <drizzled/plugin/client.h>
00032 #include <drizzled/pthread_globals.h>
00033 
00034 #include <set>
00035 
00036 using namespace std;
00037 using namespace drizzled;
00038 
00039 namespace session_dictionary {
00040 
00041 Sessions::Sessions() :
00042   plugin::TableFunction("DATA_DICTIONARY", "SESSIONS")
00043 {
00044   add_field("SESSION_ID", plugin::TableFunction::NUMBER, 0, false);
00045   add_field("SESION_USERNAME", 16);
00046   add_field("SESSION_HOST", NI_MAXHOST);
00047   add_field("SESSION_CATALOG", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00048   add_field("SESSION_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, true);
00049   add_field("COMMAND", 16);
00050   add_field("STATE", plugin::TableFunction::STRING, 256, true);
00051   add_field("QUERY", plugin::TableFunction::STRING, PROCESS_LIST_WIDTH, true);
00052   add_field("HAS_GLOBAL_LOCK", plugin::TableFunction::BOOLEAN, 0, false);
00053   add_field("IS_INTERACTIVE", plugin::TableFunction::BOOLEAN, 0, false);
00054   add_field("IS_ADMIN", plugin::TableFunction::BOOLEAN, 0, false);
00055   add_field("IS_CONSOLE", plugin::TableFunction::BOOLEAN, 0, false);
00056 }
00057 
00058 Sessions::Generator::Generator(Field **arg) :
00059   plugin::TableFunction::Generator(arg),
00060   session_generator(*getSession().user())
00061 {
00062 }
00063 
00064 Sessions::Generator::~Generator()
00065 {
00066 }
00067 
00068 bool Sessions::Generator::populate()
00069 {
00070   drizzled::Session::pointer tmp;
00071 
00072   while ((tmp= session_generator))
00073   {
00074     drizzled::session::State::const_shared_ptr state(tmp->state());
00075     identifier::User::const_shared_ptr tmp_sctx= tmp->user();
00076 
00077     /* ID */
00078     push((int64_t) tmp->thread_id);
00079 
00080     /* USER */
00081     if (not tmp_sctx->username().empty())
00082       push(tmp_sctx->username());
00083     else 
00084       push(_("no user"));
00085 
00086     /* HOST */
00087     push(tmp_sctx->address());
00088 
00089     /* CATALOG */
00090     push(tmp->catalog().name());
00091 
00092     /* SCHEMA */
00093     drizzled::util::string::const_shared_ptr schema(tmp->schema());
00094     if (schema and not schema->empty())
00095     {
00096       push(*schema);
00097     }
00098     else
00099     {
00100       push();
00101     }
00102 
00103     /* COMMAND */
00104     const char *val= tmp->getKilled() == Session::KILL_CONNECTION ? "Killed" : NULL;
00105     if (val)
00106     {
00107       push(val);
00108     }
00109     else
00110     {
00111       push(getCommandName(tmp->command));
00112     }
00113 
00114     /* STATE */
00115     const char *step= tmp->get_proc_info();
00116     step ? push(step): push();
00117 
00118     /* QUERY */
00119     if (state)
00120     {
00121       size_t length;
00122       const char *tmp_ptr= state->query(length);
00123       push(tmp_ptr, length);
00124     }
00125     else
00126     {
00127       push();
00128     }
00129 
00130     /* HAS_GLOBAL_LOCK */
00131     bool has_global_lock= tmp->isGlobalReadLock();
00132     push(has_global_lock);
00133 
00134     /* IS_INTERACTIVE */
00135     push(tmp->getClient()->isInteractive());
00136 
00137     /* IS_ADMIN */
00138     push(tmp->getClient()->isAdmin());
00139 
00140     /* IS_CONSOLE */
00141     push(tmp->getClient()->isConsole());
00142 
00143     return true;
00144   }
00145 
00146   return false;
00147 }
00148 
00149 } // namespace session_dictionary