~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-06 05:17:09 UTC
  • Revision ID: brian@tangent.org-20110106051709-oa0se8ur02uc6i9o
Added native functions into the function table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
26
#include "drizzled/internal/m_string.h"
27
27
#include <algorithm>
32
32
#include "mysql_protocol.h"
33
33
#include "mysql_password.h"
34
34
#include "options.h"
 
35
#include "table_function.h"
35
36
 
36
37
#include "drizzled/identifier.h"
37
38
 
65
66
ListenMySQLProtocol::~ListenMySQLProtocol()
66
67
{ }
67
68
 
68
 
void ListenMySQLProtocol::addCountersToTable()
69
 
{
70
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters()->connectionCount));
71
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters()->connected));
72
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters()->failedConnections));
73
 
}
74
 
 
75
69
const std::string ListenMySQLProtocol::getHost(void) const
76
70
{
77
71
  return _hostname;
402
396
}
403
397
 
404
398
 
405
 
void ClientMySQLProtocol::sendError(drizzled::error_t sql_errno, const char *err)
 
399
void ClientMySQLProtocol::sendError(uint32_t sql_errno, const char *err)
406
400
{
407
401
  uint32_t length;
408
402
  /*
410
404
  */
411
405
  unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
412
406
 
413
 
  assert(sql_errno != EE_OK);
 
407
  assert(sql_errno);
414
408
  assert(err && err[0]);
415
409
 
416
410
  /*
435
429
    return;
436
430
  }
437
431
 
438
 
  int2store(buff, static_cast<uint16_t>(sql_errno));
 
432
  int2store(buff,sql_errno);
439
433
  pos= buff+2;
440
434
 
441
435
  /* The first # is to make the client backward compatible */
442
436
  buff[2]= '#';
443
 
  pos= (unsigned char*) strcpy((char*) buff+3, error::convert_to_sqlstate(sql_errno));
444
 
  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));
445
439
 
446
440
  char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
447
441
  tmp+= strlen((char*)pos);
549
543
        pos[6]= 15;
550
544
        break;
551
545
 
552
 
      case DRIZZLE_TYPE_MICROTIME:
553
 
        pos[6]= 15;
554
 
        break;
555
 
 
556
546
      case DRIZZLE_TYPE_UUID:
557
547
        pos[6]= 15;
558
548
        break;
985
975
 
986
976
static int init(drizzled::module::Context &context)
987
977
{  
 
978
  context.add(new MysqlProtocolStatus);
 
979
 
988
980
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
989
981
  time_t seed_time= time(NULL);
990
982
  random_seed1= seed_time % random_max;
996
988
  context.add(mysql_password);
997
989
 
998
990
  listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
999
 
  listen_obj->addCountersToTable();
1000
991
  context.add(listen_obj); 
1001
992
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
1002
993
  context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
1016
1007
{
1017
1008
  context("port",
1018
1009
          po::value<port_constraint>(&port)->default_value(3306),
1019
 
          _("Port number to use for connection or 0 for default to with MySQL "
 
1010
          N_("Port number to use for connection or 0 for default to with MySQL "
1020
1011
                              "protocol."));
1021
1012
  context("connect-timeout",
1022
1013
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1023
 
          _("Connect Timeout."));
 
1014
          N_("Connect Timeout."));
1024
1015
  context("read-timeout",
1025
1016
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
1026
 
          _("Read Timeout."));
 
1017
          N_("Read Timeout."));
1027
1018
  context("write-timeout",
1028
1019
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
1029
 
          _("Write Timeout."));
 
1020
          N_("Write Timeout."));
1030
1021
  context("retry-count",
1031
1022
          po::value<retry_constraint>(&retry_count)->default_value(10),
1032
 
          _("Retry Count."));
 
1023
          N_("Retry Count."));
1033
1024
  context("buffer-length",
1034
1025
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1035
 
          _("Buffer length."));
 
1026
          N_("Buffer length."));
1036
1027
  context("bind-address",
1037
1028
          po::value<string>()->default_value(""),
1038
 
          _("Address to bind to."));
 
1029
          N_("Address to bind to."));
