Drizzled Public API Documentation

user_commands.cc

00001 /*
00002  * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *   * Redistributions of source code must retain the above copyright notice,
00009  *     this list of conditions and the following disclaimer.
00010  *   * Redistributions in binary form must reproduce the above copyright notice,
00011  *     this list of conditions and the following disclaimer in the documentation
00012  *     and/or other materials provided with the distribution.
00013  *   * Neither the name of Joseph Daly nor the names of its contributors
00014  *     may be used to endorse or promote products derived from this software
00015  *     without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027  * THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "user_commands.h"
00031 
00032 using namespace drizzled;
00033 using namespace std;
00034 
00035 const char* UserCommands::USER_COUNTS[] =
00036 {
00037   "COUNT_SELECT",
00038   "COUNT_DELETE",
00039   "COUNT_UPDATE",
00040   "COUNT_INSERT",
00041   "COUNT_ROLLBACK",
00042   "COUNT_COMMIT",
00043   "COUNT_CREATE",
00044   "COUNT_ALTER",
00045   "COUNT_DROP",
00046   "COUNT_ADMIN"
00047 };
00048 
00049 const char* UserCommands::COM_STATUS_VARS[] =
00050 {
00051   "select",
00052   "create_table",
00053   "create_index",
00054   "alter_table",
00055   "update",
00056   "insert",
00057   "insert_select",
00058   "delete",
00059   "truncate",
00060   "drop_table",
00061   "drop_index",
00062   "show_create",
00063   "show_create_db",
00064   "load",
00065   "set_option",
00066   "unlock_tables",
00067   "change_db",
00068   "create_db",
00069   "drop_db",
00070   "alter_db",
00071   "replace",
00072   "replace_select",
00073   "check",
00074   "flush",
00075   "kill",
00076   "analyze",
00077   "rollback",
00078   "rollback_to_savepoint",
00079   "commit",
00080   "savepoint",
00081   "release_savepoint",
00082   "begin",
00083   "rename_table",
00084   "show_warns",
00085   "empty_query",
00086   "show_errors",
00087   "checksum"
00088 };
00089 
00090 UserCommands::UserCommands()
00091 {
00092   init();
00093 }
00094 
00095 void UserCommands::init()
00096 {
00097   vector<uint64_t>::iterator it= vector_of_command_counts.begin();
00098   for (int j=0; j < SQLCOM_END; ++j)
00099   {
00100     it=  vector_of_command_counts.insert(it, 0);
00101   }
00102   vector_of_command_counts.resize(SQLCOM_END);
00103 }
00104 
00105 void UserCommands::incrementCount(uint32_t index, uint32_t i)
00106 {
00107   uint64_t *count= &(vector_of_command_counts.at(index));
00108   *count= *count + i;
00109 }
00110 
00111 uint64_t UserCommands::getCount(uint32_t index)
00112 {
00113   uint64_t *count= &(vector_of_command_counts.at(index));
00114   return *count;
00115 }
00116 
00117 uint64_t UserCommands::getUserCount(uint32_t index)
00118 {
00119   switch (index)
00120   {
00121     case COUNT_SELECT:
00122       return getCount(SQLCOM_SELECT);
00123     case COUNT_DELETE:
00124       return getCount(SQLCOM_DELETE);
00125     case COUNT_UPDATE:
00126       return getCount(SQLCOM_UPDATE);
00127     case COUNT_INSERT:
00128       return getCount(SQLCOM_INSERT);
00129     case COUNT_ROLLBACK:
00130       return getCount(SQLCOM_ROLLBACK);
00131     case COUNT_COMMIT:
00132       return getCount(SQLCOM_COMMIT);
00133     case COUNT_CREATE:
00134       return getCount(SQLCOM_CREATE_TABLE);
00135     case COUNT_ALTER:
00136       return getCount(SQLCOM_ALTER_TABLE);
00137     case COUNT_DROP:
00138       return getCount(SQLCOM_DROP_TABLE);
00139     default:
00140       return 0;
00141   }
00142 }
00143 
00144 void UserCommands::reset()
00145 {
00146   for (uint32_t j= 0; j < SQLCOM_END; ++j)
00147   {
00148     uint64_t *count= &(vector_of_command_counts.at(j));
00149     *count= 0;
00150   }
00151 }
00152 
00153 UserCommands::UserCommands(const UserCommands &user_commands)
00154 {
00155   init();
00156 
00157   for (uint32_t j= 0; j < SQLCOM_END; ++j)
00158   {
00159     uint64_t *my_count= &(vector_of_command_counts.at(j));
00160     uint64_t other_count= user_commands.vector_of_command_counts.at(j);
00161     *my_count= other_count;
00162   }
00163 }
00164 
00165 void UserCommands::merge(UserCommands *user_commands)
00166 {
00167   for (uint32_t j= 0; j < SQLCOM_END; ++j)
00168   {
00169     uint64_t *my_count= &(vector_of_command_counts.at(j));
00170     uint64_t other_count= user_commands->vector_of_command_counts.at(j);
00171     *my_count= *my_count + other_count;
00172   }
00173 }
00174 
00175 void UserCommands::logCommand(enum_sql_command sql_command)
00176 {
00177   if (sql_command < SQLCOM_END)
00178   {
00179     incrementCount(sql_command);
00180   }
00181 }