From: Date: April 18 2006 9:34am Subject: Re: getting table meta data (primary key, in this case) List-Archive: http://lists.mysql.com/internals/33521 Message-Id: MIME-Version: 1.0 (Apple Message framework v746.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Content-Transfer-Encoding: 7bit Hi! On 15 Apr 2006, at 19:03, Eric Prud'hommeaux wrote: [skip] > worky (and quite easily)!!! Thank you SO much for the pointer! > > [[ > bool Item_variable::fix_fields(THD *thd, Item **reference) { > if (primary_key) > field_name = get_primary_key(); > return Item_field::fix_fields(thd, reference); > } > > const char* sparqlFrob::get_primary_key(const char *table_name) { > TABLE_LIST *entry = thd->main_lex.select_lex.context.table_list; > while (entry) { > if (!strcmp(table_name, entry->table_name)) { > KEY *key = entry->table->key_info; in above statement you have to be sure that 'entry' is not VIEW of several tables with MERGE algorithm, so you better check entry->table, also above have not much sense (now, in future things may change) for VIEW with TEMPTABLE algorithm, but it will not crash at least. > for (unsigned i = 0; i < entry->table->s->keys; i++) { // > from sql_show.cc::2905 get_schema_stat_record > if (!strcmp(key[i].name, "PRIMARY")) { > assert(key[i].key_parts == 1); > return key[i].key_part->field->field_name; > } > } > assert(0); // couldn't find PRIMARY KEY > } > entry = entry->next_global; > } > assert(0); // couldn't find table > } > ]] > It brings up 2 questions: > > Is there a way to kill a thread ('cause something went horribly > wrong), without killing the whole server? What's the polite way to > assert(0)? set thd->kill and check it? (I am not sure) > > Is there a better way to find the table_list for a thread? I feel > like thd->main_lex.select_lex.context.table_list should really > include some inode numbers, the process id and maybe some astrology. Do you really need all query (thread) tables? Or just tables of current SELECT? For global table list you can check thd->main_lex.query_tables and move by TABLE_LIST::next_global. This list will not contain underlying view tables until table opened. Also it do not contain procedure tables (in lock operation it contain some placeholders for them). if you need SELECT list of tables there is several lists: - list of tables mentioned in the FROM clause (do not include view underlying tables) - tree of join structure - list of leaf tables of join tree (build in setup_tables()) (looks like you need this one, if you need SELECT list of tables) -- __ ___ ___ ____ __ / |/ /_ __/ __/ __ \/ / Mr. Oleksandr Byelkin / /|_/ / // /\ \/ /_/ / /__ MySQL AB, Full-Time Developer /_/ /_/\_, /___/\___\_\___/ Lugansk, Ukraine <___/ www.mysql.com