1039
1030
  context("max-connections",
1040
1031
          po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1041
 
          _("Maximum simultaneous connections."));
 
1032
          N_("Maximum simultaneous connections."));
1042
1033
  context("admin-ip-addresses",
1043
1034
          po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1044
 
          _("A restrictive IP address list for incoming admin connections."));
 
1035
          N_("A restrictive IP address list for incoming admin connections."));
 
1036
}
 
1037
 
 
1038
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
 
1039
{
 
1040
  var->type= SHOW_LONGLONG;
 
1041
  var->value= buff;
 
1042
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
 
1043
  return 0;
 
1044
}
 
1045
 
 
1046
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
 
1047
{
 
1048
  var->type= SHOW_LONGLONG;
 
1049
  var->value= buff;
 
1050
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
 
1051
  return 0;
 
1052
}
 
1053
 
 
1054
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
 
1055
{
 
1056
  var->type= SHOW_LONGLONG;
 
1057
  var->value= buff;
 
1058
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
 
1059
  return 0;
 
1060
}
 
1061
 
 
1062
static st_show_var_func_container mysql_protocol_connection_count=
 
1063
  { &mysql_protocol_connection_count_func };
 
1064
 
 
1065
static st_show_var_func_container mysql_protocol_connected_count=
 
1066
  { &mysql_protocol_connected_count_func };
 
1067
 
 
1068
static st_show_var_func_container mysql_protocol_failed_count=
 
1069
  { &mysql_protocol_failed_count_func };
 
1070
 
 
1071
static drizzle_show_var mysql_protocol_status_variables[]= {
 
1072
  {"Connections",
 
1073
  (char*) &mysql_protocol_connection_count, SHOW_FUNC},
 
1074
  {"Connected",
 
1075
  (char*) &mysql_protocol_connected_count, SHOW_FUNC},
 
1076
  {"Failed_connections",
 
1077
  (char*) &mysql_protocol_failed_count, SHOW_FUNC},
 
1078
  {NULL, NULL, SHOW_LONGLONG}
 
1079
};
 
1080
 
 
1081
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
 
1082
  plugin::TableFunction::Generator(fields)
 
1083
{
 
1084
  status_var_ptr= mysql_protocol_status_variables;
 
1085
}
 
1086
 
 
1087
bool MysqlProtocolStatus::Generator::populate()
 
1088
{
 
1089
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
 
1090
  char * const buff= (char *) &buff_data;
 
1091
  drizzle_show_var tmp;
 
1092
 
 
1093
  if (status_var_ptr->name)
 
1094
  {
 
1095
    std::ostringstream oss;
 
1096
    string return_value;
 
1097
    const char *value;
 
1098
    int type;
 
1099
 
 
1100
    push(status_var_ptr->name);
 
1101
 
 
1102
    if (status_var_ptr->type == SHOW_FUNC)
 
1103
    {
 
1104
      ((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
 
1105
      value= buff;
 
1106
      type= tmp.type;
 
1107
    }
 
1108
    else
 
1109
    {
 
1110
      value= status_var_ptr->value;
 
1111
      type= status_var_ptr->type;
 
1112
    }
 
1113
 
 
1114
    switch(type)
 
1115
    {
 
1116
    case SHOW_LONGLONG:
 
1117
      oss << *(uint64_t*) value;
 
1118
      return_value= oss.str();
 
1119
      break;
 
1120
    default:
 
1121
      assert(0);
 
1122
    }
 
1123
    if (return_value.length())
 
1124
      push(return_value);
 
1125
    else
 
1126
      push(" ");
 
1127
 
 
1128
    status_var_ptr++;
 
1129
 
 
1130
    return true;
 
1131
  }
 
1132
  return false;
1045
1133
}
1046
1134
 
1047
1135
} /* namespace drizzle_plugin */
1055
1143
  "MySQL Protocol Module",
1056
1144
  PLUGIN_LICENSE_GPL,
1057
1145
  drizzle_plugin::init,             /* Plugin Init */
1058
 
  NULL, /* depends */
 
1146
  NULL, /* system variables */
1059
1147
  drizzle_plugin::init_options    /* config options */
1060
1148
}
1061
1149
DRIZZLE_DECLARE_PLUGIN_END;