~ubuntu-branches/ubuntu/wily/mysql-5.6/wily

« back to all changes in this revision

Viewing changes to sql/sql_digest.h

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2015-04-16 20:07:10 UTC
  • mto: (1.3.9 vivid-proposed)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20150416200710-pcrsa022082zj46k
Tags: upstream-5.6.24
ImportĀ upstreamĀ versionĀ 5.6.24

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008, 2015, 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
#ifndef SQL_DIGEST_H
 
17
#define SQL_DIGEST_H
 
18
 
 
19
#include <string.h>
 
20
class String;
 
21
#include "my_md5.h"
 
22
 
 
23
#define MAX_DIGEST_STORAGE_SIZE (1024*1024)
 
24
 
 
25
/**
 
26
  Structure to store token count/array for a statement
 
27
  on which digest is to be calculated.
 
28
*/
 
29
struct sql_digest_storage
 
30
{
 
31
  bool m_full;
 
32
  uint m_byte_count;
 
33
  unsigned char m_md5[MD5_HASH_SIZE];
 
34
  /** Character set number. */
 
35
  uint m_charset_number;
 
36
  /**
 
37
    Token array.
 
38
    Token array is an array of bytes to store tokens received during parsing.
 
39
    Following is the way token array is formed.
 
40
    ... &lt;non-id-token&gt; &lt;non-id-token&gt; &lt;id-token&gt; &lt;id_len&gt; &lt;id_text&gt; ...
 
41
    For Example:
 
42
    SELECT * FROM T1;
 
43
    &lt;SELECT_TOKEN&gt; &lt;*&gt; &lt;FROM_TOKEN&gt; &lt;ID_TOKEN&gt; &lt;2&gt; &lt;T1&gt;
 
44
  */
 
45
  unsigned char *m_token_array;
 
46
  /* Length of the token array to be considered for DIGEST_TEXT calculation. */
 
47
  uint m_token_array_length;
 
48
 
 
49
  sql_digest_storage()
 
50
  {
 
51
    reset(NULL, 0);
 
52
  }
 
53
 
 
54
  inline void reset(unsigned char *token_array, uint length)
 
55
  {
 
56
    m_token_array= token_array;
 
57
    m_token_array_length= length;
 
58
    reset();
 
59
  }
 
60
 
 
61
  inline void reset()
 
62
  {
 
63
    m_full= false;
 
64
    m_byte_count= 0;
 
65
    m_charset_number= 0;
 
66
    if (m_token_array_length > 0)
 
67
    {
 
68
      memset(m_token_array, 0, m_token_array_length);
 
69
    }
 
70
    memset(m_md5, 0, MD5_HASH_SIZE);
 
71
  }
 
72
 
 
73
  inline bool is_empty()
 
74
  {
 
75
    return (m_byte_count == 0);
 
76
  }
 
77
 
 
78
  inline void copy(const sql_digest_storage *from)
 
79
  {
 
80
    /*
 
81
      Keep in mind this is a dirty copy of something that may change,
 
82
      as the thread producing the digest is executing concurrently,
 
83
      without any lock enforced.
 
84
    */
 
85
    uint byte_count_copy= m_token_array_length < from->m_byte_count ?
 
86
                          m_token_array_length : from->m_byte_count;
 
87
 
 
88
    if (byte_count_copy > 0)
 
89
    {
 
90
      m_full= from->m_full;
 
91
      m_byte_count= byte_count_copy;
 
92
      m_charset_number= from->m_charset_number;
 
93
      memcpy(m_token_array, from->m_token_array, m_byte_count);
 
94
      memcpy(m_md5, from->m_md5, MD5_HASH_SIZE);
 
95
    }
 
96
    else
 
97
    {
 
98
      m_full= false;
 
99
      m_byte_count= 0;
 
100
      m_charset_number= 0;
 
101
    }
 
102
  }
 
103
};
 
104
typedef struct sql_digest_storage sql_digest_storage;
 
105
 
 
106
/**
 
107
  Compute a digest hash.
 
108
  @param digest_storage The digest
 
109
  @param [out] md5 The computed digest hash. This parameter is a buffer of size @c MD5_HASH_SIZE.
 
110
*/
 
111
void compute_digest_md5(const sql_digest_storage *digest_storage, unsigned char *md5);
 
112
 
 
113
/**
 
114
  Compute a digest text.
 
115
  A 'digest text' is a textual representation of a query,
 
116
  where:
 
117
  - comments are removed,
 
118
  - non significant spaces are removed,
 
119
  - literal values are replaced with a special '?' marker,
 
120
  - lists of values are collapsed using a shorter notation
 
121
  @param digest_storage The digest
 
122
  @param [out] digest_text
 
123
  @param digest_text_length Size of @c digest_text.
 
124
  @param [out] truncated true if the text representation was truncated
 
125
*/
 
126
void compute_digest_text(const sql_digest_storage *digest_storage,
 
127
                         String *digest_text);
 
128
 
 
129
#endif
 
130