diff options
Diffstat (limited to 'sql/datadict.cc')
-rw-r--r-- | sql/datadict.cc | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/sql/datadict.cc b/sql/datadict.cc index 77f7a0fcfcb..882f0b7232e 100644 --- a/sql/datadict.cc +++ b/sql/datadict.cc @@ -13,17 +13,18 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ -#include <my_global.h> +#include "mariadb.h" #include "datadict.h" #include "sql_priv.h" #include "sql_class.h" #include "sql_table.h" +#include "ha_sequence.h" static int read_string(File file, uchar**to, size_t length) { DBUG_ENTER("read_string"); - my_free(*to); + /* This can't use MY_THREAD_SPECIFIC as it's used on server start */ if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) || mysql_file_read(file, *to, length, MYF(MY_NABP))) { @@ -43,56 +44,75 @@ static int read_string(File file, uchar**to, size_t length) @param[in] path path to FRM file. @param[in/out] engine_name table engine name (length < NAME_CHAR_LEN) - engine_name is a LEX_STRING, where engine_name->str must point to + engine_name is a LEX_CSTRING, where engine_name->str must point to a buffer of at least NAME_CHAR_LEN+1 bytes. If engine_name is 0, then the function will only test if the file is a view or not - @retval FRMTYPE_ERROR error - @retval FRMTYPE_TABLE table - @retval FRMTYPE_VIEW view + @param[out] is_sequence 1 if table is a SEQUENCE, 0 otherwise + + @retval TABLE_TYPE_UNKNOWN error - file can't be opened + @retval TABLE_TYPE_NORMAL table + @retval TABLE_TYPE_SEQUENCE sequence table + @retval TABLE_TYPE_VIEW view */ -frm_type_enum dd_frm_type(THD *thd, char *path, LEX_STRING *engine_name) +Table_type dd_frm_type(THD *thd, char *path, LEX_CSTRING *engine_name, + bool *is_sequence) { File file; - uchar header[10]; //"TYPE=VIEW\n" it is 10 characters + uchar header[40]; //"TYPE=VIEW\n" it is 10 characters size_t error; - frm_type_enum type= FRMTYPE_ERROR; + Table_type type= TABLE_TYPE_UNKNOWN; uchar dbt; DBUG_ENTER("dd_frm_type"); - if ((file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0))) < 0) - DBUG_RETURN(FRMTYPE_ERROR); - error= mysql_file_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP)); + *is_sequence= 0; - if (error) - goto err; - if (!strncmp((char*) header, "TYPE=VIEW\n", sizeof(header))) - { - type= FRMTYPE_VIEW; - goto err; - } + if ((file= mysql_file_open(key_file_frm, path, O_RDONLY | O_SHARE, MYF(0))) + < 0) + DBUG_RETURN(TABLE_TYPE_UNKNOWN); /* - We return FRMTYPE_TABLE if we can read the .frm file. This allows us + We return TABLE_TYPE_NORMAL if we can open the .frm file. This allows us to drop a bad .frm file with DROP TABLE */ - type= FRMTYPE_TABLE; + type= TABLE_TYPE_NORMAL; + + /* + Initialize engine name in case we are not able to find it out + The cast is safe, as engine_name->str points to a usable buffer. + */ + if (engine_name) + { + engine_name->length= 0; + ((char*) (engine_name->str))[0]= 0; + } + + if (unlikely((error= mysql_file_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP))))) + goto err; + + if (unlikely((!strncmp((char*) header, "TYPE=VIEW\n", 10)))) + { + type= TABLE_TYPE_VIEW; + goto err; + } /* engine_name is 0 if we only want to know if table is view or not */ if (!engine_name) goto err; - /* Initialize engine name in case we are not able to find it out */ - engine_name->length= 0; - engine_name->str[0]= 0; - if (!is_binary_frm_header(header)) goto err; dbt= header[3]; + if (((header[39] >> 4) & 3) == HA_CHOICE_YES) + { + DBUG_PRINT("info", ("Sequence found")); + *is_sequence= 1; + } + /* cannot use ha_resolve_by_legacy_type without a THD */ if (thd && dbt < DB_TYPE_FIRST_DYNAMIC) { @@ -134,8 +154,14 @@ frm_type_enum dd_frm_type(THD *thd, char *path, LEX_STRING *engine_name) { uint len= uint2korr(next_chunk); if (len <= NAME_CHAR_LEN) - strmake(engine_name->str, (char*)next_chunk + 2, + { + /* + The following cast is safe as the caller has allocated buffer + and it's up to this function to generate the name. + */ + strmake((char*) engine_name->str, (char*)next_chunk + 2, engine_name->length= len); + } } } @@ -170,7 +196,7 @@ bool dd_recreate_table(THD *thd, const char *db, const char *table_name, char path_buf[FN_REFLEN + 1]; DBUG_ENTER("dd_recreate_table"); - memset(&create_info, 0, sizeof(create_info)); + create_info.init(); if (path) create_info.options|= HA_LEX_CREATE_TMP_TABLE; |