~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-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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/sql_state.h>
 
24
#include <drizzled/error/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
 
#include "table_function.h"
36
 
 
37
 
#include "drizzled/identifier.h"
 
35
 
 
36
#include <drizzled/identifier.h>
 
37
 
 
38
#include <libdrizzle/constants.h>
38
39
 
39
40
#define PROTOCOL_VERSION 10
40
41
 
66
67
ListenMySQLProtocol::~ListenMySQLProtocol()
67
68
{ }
68
69
 
 
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
 
69
77
const std::string ListenMySQLProtocol::getHost(void) const
70
78
{
71
79
  return _hostname;
396
404
}
397
405
 
398
406
 
399
 
void ClientMySQLProtocol::sendError(uint32_t sql_errno, const char *err)
 
407
void ClientMySQLProtocol::sendError(drizzled::error_t sql_errno, const char *err)
400
408
{
401
409
  uint32_t length;
402
410
  /*
404
412
  */
405
413
  unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
406
414
 
407
 
  assert(sql_errno);
 
415
  assert(sql_errno != EE_OK);
408
416
  assert(err && err[0]);
409
417
 
410
418
  /*
429
437
    return;
430
438
  }
431
439
 
432
 
  int2store(buff,sql_errno);
 
440
  int2store(buff, static_cast<uint16_t>(sql_errno));
433
441
  pos= buff+2;
434
442
 
435
443
  /* The first # is to make the client backward compatible */
436
444
  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));
 
445
  pos= (unsigned char*) strcpy((char*) buff+3, error::convert_to_sqlstate(sql_errno));
 
446
  pos+= strlen(error::convert_to_sqlstate(sql_errno));
439
447
 
440
448
  char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
441
449
  tmp+= strlen((char*)pos);
470
478
*/
471
479
bool ClientMySQLProtocol::sendFields(List<Item> *list)
472
480
{
473
 
  List_iterator_fast<Item> it(*list);
 
481
  List<Item>::iterator it(list->begin());
474
482
  Item *item;
475
483
  unsigned char buff[80];
476
484
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
543
551
        pos[6]= 15;
544
552
        break;
545
553
 
 
554
      case DRIZZLE_TYPE_MICROTIME:
 
555
        pos[6]= 15;
 
556
        break;
 
557
 
546
558
      case DRIZZLE_TYPE_UUID:
547
559
        pos[6]= 15;
548
560
        break;
549
561
 
 
562
      case DRIZZLE_TYPE_BOOLEAN:
 
563
        pos[6]= DRIZZLE_COLUMN_TYPE_TINY;
 
564
        break;
 
565
 
550
566
      case DRIZZLE_TYPE_DECIMAL:
551
567
        pos[6]= (char)246;
552
568
        break;
595
611
{
596
612
  if (from->is_null())
597
613
    return store();
 
614
  if (from->type() == DRIZZLE_TYPE_BOOLEAN)
 
615
  {
 
616
    return store(from->val_int());
 
617
  }
 
618
 
598
619
  char buff[MAX_FIELD_WIDTH];
599
620
  String str(buff,sizeof(buff), &my_charset_bin);
600
621
 
971
992
 
972
993
static int init(drizzled::module::Context &context)
973
994
{  
974
 
  context.add(new MysqlProtocolStatus);
975
 
 
976
995
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
977
996
  time_t seed_time= time(NULL);
978
997
  random_seed1= seed_time % random_max;
984
1003
  context.add(mysql_password);
985
1004
 
986
1005
  listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
 
1006
  listen_obj->addCountersToTable();
987
1007
  context.add(listen_obj); 
988
1008
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
989
1009
  context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
1003
1023
{
1004
1024
  context("port",
1005
1025
          po::value<port_constraint>(&port)->default_value(3306),
1006
 
          N_("Port number to use for connection or 0 for default to with MySQL "
 
1026
          _("Port number to use for connection or 0 for default to with MySQL "
1007
1027
                              "protocol."));
1008
1028
  context("connect-timeout",
1009
1029
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1010
 
          N_("Connect Timeout."));
 
1030
          _("Connect Timeout."));
1011
1031
  context("read-timeout",
1012
1032
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
1013
 
          N_("Read Timeout."));
 
1033
          _("Read Timeout."));
1014
1034
  context("write-timeout",
1015
1035
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
1016
 
          N_("Write Timeout."));
 
1036
          _("Write Timeout."));
1017
1037
  context("retry-count",
1018
1038
          po::value<retry_constraint>(&retry_count)->default_value(10),
1019
 
          N_("Retry Count."));
 
1039
          _("Retry Count."));
1020
1040
  context("buffer-length",
1021
1041
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1022
 
          N_("Buffer length."));
 
1042
          _("Buffer length."));
