1590
/* Find where a form starts */
1591
/* if formname is NULL then only formnames is read */
1593
ulong get_form_pos(File file, unsigned char *head, TYPELIB *save_names)
1595
uint32_t a_length,names,length;
1596
unsigned char *pos,*buf;
1599
names=uint2korr(head+8);
1600
a_length=(names+2)*sizeof(char *); /* Room for two extra */
1605
save_names->type_names=0; /* Clear if error */
1609
length=uint2korr(head+4);
1610
lseek(file,64,SEEK_SET);
1611
if (!(buf= (unsigned char*) malloc(length+a_length+names*4)) ||
1612
my_read(file, buf+a_length, (size_t) (length+names*4),
1614
{ /* purecov: inspected */
1617
return(0L); /* purecov: inspected */
1619
pos= buf+a_length+length;
1620
ret_value=uint4korr(pos);
1625
free((unsigned char*) buf);
1628
memset(save_names, 0, sizeof(save_names));
1632
str=(char *) (buf+a_length);
1633
fix_type_pointers((const char ***) &buf,save_names,1,&str);
1640
Read string from a file with malloc
1643
We add an \0 at end of the read string to make reading of C strings easier
1646
int read_string(File file, unsigned char**to, size_t length)
1651
if (!(*to= (unsigned char*) malloc(length+1)) ||
1652
my_read(file, *to, length,MYF(MY_NABP)))
1657
return(1); /* purecov: inspected */
1659
*((char*) *to+length)= '\0';
1664
/* Add a new form to a form file */
1666
off_t make_new_entry(File file, unsigned char *fileinfo, TYPELIB *formnames,
1667
const char *newname)
1669
uint32_t i,bufflength,maxlength,n_length,length,names;
1670
off_t endpos,newpos;
1671
unsigned char buff[IO_SIZE];
1674
length=(uint32_t) strlen(newname)+1;
1675
n_length=uint2korr(fileinfo+4);
1676
maxlength=uint2korr(fileinfo+6);
1677
names=uint2korr(fileinfo+8);
1678
newpos=uint4korr(fileinfo+10);
1680
if (64+length+n_length+(names+1)*4 > maxlength)
1683
int4store(fileinfo+10,newpos);
1684
endpos= lseek(file,0,SEEK_END);/* Copy from file-end */
1685
bufflength= (uint32_t) (endpos & (IO_SIZE-1)); /* IO_SIZE is a power of 2 */
1687
while (endpos > maxlength)
1689
lseek(file,(off_t) (endpos-bufflength),SEEK_SET);
1690
if (my_read(file, buff, bufflength, MYF(MY_NABP+MY_WME)))
1692
lseek(file,(off_t) (endpos-bufflength+IO_SIZE),SEEK_SET);
1693
if ((my_write(file, buff,bufflength,MYF(MY_NABP+MY_WME))))
1695
endpos-=bufflength; bufflength=IO_SIZE;
1697
memset(buff, 0, IO_SIZE); /* Null new block */
1698
lseek(file,(ulong) maxlength,SEEK_SET);
1699
if (my_write(file,buff,bufflength,MYF(MY_NABP+MY_WME)))
1701
maxlength+=IO_SIZE; /* Fix old ref */
1702
int2store(fileinfo+6,maxlength);
1703
for (i=names, pos= (unsigned char*) *formnames->type_names+n_length-1; i--;
1706
endpos=uint4korr(pos)+IO_SIZE;
1707
int4store(pos,endpos);
1714
sprintf((char*)buff,"/%s/",newname);
1717
sprintf((char*)buff,"%s/",newname); /* purecov: inspected */
1718
lseek(file, 63 + n_length,SEEK_SET);
1719
if (my_write(file, buff, (size_t) length+1,MYF(MY_NABP+MY_WME)) ||
1720
(names && my_write(file,(unsigned char*) (*formnames->type_names+n_length-1),
1721
names*4, MYF(MY_NABP+MY_WME))) ||
1722
my_write(file, fileinfo+10, 4,MYF(MY_NABP+MY_WME)))
1723
return(0L); /* purecov: inspected */
1725
int2store(fileinfo+8,names+1);
1726
int2store(fileinfo+4,n_length+length);
1727
assert(ftruncate(file, newpos)==0);/* Append file with '\0' */
1729
} /* make_new_entry */
1732
1580
/* error message when opening a form file */
1734
1582
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1803
1651
} /* open_table_error */
1807
** fix a str_type to a array type
1808
** typeparts separated with some char. differents types are separated
1813
fix_type_pointers(const char ***array, TYPELIB *point_to_type, uint32_t types,
1816
char *type_name, *ptr;
1822
point_to_type->name=0;
1823
point_to_type->type_names= *array;
1825
if ((chr= *ptr)) /* Test if empty type */
1827
while ((type_name=strchr(ptr+1,chr)) != NULL)
1829
*((*array)++) = ptr+1;
1830
*type_name= '\0'; /* End string */
1833
ptr+=2; /* Skip end mark and last 0 */
1837
point_to_type->count= (uint32_t) (*array - point_to_type->type_names);
1839
*((*array)++)= NULL; /* End of type */
1841
*names=ptr; /* Update end */
1843
} /* fix_type_pointers */
1846
1654
TYPELIB *typelib(MEM_ROOT *mem_root, List<String> &strings)
1848
1656
TYPELIB *result= (TYPELIB*) alloc_root(mem_root, sizeof(TYPELIB));
2002
1810
return (my_rename(from_s.c_str(),to_s.c_str(),MYF(MY_WME)));
2007
Allocate string field in MEM_ROOT and return it as String
2011
mem MEM_ROOT for allocating
2012
field Field for retrieving of string
2020
bool get_field(MEM_ROOT *mem, Field *field, String *res)
2022
char buff[MAX_FIELD_WIDTH], *to;
2023
String str(buff,sizeof(buff),&my_charset_bin);
2026
field->val_str(&str);
2027
if (!(length= str.length()))
2032
if (!(to= strmake_root(mem, str.ptr(), length)))
2033
length= 0; // Safety fix
2034
res->set(to, length, ((Field_str*)field)->charset());
2040
Allocate string field in MEM_ROOT and return it as NULL-terminated string
2044
mem MEM_ROOT for allocating
2045
field Field for retrieving of string
2048
NULL string is empty
2049
# pointer to NULL-terminated string value of field
2052
char *get_field(MEM_ROOT *mem, Field *field)
2054
char buff[MAX_FIELD_WIDTH], *to;
2055
String str(buff,sizeof(buff),&my_charset_bin);
2058
field->val_str(&str);
2059
length= str.length();
2060
if (!length || !(to= (char*) alloc_root(mem,length+1)))
2062
memcpy(to,str.ptr(),(uint32_t) length);
2069
1815
given a buffer with a key value, and a map of keyparts
2182
Checks whether a table is intact. Should be done *just* after the table has
2185
@param[in] table The table to check
2186
@param[in] table_f_count Expected number of columns in the table
2187
@param[in] table_def Expected structure of the table (column name
2191
@retval TRUE There was an error. An error message is output
2192
to the error log. We do not push an error
2193
message into the error stack because this
2194
function is currently only called at start up,
2195
and such errors never reach the user.
2199
Table::table_check_intact(const uint32_t table_f_count,
2200
const TABLE_FIELD_W_TYPE *table_def)
2204
bool fields_diff_count;
2206
fields_diff_count= (s->fields != table_f_count);
2207
if (fields_diff_count)
2210
/* previous MySQL version */
2211
if (DRIZZLE_VERSION_ID > s->mysql_version)
2213
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE),
2214
alias, table_f_count, s->fields,
2215
s->mysql_version, DRIZZLE_VERSION_ID);
2218
else if (DRIZZLE_VERSION_ID == s->mysql_version)
2220
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED), alias,
2221
table_f_count, s->fields);
2225
Something has definitely changed, but we're running an older
2226
version of MySQL with new system tables.
2227
Let's check column definitions. If a column was added at
2228
the end of the table, then we don't care much since such change
2229
is backward compatible.
2232
char buffer[STRING_BUFFER_USUAL_SIZE];
2233
for (i=0 ; i < table_f_count; i++, table_def++)
2235
String sql_type(buffer, sizeof(buffer), system_charset_info);
2239
Field *cur_field= this->field[i];
2241
if (strncmp(cur_field->field_name, table_def->name.str,
2242
table_def->name.length))
2245
Name changes are not fatal, we use ordinal numbers to access columns.
2246
Still this can be a sign of a tampered table, output an error
2249
errmsg_printf(ERRMSG_LVL_ERROR, _("Incorrect definition of table %s.%s: "
2250
"expected column '%s' at position %d, found '%s'."),
2251
s->db.str, alias, table_def->name.str, i,
2252
cur_field->field_name);
2254
cur_field->sql_type(sql_type);
2256
Generally, if column types don't match, then something is
2259
However, we only compare column definitions up to the
2260
length of the original definition, since we consider the
2261
following definitions compatible:
2263
1. DATETIME and DATETIM
2264
2. INT(11) and INT(11
2265
3. SET('one', 'two') and SET('one', 'two', 'more')
2267
For SETs or ENUMs, if the same prefix is there it's OK to
2268
add more elements - they will get higher ordinal numbers and
2269
the new table definition is backward compatible with the
2272
if (strncmp(sql_type.c_ptr_safe(), table_def->type.str,
2273
table_def->type.length - 1))
2275
errmsg_printf(ERRMSG_LVL_ERROR,
2276
_("Incorrect definition of table %s.%s: "
2277
"expected column '%s' at position %d to have type "
2278
"%s, found type %s."),
2280
table_def->name.str, i, table_def->type.str,
2281
sql_type.c_ptr_safe());
2284
else if (table_def->cset.str && !cur_field->has_charset())
2286
errmsg_printf(ERRMSG_LVL_ERROR,
2287
_("Incorrect definition of table %s.%s: "
2288
"expected the type of column '%s' at position %d "
2289
"to have character set '%s' but the type has no "
2292
table_def->name.str, i, table_def->cset.str);
2295
else if (table_def->cset.str &&
2296
strcmp(cur_field->charset()->csname, table_def->cset.str))
2298
errmsg_printf(ERRMSG_LVL_ERROR,
2299
_("Incorrect definition of table %s.%s: "
2300
"expected the type of column '%s' at position %d "
2301
"to have character set '%s' but found "
2302
"character set '%s'."),
2304
table_def->name.str, i, table_def->cset.str,
2305
cur_field->charset()->csname);
2311
errmsg_printf(ERRMSG_LVL_ERROR,
2312
_("Incorrect definition of table %s.%s: "
2313
"expected column '%s' at position %d to have type %s "
2314
" but the column is not found."),
2316
table_def->name.str, i, table_def->type.str);
2325
1928
Create Item_field for each column in the table.
2807
2384
mark_auto_increment_column();
2812
Cleanup this table for re-execution.
2815
TableList::reinit_before_use()
2818
void TableList::reinit_before_use(Session *session)
2821
Reset old pointers to TABLEs: they are not valid since the tables
2822
were closed in the end of previous prepare or execute call.
2825
/* Reset is_schema_table_processed value(needed for I_S tables */
2826
schema_table_state= NOT_PROCESSED;
2828
TableList *embedded; /* The table at the current level of nesting. */
2829
TableList *parent_embedding= this; /* The parent nested table reference. */
2832
embedded= parent_embedding;
2833
if (embedded->prep_on_expr)
2834
embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(session);
2835
parent_embedding= embedded->embedding;
2837
while (parent_embedding &&
2838
parent_embedding->nested_join->join_list.head() == embedded);
2842
2388
Return subselect that contains the FROM list this table is taken from