137
137
** List all table types supported
138
138
***************************************************************************/
141
find_files() - find files in a given directory.
145
session thread handler
146
files put found files in this list
147
db database name to set in TableList structure
148
path path to database
149
wild filter for found files
150
dir read databases in path if true, read .frm files in
154
FIND_FILES_OK success
155
FIND_FILES_OOM out of memory error
156
FIND_FILES_DIR no such directory, or directory can't be read
161
find_files(Session *session, List<LEX_STRING> *files, const char *db,
162
const char *path, const char *wild, bool dir)
143
* Find files in a given directory.
145
* @param[in] session Thread handler
146
* @param[out] files Put found files in this list
147
* @param[in] db Used in error message when directory is not found
148
* @param[in] path Path to database
149
* @param[in] wild Filter for found files
150
* @param[in] dir Read databases in path if true, read .frm files in
153
* @retval FIND_FILES_OK Success
154
* @retval FIND_FILES_OOM Out of memory error
155
* @retval FIND_FILES_DIR No such directory or directory can't be read
157
find_files_result find_files(Session *session, vector<LEX_STRING*> &files,
158
const char *db, const char *path, const char *wild,
168
LEX_STRING *file_name= 0;
169
uint32_t file_name_len;
170
TableList table_list;
172
if (wild && !wild[0])
175
if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0))))
161
if (wild && (wild[0] == '\0'))
164
MY_DIR *dirp= my_dir(path, MYF(dir ? MY_WANT_STAT : 0));
177
167
if (my_errno == ENOENT)
178
168
my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), db);
180
170
my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), path, my_errno);
181
172
return(FIND_FILES_DIR);
184
for (i=0 ; i < (uint32_t) dirp->number_off_files ; i++)
175
for (unsigned i= 0; i < dirp->number_off_files; i++)
177
uint32_t file_name_len;
186
178
char uname[NAME_LEN + 1]; /* Unencoded name */
187
file=dirp->dir_entry+i;
179
FILEINFO *file= dirp->dir_entry+i;
189
182
{ /* Return databases */
190
183
if ((file->name[0] == '.' &&
191
184
((file->name[1] == '.' && file->name[2] == '\0') ||
192
185
file->name[1] == '\0')))
193
186
continue; /* . or .. */
196
char buff[FN_REFLEN];
197
if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
199
/* Only show the sym file if it points to a directory */
201
*ext=0; /* Remove extension */
202
unpack_dirname(buff, file->name);
203
end= strchr(buff, '\0');
204
if (end != buff && end[-1] == FN_LIBCHAR)
205
end[-1]= 0; // Remove end FN_LIBCHAR
206
if (stat(buff, file->mystat))
210
188
if (!S_ISDIR(file->mystat->st_mode))
213
191
file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
214
192
if (wild && wild_compare(uname, wild, 0))
217
session->make_lex_string(file_name, uname, file_name_len, true)))
220
return(FIND_FILES_OOM);
225
197
// Return only .frm files which aren't temp files.
226
if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),".dfe") ||
198
char *ext= fn_rext(file->name);
199
if (my_strcasecmp(system_charset_info, ext, ".dfe") ||
227
200
is_prefix(file->name, TMP_FILE_PREFIX))
230
204
file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
1751
1724
Create list of existing databases. It is used in case
1752
1725
of select from information schema table
1754
if (files->push_back(i_s_name_copy))
1727
files.push_back(i_s_name_copy);
1756
1729
*with_i_schema= 1;
1757
1730
return (find_files(session, files, NULL,
1758
1731
drizzle_data_home, NULL, 1) != FIND_FILES_OK);
1762
struct st_add_schema_table
1764
List<LEX_STRING> *files;
1769
1735
class AddSchemaTable : public unary_function<InfoSchemaTable *, bool>
1771
1737
Session *session;
1772
st_add_schema_table *data;
1739
vector<LEX_STRING*> &files;
1774
AddSchemaTable(Session *session_arg, st_add_schema_table *data_arg)
1775
: session(session_arg), data(data_arg) {}
1742
AddSchemaTable(Session *session_arg, vector<LEX_STRING*> &files_arg, const char *wild_arg)
1743
: session(session_arg), wild(wild_arg), files(files_arg)
1776
1746
result_type operator() (argument_type schema_table)
1778
LEX_STRING *file_name= 0;
1779
List<LEX_STRING> *file_list= data->files;
1780
const char *wild= data->wild;
1782
1748
if (schema_table->isHidden())
1786
if (wild_case_compare(files_charset_info,
1787
schema_table->getTableName().c_str(),
1792
if ((file_name= session->make_lex_string(file_name,
1793
schema_table->getTableName().c_str(),
1794
schema_table->getTableName().length(),
1796
!file_list->push_back(file_name))
1753
const string &schema_table_name= schema_table->getTableName();
1755
if (wild && wild_case_compare(files_charset_info, schema_table_name.c_str(), wild))
1760
LEX_STRING *file_name= 0;
1761
file_name= session->make_lex_string(file_name, schema_table_name.c_str(),
1762
schema_table_name.length(), true);
1763
if (file_name == NULL)
1768
files.push_back(file_name);
1803
static int schema_tables_add(Session *session, List<LEX_STRING> *files, const char *wild)
1774
static int schema_tables_add(Session *session, vector<LEX_STRING*> &files, const char *wild)
1805
LEX_STRING *file_name= 0;
1806
1776
InfoSchemaTable *tmp_schema_table= schema_tables;
1807
st_add_schema_table add_data;
1809
1778
for (; tmp_schema_table->getTableName().length() != 0; tmp_schema_table++)
1811
1780
if (tmp_schema_table->isHidden())
1815
if (wild_case_compare(files_charset_info,
1816
tmp_schema_table->getTableName().c_str(),
1821
session->make_lex_string(file_name,
1822
tmp_schema_table->getTableName().c_str(),
1823
tmp_schema_table->getTableName().length(),
1825
!files->push_back(file_name))
1785
const string &schema_table_name= tmp_schema_table->getTableName();
1787
if (wild && wild_case_compare(files_charset_info, schema_table_name.c_str(), wild))
1792
LEX_STRING *file_name= 0;
1793
file_name= session->make_lex_string(file_name, schema_table_name.c_str(),
1794
schema_table_name.length(), true);
1795
if (file_name == NULL)
1800
files.push_back(file_name);
1830
add_data.files= files;
1831
add_data.wild= wild;
1832
vector<InfoSchemaTable *>::iterator iter=
1833
find_if(all_schema_tables.begin(), all_schema_tables.end(),
1834
AddSchemaTable(session, &add_data));
1803
vector<InfoSchemaTable *>::iterator iter= find_if(all_schema_tables.begin(),
1804
all_schema_tables.end(),
1805
AddSchemaTable(session, files, wild));
1835
1807
if (iter != all_schema_tables.end())
2235
if (make_db_list(session, &db_names, &lookup_field_vals, &with_i_schema))
2205
if (make_db_list(session, db_names, &lookup_field_vals, &with_i_schema))
2237
it.rewind(); /* To get access to new elements in basis list */
2238
while ((db_name= it++))
2208
for (vector<LEX_STRING*>::iterator db_name= db_names.begin(); db_name != db_names.end(); ++db_name )
2210
session->no_warnings_for_error= 1;
2211
table_names.clear();
2212
int res= make_table_name_list(session, table_names, lex,
2214
with_i_schema, *db_name);
2216
if (res == 2) /* Not fatal error, continue */
2223
for (vector<LEX_STRING*>::iterator table_name= table_names.begin(); table_name != table_names.end(); ++table_name)
2241
session->no_warnings_for_error= 1;
2242
List<LEX_STRING> table_names;
2243
int res= make_table_name_list(session, &table_names, lex,
2245
with_i_schema, db_name);
2246
if (res == 2) /* Not fatal error, continue */
2225
table->restoreRecordAsDefault();
2226
table->field[schema_table->getFirstColumnIndex()]->
2227
store((*db_name)->str, (*db_name)->length, system_charset_info);
2228
table->field[schema_table->getSecondColumnIndex()]->
2229
store((*table_name)->str, (*table_name)->length, system_charset_info);
2251
List_iterator_fast<LEX_STRING> it_files(table_names);
2252
while ((table_name= it_files++))
2231
if (!partial_cond || partial_cond->val_int())
2254
table->restoreRecordAsDefault();
2255
table->field[schema_table->getFirstColumnIndex()]->
2256
store(db_name->str, db_name->length, system_charset_info);
2257
table->field[schema_table->getSecondColumnIndex()]->
2258
store(table_name->str, table_name->length, system_charset_info);
2234
If table is I_S.tables and open_table_method is 0 (eg SKIP_OPEN)
2235
we can skip table opening and we don't have lookup value for
2236
table name or lookup value is wild string(table name list is
2237
already created by make_table_name_list() function).
2239
if (! table_open_method &&
2240
schema_table->getTableName().compare("TABLES") == 0 &&
2241
(! lookup_field_vals.table_value.length ||
2242
lookup_field_vals.wild_table_value))
2244
if (schema_table_store_record(session, table))
2245
goto err; /* Out of space in temporary table */
2260
if (!partial_cond || partial_cond->val_int())
2249
/* SHOW Table NAMES command */
2250
if (schema_table->getTableName().compare("TABLE_NAMES") == 0)
2263
If table is I_S.tables and open_table_method is 0 (eg SKIP_OPEN)
2264
we can skip table opening and we don't have lookup value for
2265
table name or lookup value is wild string(table name list is
2266
already created by make_table_name_list() function).
2268
if (! table_open_method &&
2269
schema_table->getTableName().compare("TABLES") == 0 &&
2270
(! lookup_field_vals.table_value.length ||
2271
lookup_field_vals.wild_table_value))
2273
if (schema_table_store_record(session, table))
2274
goto err; /* Out of space in temporary table */
2252
if (fill_schema_table_names(session, tables->table, *db_name,
2253
*table_name, with_i_schema))
2278
/* SHOW Table NAMES command */
2279
if (schema_table->getTableName().compare("TABLE_NAMES") == 0)
2258
if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2281
if (fill_schema_table_names(session, tables->table, db_name,
2282
table_name, with_i_schema))
2261
if (!fill_schema_table_from_frm(session, tables, schema_table, *db_name,
2266
LEX_STRING tmp_lex_string, orig_db_name;
2268
Set the parent lex of 'sel' because it is needed by
2269
sel.init_query() which is called inside make_table_list.
2271
session->no_warnings_for_error= 1;
2272
sel.parent_lex= lex;
2273
/* db_name can be changed in make_table_list() func */
2274
if (!session->make_lex_string(&orig_db_name, (*db_name)->str,
2275
(*db_name)->length, false))
2278
if (make_table_list(session, &sel, *db_name, *table_name))
2281
TableList *show_table_list= (TableList*) sel.table_list.first;
2282
lex->all_selects_list= &sel;
2283
lex->derived_tables= 0;
2284
lex->sql_command= SQLCOM_SHOW_FIELDS;
2285
show_table_list->i_s_requested_object=
2286
schema_table->getRequestedObject();
2287
res= session->open_normal_and_derived_tables(show_table_list, DRIZZLE_LOCK_IGNORE_FLUSH);
2288
lex->sql_command= save_sql_command;
2290
XXX-> show_table_list has a flag i_is_requested,
2291
and when it's set, open_normal_and_derived_tables()
2292
can return an error without setting an error message
2293
in Session, which is a hack. This is why we have to
2294
check for res, then for session->is_error() only then
2295
for session->main_da.sql_errno().
2297
if (res && session->is_error() &&
2298
session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2301
Hide error for not existing table.
2302
This error can occur for example when we use
2303
where condition with db name and table name and this
2304
table does not exist.
2307
session->clear_error();
2287
if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2290
if (!fill_schema_table_from_frm(session, tables, schema_table, db_name,
2295
LEX_STRING tmp_lex_string, orig_db_name;
2297
Set the parent lex of 'sel' because it is needed by
2298
sel.init_query() which is called inside make_table_list.
2300
session->no_warnings_for_error= 1;
2301
sel.parent_lex= lex;
2302
/* db_name can be changed in make_table_list() func */
2303
if (!session->make_lex_string(&orig_db_name, db_name->str,
2304
db_name->length, false))
2306
if (make_table_list(session, &sel, db_name, table_name))
2308
TableList *show_table_list= (TableList*) sel.table_list.first;
2309
lex->all_selects_list= &sel;
2310
lex->derived_tables= 0;
2311
lex->sql_command= SQLCOM_SHOW_FIELDS;
2312
show_table_list->i_s_requested_object=
2313
schema_table->getRequestedObject();
2314
res= session->open_normal_and_derived_tables(show_table_list, DRIZZLE_LOCK_IGNORE_FLUSH);
2315
lex->sql_command= save_sql_command;
2317
XXX-> show_table_list has a flag i_is_requested,
2318
and when it's set, open_normal_and_derived_tables()
2319
can return an error without setting an error message
2320
in Session, which is a hack. This is why we have to
2321
check for res, then for session->is_error() only then
2322
for session->main_da.sql_errno().
2324
if (res && session->is_error() &&
2325
session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2328
Hide error for not existing table.
2329
This error can occur for example when we use
2330
where condition with db name and table name and this
2331
table does not exist.
2334
session->clear_error();
2339
We should use show_table_list->alias instead of
2340
show_table_list->table_name because table_name
2341
could be changed during opening of I_S tables. It's safe
2342
to use alias because alias contains original table name
2345
session->make_lex_string(&tmp_lex_string, show_table_list->alias,
2346
strlen(show_table_list->alias), false);
2347
res= schema_table->processTable(session, show_table_list, table,
2350
session->close_tables_for_reopen(&show_table_list);
2352
assert(!lex->query_tables_own_last);
2312
We should use show_table_list->alias instead of
2313
show_table_list->table_name because table_name
2314
could be changed during opening of I_S tables. It's safe
2315
to use alias because alias contains original table name
2318
session->make_lex_string(&tmp_lex_string, show_table_list->alias,
2319
strlen(show_table_list->alias), false);
2320
res= schema_table->processTable(session, show_table_list, table,
2323
session->close_tables_for_reopen(&show_table_list);
2325
assert(!lex->query_tables_own_last);
2359
If we have information schema its always the first table and only
2360
the first table. Reset for other tables.
2332
If we have information schema its always the first table and only
2333
the first table. Reset for other tables.
2368
2341
session->restore_backup_open_tables_state(&open_tables_state_backup);
2369
2342
lex->derived_tables= derived_tables;