MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
table_accounts.cc
1 /* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #include "my_global.h"
17 #include "my_pthread.h"
18 #include "table_accounts.h"
19 #include "pfs_instr_class.h"
20 #include "pfs_instr.h"
21 #include "pfs_account.h"
22 #include "pfs_visitor.h"
23 
24 THR_LOCK table_accounts::m_table_lock;
25 
26 static const TABLE_FIELD_TYPE field_types[]=
27 {
28  {
29  { C_STRING_WITH_LEN("USER") },
30  { C_STRING_WITH_LEN("char(16)") },
31  { NULL, 0}
32  },
33  {
34  { C_STRING_WITH_LEN("HOST") },
35  { C_STRING_WITH_LEN("char(60)") },
36  { NULL, 0}
37  },
38  {
39  { C_STRING_WITH_LEN("CURRENT_CONNECTIONS") },
40  { C_STRING_WITH_LEN("bigint(20)") },
41  { NULL, 0}
42  },
43  {
44  { C_STRING_WITH_LEN("TOTAL_CONNECTIONS") },
45  { C_STRING_WITH_LEN("bigint(20)") },
46  { NULL, 0}
47  }
48 };
49 
51 table_accounts::m_field_def=
52 { 4, field_types };
53 
56 {
57  { C_STRING_WITH_LEN("accounts") },
60  NULL, /* write_row */
61  table_accounts::delete_all_rows,
62  NULL, /* get_row_count */
63  1000, /* records */
64  sizeof(PFS_simple_index), /* ref length */
65  &m_table_lock,
66  &m_field_def,
67  false /* checked */
68 };
69 
71 {
72  return new table_accounts();
73 }
74 
75 int
76 table_accounts::delete_all_rows(void)
77 {
85  return 0;
86 }
87 
88 table_accounts::table_accounts()
89  : cursor_by_account(& m_share),
90  m_row_exists(false)
91 {}
92 
93 void table_accounts::make_row(PFS_account *pfs)
94 {
95  pfs_lock lock;
96 
97  m_row_exists= false;
98  pfs->m_lock.begin_optimistic_lock(&lock);
99 
100  if (m_row.m_account.make_row(pfs))
101  return;
102 
104  PFS_connection_iterator::visit_account(pfs, true, & visitor);
105 
106  if (! pfs->m_lock.end_optimistic_lock(& lock))
107  return;
108 
109  m_row.m_connection_stat.set(& visitor.m_stat);
110  m_row_exists= true;
111 }
112 
114  unsigned char *buf,
115  Field **fields,
116  bool read_all)
117 {
118  Field *f;
119 
120  if (unlikely(! m_row_exists))
121  return HA_ERR_RECORD_DELETED;
122 
123  /* Set the null bits */
124  DBUG_ASSERT(table->s->null_bytes == 1);
125  buf[0]= 0;
126 
127  for (; (f= *fields) ; fields++)
128  {
129  if (read_all || bitmap_is_set(table->read_set, f->field_index))
130  {
131  switch(f->field_index)
132  {
133  case 0: /* USER */
134  case 1: /* HOST */
135  m_row.m_account.set_field(f->field_index, f);
136  break;
137  case 2: /* CURRENT_CONNECTIONS */
138  case 3: /* TOTAL_CONNECTIONS */
139  m_row.m_connection_stat.set_field(f->field_index - 2, f);
140  break;
141  default:
142  DBUG_ASSERT(false);
143  }
144  }
145  }
146  return 0;
147 }
148