~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

Merge with trunk.

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