~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Monty Taylor
  • Date: 2009-06-09 14:30:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1060.
  • Revision ID: mordred@inaugust.com-20090609143027-4gx5zvwmqtznqeoi
RemovedĀ strconvert.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
using namespace std;
38
38
extern drizzled::TransactionServices transaction_services;
39
39
 
 
40
static const char hexchars[]= "0123456789abcdef";
40
41
bool is_primary_key(KEY *key_info)
41
42
{
42
43
  static const char * primary_key_name="PRIMARY";
91
92
 
92
93
  SYNOPSIS
93
94
    filename_to_tablename()
94
 
      from                      The file name in my_charset_filename.
95
 
      to                OUT     The table name in system_charset_info.
 
95
      from                      The file name
 
96
      to                OUT     The table name
96
97
      to_length                 The size of the table name buffer.
97
98
 
98
99
  RETURN
100
101
*/
101
102
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length)
102
103
{
103
 
  uint32_t errors;
104
 
  uint32_t res;
 
104
  uint32_t length= 0;
105
105
 
106
106
  if (!memcmp(from, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH))
107
107
  {
108
108
    /* Temporary table name. */
109
 
    res= strlen(strncpy(to, from, to_length));
 
109
    length= strlen(strncpy(to, from, to_length));
110
110
  }
111
111
  else
112
112
  {
113
 
    res= strconvert(&my_charset_filename, from,
114
 
                    system_charset_info,  to, to_length, &errors);
115
 
    if (errors) // Old 5.0 name
 
113
    for (; *from  && length < to_length; length++, from++)
116
114
    {
117
 
      to[0]='\0';
118
 
      strncat(to, from, to_length-1);
119
 
      res= strlen(to);
120
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid (old?) table or database name '%s'"), from);
121
 
    }
 
115
      if (*from != '@')
 
116
      {
 
117
        to[length]= *from;
 
118
        continue;
 
119
      }
 
120
      /* We've found an escaped char - skip the @ */
 
121
      from++;
 
122
      /* There will be a three-position hex-char version of the char */
 
123
      for (int x=2; x >= 0; x--)
 
124
      {
 
125
        if (*from >= '0' && *from <= '9')
 
126
          to[length] += ((*from++ - '0') << (4 * x));
 
127
        else if (*from >= 'a' && *from <= 'f')
 
128
          to[length] += ((*from++ - 'a' + 10) << (4 * x));
 
129
      }
 
130
      /* Backup because we advanced extra in the inner loop */
 
131
      from--;
 
132
    } 
122
133
  }
123
134
 
124
 
  return(res);
 
135
  return length;
125
136
}
126
137
 
127
138
 
130
141
 
131
142
  SYNOPSIS
132
143
    tablename_to_filename()
133
 
      from                      The table name in system_charset_info.
134
 
      to                OUT     The file name in my_charset_filename.
 
144
      from                      The table name
 
145
      to                OUT     The file name
135
146
      to_length                 The size of the file name buffer.
136
147
 
137
148
  RETURN
138
 
    File name length.
 
149
    true if errors happen. false on success.
139
150
*/
140
 
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length)
 
151
static
 
