~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

  • Committer: Brian Aker
  • Date: 2011-01-13 09:25:42 UTC
  • mfrom: (2073.1.4 catalogs)
  • mto: This revision was merged to the branch mainline in revision 2080.
  • Revision ID: brian@gir-3-20110113092542-lmomnx1t0zpidzl4
Merge of current catalogs

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
#include <drizzled/gettext.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/query_id.h>
24
 
#include <drizzled/error/sql_state.h>
 
24
#include <drizzled/sql_state.h>
25
25
#include <drizzled/session.h>
26
 
#include <drizzled/internal/m_string.h>
 
26
#include "drizzled/internal/m_string.h"
27
27
#include <algorithm>
28
28
#include <boost/program_options.hpp>
29
29
#include <drizzled/module/option_map.h>
30
 
#include <drizzled/util/tokenize.h>
 
30
#include "drizzled/util/tokenize.h"
31
31
#include "errmsg.h"
32
32
#include "mysql_protocol.h"
33
33
#include "mysql_password.h"
34
34
#include "options.h"
35
 
 
36
 
#include <drizzled/identifier.h>
37
 
 
38
 
#include <libdrizzle/constants.h>
 
35
#include "table_function.h"
 
36
 
 
37
#include "drizzled/identifier.h"
39
38
 
40
39
#define PROTOCOL_VERSION 10
41
40
 
67
66
ListenMySQLProtocol::~ListenMySQLProtocol()
68
67
{ }
69
68
 
70
 
void ListenMySQLProtocol::addCountersToTable()
71
 
{
72
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters()->connectionCount));
73
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters()->connected));
74
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters()->failedConnections));
75
 
}
76
 
 
77
69
const std::string ListenMySQLProtocol::getHost(void) const
78
70
{
79
71
  return _hostname;
404
396
}
405
397
 
406
398
 
407
 
void ClientMySQLProtocol::sendError(drizzled::error_t sql_errno, const char *err)
 
