1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2011-2013 Kentoku SHIBA
4 Copyright(C) 2011-2012 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_column_name.hpp"
24
25#define MRN_MIN_INDEX_COLUMN_NAME_LENGTH 65
26
27// for debug
28#define MRN_CLASS_NAME "mrn::IndexColumnName"
29
30namespace mrn {
31 IndexColumnName::IndexColumnName(const char *table_name,
32 const char *mysql_column_name)
33 : table_name_(table_name),
34 mysql_column_name_(mysql_column_name) {
35 uchar encoded_mysql_column_name_multibyte[MRN_MAX_KEY_SIZE];
36 const uchar *mysql_column_name_multibyte =
37 reinterpret_cast<const uchar *>(mysql_column_name_);
38 encode(encoded_mysql_column_name_multibyte,
39 encoded_mysql_column_name_multibyte + MRN_MAX_KEY_SIZE,
40 mysql_column_name_multibyte,
41 mysql_column_name_multibyte + strlen(mysql_column_name_));
42 snprintf(name_, MRN_MAX_KEY_SIZE,
43 "%s-%s", table_name_, encoded_mysql_column_name_multibyte);
44 length_ = strlen(name_);
45 if (length_ < MRN_MIN_INDEX_COLUMN_NAME_LENGTH) {
46 memset(name_ + length_, '-', MRN_MIN_INDEX_COLUMN_NAME_LENGTH - length_);
47 length_ = MRN_MIN_INDEX_COLUMN_NAME_LENGTH;
48 name_[length_] = '\0';
49 }
50 }
51
52 const char *IndexColumnName::c_str() {
53 return name_;
54 }
55
56 size_t IndexColumnName::length() {
57 return length_;
58 }
59
60 uint IndexColumnName::encode(uchar *encoded_start,
61 uchar *encoded_end,
62 const uchar *mysql_string_start,
63 const uchar *mysql_string_end) {
64 MRN_DBUG_ENTER_METHOD();
65 my_charset_conv_mb_wc mb_wc = system_charset_info->cset->mb_wc;
66 my_charset_conv_wc_mb wc_mb = my_charset_filename.cset->wc_mb;
67 DBUG_PRINT("info", ("mroonga: in=%s", mysql_string_start));
68 encoded_end--;
69 uchar *encoded = encoded_start;
70 const uchar *mysql_string = mysql_string_start;
71 while (mysql_string < mysql_string_end && encoded < encoded_end) {
72 my_wc_t wc;
73 int mb_wc_converted_length;
74 int wc_mb_converted_length;
75 mb_wc_converted_length =
76 (*mb_wc)(NULL, &wc, mysql_string, mysql_string_end);
77 if (mb_wc_converted_length > 0) {
78 wc_mb_converted_length = (*wc_mb)(NULL, wc, encoded, encoded_end);
79 if (wc_mb_converted_length <= 0) {
80 break;
81 }
82 } else if (mb_wc_converted_length == MY_CS_ILSEQ) {
83 *encoded = *mysql_string;
84 mb_wc_converted_length = 1;
85 wc_mb_converted_length = 1;
86 } else {
87 break;
88 }
89 mysql_string += mb_wc_converted_length;
90 encoded += wc_mb_converted_length;
91 }
92 *encoded = '\0';
93 DBUG_PRINT("info", ("mroonga: out=%s", encoded_start));
94 DBUG_RETURN(encoded - encoded_start);
95 }
96}
97