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 #include <drizzled/session/state.h>
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("SESSION_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_CONSOLE", plugin::TableFunction::BOOLEAN, 0, false);
00055 }
00056 
00057 Sessions::Generator::Generator(Field **arg) :
00058   plugin::TableFunction::Generator(arg),
00059   session_generator(*getSession().user())
00060 {
00061 }
00062 
00063 bool Sessions::Generator::populate()
00064 {
00065   while (Session* tmp= session_generator)
00066   {
00067     boost::shared_ptr<session::State> state(tmp->state());
00068     identifier::user::ptr tmp_sctx= tmp->user();
00069 
00070     /* ID */
00071     push((int64_t) tmp->thread_id);
00072 
00073     /* USER */
00074     if (not tmp_sctx->username().empty())
00075       push(tmp_sctx->username());
00076     else 
00077       push(_("no user"));
00078 
00079     /* HOST */
00080     push(tmp_sctx->address());
00081 
00082     /* CATALOG */
00083     push(tmp->catalog().name());
00084 
00085     /* SCHEMA */
00086     util::string::ptr schema(tmp->schema());
00087     if (schema and not schema->empty())
00088     {
00089       push(*schema);
00090     }
00091     else
00092     {
00093       push();
00094     }
00095 
00096     /* COMMAND */
00097     if (tmp->getKilled() == Session::KILL_CONNECTION)
00098     {
00099       push("Killed");
00100     }
00101     else
00102     {
00103       push(getCommandName(tmp->command));
00104     }
00105 
00106     /* STATE */
00107     const char *step= tmp->get_proc_info();
00108     step ? push(step): push();
00109 
00110     /* QUERY */
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     /* IS_INTERACTIVE */
00127     push(tmp->getClient()->isInteractive());
00128 
00129     /* IS_CONSOLE */
00130     push(tmp->getClient()->isConsole());
00131 
00132     return true;
00133   }
00134 
00135   return false;
00136 }
00137 
00138 } // namespace session_dictionary