mysql_com.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /*
  14. ** Common definition between mysql server & client
  15. */
  16. #ifndef _mysql_com_h
  17. #define _mysql_com_h
  18. #define NAME_LEN 64 /* Field/table name length */
  19. #define HOSTNAME_LENGTH 60
  20. #define USERNAME_LENGTH 16
  21. #define SERVER_VERSION_LENGTH 60
  22. #define SQLSTATE_LENGTH 5
  23. /*
  24. USER_HOST_BUFF_SIZE -- length of string buffer, that is enough to contain
  25. username and hostname parts of the user identifier with trailing zero in
  26. MySQL standard format:
  27. user_name_part@host_name_part\0
  28. */
  29. #define USER_HOST_BUFF_SIZE HOSTNAME_LENGTH + USERNAME_LENGTH + 2
  30. #define LOCAL_HOST "localhost"
  31. #define LOCAL_HOST_NAMEDPIPE "."
  32. #if defined(__WIN__) && !defined( _CUSTOMCONFIG_)
  33. #define MYSQL_NAMEDPIPE "MySQL"
  34. #define MYSQL_SERVICENAME "MySQL"
  35. #endif /* __WIN__ */
  36. /*
  37. You should add new commands to the end of this list, otherwise old
  38. servers won't be able to handle them as 'unsupported'.
  39. */
  40. enum enum_server_command
  41. {
  42. COM_SLEEP, COM_QUIT, COM_INIT_DB, COM_QUERY, COM_FIELD_LIST,
  43. COM_CREATE_DB, COM_DROP_DB, COM_REFRESH, COM_SHUTDOWN, COM_STATISTICS,
  44. COM_PROCESS_INFO, COM_CONNECT, COM_PROCESS_KILL, COM_DEBUG, COM_PING,
  45. COM_TIME, COM_DELAYED_INSERT, COM_CHANGE_USER, COM_BINLOG_DUMP,
  46. COM_TABLE_DUMP, COM_CONNECT_OUT, COM_REGISTER_SLAVE,
  47. COM_STMT_PREPARE, COM_STMT_EXECUTE, COM_STMT_SEND_LONG_DATA, COM_STMT_CLOSE,
  48. COM_STMT_RESET, COM_SET_OPTION, COM_STMT_FETCH,
  49. /* don't forget to update const char *command_name[] in sql_parse.cc */
  50. /* Must be last */
  51. COM_END
  52. };
  53. /*
  54. Length of random string sent by server on handshake; this is also length of
  55. obfuscated password, recieved from client
  56. */
  57. #define SCRAMBLE_LENGTH 20
  58. #define SCRAMBLE_LENGTH_323 8
  59. /* length of password stored in the db: new passwords are preceeded with '*' */
  60. #define SCRAMBLED_PASSWORD_CHAR_LENGTH (SCRAMBLE_LENGTH*2+1)
  61. #define SCRAMBLED_PASSWORD_CHAR_LENGTH_323 (SCRAMBLE_LENGTH_323*2)
  62. #define NOT_NULL_FLAG 1 /* Field can't be NULL */
  63. #define PRI_KEY_FLAG 2 /* Field is part of a primary key */
  64. #define UNIQUE_KEY_FLAG 4 /* Field is part of a unique key */
  65. #define MULTIPLE_KEY_FLAG 8 /* Field is part of a key */
  66. #define BLOB_FLAG 16 /* Field is a blob */
  67. #define UNSIGNED_FLAG 32 /* Field is unsigned */
  68. #define ZEROFILL_FLAG 64 /* Field is zerofill */
  69. #define BINARY_FLAG 128 /* Field is binary */
  70. /* The following are only sent to new clients */
  71. #define ENUM_FLAG 256 /* field is an enum */
  72. #define AUTO_INCREMENT_FLAG 512 /* field is a autoincrement field */
  73. #define TIMESTAMP_FLAG 1024 /* Field is a timestamp */
  74. #define SET_FLAG 2048 /* field is a set */
  75. #define NO_DEFAULT_VALUE_FLAG 4096 /* Field doesn't have default value */
  76. #define NUM_FLAG 32768 /* Field is num (for clients) */
  77. #define PART_KEY_FLAG 16384 /* Intern; Part of some key */
  78. #define GROUP_FLAG 32768 /* Intern: Group field */
  79. #define UNIQUE_FLAG 65536 /* Intern: Used by sql_yacc */
  80. #define BINCMP_FLAG 131072 /* Intern: Used by sql_yacc */
  81. #define REFRESH_GRANT 1 /* Refresh grant tables */
  82. #define REFRESH_LOG 2 /* Start on new log file */
  83. #define REFRESH_TABLES 4 /* close all tables */
  84. #define REFRESH_HOSTS 8 /* Flush host cache */
  85. #define REFRESH_STATUS 16 /* Flush status variables */
  86. #define REFRESH_THREADS 32 /* Flush thread cache */
  87. #define REFRESH_SLAVE 64 /* Reset master info and restart slave
  88. thread */
  89. #define REFRESH_MASTER 128 /* Remove all bin logs in the index
  90. and truncate the index */
  91. /* The following can't be set with mysql_refresh() */
  92. #define REFRESH_READ_LOCK 16384 /* Lock tables for read */
  93. #define REFRESH_FAST 32768 /* Intern flag */
  94. /* RESET (remove all queries) from query cache */
  95. #define REFRESH_QUERY_CACHE 65536
  96. #define REFRESH_QUERY_CACHE_FREE 0x20000L /* pack query cache */
  97. #define REFRESH_DES_KEY_FILE 0x40000L
  98. #define REFRESH_USER_RESOURCES 0x80000L
  99. #define CLIENT_LONG_PASSWORD 1 /* new more secure passwords */
  100. #define CLIENT_FOUND_ROWS 2 /* Found instead of affected rows */
  101. #define CLIENT_LONG_FLAG 4 /* Get all column flags */
  102. #define CLIENT_CONNECT_WITH_DB 8 /* One can specify db on connect */
  103. #define CLIENT_NO_SCHEMA 16 /* Don't allow database.table.column */
  104. #define CLIENT_COMPRESS 32 /* Can use compression protocol */
  105. #define CLIENT_ODBC 64 /* Odbc client */
  106. #define CLIENT_LOCAL_FILES 128 /* Can use LOAD DATA LOCAL */
  107. #define CLIENT_IGNORE_SPACE 256 /* Ignore spaces before '(' */
  108. #define CLIENT_PROTOCOL_41 512 /* New 4.1 protocol */
  109. #define CLIENT_INTERACTIVE 1024 /* This is an interactive client */
  110. #define CLIENT_SSL 2048 /* Switch to SSL after handshake */
  111. #define CLIENT_IGNORE_SIGPIPE 4096 /* IGNORE sigpipes */
  112. #define CLIENT_TRANSACTIONS 8192 /* Client knows about transactions */
  113. #define CLIENT_RESERVED 16384 /* Old flag for 4.1 protocol */
  114. #define CLIENT_SECURE_CONNECTION 32768 /* New 4.1 authentication */
  115. #define CLIENT_MULTI_STATEMENTS 65536 /* Enable/disable multi-stmt support */
  116. #define CLIENT_MULTI_RESULTS 131072 /* Enable/disable multi-results */
  117. #define CLIENT_REMEMBER_OPTIONS (((ulong) 1) << 31)
  118. #define SERVER_STATUS_IN_TRANS 1 /* Transaction has started */
  119. #define SERVER_STATUS_AUTOCOMMIT 2 /* Server in auto_commit mode */
  120. #define SERVER_STATUS_MORE_RESULTS 4 /* More results on server */
  121. #define SERVER_MORE_RESULTS_EXISTS 8 /* Multi query - next query exists */
  122. #define SERVER_QUERY_NO_GOOD_INDEX_USED 16
  123. #define SERVER_QUERY_NO_INDEX_USED 32
  124. /*
  125. The server was able to fulfill the clients request and opened a
  126. read-only non-scrollable cursor for a query. This flag comes
  127. in reply to COM_STMT_EXECUTE and COM_STMT_FETCH commands.
  128. */
  129. #define SERVER_STATUS_CURSOR_EXISTS 64
  130. /*
  131. This flag is sent when a read-only cursor is exhausted, in reply to
  132. COM_STMT_FETCH command.
  133. */
  134. #define SERVER_STATUS_LAST_ROW_SENT 128
  135. #define SERVER_STATUS_DB_DROPPED 256 /* A database was dropped */
  136. #define SERVER_STATUS_NO_BACKSLASH_ESCAPES 512
  137. #define MYSQL_ERRMSG_SIZE 512
  138. #define NET_READ_TIMEOUT 30 /* Timeout on read */
  139. #define NET_WRITE_TIMEOUT 60 /* Timeout on write */
  140. #define NET_WAIT_TIMEOUT 8*60*60 /* Wait for new query */
  141. #define ONLY_KILL_QUERY 1
  142. struct st_vio; /* Only C */
  143. typedef struct st_vio Vio;
  144. #define MAX_TINYINT_WIDTH 3 /* Max width for a TINY w.o. sign */
  145. #define MAX_SMALLINT_WIDTH 5 /* Max width for a SHORT w.o. sign */
  146. #define MAX_MEDIUMINT_WIDTH 8 /* Max width for a INT24 w.o. sign */
  147. #define MAX_INT_WIDTH 10 /* Max width for a LONG w.o. sign */
  148. #define MAX_BIGINT_WIDTH 20 /* Max width for a LONGLONG */
  149. #define MAX_CHAR_WIDTH 255 /* Max length for a CHAR colum */
  150. #define MAX_BLOB_WIDTH 8192 /* Default width for blob */
  151. typedef struct st_net {
  152. #if !defined(CHECK_EMBEDDED_DIFFERENCES) || !defined(EMBEDDED_LIBRARY)
  153. Vio* vio;
  154. unsigned char *buff,*buff_end,*write_pos,*read_pos;
  155. my_socket fd; /* For Perl DBI/dbd */
  156. unsigned long max_packet,max_packet_size;
  157. unsigned int pkt_nr,compress_pkt_nr;
  158. unsigned int write_timeout, read_timeout, retry_count;
  159. int fcntl;
  160. my_bool compress;
  161. /*
  162. The following variable is set if we are doing several queries in one
  163. command ( as in LOAD TABLE ... FROM MASTER ),
  164. and do not want to confuse the client with OK at the wrong time
  165. */
  166. unsigned long remain_in_buf,length, buf_length, where_b;
  167. unsigned int *return_status;
  168. unsigned char reading_or_writing;
  169. char save_char;
  170. my_bool no_send_ok; /* For SPs and other things that do multiple stmts */
  171. my_bool no_send_eof; /* For SPs' first version read-only cursors */
  172. /*
  173. Set if OK packet is already sent, and we do not need to send error
  174. messages
  175. */
  176. my_bool no_send_error;
  177. /*
  178. Pointer to query object in query cache, do not equal NULL (0) for
  179. queries in cache that have not stored its results yet
  180. */
  181. #endif
  182. char last_error[MYSQL_ERRMSG_SIZE], sqlstate[SQLSTATE_LENGTH+1];
  183. unsigned int last_errno;
  184. unsigned char error;
  185. gptr query_cache_query;
  186. my_bool report_error; /* We should report error (we have unreported error) */
  187. my_bool return_errno;
  188. } NET;
  189. #define packet_error (~(unsigned long) 0)
  190. enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
  191. MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
  192. MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
  193. MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
  194. MYSQL_TYPE_LONGLONG,MYSQL_TYPE_INT24,
  195. MYSQL_TYPE_DATE, MYSQL_TYPE_TIME,
  196. MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
  197. MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
  198. MYSQL_TYPE_BIT,
  199. MYSQL_TYPE_NEWDECIMAL=246,
  200. MYSQL_TYPE_ENUM=247,
  201. MYSQL_TYPE_SET=248,
  202. MYSQL_TYPE_TINY_BLOB=249,
  203. MYSQL_TYPE_MEDIUM_BLOB=250,
  204. MYSQL_TYPE_LONG_BLOB=251,
  205. MYSQL_TYPE_BLOB=252,
  206. MYSQL_TYPE_VAR_STRING=253,
  207. MYSQL_TYPE_STRING=254,
  208. MYSQL_TYPE_GEOMETRY=255
  209. };
  210. /* For backward compatibility */
  211. #define CLIENT_MULTI_QUERIES CLIENT_MULTI_STATEMENTS
  212. #define FIELD_TYPE_DECIMAL MYSQL_TYPE_DECIMAL
  213. #define FIELD_TYPE_NEWDECIMAL MYSQL_TYPE_NEWDECIMAL
  214. #define FIELD_TYPE_TINY MYSQL_TYPE_TINY
  215. #define FIELD_TYPE_SHORT MYSQL_TYPE_SHORT
  216. #define FIELD_TYPE_LONG MYSQL_TYPE_LONG
  217. #define FIELD_TYPE_FLOAT MYSQL_TYPE_FLOAT
  218. #define FIELD_TYPE_DOUBLE MYSQL_TYPE_DOUBLE
  219. #define FIELD_TYPE_NULL MYSQL_TYPE_NULL
  220. #define FIELD_TYPE_TIMESTAMP MYSQL_TYPE_TIMESTAMP
  221. #define FIELD_TYPE_LONGLONG MYSQL_TYPE_LONGLONG
  222. #define FIELD_TYPE_INT24 MYSQL_TYPE_INT24
  223. #define FIELD_TYPE_DATE MYSQL_TYPE_DATE
  224. #define FIELD_TYPE_TIME MYSQL_TYPE_TIME
  225. #define FIELD_TYPE_DATETIME MYSQL_TYPE_DATETIME
  226. #define FIELD_TYPE_YEAR MYSQL_TYPE_YEAR
  227. #define FIELD_TYPE_NEWDATE MYSQL_TYPE_NEWDATE
  228. #define FIELD_TYPE_ENUM MYSQL_TYPE_ENUM
  229. #define FIELD_TYPE_SET MYSQL_TYPE_SET
  230. #define FIELD_TYPE_TINY_BLOB MYSQL_TYPE_TINY_BLOB
  231. #define FIELD_TYPE_MEDIUM_BLOB MYSQL_TYPE_MEDIUM_BLOB
  232. #define FIELD_TYPE_LONG_BLOB MYSQL_TYPE_LONG_BLOB
  233. #define FIELD_TYPE_BLOB MYSQL_TYPE_BLOB
  234. #define FIELD_TYPE_VAR_STRING MYSQL_TYPE_VAR_STRING
  235. #define FIELD_TYPE_STRING MYSQL_TYPE_STRING
  236. #define FIELD_TYPE_CHAR MYSQL_TYPE_TINY
  237. #define FIELD_TYPE_INTERVAL MYSQL_TYPE_ENUM
  238. #define FIELD_TYPE_GEOMETRY MYSQL_TYPE_GEOMETRY
  239. #define FIELD_TYPE_BIT MYSQL_TYPE_BIT
  240. /* Shutdown/kill enums and constants */
  241. /* Bits for THD::killable. */
  242. #define MYSQL_SHUTDOWN_KILLABLE_CONNECT (unsigned char)(1 << 0)
  243. #define MYSQL_SHUTDOWN_KILLABLE_TRANS (unsigned char)(1 << 1)
  244. #define MYSQL_SHUTDOWN_KILLABLE_LOCK_TABLE (unsigned char)(1 << 2)
  245. #define MYSQL_SHUTDOWN_KILLABLE_UPDATE (unsigned char)(1 << 3)
  246. enum mysql_enum_shutdown_level {
  247. /*
  248. We want levels to be in growing order of hardness (because we use number
  249. comparisons). Note that DEFAULT does not respect the growing property, but
  250. it's ok.
  251. */
  252. SHUTDOWN_DEFAULT = 0,
  253. /* wait for existing connections to finish */
  254. SHUTDOWN_WAIT_CONNECTIONS= MYSQL_SHUTDOWN_KILLABLE_CONNECT,
  255. /* wait for existing trans to finish */
  256. SHUTDOWN_WAIT_TRANSACTIONS= MYSQL_SHUTDOWN_KILLABLE_TRANS,
  257. /* wait for existing updates to finish (=> no partial MyISAM update) */
  258. SHUTDOWN_WAIT_UPDATES= MYSQL_SHUTDOWN_KILLABLE_UPDATE,
  259. /* flush InnoDB buffers and other storage engines' buffers*/
  260. SHUTDOWN_WAIT_ALL_BUFFERS= (MYSQL_SHUTDOWN_KILLABLE_UPDATE << 1),
  261. /* don't flush InnoDB buffers, flush other storage engines' buffers*/
  262. SHUTDOWN_WAIT_CRITICAL_BUFFERS= (MYSQL_SHUTDOWN_KILLABLE_UPDATE << 1) + 1,
  263. /* Now the 2 levels of the KILL command */
  264. #if MYSQL_VERSION_ID >= 50000
  265. KILL_QUERY= 254,
  266. #endif
  267. KILL_CONNECTION= 255
  268. };
  269. enum enum_cursor_type
  270. {
  271. CURSOR_TYPE_NO_CURSOR= 0,
  272. CURSOR_TYPE_READ_ONLY= 1,
  273. CURSOR_TYPE_FOR_UPDATE= 2,
  274. CURSOR_TYPE_SCROLLABLE= 4
  275. };
  276. /* options for mysql_set_option */
  277. enum enum_mysql_set_option
  278. {
  279. MYSQL_OPTION_MULTI_STATEMENTS_ON,
  280. MYSQL_OPTION_MULTI_STATEMENTS_OFF
  281. };
  282. #define net_new_transaction(net) ((net)->pkt_nr=0)
  283. #ifdef __cplusplus
  284. extern "C" {
  285. #endif
  286. my_bool my_net_init(NET *net, Vio* vio);
  287. void my_net_local_init(NET *net);
  288. void net_end(NET *net);
  289. void net_clear(NET *net);
  290. my_bool net_realloc(NET *net, unsigned long length);
  291. my_bool net_flush(NET *net);
  292. my_bool my_net_write(NET *net,const char *packet,unsigned long len);
  293. my_bool net_write_command(NET *net,unsigned char command,
  294. const char *header, unsigned long head_len,
  295. const char *packet, unsigned long len);
  296. int net_real_write(NET *net,const char *packet,unsigned long len);
  297. unsigned long my_net_read(NET *net);
  298. /*
  299. The following function is not meant for normal usage
  300. Currently it's used internally by manager.c
  301. */
  302. struct sockaddr;
  303. int my_connect(my_socket s, const struct sockaddr *name, unsigned int namelen,
  304. unsigned int timeout);
  305. struct rand_struct {
  306. unsigned long seed1,seed2,max_value;
  307. double max_value_dbl;
  308. };
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. /* The following is for user defined functions */
  313. enum Item_result {STRING_RESULT=0, REAL_RESULT, INT_RESULT, ROW_RESULT,
  314. DECIMAL_RESULT};
  315. typedef struct st_udf_args
  316. {
  317. unsigned int arg_count; /* Number of arguments */
  318. enum Item_result *arg_type; /* Pointer to item_results */
  319. char **args; /* Pointer to argument */
  320. unsigned long *lengths; /* Length of string arguments */
  321. char *maybe_null; /* Set to 1 for all maybe_null args */
  322. char **attributes; /* Pointer to attribute name */
  323. unsigned long *attribute_lengths; /* Length of attribute arguments */
  324. } UDF_ARGS;
  325. /* This holds information about the result */
  326. typedef struct st_udf_init
  327. {
  328. my_bool maybe_null; /* 1 if function can return NULL */
  329. unsigned int decimals; /* for real functions */
  330. unsigned long max_length; /* For string functions */
  331. char *ptr; /* free pointer for function data */
  332. my_bool const_item; /* 0 if result is independent of arguments */
  333. } UDF_INIT;
  334. /* Constants when using compression */
  335. #define NET_HEADER_SIZE 4 /* standard header size */
  336. #define COMP_HEADER_SIZE 3 /* compression header extra size */
  337. /* Prototypes to password functions */
  338. #ifdef __cplusplus
  339. extern "C" {
  340. #endif
  341. /*
  342. These functions are used for authentication by client and server and
  343. implemented in sql/password.c
  344. */
  345. void randominit(struct rand_struct *, unsigned long seed1,
  346. unsigned long seed2);
  347. double my_rnd(struct rand_struct *);
  348. void create_random_string(char *to, unsigned int length, struct rand_struct *rand_st);
  349. void hash_password(unsigned long *to, const char *password, unsigned int password_len);
  350. void make_scrambled_password_323(char *to, const char *password);
  351. void scramble_323(char *to, const char *message, const char *password);
  352. my_bool check_scramble_323(const char *, const char *message,
  353. unsigned long *salt);
  354. void get_salt_from_password_323(unsigned long *res, const char *password);
  355. void make_password_from_salt_323(char *to, const unsigned long *salt);
  356. void make_scrambled_password(char *to, const char *password);
  357. void scramble(char *to, const char *message, const char *password);
  358. my_bool check_scramble(const char *reply, const char *message,
  359. const unsigned char *hash_stage2);
  360. void get_salt_from_password(unsigned char *res, const char *password);
  361. void make_password_from_salt(char *to, const unsigned char *hash_stage2);
  362. char *octet2hex(char *to, const char *str, unsigned int len);
  363. /* end of password.c */
  364. char *get_tty_password(char *opt_message);
  365. const char *mysql_errno_to_sqlstate(unsigned int mysql_errno);
  366. /* Some other useful functions */
  367. my_bool my_init(void);
  368. extern int modify_defaults_file(const char *file_location, const char *option,
  369. const char *option_value,
  370. const char *section_name, int remove_option);
  371. int load_defaults(const char *conf_file, const char **groups,
  372. int *argc, char ***argv);
  373. my_bool my_thread_init(void);
  374. void my_thread_end(void);
  375. #ifdef _global_h
  376. ulong STDCALL net_field_length(uchar **packet);
  377. my_ulonglong net_field_length_ll(uchar **packet);
  378. char *net_store_length(char *pkg, ulonglong length);
  379. #endif
  380. #ifdef __cplusplus
  381. }
  382. #endif
  383. #define NULL_LENGTH ((unsigned long) ~0) /* For net_store_length */
  384. #define MYSQL_STMT_HEADER 4
  385. #define MYSQL_LONG_DATA_HEADER 6
  386. #endif