152
bool tablename_to_filename(const char *from, char *to, size_t to_length)
141
153
{
142
 
  uint32_t errors, length;
143
 
 
144
 
  length= strconvert(system_charset_info, from,
145
 
                     &my_charset_filename, to, to_length, &errors);
 
154
  
 
155
  size_t length= 0;
 
156
  for (; *from  && length < to_length; length++, from++)
 
157
  {
 
158
    if ((*from >= '0' && *from <= '9') ||
 
159
        (*from >= 'A' && *from <= 'Z') ||
 
160
        (*from >= 'a' && *from <= 'z') ||
 
161
        (*from == '_') || (*from == ' ') ||
 
162
        ((unsigned char)*from >= 128))
 
163
    {
 
164
      to[length]= *from;
 
165
      continue;
 
166
    }
 
167
   
 
168
    if (length + 4 >= to_length)
 
169
      return true;
 
170
 
 
171
    /* We need to escape this char in a way that can be reversed */
 
172
    to[length++]= '@';
 
173
    to[length++]= hexchars[(*from >> 8) & 15];
 
174
    to[length++]= hexchars[(*from >> 4) & 15];
 
175
    to[length]= hexchars[(*from) & 15];
 
176
  }
 
177
 
146
178
  if (check_if_legal_tablename(to) &&
147
179
      length + 4 < to_length)
148
180
  {
149
181
    memcpy(to + length, "@@@", 4);
150
182
    length+= 3;
151
183
  }
152
 
  return(length);
 
184
  return false;
153
185
}
154
186
 
155
187
 
158
190
 
159
191
  SYNOPSIS
160
192
   build_table_filename()
161
 
     buff                       Where to write result in my_charset_filename.
 
193
     buff                       Where to write result
162
194
                                This may be the same as table_name.
163
195
     bufflen                    buff size
164
 
     db                         Database name in system_charset_info.
165
 
     table_name                 Table name in system_charset_info.
 
196
     db                         Database name
 
197
     table_name                 Table name
166
198
     ext                        File extension.
167
199
     flags                      FN_FROM_IS_TMP or FN_TO_IS_TMP or FN_IS_TMP
168
200
                                table_name is temporary, do not change.
188
220
 
189
221
size_t build_table_filename(char *buff, size_t bufflen, const char *db, const char *table_name, bool is_tmp)
190
222
{
191
 
  string table_path;
192
223
  char dbbuff[FN_REFLEN];
193
224
  char tbbuff[FN_REFLEN];
194
 
  int rootdir_len= strlen(FN_ROOTDIR);
 
225
  bool conversion_error= false;
195
226
 
 
227
  memset(tbbuff, 0, sizeof(tbbuff));
196
228
  if (is_tmp) // FN_FROM_IS_TMP | FN_TO_IS_TMP
197
229
    strncpy(tbbuff, table_name, sizeof(tbbuff));
198
230
  else
199
 
    tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
 
231
  {
 
232
    conversion_error= tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
 
233
    if (conversion_error)
 
234
    {
 
235
      errmsg_printf(ERRMSG_LVL_ERROR,
 
236
                    _("Table name cannot be encoded and fit within filesystem "
 
237
                      "name length restrictions."));
 
238
      return 0;
 
239
    }
 
240
  }
 
241
  memset(dbbuff, 0, sizeof(dbbuff));
 
242
  conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
 
243
  if (conversion_error)
 
244
  {
 
245
    errmsg_printf(ERRMSG_LVL_ERROR,
 
246
                  _("Schema name cannot be encoded and fit within filesystem "
 
247
                    "name length restrictions."));
 
248
    return 0;
 
249
  }
 
250
   
200
251
 
201
 
  tablename_to_filename(db, dbbuff, sizeof(dbbuff));
202
 
  table_path= drizzle_data_home;
 
252
  int rootdir_len= strlen(FN_ROOTDIR);
 
253
  string table_path(drizzle_data_home);
203
254
  int without_rootdir= table_path.length()-rootdir_len;
204
255
 
205
256
  /* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
206
257
  if (without_rootdir >= 0)
207
258
  {
208
 
    char *tmp= (char*)table_path.c_str()+without_rootdir;
 
259
    const char *tmp= table_path.c_str()+without_rootdir;
209
260
    if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
210
261
      table_path.append(FN_ROOTDIR);
211
262
  }
232
283
  SYNOPSIS
233
284
   build_tmptable_filename()
234
285
     session                    The thread handle.
235
 
     buff                       Where to write result in my_charset_filename.
 
286
     buff                       Where to write result
236
287
     bufflen                    buff size
237
288
 
238
289
  NOTES