1023
1043
  context("bind-address",
1024
1044
          po::value<string>()->default_value(""),
1025
 
          N_("Address to bind to."));
 
1045
          _("Address to bind to."));
1026
1046
  context("max-connections",
1027
1047
          po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1028
 
          N_("Maximum simultaneous connections."));
 
1048
          _("Maximum simultaneous connections."));
1029
1049
  context("admin-ip-addresses",
1030
1050
          po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1031
 
          N_("A restrictive IP address list for incoming admin connections."));
1032
 
}
1033
 
 
1034
 
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1035
 
{
1036
 
  var->type= SHOW_LONGLONG;
1037
 
  var->value= buff;
1038
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1039
 
  return 0;
1040
 
}
1041
 
 
1042
 
static int mysql_protocol_connected_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->connected;
1047
 
  return 0;
1048
 
}
1049
 
 
1050
 
static int mysql_protocol_failed_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->failedConnections;
1055
 
  return 0;
1056
 
}
1057
 
 
1058
 
static st_show_var_func_container mysql_protocol_connection_count=
1059
 
  { &mysql_protocol_connection_count_func };
1060
 
 
1061
 
static st_show_var_func_container mysql_protocol_connected_count=
1062
 
  { &mysql_protocol_connected_count_func };
1063
 
 
1064
 
static st_show_var_func_container mysql_protocol_failed_count=
1065
 
  { &mysql_protocol_failed_count_func };
1066
 
 
1067
 
static drizzle_show_var mysql_protocol_status_variables[]= {
1068
 
  {"Connections",
1069
 
  (char*) &mysql_protocol_connection_count, SHOW_FUNC},
1070
 
  {"Connected",
1071
 
  (char*) &mysql_protocol_connected_count, SHOW_FUNC},
1072
 
  {"Failed_connections",
1073
 
  (char*) &mysql_protocol_failed_count, SHOW_FUNC},
1074
 
  {NULL, NULL, SHOW_LONGLONG}
1075
 
};
1076
 
 
1077
 
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1078
 
  plugin::TableFunction::Generator(fields)
1079
 
{
1080
 
  status_var_ptr= mysql_protocol_status_variables;
1081
 
}
1082
 
 
1083
 
bool MysqlProtocolStatus::Generator::populate()
1084
 
{
1085
 
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1086
 
  char * const buff= (char *) &buff_data;
1087
 
  drizzle_show_var tmp;
1088
 
 
1089
 
  if (status_var_ptr->name)
1090
 
  {
1091
 
    std::ostringstream oss;
1092
 
    string return_value;
1093
 
    const char *value;
1094
 
    int type;
1095
 
 
1096
 
    push(status_var_ptr->name);
1097
 
 
1098
 
    if (status_var_ptr->type == SHOW_FUNC)
1099
 
    {
1100
 
      ((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1101
 
      value= buff;
1102
 
      type= tmp.type;
1103
 
    }
1104
 
    else
1105
 
    {
1106
 
      value= status_var_ptr->value;
1107
 
      type= status_var_ptr->type;
1108
 
    }
1109
 
 
1110
 
    switch(type)
1111
 
    {
1112
 
    case SHOW_LONGLONG:
1113
 
      oss << *(uint64_t*) value;
1114
 
      return_value= oss.str();
1115
 
      break;
1116
 
    default:
1117
 
      assert(0);
1118
 
    }
1119
 
    if (return_value.length())
1120
 
      push(return_value);
1121
 
    else
1122
 
      push(" ");
1123
 
 
1124
 
    status_var_ptr++;
1125
 
 
1126
 
    return true;
1127
 
  }
1128
 
  return false;
 
1051
          _("A restrictive IP address list for incoming admin connections."));
1129
1052
}
1130
1053
 
1131
1054
} /* namespace drizzle_plugin */
1139
1062
  "MySQL Protocol Module",
1140
1063
  PLUGIN_LICENSE_GPL,
1141
1064
  drizzle_plugin::init,             /* Plugin Init */
1142
 
  NULL, /* system variables */
 
1065
  NULL, /* depends */
1143
1066
  drizzle_plugin::init_options    /* config options */
1144
1067
}
1145
1068
DRIZZLE_DECLARE_PLUGIN_END;