1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2011 Kentoku SHIBA
4 Copyright(C) 2011-2015 Kouhei Sutou <kou@clear-code.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#include <mrn_mysql.h>
22
23#include "mrn_index_table_name.hpp"
24
25// for debug
26#define MRN_CLASS_NAME "mrn::IndexTableName"
27
28namespace mrn {
29 const char *IndexTableName::SEPARATOR = "#";
30 const char *IndexTableName::OLD_SEPARATOR = "-";
31
32 bool IndexTableName::is_custom_name(const char *table_name,
33 size_t table_name_length,
34 const char *index_table_name,
35 size_t index_table_name_length)
36 {
37 MRN_DBUG_ENTER_METHOD();
38
39 if (index_table_name_length <= (table_name_length + strlen(SEPARATOR))) {
40 DBUG_RETURN(true);
41 }
42
43 if (strncmp(table_name, index_table_name, table_name_length) != 0) {
44 DBUG_RETURN(true);
45 }
46
47 if ((strncmp(OLD_SEPARATOR,
48 index_table_name + table_name_length,
49 strlen(OLD_SEPARATOR)) != 0) &&
50 (strncmp(SEPARATOR,
51 index_table_name + table_name_length,
52 strlen(SEPARATOR)) != 0)) {
53 DBUG_RETURN(true);
54 }
55
56 DBUG_RETURN(false);
57 }
58
59 IndexTableName::IndexTableName(const char *table_name,
60 const char *mysql_index_name)
61 : table_name_(table_name),
62 mysql_index_name_(mysql_index_name) {
63 uchar encoded_mysql_index_name_multibyte[MRN_MAX_KEY_SIZE];
64 const uchar *mysql_index_name_multibyte =
65 reinterpret_cast<const uchar *>(mysql_index_name_);
66 encode(encoded_mysql_index_name_multibyte,
67 encoded_mysql_index_name_multibyte + MRN_MAX_KEY_SIZE,
68 mysql_index_name_multibyte,
69 mysql_index_name_multibyte + strlen(mysql_index_name_));
70 snprintf(old_name_, MRN_MAX_KEY_SIZE,
71 "%s%s%s",
72 table_name_,
73 OLD_SEPARATOR,
74 encoded_mysql_index_name_multibyte);
75 old_length_ = strlen(old_name_);
76 snprintf(name_, MRN_MAX_KEY_SIZE,
77 "%s%s%s",
78 table_name_,
79 SEPARATOR,
80 encoded_mysql_index_name_multibyte);
81 length_ = strlen(name_);
82 }
83
84 const char *IndexTableName::c_str() {
85 return name_;
86 }
87
88 size_t IndexTableName::length() {
89 return length_;
90 }
91
92 const char *IndexTableName::old_c_str() {
93 return old_name_;
94 }
95
96 size_t IndexTableName::old_length() {
97 return old_length_;
98 }
99
100 uint IndexTableName::encode(uchar *encoded_start,
101 uchar *encoded_end,
102 const uchar *mysql_string_start,
103 const uchar *mysql_string_end) {
104 MRN_DBUG_ENTER_METHOD();
105 my_charset_conv_mb_wc mb_wc = system_charset_info->cset->mb_wc;
106 my_charset_conv_wc_mb wc_mb = my_charset_filename.cset->wc_mb;
107 DBUG_PRINT("info", ("mroonga: in=%s", mysql_string_start));
108 encoded_end--;
109 uchar *encoded = encoded_start;
110 const uchar *mysql_string = mysql_string_start;
111 while (mysql_string < mysql_string_end && encoded < encoded_end) {
112 my_wc_t wc;
113 int mb_wc_converted_length;
114 int wc_mb_converted_length;
115 mb_wc_converted_length =
116 (*mb_wc)(NULL, &wc, mysql_string, mysql_string_end);
117 if (mb_wc_converted_length > 0) {
118 wc_mb_converted_length = (*wc_mb)(NULL, wc, encoded, encoded_end);
119 if (wc_mb_converted_length <= 0) {
120 break;
121 }
122 } else if (mb_wc_converted_length == MY_CS_ILSEQ) {
123 *encoded = *mysql_string;
124 mb_wc_converted_length = 1;
125 wc_mb_converted_length = 1;
126 } else {
127 break;
128 }
129 mysql_string += mb_wc_converted_length;
130 encoded += wc_mb_converted_length;
131 }
132 *encoded = '\0';
133 DBUG_PRINT("info", ("mroonga: out=%s", encoded_start));
134 DBUG_RETURN(encoded - encoded_start);
135 }
136}
137