MySQL Proxy ATABLE A-5 continued COM_SET_OPTION string.bytepacket This packet contains a set command... Description BIT_ANDexpr No Bitwise AND; returns true if all rows are true and fal
Trang 1MySQL Proxy A
TABLE A-5 (continued )
[x].type
The backend server was definedwith proxy-backend-address
This packet was successful
MYSQLD_PACKET_RAW proxy.response.type This packet contains raw
hex 0x00700 corresponds toversion 0.7.0
COM_SLEEP string.byte(packet) This packet contains a sleep
Trang 2COM_STATISTICS string.byte(packet) This packet contains a
statistics command.COM_PROCESS_INFO string.byte(packet) This packet contains a
process info command
connect command.COM_PROCESS_KILL string.byte(packet) This packet contains a kill
COM_DELAYED_INSERT string.byte(packet) This packet contains a
delayed insert command.COM_CHANGE_USER string.byte(packet) This packet contains a
change user command.COM_BINLOG_DUMP string.byte(packet) This packet contains a
binlog dump command.COM_TABLE_DUMP string.byte(packet) This packet contains a table
dump command
COM_CONNECT_OUT string.byte(packet) This packet contains a
connect out command.COM_REGISTER_SLAVE string.byte(packet) This packet contains a
register slave command.COM_STMT_CLOSE string.byte(packet) This packet contains a close
string.byte(packet) This packet contains a send
long data command.COM_STMT_RESET string.byte(packet) This packet contains a reset
command
Trang 3MySQL Proxy A
TABLE A-5 (continued )
COM_SET_OPTION string.byte(packet) This packet contains a set
command
MYSQL_TYPE_NEWDECIMAL proxy.response
resultset { field{{type,name}} }
This field has a type ofDECIMAL
resultset { field{{type,name}} }
This field has a type ofTINY
resultset { field{{type,name}} }
This field has a type ofSHORT
resultset { field{{type,name}} }
This field has a type ofLONG
resultset { field{{type,name}} }
This field has a type ofFLOAT
resultset { field{{type,name}} }
This field has a type ofDOUBLE
resultset { field{{type,name}} }
This field has a type ofNULL
MYSQL_TYPE_TIMESTAMP proxy.response
resultset { field{{type,name}} }
This field has a type ofTIMESTAMP
MYSQL_TYPE_LONGLONG proxy.response
resultset { field{{type,name}} }
This field has a type ofLONGLONG
resultset { field{{type,name}} }
This field has a type of INT
resultset { field{{type,name}} }
This field has a type ofDATE
resultset { field{{type,name}} }
This field has a type ofTIME
continued
Trang 4MYSQL_TYPE_DATETIME proxy.response.
resultset { field{{type,name}} }
This field has a type ofDATETIME
resultset { field{{type,name}} }
This field has a type ofYEAR
resultset { field{{type,name}} }
This field has a type ofNEWDATE
resultset { field{{type,name}} }
This field has a type ofENUM
resultset { field{{type,name}} }
This field has a type of SET
MYSQL_TYPE_TINY_BLOB proxy.response
resultset { field{{type,name}} }
This field has a type ofTINY_BLOB
MYSQL_TYPE_
MEDIUM_BLOB
proxy.response
resultset { field{{type,name}} }
This field has a type ofMEDIUM_BLOB
MYSQL_TYPE_LONG_BLOB proxy.response
resultset { field{{type,name}} }
This field has a type ofLONG_BLOB
resultset { field{{type,name}} }
This field has a type ofBLOB
MYSQL_TYPE_VAR_STRING proxy.response
resultset { field{{type,name}} }
This field has a type ofVAR_STRING
resultset { field{{type,name}} }
This field has a type ofSTRING
MYSQL_TYPE_GEOMETRY proxy.response
resultset { field{{type,name}} }
This field has a type ofGEOMETRY
resultset { field{{type,name}} }
This field has a type of BIT
Trang 5MySQL Proxy Internal Structures
connection backend_ndx
send_queueusernameaddressscrambled_password
mysqld_versionscramble_buffer
addressstatetype
usersservers
queue
continued
Trang 6type
rowsrrrmsg
packet
texttoken_idtoken_name
Theproxy.global.configstructure can be extended to store user-defined variables For example, therw-splitting.luasample script extends theproxy.global.configstructure
to include:
■ proxy.global.config.rwsplit
■ proxy.global.config.rwsplit.min_idle_connections
■ proxy.global.config.rwsplit.max_idle_connections
MySQL proxy tokens
The proxy tokenizer has a set of tokens that it uses to define parts of a statement The
‘‘Proxy Tokenizer’’ section earlier showed a few such tokens, including ones for punctuation (TK_OBRACE,TK_CBRACE,TK_DOT,TK_STAR), type (TK_LITERAL,TK_STRING), and SQL keywords (TK_SQL_SELECT,TK_SQL_FROM,TK_SQL_WHERE,TK_SQL_LIKE).
The tokens inmysql-proxy0.6 0 that are not SQL statement keywords can be found in the source code filesql-tokenizer.cand are listed in Table A-7.
The SQL keyword tokens inmysql-proxy0.6.0 are also found in the source code filesql-tokenizer.cand are listed in Table A-8 There are no examples given, because each token name is the keyword withTK_SQL_appended For example, the tokenTK_SQL_ALTERcorresponds to the SQL keywordALTER.
Trang 7MySQL Proxy A
TABLE A-7
MySQL Proxy Tokens
VARIABLES, etc.)
@and @@ variables Database, table, and column names
continued
Trang 9MySQL Proxy A
TABLE A-8 (continued )
SQL Statement Keywords
TK_SQL_BINARYTK_SQL_BITTK_SQL_BLOBTK_SQL_BOTHTK_SQL_BYTK_SQL_CALLTK_SQL_CASCADETK_SQL_CASETK_SQL_CHANGETK_SQL_CHARTK_SQL_CHARACTERTK_SQL_CHECKTK_SQL_COLLATETK_SQL_COLUMNTK_SQL_CONDITIONTK_SQL_CONSTRAINTTK_SQL_CONTINUETK_SQL_CONVERTTK_SQL_CREATETK_SQL_CROSSTK_SQL_CURRENT_DATETK_SQL_CURRENT_TIMETK_SQL_CURRENT_TIMESTAMPTK_SQL_CURRENT_USERTK_SQL_CURSORTK_SQL_DATABASETK_SQL_DATABASESTK_SQL_DATE
continued
Trang 10TK_SQL_DECIMALTK_SQL_DECLARETK_SQL_DEFAULTTK_SQL_DELAYEDTK_SQL_DELETETK_SQL_DESCTK_SQL_DESCRIBETK_SQL_DETERMINISTICTK_SQL_DISTINCTTK_SQL_DISTINCTROWTK_SQL_DIV
TK_SQL_DOUBLETK_SQL_DROPTK_SQL_DUALTK_SQL_EACHTK_SQL_ELSETK_SQL_ELSEIFTK_SQL_ENCLOSEDTK_SQL_ENUMTK_SQL_ESCAPEDTK_SQL_EXISTSTK_SQL_EXITTK_SQL_EXPLAINTK_SQL_FALSE
Trang 11MySQL Proxy A
TABLE A-8 (continued )
SQL Statement Keywords
TK_SQL_FETCHTK_SQL_FLOATTK_SQL_FLOAT4TK_SQL_FLOAT8TK_SQL_FORTK_SQL_FORCETK_SQL_FOREIGNTK_SQL_FROMTK_SQL_FULLTEXTTK_SQL_GRANTTK_SQL_GROUPTK_SQL_HAVINGTK_SQL_HIGH_PRIORITYTK_SQL_HOUR_MICROSECONDTK_SQL_HOUR_MINUTETK_SQL_HOUR_SECONDTK_SQL_IF
TK_SQL_IGNORETK_SQL_INTK_SQL_INDEXTK_SQL_INFILETK_SQL_INNERTK_SQL_INOUTTK_SQL_INSENSITIVETK_SQL_INSERTTK_SQL_INTTK_SQL_INT1TK_SQL_INT2
continued
Trang 12TK_SQL_LONGTK_SQL_LONGBLOBTK_SQL_LONGTEXTTK_SQL_LOOPTK_SQL_LOW_PRIORITYTK_SQL_MASTER_SSL_VERIFY_SERVER_CERT
Trang 13MySQL Proxy A
TABLE A-8 (continued )
SQL Statement Keywords
TK_SQL_MATCHTK_SQL_MEDIUMBLOBTK_SQL_MEDIUMINTTK_SQL_MIDDLEINTTK_SQL_MINUTE_MICROSECONDTK_SQL_MINUTE_SECONDTK_SQL_MOD
TK_SQL_MODIFIESTK_SQL_NATURALTK_SQL_NOTK_SQL_NOTTK_SQL_NO_WRITE_TO_BINLOGTK_SQL_NULL
TK_SQL_NUMERICTK_SQL_ONTK_SQL_OPTIMIZETK_SQL_OPTIONTK_SQL_OPTIONALLYTK_SQL_OR
TK_SQL_ORDERTK_SQL_OUTTK_SQL_OUTERTK_SQL_OUTFILETK_SQL_PRECISIONTK_SQL_PRIMARYTK_SQL_PROCEDURETK_SQL_PURGETK_SQL_RANGE
continued
Trang 14TK_SQL_SENSITIVETK_SQL_SEPARATORTK_SQL_SETTK_SQL_SHOWTK_SQL_SMALLINTTK_SQL_SPATIALTK_SQL_SPECIFICTK_SQL_SQL
Trang 15MySQL Proxy A
TABLE A-8 (continued )
SQL Statement Keywords
TK_SQL_SQL_BIG_RESULTTK_SQL_SQL_CALC_FOUND_ROWSTK_SQL_SQLEXCEPTION
TK_SQL_SQL_SMALL_RESULTTK_SQL_SQLSTATE
TK_SQL_SQLWARNINGTK_SQL_SSL
TK_SQL_STARTINGTK_SQL_STRAIGHT_JOINTK_SQL_TABLE
TK_SQL_TERMINTEDTK_SQL_TEXTTK_SQL_THENTK_SQL_TIMETK_SQL_TIMESTAMPTK_SQL_TINYBLOBTK_SQL_TINYINTTK_SQL_TINYTEXTTK_SQL_TOTK_SQL_TRAILINGTK_SQL_TRIGGERTK_SQL_TRUETK_SQL_UNDOTK_SQL_UNIONTK_SQL_UNIQUETK_SQL_UNLOCKTK_SQL_UNSIGNEDTK_SQL_UPDATE
continued
Trang 16TK_SQL_VARBINARYTK_SQL_VARCHARTK_SQL_VARCHARACTERTK_SQL_VARYINGTK_SQL_WHENTK_SQL_WHERETK_SQL_WHILETK_SQL_WITHTK_SQL_WRITETK_SQL_X509TK_SQL_XORTK_SQL_YEAR_MONTHTK_SQL_ZEROFILL
Summary
In this appendix you have learned how to use the MySQL Proxy, including:
■ Using multiple backends with MySQL Proxy
■ How MySQL Proxy’s default round-robin connection logic works
■ A brief Lua tutorial
■ How MySQL Proxy tokenizes queries
■ Query injection with MySQL Proxy
■ Reference material for more advanced MySQL Proxy scripting
Trang 17Functions and Operators
IN THIS APPENDIX
Using aggregation functions Using bitwise operators Compressing and encrypting data
Testing, logic, and control flow Using server-level functions Working with data types
T his appendix contains the functions and operators available in
MySQL, including whether they are an SQL Standard or a MySQL Extension.
Using Aggregation Functions
Aggregation functions take a single expression as an argument
but may take input from many rows; they are row-wise functions.
For example, theAVG(expr)function takes a simple average (the sum divided by the count):
mysql> CREATE TABLE nums (num TINYINT);
Query OK, 0 rows affected (0.09 sec)mysql> INSERT INTO nums (num) VALUES (1),(2),(3);
Query OK, 3 rows affected (0.48 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> select AVG(num) FROM nums;
+ -+
| AVG(num) |+ -+
| 2.0000 |+ -+
1 row in set (0.03 sec)Although aggregate functions work on expressions that encompass one or more rows, they only take one expression as an
argument For example, the syntax of AVG(expr)does not allow for the following:
Trang 18right syntax to use near ’2,3)’ at line 1
One exception to this rule is that COUNT(DISTINCT expr) can take multiple arguments.
Whenexprevaluates asNULL, for example when there are no rows that match, most gation functions returnNULL Functions that return 0 whenexprevaluates asNULLareCOUNT(*),COUNT(expr),COUNT(DISTINCT expr),BIT_OR, andBIT_XOR.
aggre-Bug
There is one function that returns an unexpected result:BIT_AND(NULL):
mysql> SELECT BIT_AND(NULL);
+ -+
+ -+
| 18446744073709551615 |+ -+
1 row in set (0.00 sec)
For the curious,18446744073709551615is a 63-bit string where each bit is 1 This has beenreported as MySQL bug 37754 and can be seen at:http://bugs.mysql.com/bug.php?
id=37754
Table B-1 lists the aggregate functions and a brief description of each one.
Using Bitwise Operators
MySQL extends standard SQL with special functions for bits Bitwise operators implicitly cast arguments toUNSIGNED BIGINT This has the most noticeable effect on bit inversion (∼):mysql> SELECT 5,∼5,BIN(5),BIN(∼5)\G
*************************** 1 row ***************************5: 5
∼5: 18446744073709551610BIN(5): 101
BIN(∼5): 1111111111111111111111111111111111111111111111111111111111111010
1 row in set (0.00 sec)Aggregation functions for bits are listed in Table B-1.
Trang 19Functions and Operators B
TABLE B-1
Aggregation FunctionsFunction Syntax Standard
SQL?
Description
BIT_AND(expr) No Bitwise AND; returns true if all rows are true and false if
any rows are false
BIT_OR(expr) No Bitwise OR; returns true if any row is true; returns false if
all rows are false
BIT_XOR(expr) No Bitwise XOR; a pair where exactly one value is true
returns true; all other pairs will return false More than 2rows are done commutatively
COUNT(expr)COUNT(*)COUNT(DISTINCT expr)
YesYesYes
Returns a count of all non-NULL values of expr
COUNT(*)returns a count of all rows
COUNT(DISTINCT expr)returns a count of how manyrows contain different values for expr
GROUP_CONCAT(expr) No Concatenates non-NULL values, separated by a comma
by default
STD(expr) No Nonstandard SQL equivalent to STDDEV_POP(expr)
STDDEV_POP(expr) Yes Population standard deviation Same as
SQRT(VAR_POP(expr))
STDDEV_SAMP(expr) Yes Sample standard deviation Same as
SQRT(VAR_SAMP(expr))
STDDEV(expr) No Nonstandard SQL equivalent to STDDEV_POP(expr)
SUM(expr) Yes Returns the sum of all values of expr
VAR_POP(expr) Yes Population standard variance This is calculated by taking
the average of the square of the differences betweeneach value and the average value
VAR_SAMP(expr) Yes Population standard variance This is calculated by taking
the sum of the square of the differences between eachvalue and the average value, and dividing by the number
of values (rows) minus one
VARIANCE(expr) No Nonstandard SQL equivalent to VAR_POP(expr)
Trang 20TABLE B-2
Bit FunctionsFunction
Syntax
Standard SQL?
Description
& No Bitwise AND, returns true when both bit positions are true
| No Bitwise OR, returns true when either or both bit positions are true
ˆ No Bitwise XOR, returns true when exactly one bit position is true
<< No Shifts to the left
>> No Shifts to the rightBIT_COUNT No Returns the number of bits that are set
Combining multiple statements
Much like basic arithmetic operators (such as + - * / ), these bitwise operators can be combined into longer statements, which are then parsed using precedence rules.
Precedence from highest to least is shown in Table B-3.
TABLE B-3
Bit Operator Precedence
Let’s walk through an example:
mysql> SELECT 5 | 2 ˆ 7 << 3 | 4 & 6;
+ -+
Trang 21Functions and Operators B
| 5 | 2 ˆ 7 << 3 | 4 & 6 |+ -+
1 row in set (0.00 sec)The expression is now:
101 | ((10 ˆ 111) << 11) | (100 & 110)The first operation is10 ˆ 111, which evaluates to101, or5in base 10.
101 | (101 << 11) | (100 & 110)
101 << 11shifts the bits in 101 to the left by three positions to produce101000, or40in base 10.
101 | 101000 | (100 & 110)Evaluating the remaining expression in parenthesis,100 & 110equates to100, or4in base 10.
101 | 101000 | 100Just like arithmetic operators, it is standard to evaluate from left to right when all the operators have the same precedence.101 | 101000evaluates to101101, or45in base 10, which leaves the expression as:
101 | 101101The remaining expression101 | 101101evaluates to101101for a final result of 45.
Trang 22The binaries from MySQL are compiled with compression libraries If MySQL is not compiled with a compression library, compression functions will returnNULL Table B-4 lists the compres- sion functions and a brief description of each one.
TABLE B-4
Compression FunctionsFunction Syntax Standard SQL? Description
COMPRESS(expr) No Returns the compressed value of exprUNCOMPRESS(expr) No Returns the uncompressed value of expr Returns
NULLif expr is not a compressed value
UNCOMPRESSED_
LENGTH(expr)
No Returns the length of expr before compression
UNCOMPRESSED_LENGTH(COMPRESS(expr))isequivalent to LENGTH(expr)
SQL?
Description
AES_DECRYPT(str,key)
No Returns the result of AES decryption of str using key
AES_DECRYPT(AES_ENCRYPT(x,y),y)is equivalent to x.AES_ENCRYPT
(str,key)
No Returns the AES encryption of str using key Returns NULL if
either str or key is NULL Uses 128-bit keys by default; thiscan be increased to 256-bit keys by modifying the source code.DECODE
(str,pass)
No Returns the result of decoding str using a password of pass
DECODE(ENCODE(x,y),y)is equivalent to x
DES_DECRYPT(str[,key])
No Returns the result of decoding str using key See
DES_ENCRYPT()for a description of how key is used as thekey string or key number
Trang 23Functions and Operators B
No Returns the Triple-DES encryption of str If key is a string, key
is used as the key string If key is a number from 0–9, the keystring is the key number from the DES key file If key is notspecified, the first key number from the DES key file is used forencryption
ENCODE(str,pass)
No Returns the result of encoding str with a password of pass
ENCRYPT(str[,pass])
No Returns the encryption of str with a salt of pass, using the
UNIX function crypt() Returns NULL on systems that do nothave crypt(), including Windows
MD5(str) No Returns the 32-character hex string representing the 128-bit
MD5 checksum of str, for non-NULL values of str ReturnsNULLif str is equivalent to NULL
OLD_PASSWORD(str)
No Returns the old, pre-MySQL 4.1 of the PASSWORD function
applied to str Used to generate password hashes on newerdatabase servers that need to support older clients/clientlibraries See PASSWORD
PASSWORD(str) No Returns the one-way password hash of str Used in the
MySQL privilege system Pre-MySQL 4.1, the encryption isweaker (see the OLD_PASSWORD)
SHA1(str),SHA(str)
No Returns the 40-character hex string representing the 160-bit
SHA1 checksum of str, for non-NULL values of str ReturnsNULLif str is equivalent to NULL
SHA2(str,bit_length)
No Added in MySQL version 6.0.5 Acceptable values for the
required bit_length field are 224, 256, 384 and 512, whichwill return the 224-bit, 256-bit, 384-bit and 512-bit SHA2checksums for non-NULL values of str, respectively ReturnsNULLif str is equivalent to NULL
Testing, Logic, and Control Flow
SQL would be much less powerful without the filtering power of comparison operators and the ability to test and control procedural flow Table B-6 lists the comparison functions and a brief description of each one.
Trang 24Comparison FunctionsFunction Syntax Standard
SQL?
Description
expr1 BETWEENexpr2 AND expr3
No Equivalent to expr1>=expr2 AND expr1<=expr3
COALESCE(list) Yes Returns the first non-null value in a list containing one or
more expressionsexpr1 <=> expr2 No Returns true if expr1 equals expr2, including if they are
both NULL (NULL-safe)expr1 = expr2 Yes Returns true if expr1 equals expr2; not NULL-safeexpr1 >= expr2 Yes Returns true if expr1 is greater than or equal to expr2; not
NULL-safeexpr1 > expr2 Yes Returns true if expr1 is greater than expr2GREATEST(list) No Returns the greatest value of a list containing two or more
expressionsexpr1 IN(list) No Returns true if the value of expr1 is contained in a list of
one or more expressionsINTERVAL(list) No The list argument is two or more integer values separated
by commas When INTERVAL is called it returns a countthe number of integers in the list that are smaller in valuethan the initial integer If there are no smaller integers in thelist than the first value a 0 is returned
expr IS NOT NULL Yes Returns true if expr is not equivalent to NULLexpr IS NOT bool Yes Returns true if the Boolean value of expr is equivalent to
boolexpr IS NULL Yes Returns true if expr is equivalent to NULLexpr IS bool Yes Returns true if the Boolean value of expr is equivalent to
boolISNULL(expr) No Returns true if expr is equivalent to NULL Nonstandard
SQL equivalent to expr IS NULLLEAST(list) No Returns the least value of a list containing two or more
expressionsexpr1 <= expr2 Yes Returns true if expr1 is less than or equal to expr2; not
NULL-safeexpr1 < expr2 Yes Returns true if expr1 is less than expr2
Trang 25Functions and Operators B
Yes Returns true if expr matches pattern; pattern uses SQL
regular expressions The default escape character is ‘‘\’’,used to match the literal values of the wildcard characters
‘‘%’’ and ‘‘_’’ The optional ESCAPE parameter sets theescape character to char
expr1 != expr2expr1 <> expr2
NoYes
Returns true if expr1 is not equivalent to expr2
expr1 SOUNDS LIKEexpr2
No Equivalent to SOUNDEX(expr1)= SOUNDEX(expr2) (see
CASE WHEN test1 THENexpr1 [WHEN test2 THENexpr2 ] [ELSEexprN] END
No Returns the exprX of the first testX that matches val
If no testX matches and an optional ELSE statement isdefined, returns exprN If no testX matches and noELSEstatement is defined, returns NULL
Returns the exprX of the first testX that evaluates astrue If no testX evaluates to true and an optional ELSEstatement is defined, returns exprN If no testXevaluates to true and no ELSE statement is defined,returns NULL
IF(expr1,expr2,expr3)
No Returns expr2 when expr1 evaluates as true Returns
expr3when expr1 evaluates as false or NULL
IFNULL(expr1, expr2) No Returns expr2 if expr1 is equivalent to NULL Returns
expr1if expr1 is not equivalent to NULL Equivalent toIF(ISNULL(expr1),expr2,expr1)
NULLIF(expr1, expr2) Yes Returns expr1 if expr1 does not equal expr2 Returns
NULLif expr1 equals expr2