Discussion Writing Applications That Adapt to the MySQL Server Version

Ask t he server for it s version num ber. I f t he server is t oo old, m aybe you can fall back t o a workaround for t he m issing feat ure, if one exist s.

9.14.3 Discussion

I f youre writ ing an applicat ion t hat can perform cert ain funct ions only if t he MySQL server support s t he necessary underlying operat ions, t he server version num ber allows you t o det erm ine whet her t hose operat ions are available or whet her you need t o perform som e sort of workaround, assum ing t here is one . To get t he server version, issue a SELECT VERSION st at em ent . The result is a st ring t hat looks som et hing like 3.23.27-gamma . I n ot her words, it ret urns a st ring consist ing of m aj or, m inor, and t eeny version num bers, and possibly som e suffix. The version st ring can be used as is for present at ion purposes if you want t o produce a st at us display for t he user. However, for com parisons, it s sim pler t o work wit h a num ber—in part icular, a 5-digit num ber in Mmmtt form at , where M , mm , tt are t he m aj or, m inor, and t eeny version num bers. The conversion can be perform ed by split t ing t he st ring at t he periods, st ripping off from t he t hird piece t he suffix t hat begins w it h t he first non- num eric charact er, and t hen j oining t he pieces. [ 4] [ 4] My first at t em pt at t he conversion algorit hm was t o break t he version st ring at periods, t hen t o st rip t he suffix from t he t hird piece beginning wit h t he - charact er. That algorit hm failed wit h t he release of MySQL 3.23.29a- gam m a. St ripping -gamma from 29a-gamma leav es 29a , w hich is not a num ber. Heres a DBI funct ion t hat t akes a dat abase handle argum ent and ret urns a t wo-elem ent list cont aining bot h t he st ring and num eric form s of t he server version. The code assum es t hat t he m inor and t eeny version part s are less t han 100 and t hus no m ore t han t wo digit s each. That should be a valid assum pt ion, because t he source code for MySQL it self uses t he sam e form at . sub get_server_version { my dbh = shift; my ver_str, ver_num; my major, minor, teeny; fetch result into scalar string ver_str = dbh-selectrow_array SELECT VERSION ; return undef unless defined ver_str; major, minor, teeny = split \., ver_str; teeny =~ s\D; strip any non-numeric suffix if present ver_num = major10000 + minor100 + teeny; return ver_str, ver_num; } To get bot h form s of t he version inform at ion at once, call t he funct ion like t his: my ver_str, ver_num = get_server_version dbh; To get j ust one of t he values, call it as follows: my ver_str = get_server_version dbh[0]; string form my ver_num = get_server_version dbh[1]; numeric form The following exam ples dem onst rat e how t o use t he num eric version value t o check whet her t he server support s cert ain feat ures: my ver_num = get_server_version dbh[1]; printf GET_LOCK RELEASE_LOCK : s\n, ver_num = 32127 ? yes : no; printf Functional GRANT statement: s\n, ver_num = 32211 ? yes : no; printf Temporary tables: s\n, ver_num = 32302 ? yes : no; printf Quoted identifiers: s\n, ver_num = 32306 ? yes : no; printf UNION statement: s\n, ver_num = 40000 ? yes : no; printf Subselects: s\n, ver_num = 40100 ? yes : no;

9.15 Determining the Current Database