summaryrefslogtreecommitdiffstats
path: root/storage/perfschema/pfs_prepared_stmt.cc
blob: 50e4e27bb1ed084c7fed145d03bd3153d97defb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (c) 2014, 2023, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is also distributed with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have included with MySQL.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License, version 2.0, for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

/**
  @file storage/perfschema/pfs_prepared_stmt.cc
  Prepared Statement data structures (implementation).
*/

/*
  This code needs extra visibility in the lexer structures
*/

#include "my_global.h"
#include "my_sys.h"
#include "pfs_instr.h"
#include "pfs_prepared_stmt.h"
#include "pfs_global.h"
#include "sql_string.h"
#include "pfs_buffer_container.h"
#include <string.h>

/**
  Initialize table PREPARED_STATEMENTS_INSTANCE.
  @param param performance schema sizing
*/
int init_prepared_stmt(const PFS_global_param *param)
{
  if (global_prepared_stmt_container.init(param->m_prepared_stmt_sizing))
    return 1;

  reset_prepared_stmt_instances();
  return 0;
}

/** Cleanup table PREPARED_STATEMENTS_INSTANCE. */
void cleanup_prepared_stmt(void)
{
  global_prepared_stmt_container.cleanup();
}

void PFS_prepared_stmt::reset_data()
{
  m_prepare_stat.reset();
  m_reprepare_stat.reset();
  m_execute_stat.reset();
}

static void fct_reset_prepared_stmt_instances(PFS_prepared_stmt *pfs)
{
  pfs->reset_data();
}

void reset_prepared_stmt_instances()
{
  global_prepared_stmt_container.apply_all(fct_reset_prepared_stmt_instances);
}

PFS_prepared_stmt*
create_prepared_stmt(void *identity,
                     PFS_thread *thread, PFS_program *pfs_program,
                     PFS_events_statements *pfs_stmt, uint stmt_id,
                     const char* stmt_name, uint stmt_name_length)
{
  PFS_prepared_stmt *pfs= NULL;
  pfs_dirty_state dirty_state;

  /* Create a new record in prepared stmt stat array. */
  pfs= global_prepared_stmt_container.allocate(& dirty_state);
  if (pfs != NULL)
  {
    /* Reset the stats. */
    pfs->reset_data();
    /* Do the assignments. */
    pfs->m_identity= identity;

    pfs->m_sqltext_length= 0;

    if (stmt_name != NULL)
    {
      pfs->m_stmt_name_length= stmt_name_length;
      if (pfs->m_stmt_name_length > PS_NAME_LENGTH)
        pfs->m_stmt_name_length= PS_NAME_LENGTH;
      strncpy(pfs->m_stmt_name, stmt_name, pfs->m_stmt_name_length);
    }
    else
      pfs->m_stmt_name_length= 0;

    pfs->m_stmt_id= stmt_id;
    pfs->m_owner_thread_id= thread->m_thread_internal_id;

    /* If this statement prepare is called from a SP. */
    if (pfs_program)
    {
      pfs->m_owner_object_type= pfs_program->m_type;
      strncpy(pfs->m_owner_object_schema, pfs_program->m_schema_name, pfs_program->m_schema_name_length);
      pfs->m_owner_object_schema_length= pfs_program->m_schema_name_length;
      strncpy(pfs->m_owner_object_name, pfs_program->m_object_name, pfs_program->m_object_name_length);
      pfs->m_owner_object_name_length= pfs_program->m_object_name_length;
    }
    else
    {
      pfs->m_owner_object_type= NO_OBJECT_TYPE;
      pfs->m_owner_object_schema_length= 0;
      pfs->m_owner_object_name_length= 0;
    }

    if (pfs_stmt)
    {
      if (pfs_program)
        pfs->m_owner_event_id= pfs_stmt->m_event.m_nesting_event_id;
      else
        pfs->m_owner_event_id= pfs_stmt->m_event.m_event_id;
    }

    /* Insert this record. */
    pfs->m_lock.dirty_to_allocated(& dirty_state);
  }

  return pfs;
}

void delete_prepared_stmt(PFS_prepared_stmt *pfs)
{
  global_prepared_stmt_container.deallocate(pfs);
  return;
}