399
void ClientMySQLProtocol::sendError(uint32_t sql_errno, const char *err)
408
400
{
409
401
  uint32_t length;
410
402
  /*
412
404
  */
413
405
  unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
414
406
 
415
 
  assert(sql_errno != EE_OK);
 
407
  assert(sql_errno);
416
408
  assert(err && err[0]);
417
409
 
418
410
  /*
437
429
    return;
438
430
  }
439
431
 
440
 
  int2store(buff, static_cast<uint16_t>(sql_errno));
 
432
  int2store(buff,sql_errno);
441
433
  pos= buff+2;
442
434
 
443
435
  /* The first # is to make the client backward compatible */
444
436
  buff[2]= '#';
445
 
  pos= (unsigned char*) strcpy((char*) buff+3, error::convert_to_sqlstate(sql_errno));
446
 
  pos+= strlen(error::convert_to_sqlstate(sql_errno));
 
437
  pos= (unsigned char*) strcpy((char*) buff+3, drizzle_errno_to_sqlstate(sql_errno));
 
438
  pos+= strlen(drizzle_errno_to_sqlstate(sql_errno));
447
439
 
448
440
  char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
449
441
  tmp+= strlen((char*)pos);
478
470
*/
479
471
bool ClientMySQLProtocol::sendFields(List<Item> *list)
480
472
{
481
 
  List<Item>::iterator it(list->begin());
 
473
  List_iterator_fast<Item> it(*list);
482
474
  Item *item;
483
475
  unsigned char buff[80];
484
476
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
560
552
        break;
561
553
 
562
554
      case DRIZZLE_TYPE_BOOLEAN:
563
 
        pos[6]= DRIZZLE_COLUMN_TYPE_TINY;
 
555
        pos[6]= 15;
564
556
        break;
565
557
 
566
558
      case DRIZZLE_TYPE_DECIMAL:
611
603
{
612
604
  if (from->is_null())
613
605
    return store();
614
 
  if (from->type() == DRIZZLE_TYPE_BOOLEAN)
615
 
  {
616
 
    return store(from->val_int());
617
 
  }
618
 
 
619
606
  char buff[MAX_FIELD_WIDTH];
620
607
  String str(buff,sizeof(buff), &my_charset_bin);
621
608
 
992
979
 
993
980
static int init(drizzled::module::Context &context)
994
981
{  
 
982
  context.add(new MysqlProtocolStatus);
 
983
 
995
984
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
996
985
  time_t seed_time= time(NULL);
997
986
  random_seed1= seed_time % random_max;
1003
992
  context.add(mysql_password);
1004
993
 
1005
994
  listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
1006
 
  listen_obj->addCountersToTable();
1007
995
  context.add(listen_obj); 
1008
996
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
1009
997
  context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
1051
1039
          _("A restrictive IP address list for incoming admin connections."));
1052
1040
}
1053
1041
 
 
1042
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
 
1043
{
 
1044
  var->type= SHOW_LONGLONG;
 
1045
  var->value= buff;
 
1046
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
 
1047
  return 0;
 
1048
}
 
1049
 
 
1050
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
 
1051
{
 
1052
  var->type= SHOW_LONGLONG;
 
1053
  var->value= buff;
 
1054
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
 
1055
  return 0;
 
1056
}
 
1057
 
 
1058
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
 
1059
{
 
1060
  var->type= SHOW_LONGLONG;
 
1061
  var->value= buff;
 
1062
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
 
1063
  return 0;
 
1064
}
 
1065
 
 
1066
static st_show_var_func_container mysql_protocol_connection_count=
 
1067
  { &mysql_protocol_connection_count_func };
 
1068
 
 
1069
static st_show_var_func_container mysql_protocol_connected_count=
 
1070
  { &mysql_protocol_connected_count_func };
 
1071
 
 
1072
static st_show_var_func_container mysql_protocol_failed_count=
 
1073
  { &mysql_protocol_failed_count_func };
 
1074
 
 
1075
static drizzle_show_var mysql_protocol_status_variables[]= {
 
1076
  {"Connections",
 
1077
  (char*) &mysql_protocol_connection_count, SHOW_FUNC},
 
1078
  {"Connected",
 
1079
  (char*) &mysql_protocol_connected_count, SHOW_FUNC},
 
1080
  {"Failed_connections",
 
1081
  (char*) &mysql_protocol_failed_count, SHOW_FUNC},
 
1082
  {NULL, NULL, SHOW_LONGLONG}
 
1083
};
 
1084
 
 
1085
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
 
1086
  plugin::TableFunction::Generator(fields)
 
1087
{
 
1088
  status_var_ptr= mysql_protocol_status_variables;
 
1089
}
 
1090
 
 
1091
bool MysqlProtocolStatus::Generator::populate()
 
1092
{
 
1093
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
 
1094
  char * const buff= (char *) &buff_data;
 
1095
  drizzle_show_var tmp;
 
1096
 
 
1097
  if (status_var_ptr->name)
 
1098
  {
 
1099
    std::ostringstream oss;
 
1100
    string return_value;
 
1101
    const char *value;
 
1102
    int type;
 
1103
 
 
1104
    push(status_var_ptr->name);
 
1105
 
 
1106
    if (status_var_ptr->type == SHOW_FUNC)
 
1107
    {
 
1108
      ((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
 
1109
      value= buff;
 
1110
      type= tmp.type;
 
1111
    }
 
1112
    else
 
1113
    {
 
1114
      value= status_var_ptr->value;
 
1115
      type= status_var_ptr->type;
 
1116
    }
 
1117
 
 
1118
    switch(type)
 
1119
    {
 
1120
    case SHOW_LONGLONG:
 
1121
      oss << *(uint64_t*) value;
 
1122
      return_value= oss.str();
 
1123
      break;
 
1124
    default:
 
1125
      assert(0);
 
1126
    }
 
1127
    if (return_value.length())
 
1128
      push(return_value);
 
1129
    else
 
1130
      push(" ");
 
1131
 
 
1132
    status_var_ptr++;
 
1133
 
 
1134
    return true;
 
1135
  }
 
1136
  return false;
 
1137
}
 
1138
 
1054
1139
} /* namespace drizzle_plugin */
1055
1140
 
1056
1141
DRIZZLE_DECLARE_PLUGIN
1062
1147
  "MySQL Protocol Module",
1063
1148
  PLUGIN_LICENSE_GPL,
1064
1149
  drizzle_plugin::init,             /* Plugin Init */
1065
 
  NULL, /* depends */
 
1150
  NULL, /* system variables */
1066
1151
  drizzle_plugin::init_options    /* config options */
1067
1152
}
1068
1153
DRIZZLE_DECLARE_PLUGIN_END;