站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽27.聽Extending MySQL - MySQL 5.1参考手册中文版

绗27绔狅細鎵╁睍MySQL

27.1.聽MySQL鍐呴儴鎺т欢

    鏈珷鍖呭惈璁稿鍦ㄤ綘澶勭悊MySQL浠g爜鏃堕渶瑕佷簡瑙g殑浣犱簨鎯呫傚鏋滀綘鎯虫姇鍏ュ埌MySQL鐨勫紑鍙戜腑锛屾垨鎯宠鎺ヨЕ鍒版渶鏂扮殑涓棿鐗堟湰鐨勪唬鐮侊紝鎴栬呭氨鏄兂浜嗚В寮鍙戠殑杩涘害锛岃鍙傞槄2.8.3鑺傦紝鈥滀粠寮鍙戞簮浠g爜鏍戝畨瑁呪鐨勮鏄庛傚鏋滀綘瀵筂ySQL鐨勫唴閮ㄦ彃浠舵劅鍏磋叮锛屼綘涔熷彲浠ヨ闃呮垜浠殑鍐呴儴鎻掍欢閭欢鍒楄〃銆傝繖涓垪琛ㄧ殑娴侀噺鐩稿浣庝竴浜涖傛鐭ュ浣曡闃呯殑璇︽儏锛岃鍙傞槄1.7.1.1鑺傦紝鈥淢ySQL閭欢鍒楄〃鈥銆傚湪MySQL AB 鐨勬墍鏈夊紑鍙戜汉鍛橀兘鍦ㄥ唴閮ㄦ彃浠跺垪琛ㄩ噷锛 姝ゅ锛屾垜浠府鍔╅偅浜涙鍦ㄥ鐞哅ySQL浠g爜鐨勪汉銆傝闅忔剰浣跨敤杩欎釜閭欢鍒楄〃鏉ラ棶浠g爜鏈夊叧鐨勯棶棰橈紝涔熷彲鐢ㄥ畠鏉ュ彂閫佷綘鎯冲鐚粰MySQL椤圭洰鐨 琛ヤ竵锛

27.1.1.聽MySQL绾跨▼

    MySQL鏈嶅姟鍣ㄥ垱寤哄涓嬬嚎绋嬶細

  • TCP/IP 杩炴帴绾跨▼澶勭悊鎵鏈夎繛鎺ヨ姹傦紝骞朵负姣忎竴涓繛鎺ュ垱寤轰竴涓柊鐨勪笓鐢ㄧ嚎绋嬫潵澶勭悊璁よ瘉鍜孲QL鏌ヨ澶勭悊銆

  • Windows NT 骞冲彴涓婃湁涓涓悕涓虹閬撳鐞嗙▼搴忥紙pipe handler锛夌殑绾跨▼锛屽畠鍜屽悕涓虹閬撹繛鎺ヨ姹傦紙pipe connect requests锛夌殑TCP/IP杩炴帴绾跨▼鍋氬悓鏍风殑宸ヤ綔銆

  • 淇″彿绾跨▼澶勭悊鎵鏈夌殑淇″彿锛岃繖涓嚎绋嬮氬父涔熷鐞嗘姤璀﹀拰璋冪敤process_alarm() 鍑芥暟鏉ュ己鍒朵娇寰楃┖闂叉椂闂村お闀跨殑杩炴帴瓒呮椂銆

  • mysqld鏄笌DUSE_ALARM_THREAD绾跨▼涓璧风紪璇戠殑锛岃繖涓笓鐢ㄧ嚎绋嬫槸澶勭悊 鍒涘缓鐨勮鎶ョ殑銆傝繖涓嚎绋嬬敤鍦ㄤ竴浜泂igwait()鍑芥暟鏈夐棶棰樼殑绯荤粺涓婏紝鎴栬呯敤鍦ㄤ綘鎯冲湪搴旂敤绋嬪簭涓娇鐢╰hr_alarm()浠g爜鑰屼笉甯︿笓鐢ㄤ俊鍙峰鐞嗙嚎绋嬩箣鏃躲

  • 鑻ユ兂浣跨敤flush_time=val閫夐」锛屼細鍒涘缓涓涓笓鐢ㄧ嚎绋嬩互缁欏畾鐨勬椂闂撮棿闅斿埛鏂版墍鏈夎〃鏍笺

  • 姣忎釜杩炴帴閮芥湁瀹冭嚜宸辩殑绾跨▼銆

  • 姣忎釜琚娇鐢↖NSERT DELAYED 鐨勪笉鍚岃〃鏍奸兘浼氭湁鑷繁鐨勭嚎绋嬨

  • 鑻ヤ娇鐢ㄤ簡master-host, 鍒欎細鍒涘缓涓涓粠灞炵殑澶嶅埗绾跨▼浠庝富绾跨▼璇诲彇骞跺疄鏂芥洿鏂般

mysqladmin processlist 浠呮樉绀鸿繛鎺ワ紝INSERT DELAYED, 鍙婂鍒剁嚎绋

27.1.2.聽MySQL娴嬭瘯濂椾欢

     鍖呭惈鍦║nix婧愮爜鍜屼簩杩涘埗鍒嗗彂鐗堜腑鐨勬祴璇曠郴缁熷彲浠ヨ鐢ㄦ埛鍜屽紑鍙戜汉鍛樺MySQL浠g爜鏂借鍥炲綊娴嬭瘯銆傝繖浜涙祴璇曞彲浠ュ湪Unix涓婅繘琛岋紝鐩墠瀹冧滑杩樹笉鑳藉湪鍘熺敓鐨刉indows鐜涓嬭繘琛屻

      褰撳墠鐨勬祴璇曟渚嬪浠朵笉鑳藉湪MySQL涓祴璇曟墍鏈変笢瑗匡紝浣嗘槸瀹冭兘鍙戠幇SQL澶勭悊浠g爜锛孫S/library鏂囦欢涓ぇ澶氭暟鏄庢樉鐨勭己闄凤紝骞朵笖鍦ㄦ祴璇曞浠舵柟闈篃鏄潪甯稿交搴曠殑銆傛垜浠殑缁堟瀬鐩爣鏄100%鐨勪唬鐮佽繘琛屾祴璇曘傛垜浠杩庡ぇ瀹剁粰鎴戜滑鐨勬祴璇曞浠舵坊鍔犲唴瀹广備綘鍙兘浼氱壒鍒兂璐$尞鍑洪偅浜涙鏌ヤ綘绯荤粺閲屽姛鑳芥у嵄鏈虹殑娴嬭瘯锛屽洜涓鸿繖灏嗙‘淇濇湭鏉ユ墍鏈夊彂琛岀増鐨凪ySQL浼氫笌浣犵殑搴旂敤绋嬪簭涓璧锋洿濂藉湴杩愯銆

27.1.2.1.聽杩愯MySQL娴嬭瘯濂椾欢

    娴嬭瘯绯荤粺鍖呮嫭涓涓祴璇曡瑷瑙i噴鍣(mysqltest)锛屼竴涓繍琛屾墍鏈夋祴璇曠殑澶栧3鑴氭湰(mysql-test-run)锛岀敤涓撶敤璇█缂栧啓鐨勬祴璇曟渚嬶紝浠ュ強瀹冧滑鐨勯鏈熺粨鏋溿傚湪绯荤粺涓婄紪璇戝ソ涔嬪悗锛屽湪婧愪唬鐮佺殑root涓嬮敭鍏make test 鎴杕ysql-test/mysql-test-run銆傚鏋滃畨瑁呬簡涓涓簩杩涘埗鍒嗗彂鐗堬紝 cd 鍒板畨瑁卹oot (濡 /usr/local/mysql), 鐒跺悗閿叆 scripts/mysql-test-run銆傛墍鏈夋祴璇曞簲璇ラ兘閫氳繃锛屽亣浣挎湁娌¢氳繃鐨勶紝鑻ユ槸涓涓狹ySQL閲岀殑缂洪櫡锛屼綘鍙互璇曠潃鎵炬壘鏄洜涓轰粈涔堬紝骞朵笖鎶ュ憡杩欎釜闂銆傝鍙傞槄27.1.2.3鑺傦紝鈥滃湪MySQL娴嬭瘯濂椾欢閲屾姤鍛婄己闄封

濡傛灉浣犳兂瑕佽繍琛屾祴璇曞浠剁殑鏈哄櫒涓婂凡缁忚繍琛屼簡涓涓 mysqld 锛屽彧瑕佸畠涓嶅崰鐢9306 鍜 9307绔彛锛屽氨涓嶇敤鍋滄帀瀹冦傚鏋滃崰鐢ㄤ簡鍏朵腑鐨勪竴涓紝浠ュ彲浠ョ紪杈mysql-test-run鎶婁富绔彛鍜岋紙鎴栵級浠庣鍙e彿鏀逛负鍏跺畠鍙敤鐨勩.

鍙娇鐢ㄤ笅闈㈡寚浠よ繍琛屽崟涓祴璇曟渚 mysql-test/mysql-test-run test_name.

鑻ヤ竴涓祴璇曟湭閫氳繃锛屼綘鍙互鐢--force閫夐」鏉ユ鏌ヨ繍琛岀潃鐨mysql-test-run鐪嬫槸鍚︽槸鍒殑娴嬭瘯鏈氳繃

27.1.2.2.聽鎵╁睍MySQL娴嬭瘯濂椾欢

浣犲彲浠ョ敤mysqltest 璇█缂栧啓浣犺嚜宸辩殑娴嬭瘯妗堜緥銆備笉骞稿湴鏄紝鎴戜滑杩樻病鏈夊啓瀹岀浉鍏虫柟闈㈠畬鏁村湴鏂囨。銆備絾鏄紝浣犲彲浠ユ煡鐪嬫垜浠幇鏈夌殑娴嬭瘯妗堜緥锛屽苟灏嗗畠浠綔涓鸿寖渚嬨備笅闈㈠嚑鐐瑰皢鏈夊姪浜庝綘鍏ユ墜锛

  • 娴嬭瘯浣嶄簬 mysql-test/t/*.test

  • 娴嬭瘯妗堜緥鍖呮嫭缁堟澹版槑锛屾祴璇曟渚嬬被浼间簬mysql鍛戒护琛屽鎴风鐨勮緭鍏ャ 榛樿鐨勫0鏄庢槸涓涓鍙戦佸埌MySQL鏈嶅姟鍣ㄧ殑鏌ヨ锛岄櫎闈炶繖涓0鏄庤璇嗗埆涓哄唴閮ㄥ懡浠わ紙濡 sleep锛夈

  • 鎵鏈変骇鐢熺粨鏋滅殑鏌ヨ锛屼緥濡係ELECT, SHOW, EXPLAIN绛夛紝蹇呴』鍦 @/path/to/result/file涔嬪墠銆傞偅涓枃浠跺繀椤诲寘鍚湡鏈涚殑缁撴灉銆傜敓鎴愮粨鏋滄枃浠剁殑涓涓畝鍗曞姙娉曟槸鍦╩ysql-test鐩綍杩愯mysqltest -r < t/test-case-name.test 锛岀劧鍚庣紪杈戠敓鎴愮殑缁撴灉鏂囦欢锛屽鏋滈渶瑕侊紝鍙皢瀹冧滑璋冩暣鍒版兂瑕佺殑杈撳嚭绔傚湪閭g鎯呭喌涓嬶紝瑕佸皬蹇冮伩鍏嶆坊鍔犳垨鍒犻櫎浠讳綍涓嶅彲瑙佺殑瀛楃锛岀‘淇濆彧鏀瑰彉鏂囨湰鍜岋紙鎴栵級鍒犻櫎琛屻傚鏋滄彃鍏ヤ竴琛岋紝瑕佺‘淇濇彃鍏ョ殑鍖哄煙琚竴涓‖鏍囪瘑闅斿紑锛屼笖鍦ㄨ灏炬湁涓涓‖鏍囪瘑銆備綘鍙兘浼氭兂瑕佷娇鐢od -c鏉ョ‘淇濅綘鐨勬枃鏈紪杈戝櫒鍦ㄧ紪杈 姝ラ涓病鏈夋悶涔变换浣曚笢瑗裤傚綋浣犲彂鐜颁竴涓己闄疯屼笉寰椾笉缂栬緫mysqltest -r鐨勮緭鍑烘椂锛屾垜浠湡甯屾湜浣犱笉瑕佺紪杈戝畠銆

  • 涓哄拰鎴戜滑鐨勮缃竴鑷达紝浣犲簲璇ユ妸浣犵殑缁撴灉鏂囦欢鏀惧湪mysql-test/r 鐩綍锛屽苟鍙栧悕涓簍est_name.result銆傚鏋滄祴璇曚骇鐢熶笉姝竴涓粨鏋滐紝浣犲簲璇ヤ娇鐢ㄨ濡 test_name.a.result锛宼est_name.b.result绛夎繖鏍风殑鍚嶅瓧銆

  • 濡傛灉澹版槑杩斿洖涓涓敊璇紝浣犲彲浠ュ湪澹版槑鐨勫墠涓琛屼娇鐢--error error-number鏉ヨ缁嗚鏄庡畠銆傞敊璇彿鍙兘鏄敱鈥,鈥濆垎寮鐨勫彲鑳介敊璇彿鐨勫垪琛ㄣ

  • 濡傛灉浣犳缂栧啓涓涓噸澶嶇殑娴嬭瘯妗堜緥锛屼綘搴旇鍦ㄦ祴璇曟枃浠剁殑绗竴琛屽啓锛歴ource include/master-slave.inc;銆傜敤connection master; 鍜 connection slave;鏉ュ垏鎹富妗堜緥鍜屼粠妗堜緥銆傚鏋滀綘闇瑕佸涓涓浛鎹㈢殑杩炴帴鍋氱偣浠涔堬紝瀵逛簬涓昏繛鎺ワ紝鐢╟onnection master1;锛屽浜庝粠杩炴帴锛岀敤connection slave1;銆

  • 濡傛灉闇瑕佸湪涓涓惊鐜噷鍋氱偣浠涔堬紝鍙互鐢ㄤ簺杩欐牱鐨勫唴瀹癸細

    let $1=1000;
    while ($1)
    {
     # do your queries here
     dec $1;
    }
    
  • 鍦ㄦ煡璇箣闂翠紤鐪狅紝浣跨敤sleep鍛戒护銆傛鍛戒护鏀寔鍑犲垎涔嬪嚑绉掞紝鎵浠ワ紝渚嬪浣犳兂瑕佷紤鐪1.3绉掞紝浣犲彲浠ヤ娇鐢sleep 1.3; 鍛戒护

  • 瀵逛綘鐨勬祴璇曟渚嬭杩愯甯﹂檮鍔犻夐」鐨勪粠妗堜緥锛屼互鍛戒护琛屾柟寮忔妸瀹冧滑鏀惧湪mysql-test/t/test_name-slave.opt銆傚浜庝富妗堜緥锛屾妸瀹冧滑鏀惧湪mysql-test/t/test_name-master.opt銆

  • 濡傛灉瀵规祴璇曞浠舵湁闂锛屽拰鎯宠鐚嚭涓涓祴璇曟渚嬶紝鍙戦侀偖浠朵俊鎭埌MySQL 鍐呴儴鎻掍欢 閭欢鍒楄〃銆傝鍙傞槄1.7.1.1鑺傦紝鈥淢ySQL閭欢鍒楄〃鈥銆 铏界劧杩欎釜鍒楄〃涓嶆帴鍙楅檮浠讹紝浣犲彲浠ユ妸鐩稿叧鏂囦欢閫氳繃ftp涓婁紶鍒帮細ftp://ftp.mysql.com/pub/mysql/upload/

27.1.2.3. 鍦∕ySQL娴嬭瘯濂椾欢涓姤鍛婄己闄

濡傛灉浣犵殑MySQL鐨勭増鏈病鏈夐氳繃娴嬭瘯濂椾欢锛屼綘鍙互閲囧彇濡備笅鎺柦锛

  • 鍦ㄥ敖鍙兘澶氬湴鎵惧埌鍑洪敊涔嬫椂鐨勯敊璇箣鍓嶏紝涓嶈鍙戦佺己闄锋姤鍛娿傛煡鎵句箣鏃讹紝璇蜂娇鐢mysqlbug鑴氭湰姣斾究鎴戜滑鑳借幏鍙栦綘鐨勭郴缁熷拰MySQL鐗堟湰淇℃伅锛屽弬闃1.7.1.3鑺 锛屸滃浣曟姤鍛婄己闄锋垨闂

  • 纭繚鍖呭惈浜mysql-test-run鐨勮緭鍑猴紝浠ュ強  mysql-test/r鐩綍涓嬫墍鏈.reject鏂囦欢鐨勫唴瀹广

  • 濡傛灉娴嬭瘯濂椾欢閲岀殑娴嬭瘯鏈氳繃锛岀敤濡備笅鍛戒护妫鏌ヤ竴涓嬬湅瀹冭嚜宸辫繍琛屾椂鏄惁閫氳繃娴嬭瘯锛

    cd mysql-test
    mysql-test-run --local test-name
    

    濡傛灉鏈兘閫氳繃锛屼綘搴旇鐢 --with-debug 閰嶇疆MySQL骞朵娇鐢--debug閫夐」鏉ヨ繍琛mysql-test-run銆傚鏋滆繖鏍蜂篃鏈兘閫氳繃锛岃鎶婅拷韪枃浠var/tmp/master.trace 涓婁紶鍒 ftp://ftp.mysql.com/pub/mysql/upload/ 浠ヤ究鎴戜滑鑳芥鏌ュ畠銆傝璁板緱涔熻鍖呭惈浣犵郴缁熺殑瀹屾暣鎻忚堪锛mysqld 浜岃繘鍒舵枃浠剁殑鐗堟湰锛屼互鍙婁綘鏄浣曠紪璇戝畠鐨勩

  • 涔熻瘯鐫甯--force閫夐」杩愯涓涓mysql-test-run 锛岀湅鏄惁杩樻湁鍒殑娴嬭瘯鏈氳繃銆

  • 濡傛灉浣犳槸鑷繁缂栬瘧鐨凪ySQL锛屾煡鐪嬫垜浠殑鎵嬪唽鐪嬬湅濡備綍鍦ㄤ綘鐨勫钩鍙颁笂缂栬瘧MySQL锛屾渶濂界敤涓涓湪http://dev.mysql.com/downloads/涓婃垜浠凡缁忎负浣犵紪璇戝ソ鐨勪簩杩涘埗鐗堟湰銆傛垜浠墍鏈夋爣鍑嗙殑浜岃繘鍒剁増鏈兘鑳介氳繃娴嬭瘯濂椾欢鐨勬祴璇曪紒

  • 濡傛灉閿欒鏄疪esult length mismatch 鎴 Result content mismatch 锛岃繖鎰忓懗娴嬭瘯鐨勮緭鍑轰簬鏈熸湜鐨勮緭鍑轰笉鍖归厤锛岃繖鍙兘鏄湪MySQL鎴栦綘鐨mysqld 鐗堟湰閲岀殑缂洪櫡鍦ㄦ煇浜涚幆澧冧笅浜х敓绋嶆湁涓嶅悓鐨勭粨鏋溿

    鏈氳繃鐨勬祴璇曠粨鏋滄斁鍦ㄥ拰缁撴灉鏂囦欢鍚屼富鍚嶄絾鎵╁睍鍚嶄负.reject鐨勬枃浠堕噷銆傚鏋滄祴璇曟渚嬫湭閫氳繃锛屼綘搴旇瀵逛袱涓枃浠跺仛diff鎿嶄綔銆傚鏋滀綘涓嶈兘鍙戠幇瀹冧滑鏄浣曚笉鍚岋紝鐢╫d -c 鍛戒护妫鏌ュ畠浠紝涔熸鏌ヤ竴涓嬫枃浠堕暱搴︺

  • 濡傛灉娴嬭瘯瀹屽叏鏈氳繃锛屼綘搴旇妫鏌ysql-test/var/log鐩綍涓嬬殑鏃ュ織鏂囦欢浠ヨ幏寰楁湁鍏抽敊璇殑涓浜涙彁绀恒

  • 濡傛灉浣犳槸涓鸿皟璇曡岀紪璇慚ySQL锛岃瘯涓涓嬪甫--gdb鍜岋紙鎴栵級--debug鍙傛暟杩愯mysql-test-run 銆傝鍙傞槄E.1.2鑺傦紝鈥滃垱寤鸿窡韪枃浠垛

    濡傛灉浣犳病鏈変负璋冭瘯鑰岀紪璇慚ySQL锛岃繖搴旇鏄綘鍙兘鍘诲仛鐨勩傚彧瑕佸甫锛-with-debug鍙傛暟杩愯configure銆 璇峰弬闃2.8鑺傦紝鈥滀娇鐢ㄦ簮鐮佸垎鍙戠増瀹夎MySQL 鈥

27.2.聽涓篗ySQL娣诲姞鏂板嚱鏁

鏈変袱涓斿緞鏉ヤ负MySQL娣诲姞鏂板嚱鏁帮細

  • 浣犲彲浠ラ氳繃鑷鍖诲嚱鏁版帴鍙 (UDF)鏉ユ坊鍔犲嚱鏁般傝嚜瀹氫箟鍑芥暟琚紪璇戜负鐩爣鏂囦欢锛岀劧鍚庣敤CREATE FUNCTION 鍜孌ROP FUNCTION 澹版槑鍔ㄦ佸湴娣诲叆鍒版湇鍔″櫒涓強浠庢湇鍔″櫒涓Щ鍑恒傚弬闃27.2.2鑺傦紝鈥淐REATE FUNCTION/DROP FUNCTION 璇硶鈥

  • 浣犲彲浠ュ皢鍑芥暟娣诲姞涓篗ySQL鍥烘湁锛堝唴寤猴級鍑芥暟銆傚浐鏈夊嚱鏁拌缂栬瘧杩mysqld鏈嶅姟鍣ㄤ腑锛屾垚涓烘案涔呭彲鐢ㄧ殑銆

姣忕閫斿緞閮芥湁鍏朵紭鐐瑰拰缂虹偣锛

  • 濡傛灉浣犵紪鍐欒嚜瀹氫箟鍑芥暟锛屼綘闄や簡瀹夎鏈嶅姟鍣ㄦ湰韬箣澶栬繕瑕佸畨瑁呯洰鏍囨枃浠躲傚鏋滃皢浣犵殑鍑芥暟缂栬瘧杩涙湇鍔″櫒涓紝浣犲氨涓嶉渶瑕佽繖涔堝仛浜嗐

  • 浣犲彲浠ョ粰浜岃繘鍒剁増鏈殑MySQL鍒嗗彂鐗堟坊鍔燯DF銆傚浐鏈夊嚱鏁伴渶瑕佷綘鍘讳慨姝f簮鐮佸垎鍙戠増銆.

  • 濡傛灉浣犲崌绾т綘鐨凪ySQL鍒嗗彂鐗堬紝浣犲彲浠ョ户缁娇鐢ㄥ厛鍓嶅畨瑁呬簡鐨刄DF锛 闄ら潪浣犲崌绾у埌涓涓猆DF鎺ュ彛鏀瑰彉浜嗙殑鏂扮増鏈傚鍥烘湁鍑芥暟鑰岃█锛屾瘡娆″崌绾т綘閮藉繀椤婚噸澶嶄竴娆′慨姝c

鏃犺浣犱娇鐢ㄥ摢绉嶆柟娉曞幓娣诲姞鏂板嚱鏁帮紝瀹冧滑閮藉彲浠ヨSQL澹版槑璋冪敤锛屽氨鍍 ABS() 鎴 SOUNDEX()杩欐牱鐨勫浐鏈夊嚱鏁颁竴鏍枫

鍙︿竴涓坊鍔犲嚱鏁扮殑鏂规硶鏃跺垱寤哄瓨鍌ㄥ嚱鏁般傝繖浜涘嚱鏁版椂鐢⊿QL澹版槑缂栧啓鐨勶紝鑰屼笉鏄紪璇戠洰鏍囦唬鐮併傜紪鍐欏瓨鍌ㄥ嚱鏁扮殑璇硶鍦绗20绔狅細瀛樺偍绋嬪簭鍜屽嚱鏁 涓弿杩般

涓嬮潰鐨勫皬鑺傛弿杩癠DF鎺ュ彛鐨勭壒鎬э紝缁欏嚭缂栧啓UDF鐨勬寚浠わ紝骞惰璁篗ySQL涓洪槻姝DF琚鐢ㄨ岄噰鍙栫殑瀹夊叏棰勯槻鎺柦銆

缁欏嚭婧愪唬鐮佺殑渚嬪瓙鏉ヨ鏄庡浣曠紪鍐橴DF锛岀湅涓鐪婱ySQL婧愮爜鍒嗗彂鐗堜腑鎻愪緵鐨剆ql/udf_example.cc 鏂囦欢銆

27.2.1.聽鑷畾涔夊嚱鏁版帴鍙g殑鐗规

MySQL鑷畾涔夊嚱鏁版帴鍙f湁濡備笅鐗规у拰鍔熻兘锛

  • 鍑芥暟鑳藉垎梅杩斿洖瀛楃涓诧紝鏁存暟鎴栧疄鏁般

  • 浣犲彲浠ュ畾涔変竴娆′綔鐢ㄤ簬涓琛岀殑绠鍗曞嚱鏁帮紝鎴栦綔鐢ㄤ簬澶氳鐨勭粍鐨勯泦鍚堝嚱鏁般

  • 鎻愪緵缁欏嚱鏁扮殑淇℃伅浣垮緱鍑芥暟鍙互妫鏌ヤ紶閫掔粰瀹冧滑鐨勫弬閲忕殑鏁扮洰鍜岀被鍨嬨

  • 浣犲彲浠ヨMySQL鍦ㄥ皢鏌愬弬閲忎紶閫掔粰鍑芥暟涔嬪墠寮哄埗鍏朵负鏌愪竴绫诲瀷銆

  • 浣犲彲浠ヨ〃绀哄嚱鏁拌繑鍥濶ULL 鎴栧彂鐢熼敊璇

27.2.2.聽CREATE FUNCTION/DROP FUNCTION 璇硶

CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL}
       SONAME shared_library_name

DROP FUNCTION function_name

涓涓嚜瀹氫箟鍑芥暟 (UDF)灏辨槸鐢ㄤ竴涓薄ABS() 鎴 CONCAT()杩欐牱鐨勫浐鏈夛紙鍐呭缓锛夊嚱鏁颁竴鏍蜂綔鐢ㄧ殑鏂板嚱鏁板幓鎵╁睍MySQL銆

function_name 鏄 鐢ㄥ湪SQL澹版槑涓互澶囪皟鐢ㄧ殑鍑芥暟鍚嶅瓧銆俁ETURNS 瀛愬彞璇存槑鍑芥暟杩斿洖鍊肩殑绫诲瀷銆 shared_library_name 鏄叡浜洰鏍囨枃浠剁殑鍩烘湰鍚嶏紝鍏变韩鐩爣鏂囦欢鍚湁瀹炵幇鍑芥暟鐨勪唬鐮併傝鏂囦欢蹇呴』浣嶄簬涓涓兘琚綘绯荤粺鐨勫姩鎬佽繛鎺ヨ呮悳绱㈢殑鐩綍閲屻

浣犲繀椤绘湁mysql 鏁版嵁搴撶殑INSERT 鏉冮檺鎵嶈兘鍒涘缓涓涓嚱鏁帮紝浣犲繀椤绘湁mysql 鏁版嵁搴撶殑DELETE鏉冮檺鎵嶈兘鎾ら攢涓涓嚱鏁般傝繖鏄洜涓篊REATE FUNCTION 寰璁板綍鍑芥暟鍚嶅瓧锛岀被鍨嬪拰鍏变韩鍚嶇殑mysql.func绯荤粺琛ㄩ噷娣诲姞浜嗕竴琛岋紝鑰孌ROP FUNCTION鍒欐槸浠庤〃涓垹鎺夎繖涓琛屻傚鏋滀綘娌℃湁杩欎釜绯荤粺琛紝浣犲簲璇ヨ繍琛mysql_fix_privilege_tables鑴氭湰鏉ュ垱寤轰竴涓傝鍙傞槄2.10.2鑺傦紝鈥滃崌绾ф巿鏉冭〃鈥

涓涓湁鏁堢殑鍑芥暟鏄竴涓敤CREATE FUNCTION鍔犺浇涓旀病鏈夌敤DROP FUNCTION绉婚櫎鐨勫嚱鏁般傛瘡娆℃湇鍔″櫒鍚姩鐨勬椂鍊欎細閲嶆柊鍔犺浇鎵鏈夋湁鏁堝嚱鏁帮紝闄ら潪浣犱娇鐢--skip-grant-tables鍙傛暟鍚姩mysqld銆傚湪杩欑鎯呭喌涓嬶紝 灏嗚烦杩嘦DF鐨勫垵濮嬪寲锛孶DF涓嶅彲鐢ㄣ

瑕佷簡瑙g紪鍐欒嚜瀹氫箟鍑芥暟鐨勮鏄庯紝璇峰弬闃27.2.3鑺傦紝鈥滄坊鍔犳柊鐨勮嚜瀹氫箟鍑芥暟鈥銆傝浣垮緱UDF鏈哄埗鑳藉璧蜂綔鐢紝蹇呴』浣跨敤C鎴栬匔++缂栧啓鍑芥暟锛屼綘鐨勭郴缁熷繀椤绘敮鎸佸姩鎬佸姞杞斤紝鑰屼笖浣犲繀椤绘槸鍔ㄦ佺紪璇戠殑mysqld锛堥潪闈欐侊級銆

涓涓狝GGREGATE鍑芥暟灏卞儚涓涓狹ySQL鍥烘湁鐨勯泦鍚堬紙鎬诲拰锛夊嚱鏁颁竴鏍疯捣浣滅敤锛屾瘮濡傦紝SUM鎴朇OUNT()鍑芥暟銆傝浣垮緱AGGREGATE 璧蜂綔鐢紝浣犵殑mysql.func琛ㄥ繀椤诲寘鎷竴涓猼ype鍒椼傚鏋滀綘鐨刴ysql.func琛ㄦ病鏈夎繖涓 鍒楋紝浣犲簲璇ヨ繍琛mysql_fix_privilege_tables鑴氭湰鏉ュ垱寤烘 鍒椼

27.2.3. 娣诲姞鏂扮殑鑷畾涔夊嚱鏁

瑕佷娇寰桿DF鏈哄埗鑳藉璧蜂綔鐢紝蹇呴』浣跨敤C鎴栬匔++缂栧啓鍑芥暟锛屼綘鐨勭郴缁熷繀椤绘敮鎸佸姩鎬佸姞杞姐侻ySQL 婧愮爜鍒嗗彂鐗堝寘鎷竴涓猻ql/udf_example.cc 鏂囦欢锛屾鏂囦欢瀹氫箟浜5涓柊鍑芥暟銆傚彲浠ュ弬鑰冭繖涓枃浠讹紝鐪婾DF鏄浣曡皟鐢ㄥ父瑙勫伐浣溿

涓轰簡鑳戒娇鐢║DF锛屼綘闇瑕佸姩鎬侀摼鎺mysqld銆備笉瑕侀厤缃甅ySQL浣跨敤--with-mysqld-ldflags=-all-static鍙傛暟銆傚鏋滀綘鎯充娇鐢ㄤ竴涓渶瑕佷粠mysqld 璁块棶绗﹀彿鐨刄DF锛堜緥濡傚湪浣跨敤default_charset_info鐨剆ql/udf_example.cc鏂囦欢涓殑metaphone鍑芥暟锛夛紝浣犲繀椤讳娇鐢-rdynamic鍙傛暟鏉ラ摼鎺ョ▼搴忥紙鍙傞槄man dlopen锛夈傚鏋滀綘璁″垝浣跨敤UDF, 涓涓粡楠屾硶鍒欏氨鏄紝鐢╳ith-mysqld-ldflags=-rdynamic璁惧畾MySQL锛岄櫎闈炰綘鏈夊緢濂界殑鐞嗙敱涓嶅幓杩欎箞鍋氥

濡傛灉浣犱娇鐢ㄧ殑鏄缂栬瘧鍒嗗彂鐗堢殑MySQL锛 璇蜂娇鐢∕ySQL-Max锛屽叾涓惈鏈変竴涓姩鎬侀摼鎺ヤ簡鐨勬湇鍔″櫒锛屽畠鍙互鏀寔鍔ㄦ佸姞杞姐

瀵逛簬姣忎釜浣犳兂瑕佷娇鐢ㄥ湪SQL澹版槑涓殑鍑芥暟锛屼綘搴旇瀹氫箟鐩稿簲鐨凜 锛堟垨 C++锛夊嚱鏁般傚湪涓嬮潰鐨勮璁轰腑锛寈xx鐢ㄦ潵琛ㄧず鑼冧緥鍑芥暟鐨勫悕瀛楋紝涓轰簡鍖哄垎浣跨敤SQL杩樻槸C/C++锛寈xx()(涓婃爣锛夎〃绀篠QL鍑芥暟璋冪敤锛寈xx()锛堜笅鏍囷級琛ㄧずC/C++鍑芥暟璋冪敤銆

浣犱负xxx()缂栧啓鏉ュ疄鐜版帴鍙g殑C/C++ 鍑芥暟濡備笅锛

  • xxx() (蹇呮湁)

    涓诲嚱鏁般 杩欐槸鍑芥暟缁撴灉琚绠楃殑鍦版柟銆係QL鍑芥暟鏁版嵁绫诲瀷涓嶤/C++鍑芥暟杩斿洖绫诲瀷鐨勫搴斿叧绯诲涓嬶細

    SQL 绫诲瀷C/C++ 绫诲瀷
    STRINGchar *
    INTEGERlong long
    REALdouble
  • xxx_init() (鍙)

    瀵箈xx()鐨勫垵濮嬪寲鍑芥暟銆傚畠鍙互琚敤鏉ワ細

    • 妫鏌ヤ紶閫掔粰xxx()鐨勫弬閲忔暟鐩

    • 妫鏌ュ弬閲忔槸鍚︿负蹇呴渶鐨勭被鍨嬶紝鎴栬咃紝闄ゆ涔嬪锛屽湪涓诲嚱鏁拌璋冪敤鐨勬椂鍊欏憡璇塎ySQL灏嗗弬閲忓己鍒朵负鎯宠鐨勭被鍨嬨

    • 鍒嗛厤涓诲嚱鏁伴渶瑕佺殑鍐呭瓨銆

    • 鎸囧畾缁撴灉鐨勬渶澶ч暱搴︺

    • 鎸囧畾锛堝浜嶳EAL 鍑芥暟锛夊皬鏁扮殑鏈澶氫綅鏁般

    • 鎸囧畾缁撴灉鏄惁鍙互涓 NULL銆

  • xxx_deinit() 锛堝彲閫夛級

    瀵箈xx()鐨勫幓鍒濆鍖栧嚱鏁般傚畠閲婃斁鍒濆鍖栧嚱鏁板垎閰嶇殑鍐呭瓨銆

褰揝QL澹版槑璋冪敤XXX()鏃讹紝MySQL璋冪敤鍒濆鍖栧嚱鏁皒xx_init()锛岃瀹冩墽琛屽繀瑕佺殑璁剧疆锛屾瘮濡傦紝妫鏌 鍙傞噺鎴栧垎閰嶅唴瀛樸傚鏋渪xx_init() 杩斿洖涓涓敊璇紝SQL澹版槑浼氶鍑哄苟缁欏嚭閿欒淇℃伅锛岃屼富鍑芥暟鍜屽幓鍒濆鍖栧嚱鏁板苟娌℃湁琚皟鐢ㄣ 鍚﹀垯锛屼富鍑芥暟xxx() 瀵规瘡涓琛岄兘琚皟鐢ㄤ竴娆°傛墍鏈夎閮藉鐞嗗畬涔嬪悗锛岃皟鐢ㄥ幓鍒濆鍖栧嚱鏁皒xx_deinit() 鎵ц蹇呰鐨勬竻闄ゃ

瀵逛簬璞UM()涓鏍峰伐浣滅殑闆嗗悎鍑芥暟锛屼綘涔熷繀椤绘彁渚涘涓嬬殑鍑芥暟锛

  • xxx_clear() 锛堝湪5.1鑺備腑蹇呴』锛

    瀵逛竴涓柊缁勯噸缃綋鍓嶉泦鍚堝间负鍒濊瘯闆嗗悎鍊硷紝浣嗕笉鎻掑叆浠讳綍鍙傞噺銆

  • xxx_add() 锛堝繀椤伙級

    娣诲姞鍙傞噺鍒板綋鍓嶉泦鍚堝笺

MySQL 鎸変笅鍒楁搷浣滄潵澶勭悊闆嗗悎UDF锛

  1. 璋冪敤 xxx_init() 璁╅泦鍚堝嚱鏁板垎閰嶅畠闇瑕佺敤鏉ュ瓨鍌ㄧ粨鏋滅殑鍐呭瓨銆

  2. 鎸夌収GROUP BY琛ㄨ揪寮忔潵鎺掑簭琛ㄣ

  3. 涓烘瘡涓柊缁勪腑鐨勭涓琛岃皟鐢▁xx_clear()鍑芥暟銆

  4. 涓哄睘浜庡悓缁勭殑姣忎竴涓柊琛岃皟鐢▁xx_add()鍑芥暟銆

  5. 褰撶粍鏀瑰彉鏃舵垨姣忕粍鐨勬渶鍚庝竴琛岃澶勭悊瀹屼箣鍚庯紝璋冪敤xxx()鏉ヨ幏鍙栭泦鍚堢粨鏋溿

  6. 閲嶅锛屼互涓3-姝ョ洿鍒版墍鏈夎琚鐞嗗畬銆

  7. 璋冪敤xxx_deinit() 鍑芥暟鍘婚噴鏀綰DF鍒嗛厤鐨勫唴瀛樸.

鎵鏈夊嚱鏁板繀椤绘椂绾跨▼瀹夊叏鐨勶紝杩欎笉浠呭涓诲嚱鏁帮紝瀵瑰垵濮嬪寲鍜屽幓鍒濆鍖栧嚱鏁颁篃涓鏍凤紝涔熷寘鎷泦鍚堝嚱鏁拌姹傜殑闄勫姞鍑芥暟銆傝繖涓姹傜殑涓涓粨鏋滃氨鏄紝浣犱笉鑳藉垎閰嶄换浣曞彉鍖栫殑鍏ㄥ眬鎴栭潤鎬佸彉閲忋傚鏋滀綘闇瑕佸唴瀛橈紝浣犲彲浠ュ湪xxx_init()鍑芥暟鍒嗛厤鍐呭瓨锛岀劧鍚庡湪xxx_deinit()鍑芥暟閲婃斁鎺夈

27.2.3.1.聽UDF 瀵圭畝鍗曞嚱鏁扮殑璋冪敤椤哄簭

涓嬮潰浠嬬粛鍒涘缓绠鍗昒DF鏃堕渶瑕佸畾涔夌殑涓嶅悓鍑芥暟銆27.2.3鑺傦紝鈥滄坊鍔犳柊鐨勮嚜瀹氫箟鍑芥暟鈥涓粙缁嶄簡MySQL璋冪敤杩欎簺鍑芥暟鐨勯『搴忋

濡傛湰鑺傛墍绀猴紝搴旇璇存槑涓诲嚱鏁皒xx()銆傛敞鎰忚繑鍥炲煎拰鍙傛暟浼氭湁鎵涓嶅悓锛岃繖鍙栧喅浜庝綘璇存槑鐨凷QL鍑芥暟xxx()鍦–REATE FUNCTION澹版槑涓繑鍥炵殑鏄疭TRING锛孖NTEGER绫诲瀷杩樻槸REAL绫诲瀷绀猴細

瀵逛簬STRING 鍨嬪嚱鏁帮細

char *xxx(UDF_INIT *initid, UDF_ARGS *args,
          char *result, unsigned long *length,
          char *is_null, char *error);

瀵逛簬INTEGER鍨嬪嚱鏁帮細

long long xxx(UDF_INIT *initid, UDF_ARGS *args,
              char *is_null, char *error);

瀵逛簬REAL鍨嬪嚱鏁帮細

double xxx(UDF_INIT *initid, UDF_ARGS *args,
              char *is_null, char *error);

鍒濆鍖栧拰鍘诲垵濮嬪寲鍑芥暟濡備笅璇存槑锛

my_bool xxx_init(UDF_INIT *initid, UDF_ARGS *args, char *message);

void xxx_deinit(UDF_INIT *initid);

initid 鍙傛暟琚紶閫掔粰鎵鏈夌殑涓変釜鍑芥暟銆傚畠鎸囧悜涓涓猆DF_INIT 缁撴瀯锛岃繖涓粨鏋勮鐢ㄦ潵鍦ㄥ嚱鏁颁箣闂翠氦鎹俊鎭俇DF_INIT 缁撴瀯椤硅窡闅忕潃銆傚垵濮嬪寲鍑芥暟搴旇缁欎换浣曞畠鎯宠鏀瑰彉鐨勯」璧嬪笺傦紙瑕佷娇鐢ㄩ」鐨勯粯璁ゅ硷紝灏辫瀹冧笉琚敼鍙橈級

  • my_bool maybe_null

    濡傛灉xxx() 鑳借繑鍥濶ULL锛寈xx_init()搴攎aybe_null 涓 1 銆傚鏋滀换涓鍙傞噺琚鏄庝簡 maybe_null鍊硷紝鍏 榛樿鍊兼槸1 銆

  • unsigned int decimals

    灏忔暟浣嶆暟銆傞粯璁ゅ兼槸浼犲埌涓诲嚱鏁扮殑鍙傞噺閲屽皬鏁扮殑鏈澶т綅鏁般傦紙渚嬪锛屽鏋滃嚱鏁颁紶閫 1.34, 1.345, 鍜1.3, 閭d箞榛樿鍊间负锛屽洜涓1.345 鏈3浣嶅皬鏁般

  • unsigned int max_length

    缁撴灉鐨勬渶澶ч暱搴︺俶ax_length 鐨勯粯璁ゅ煎洜鍑芥暟鐨勭粨鏋滅被鍨嬭屽紓銆傚瀛楃涓插嚱鏁帮紝榛樿鍊兼槸鏈闀垮弬閲忕殑闀垮害銆傚鏁村瀷鍑芥暟锛岄粯璁ゆ槸21浣嶃傚瀹炲瀷鍑芥暟锛岄粯璁ゆ槸13鍐嶅姞涓奿nitid->decimals鎸囩ず鐨勫皬鏁颁綅鏁般傦紙瀵规暟瀛楀嚱鏁帮紝闀垮害鍖呭惈姝h礋鍙锋垨鑰呭皬鏁扮偣绗︼級銆

    濡傛灉鎯宠繑鍥炲洟鍊硷紝浣犲彲浠ユ妸max_length 璁句负浠65KB鍒16MB銆傝繖涓唴瀛樹笉浼氳鍒嗛厤锛屼絾鏄鏋滄湁涓存椂鏁版嵁闇瑕佸瓨鍌紝杩欎釜璁剧疆浜嗙殑鍊艰鐢ㄦ潵鍐冲畾浣跨敤鍝 鍒楃殑绫诲瀷銆

  • char *ptr

    鍑芥暟鍙互鐢ㄤ綔鏈韩鐩殑鐨勬寚閽堛傛瘮濡傦紝鍑芥暟鍙互鐢╥nitid->ptr 鏉ュ湪鍒嗛厤浜嗙殑鍐呭瓨鍐呴儴閫氳銆 xxx_init() 搴旇鍒嗛厤鍐呭瓨锛屽苟鎸囨淳缁欒繖涓寚閽堬細

    initid->ptr = allocated_memory;
    

    鍦 xxx() 鍜 xxx_deinit()涓紝鍊熺敤 initid->ptr 鏉ヤ娇鐢ㄦ垨鍒嗛厤鍐呭瓨銆

27.2.3.2.聽UDF瀵归泦鍚堝嚱鏁扮殑璋冪敤椤哄簭

鏈妭浠嬬粛鍒涘缓闆嗗悎UDF涔嬫椂闇瑕佸畾涔夌殑涓嶅悓鍑芥暟銆27.2.3鑺傦紝鈥滄坊鍔犳柊鐨勮嚜瀹氫箟鍑芥暟鈥 浠嬬粛浜哅ySQL璋冪敤杩欎簺鍑芥暟鐨勯『搴忋

  • xxx_reset()

    褰揗ySQL鍦ㄤ竴涓柊缁勪腑鍙戠幇绗竴琛屾椂璋冪敤杩欎釜鍑芥暟銆傚畠瀵硅繖涓粍閲嶇疆浠讳綍鍐呴儴鎬诲拰鍙橀噺锛岀劧鍚庝娇鐢ㄧ粰瀹氱殑UDF_ARGS鍙傞噺浣滀负鍐呴儴鎬诲拰鍊肩殑绗竴涓笺傚涓嬭鏄 xxx_reset() 鍑芥暟锛

    char *xxx_reset(UDF_INIT *initid, UDF_ARGS *args,
                    char *is_null, char *error);
    

     鍦∕ySQL5.1鐗堜腑UDF鎺ュ彛涓嶉渶瑕佹垨涓嶄娇鐢▁xx_reset()鍑芥暟锛岃屾槸浣跨敤xxx_clear()鍑芥暟浣滀负鏇夸唬銆備絾鏄鏋滀綘鎯宠UDF涔熻兘鍦ㄨ佺増鏈殑鏈嶅姟鍣ㄤ笂杩愯锛屼綘涔熷彲浠ュ畾涔 xxx_reset() 鍜 xxx_clear() 鍑芥暟銆傦紙濡傛灉浣犱娇鐢ㄤ簡杩欎袱涓嚱鏁帮紝xxx_reset()鍑芥暟鍦ㄥ緢澶氭儏鍐典笅鍙互閫氳繃璋冪敤鍑芥暟鏉ュ唴閮ㄥ疄鐜帮紝鍗宠皟鐢▁xx_clear()鍑芥暟閲嶇疆鎵鏈夊彉閲忥紝鐒跺悗娣诲姞UDF_ARGS鍙傞噺浣滀负缁勭殑绗竴涓笺傦級

  • xxx_clear()

    褰揗ySQL闇瑕侀噸缃诲拰缁撴灉鏃惰皟鐢ㄦ鍑芥暟銆傚姣忎竴涓柊缁勶紝鍦ㄥ紑濮嬩箣鏃惰皟鐢ㄥ畠锛屼絾鏄畠涔熷彲浠ヨ璋冪敤鏉ヤ负涓涓病鏈夊尮閰嶈鍦ㄥ叾涓殑鏌ヨ閲嶇疆鍊笺傚涓嬭鏄巟xx_clear()锛

    char *xxx_clear(UDF_INIT *initid, char *is_null, char *error);
    

    鍦ㄨ皟鐢▁xx_clear()涔嬪墠is_null 琚缃寚鍚 CHAR(0) 銆

    濡傛灉鍙戠敓閿欒锛屼綘鍙互瀛樺偍涓涓煎湪 error鍙傞噺鎸囧悜鐨勫彉閲忎腑銆俥rror鎸囧悜涓鍗曞瓧鑺傚彉閲忥紝鑰屼笉鏄竴涓瓧绗︿覆缂撳啿鍖恒

    xxx_clear() 鏄疢ySQL 5.1蹇呴』鐨勩

  • xxx_add()

    涓哄悓缁勯櫎浜嗙涓琛屼箣澶栵紝鎵鏈夌殑琛岃皟鐢ㄨ繖涓嚱鏁般備綘搴旇鐢ㄥ畠鍦║DF_ARGS鍙傞噺涓悜鍐呴儴鎬诲拰鍙橀噺鍔犲笺.

    char *xxx_add(UDF_INIT *initid, UDF_ARGS *args,
                  char *is_null, char *error);
    

瀵归泦鍚圲DF鑰岃█xxx() 鍑芥暟搴旇鐢ㄤ笌闈為泦鍚圲DF涓鏍风殑鏂规硶鏉ヨ鏄庛傝鍙傞槄27.2.3.1鑺傦紝鈥淯DF璋冪敤绠鍗曞嚱鏁扮殑椤哄簭鈥

瀵逛竴涓泦鍚圲DF锛孧ySQL 鍦ㄧ粍鍐呮墍鏈夎琚鐞嗕箣鍚庤皟鐢▁xx()鍑芥暟銆傝繖閲屼綘搴旇涓鑸笉浼氭帴瑙﹀埌瀹冪殑UDF_ARGS鍙傞噺锛屼絾鏄彇鑰屼唬涔嬪湴鏍规嵁鍐呴儴鎬诲拰鍙橀噺杩斿洖缁欎綘鍊笺

鍦▁xx()涓鐞嗙殑杩斿洖鍊煎簲璇ョ敤涓庡闈為泦鍚圲DF涓鏍风殑鏂规硶鏉ユ搷浣溿傝鍙傞槄27.2.3.4鑺傦紝鈥淯DF杩斿洖鍊煎拰閿欒澶勭悊鈥

xxx_reset() 鍜 xxx_add() 鍑芥暟鐢ㄤ笌闈為泦鍚圲DF涓鏍风殑鏂规硶鏉ュ鐞嗗畠浠殑UDF_ARGS 鍙傞噺銆傝鍙傞槄27.2.3.3鑺傦紝鈥淯DF鍙傞噺澶勭悊鈥

鍒癷s_null鍜宔rror鐨勬寚閽 鍙傞噺鍜屾墍鏈夊埌xxx_reset(), xxx_clear(), xxx_add() 鍜 xxx()璋冪敤涓鏍枫備綘鍙互鐢ㄨ繖涓潵鎻愰啋浣犺幏鍙栦竴涓敊璇垨鏃犺xxx()鏄惁杩斿洖NULL鐨勪竴涓粨鏋溿備綘涓嶈兘鎶婁竴涓瓧绗︿覆瀛樺埌*error锛乪rror鎸囧悜鍗曞瓧鑺傚彉閲忚屼笉鏄瓧绗︿覆缂撳啿鍖恒

*is_null 瀵规瘡涓涓粍閮介噸缃紙璋冪敤xxx_clear()鍓嶏級锛 *error 浠庝笉閲嶇疆銆

濡傛灉 xxx()杩斿洖鏃讹紝*is_null 鎴 *error 琚缃紝MySQL杩斿洖 NULL浣滀负缁勫嚱鏁扮殑缁撴灉銆

27.2.3.3.聽UDF鍙傞噺澶勭悊

args 鍙傛暟鎸囧悜鍒楃潃缁撴瀯鍏冪殑 UDF_ARGS 缁撴瀯锛

  • unsigned int arg_count

    鍙傞噺涓暟銆傚鏋滀綘闇瑕佷綘鐨勫嚱鏁板甫鐫鏌愪釜鏁扮洰鐨勫弬閲忚璋冪敤锛屽湪鍒濆鍖栧嚱鏁版鏌ヨ繖涓硷紝渚嬪锛

    if (args->arg_count != 2)
    {
        strcpy(message,"XXX() requires two arguments");
        return 1;
    }
    
  • enum Item_result *arg_type

    涓涓寚閽堬紝瀵规瘡涓弬閲忔寚鍚戝寘鍚被鍨嬬殑涓涓暟鍒椼傚彲鑳界殑绫诲瀷鍊兼槸STRING_RESULT, INT_RESULT 鍜 REAL_RESULT銆

    瑕佺‘淇′竴涓弬閲忔槸缁欏畾绫诲瀷鐨勶紝骞朵笖濡傛灉涓嶆槸鐨勮瘽灏辫繑鍥炰竴涓敊璇紝璇锋鏌ュ垵濮嬪寲鍑芥暟涓殑arg_type鏁板垪銆傛瘮濡傦細

    if (args->arg_type[0] != STRING_RESULT ||
        args->arg_type[1] != INT_RESULT)
    {
        strcpy(message,"XXX() requires a string and an integer");
        return 1;
    }
    

    瑕佹眰浣犲嚱鏁扮殑鍙傞噺鏄煇涓绫诲瀷鐨勫彟涓鏂规硶鏄紝浣跨敤鍒濆鍖栧嚱鏁拌缃產rg_type鍏冪礌涓轰綘鎯宠鐨勭被鍨嬨傚鎵鏈夊xxx()鐨勮皟鐢ㄨ岃█锛岃繖浼氬鑷碝ySQL寮哄埗鍙傞噺涓鸿繖浜涚被鍨嬨傛瘮濡傦紝瑕佹寚瀹氭姇涓や釜鍙傞噺寮哄埗鎴愬瓧绗︿覆鍜屾暣鏁帮紝鍦▁xx_init()涓垎鍒細

    args->arg_type[0] = STRING_RESULT;
    args->arg_type[1] = INT_RESULT;
    
  • char **args

    args->args 涓庡垵濮嬪寲鍑芥暟鍋氭湁鍏充紶鍒颁綘鍑芥暟鐨勫弬閲忕殑涓鑸儏鍐靛仛閫氳銆傚浜庡父鍙傞噺i锛宎rgs->args[i] 鎸囧悜鍙傞噺鍊笺傦紙鐪嬩笅闈㈢殑璇存槑浜嗚В濡備綍濡ュ杽鍦拌闂繖涓硷級銆傚闈烇紞甯稿弬閲忥紝args->args[i] 涓 0銆備竴涓父鍙傞噺涓轰粎浣跨敤鍙傞噺鐨勮〃杈惧紡锛屽 3 鎴 4*7-2 鎴 SIN(3.14)銆備竴涓潪甯 鍙傞噺鏄竴涓涓庤涓嶅悓鐨勮〃杈惧紡锛屽锛屽垪鍚嶆垨甯﹂潪锛嶅父鍙傞噺璋冪敤鐨勫嚱鏁般

    瀵逛富鍑芥暟鐨勬瘡娆¤皟鐢紝args->args 鍖呭惈涓烘瘡涓綋鍓嶅鐞嗙殑琛屼紶閫掔殑瀹為檯鍙傞噺銆

    濡備笅浣跨敤鍙傞噺i鐨勫嚱鏁帮細

    • 缁欎竴涓猄TRING_RESULT 鍨嬬殑鍙傞噺浣滀负涓涓瓧绗︿覆鍔犱竴涓暱搴︼紝鍙互鍏佽鎵鏈変簩杩涘埗鏁版垨浠绘剰闀垮害鐨勬暟澶勭悊銆傚瓧绗︿覆鍐呭浣滀负args->args[i] 鑰屽瓧绗︿覆闀垮害涓篴rgs->lengths[i]銆備綘涓嶈兘閲囩敤null缁撳熬鐨勫瓧绗︿覆銆

    • 瀵逛竴涓狪NT_RESULT鍨嬬殑鍙傞噺锛屼綘蹇呴』杞崲args->args[i] 涓轰竴涓猯ong long 鍊硷細

      long long int_val;
      int_val = *((long long*) args->args[i]);
      
    • 瀵逛竴涓猂EAL_RESULT鍨嬪弬閲忥紝浣犲繀椤昏浆鎹rgs->args[i]涓轰竴涓弻绮惧害鍊硷細

      double    real_val;
      real_val = *((double*) args->args[i]);
      
  • unsigned long *lengths

    瀵瑰垵濮嬪寲鍑芥暟锛宭engths鏁板垪琛ㄧず瀵规瘡涓弬閲忕殑鏈澶у瓧绗︿覆闀垮害銆備綘涓嶈鏀瑰彉瀹冦傚涓诲嚱鏁扮殑姣忔璋冪敤锛宭engths鍖呭惈浜嗗褰撳墠澶勭悊琛屼紶閫掔殑浠讳綍瀛楃涓 鍙傞噺鐨勫疄闄呴暱搴︺傚浜嶪NT_RESULT 鎴 REAL_RESULT绫诲瀷鐨勫弬閲忥紝lengths 浠嶅寘鍚弬閲忕殑鏈澶ч暱搴︼紙瀵瑰垵濮嬪寲鍑芥暟锛夈

27.2.3.4.聽UDF杩斿洖鍊煎拰閿欒澶勭悊

濡傛灉娌℃湁閿欒鍙戠敓锛屽垵濮嬪寲鍑芥暟搴旇杩斿洖0锛屽惁鍒欏氨杩斿洖1銆傚鏋滄湁閿欒鍙戠敓锛寈xx_init() 搴旇鍦╩essage 鍙傛暟瀛樺偍涓涓互null缁撳熬鐨勯敊璇秷鎭傝娑堟伅琚繑鍥炵粰瀹㈡埛绔傛秷鎭紦鍐插尯鏄 MYSQL_ERRMSG_SIZE 瀛楃闀垮害锛屼絾浣犲簲璇ヨ瘯鐫鎶婃秷鎭繚鎸佸湪灏戜簬80涓瓧绗︼紝浠ヤ究瀹冭兘閫傚悎鏍囧噯缁堢灞忓箷鐨勫搴︺

瀵逛簬long long 鍜 double 绫诲瀷鐨勫嚱鏁帮紝涓诲嚱鏁 xxx()鐨勮繑鍥炶繑鍥炲兼槸鍑芥暟鍊笺傚瓧绗﹀嚱鏁拌繑鍥炰竴涓寚鍚戠粨鏋滅殑鎸囬拡锛屽苟涓旇缃 *result 鍜 *length  涓鸿繑鍥炲肩殑鍐呭鍜岄暱搴︺備緥濡傦細

memcpy(result, "result string", 13);
*length = 13;

琚紶缁 xxx() 鍑芥暟鐨勭粨鏋滅紦鍐插尯鏄 255 瀛楄妭闀裤傚鏋滀綘鐨勭粨鏋滈傚悎杩欎釜闀垮害锛屼綘灏变笉闇瑕佹媴蹇冨缁撴灉鐨勫唴瀛樺垎閰嶃

濡傛灉瀛楃涓插嚱鏁伴渶瑕佽繑鍥炰竴涓秴杩255瀛楄妭鐨勫瓧绗︿覆锛屼綘蹇呴』鐢 malloc() 鍦ㄤ綘鐨 xxx_init() 鍑芥暟鎴栬 xxx() 鍑芥暟閲屼负瀛楃涓插垎閰嶇┖闂达紝骞朵笖鍦 xxx_deinit() 鍑芥暟閲岄噴鏀炬绌洪棿銆備綘鍙互灏嗗凡鍒嗛厤鍐呭瓨瀛樺偍鍦 UDF_INIT  缁撴瀯閲岀殑 ptr  浣嶇疆浠ュ灏嗘潵 xxx() 璋冪敤銆傝鍙傞槄27.2.3.1鑺傦紝鈥淯DF 瀵圭畝鍗曞嚱鏁扮殑璋冪敤椤哄簭鈥

瑕佸湪涓诲嚱鏁颁腑鎸囨槑涓涓 NULL 鐨勮繑鍥炲硷紝璁剧疆 *is_null 涓 1 锛

*is_null = 1;

瑕佸湪涓诲嚱鏁颁腑鎸囨槑閿欒杩斿洖锛岃缃 *error 涓 1 锛

*error = 1;

濡傛灉 xxx() 瀵逛换鎰忚璁剧疆 *error 涓 1  锛屽浜庝换浣 XXX()琚皟鐢ㄧ殑璇彞澶勭悊鐨勫綋鍓嶈鍜岄殢鍚庣殑浠绘剰琛岋紝璇ュ嚱鏁板间负 NULL 锛堢敋鑷抽兘涓嶄负闅忓悗鐨勮璋冪敤 xxx()锛夈

27.2.3.5.聽缂栬瘧鍜屽畨瑁呰嚜瀹氫箟鍑芥暟

瀹炵幇UDF鐨勬枃浠跺繀椤诲湪杩愯鏈嶅姟鍣ㄧ殑涓绘満涓婄紪璇戝拰瀹夎銆傝繖涓楠ゅ湪涓嬮潰浠嬬粛锛屼互鍖呭惈鍦∕ySQL婧愮爜鍒嗗彂鐗堥噷鐨刄DF鏂囦欢sql/udf_example.cc 涓轰緥銆

绱ф帴鐫涓嬮潰鐨勬寚浠ゆ槸瀵筓nix鐨勶紝瀵筗indows鐨勬寚浠ゅ湪鏈妭绋嶅悗缁欏嚭銆

 udf_example.cc 鏂囦欢鍖呭惈涓嬪垪鍑芥暟锛

  • metaphon() 杩斿洖瀛楃涓插弬閲忕殑涓涓彉闊充綅(metaphon)瀛楃涓诧紝杩欐湁鐐硅薄涓涓帰娴嬫硶锛坰oundex锛夊瓧绗︿覆锛屼絾鏄畠鑻辫鏇村崗璋冦

  • myfunc_double()杩斿洖鍦ㄥ叾鍙傞噺涓墍鏈夊瓧绗︾殑ASCII鍊肩殑鍜岋紝闄や互鍏 鍙傞噺闀垮害涔嬪拰銆

  • myfunc_int()杩斿洖鍏跺弬閲忛暱搴︿箣鍜屻

  • sequence([const int]) 杩斿洖涓涓簭鍒楋紝浠庣粰瀹氭暟寮濮嬶紝鑻ユ病鏈夌粰瀹氭暟鍒欎粠1寮濮嬨

  • lookup() 杩斿洖瀵瑰簲涓绘満鍚嶇殑IP鏁般

  • reverse_lookup() 杩斿洖瀵瑰簲涓涓狪P鏁扮殑涓绘満鍚嶃傚嚱鏁板彲浠ュ甫'xxx.xxx.xxx.xxx'褰㈠紡鐨勪竴涓崟瀛楃涓 鍙傞噺璋冪敤锛岃涔堝甫4涓暟瀛楄皟鐢ㄣ

涓涓彲鍔ㄦ佸姞杞界殑鏂囦欢搴斾娇鐢ㄥ涓嬭繖鏍风殑鍛戒护缂栬瘧涓轰竴涓彲鍏变韩鐨勫璞℃枃浠讹細

shell> gcc -shared -o udf_example.so udf_example.cc

濡傛灉浣犱娇鐢gcc锛屼綘搴旇鑳界敤涓涓洿绠鍗曠殑鍛戒护鍒涘缓udf_example.so 锛

shell> make udf_example.so

閫氳繃杩愯MySQL婧愮爜鏍戜笅sql閲岀殑濡備笅鍛戒护锛屼綘鍙互瀹规槗鍦颁负浣犵殑绯荤粺鍐冲畾姝g‘鐨勭紪璇戝櫒閫夐」锛

shell> make udf_example.o

浣犲簲璇ヨ繍琛屼竴涓被浼间簬make鎵鏄剧ず閭f牱鐨勭紪璇戝懡浠わ紝闄や簡瑕佸湪琛屽熬闄勮繎鍒犻櫎-c閫夐」锛屽苟鍦ㄨ灏惧姞涓婂姞涓 -o udf_example.so銆傦紙鍦ㄦ煇浜涚郴缁熶笂锛屼綘鍙兘闇瑕佸湪鍛戒护琛岀暀鐫-c 閫夐」锛夈

缂栬瘧濂戒竴涓寘鍚湁UDF鐨勫叡浜洰鏍囧悗锛屼綘蹇呴』瀹夎瀹冨苟閫氱煡MySQL銆備粠udf_example.cc缂栬瘧涓涓叡浜洰鏍囨枃浠朵骇鐢熶竴涓悕瀛楃被浼间簬udf_example.so 鐨勬枃浠讹紙纭垏鍚嶅瓧鍙兘鍥犲钩鍙拌屽紓锛夈傛妸杩欎釜鏂囦欢澶嶅埗鍒 /usr/lib 杩欐牱琚綘绯荤粺鐨勫姩鎬侊紙杩愯鏃讹級閾炬帴鍣ㄦ悳绱㈠埌鐨勭洰褰曚笅锛屾垨鑰 鎶婁綘鏀惧叡浜洰鏍囨枃浠剁殑鐩綍娣诲姞鍒伴摼鎺ュ櫒閰嶇疆鏂囦欢锛堝锛/etc/ld.so.conf 锛夈

鍔ㄦ侀摼鎺ュ櫒鐨勫悕瀛楁椂绯荤粺鐗瑰畾鐨勶紙濡傦紝鍦‵reeBSD涓婃槸ld-elf.so.1 锛屽湪Linux涓婃槸 ld.so锛屽湪Mac OS X涓婃槸dyld 锛夈傛煡鐪嬩竴涓嬩綘绯荤粺鐨勬枃妗o紝鐪嬬湅閾炬帴鍣ㄧ殑鍚嶅瓧鏄粈涔堝強濡備綍閰嶇疆閾炬帴鍣ㄣ

鍦ㄨ澶氱郴缁熶笂锛屼綘涔熷彲浠ヨ缃幆澧冨彉閲廘D_LIBRARY 鎴 LD_LIBRARY_PATH 鎸囧悜浣犳斁UDF鐨勭洰褰曘俤lopen 鎵嬪唽浼氬憡璇変綘锛屽湪浣犵郴缁熶笂鐢ㄥ摢涓彉閲忓悕銆備綘鍙互鍦mysql.servermysqld_safe 鍚姩鑴氭湰閲岃缃繖涓劧鍚庨噸鍚 mysqld

鍦ㄤ竴浜涚郴缁熶笂锛岄厤缃姩鎬侀摼鎺ュ櫒鐨ldconfig涓嶈兘璇嗗埆涓嶆槸浠lib鍋氬悕瀛楀紑澶寸殑鍏变韩鐩爣銆傚湪杩欑鎯呭喌涓嬶紝浣犲簲璇ユ妸udf_example.so 鏀瑰悕涓 libudf_example.so銆

鍦╓indows绯荤粺涓婏紝浣犲彲浠ラ氳繃涓嬪垪姝ラ缂栬瘧鑷畾涔夊嚱鏁帮細

  1. 浣犻渶瑕佽幏寰桞itKeeper source repository for MySQL 5.1銆 璇峰弬闃聽2.8.3鑺傦紝鈥滀粠寮鍙戞簮鏍戝畨瑁呪

  2. 鍦ㄦ簮鏁版嵁浠撻噷鐨刅C++Files/examples/udf_example鐩綍涓嬶紝鏈夊悕涓簎df_example.def, udf_example.dsp, 鍜 udf_example.dsw  鐨勬枃浠躲

  3. 鍦ㄦ暟鎹粨鐨剆ql鐩綍涓嬶紝澶嶅埗 udf_example.cc 鏂囦欢鍒 VC++Files/examples/udf_example 鐩綍锛屽苟鏀瑰叾鍚嶄负udf_example.cpp銆

  4. Visual Studio VC++鐢ㄦ墦寮 udf_example.dsw 鏂囦欢锛岀敤瀹冩妸UDF缂栬瘧涓轰竴涓竴鑸」鐩

鍏变韩鐩爣鏂囦欢瀹夎瀹屼互鍚庯紝涓烘柊鍑芥暟淇℃伅淇敼 mysqld 锛屽仛濡備笅澹版槑锛

mysql> CREATE FUNCTION metaphon RETURNS STRING SONAME 'udf_example.so';
mysql> CREATE FUNCTION myfunc_double RETURNS REAL SONAME 'udf_example.so';
mysql> CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME 'udf_example.so';
mysql> CREATE FUNCTION lookup RETURNS STRING SONAME 'udf_example.so';
mysql> CREATE FUNCTION reverse_lookup
    ->        RETURNS STRING SONAME 'udf_example.so';
mysql> CREATE AGGREGATE FUNCTION avgcost
    ->        RETURNS REAL SONAME 'udf_example.so';

鍙互浣跨敤DROP FUNCTION鍒犻櫎鍑芥暟锛

mysql> DROP FUNCTION metaphon;
mysql> DROP FUNCTION myfunc_double;
mysql> DROP FUNCTION myfunc_int;
mysql> DROP FUNCTION lookup;
mysql> DROP FUNCTION reverse_lookup;
mysql> DROP FUNCTION avgcost;

CREATE FUNCTION 鍜 DROP FUNCTION 澹版槑鏇存柊mysql 鏁版嵁搴撲腑鐨刦unc 绯荤粺琛ㄣ傚嚱鏁板悕锛岀被鍨嬪拰鍏变韩搴撳悕瀛樿繘琛ㄤ腑銆備綘蹇呴』鏈塵ysql 鏁版嵁搴撶殑INSERT 鍜孌ELETE 鏉冮檺鏉ュ垱寤哄拰绉婚櫎鍑芥暟銆

浣犱笉鑳戒娇鐢 CREATE FUNCTION 鍘荤敯闂翠竴涓厛鍓嶅凡缁忚鍒涘缓鐨勫嚱鏁般傚鏋滀綘闇瑕侀噸鏂板畨瑁呬竴涓嚱鏁帮紝浣犲彲浠ョ敤DROP FUNCTION绉婚櫎瀹冿紝鐒跺悗鍐嶇敤CREATE FUNCTION閲嶆柊瀹夎瀹冦備綘鍙兘浼氶渶瑕佽繖涔堝仛锛屾瘮濡備綘閲嶆柊缂栬瘧鏂扮増鏈殑鍑芥暟浠ヤ究mysqld寰楀埌杩欎釜鏂扮増鏈備笉鐒讹紝鏈嶅姟鍣ㄨ繕缁х画浣跨敤鏃х殑鐗堟湰銆

涓涓湁鏁堢▼搴忔槸宸茶 CREATE FUNCTION鍔犺浇涓旀病鏈夎DROP FUNCTION绉婚櫎鐨勫嚱鏁般傛墍鏈夋湁鏁堝嚱鏁板湪姣忔鏈嶅姟鍣ㄥ惎鍔ㄦ椂閲嶆柊鍔犺浇锛岄櫎闈炰綘浣跨敤--skip-grant-tables閫夐」鏉ュ惎鍔mysqld銆傝繖绉嶆儏鍐典笅锛孶DF鐨勫垵濮嬪寲灏嗚璺宠繃锛孶DF涓嶅彲鐢ㄣ

27.2.3.6.聽鑷畾涔夊嚱鏁板畨鍏ㄩ闃叉帾鏂

MySQL 閲囧彇涓嬪垪鎺柦鏉ラ槻姝㈣鐢ㄨ嚜瀹氫箟鍑芥暟銆

浣犲繀椤绘湁 INSERT 鏉冮檺鎵嶈兘浣跨敤 CREATE FUNCTION 鍙婃湁 DELETE 鏉冮檺鎵嶈兘浣跨敤 DROP FUNCTION銆傝繖鏄緢蹇呰鐨勶紝鍥犱负杩欎簺澹版槑鍦╩ysql.func琛ㄩ噷娣诲姞鍚堝垹闄よ銆

闄や簡瀵瑰簲涓 xxx()鍑芥暟鐨剎xx 绗﹀彿锛孶DF搴旇鑷冲皯瀹氫箟涓涓鍙枫傝繖浜涜緟鍔╃鍙峰搴 xxx_init(), xxx_deinit(), xxx_reset(), xxx_clear() 鍜 xxx_add() 鍑芥暟銆mysqld 涔熸敮鎸佷竴涓帶鍒朵粎鏈変竴涓獂xx绗﹀彿鐨刄DF鏄惁琚姞杞界殑--allow-suspicious-udfs銆傝繖涓夐」 榛樿鏄叧锛屼互闃叉浠庡叡浜洰鏍囨枃浠惰屼笉鏄粠杩欎簺宸插寘鍚殑鍚堟硶UDF鍔犺浇鐨勪紒鍥俱傚鏋滀綘鏈変粎鍚玿xx绗﹀彿鐨勮佺増鏈琔DF锛屼互鍙婁笉鑳介噸缂栬瘧鏉ュ寘鍚緟鍔╃鍙风殑鑰佺増鏈琔DF锛岄偅灏辨湁蹇呰閫--allow-suspicious-udfs 閫夐」銆傚惁鍒欙紝浣犲簲璇ラ伩鍏嶆墦寮杩欎釜閫夐」銆

UDF 鐩爣鏂囦欢涓嶈兘鏀惧湪浠绘剰鐩綍銆傚畠浠繀椤讳綅浜庡姩鎬侀摼鎺ュ櫒琚厤缃潵鎼滅储鍒扮殑涓浜涚郴缁熺洰褰曘備负寮哄埗鎵ц杩欎釜闄愬埗骞堕槻姝㈡寚瀹氳鍔ㄦ侀摼鎺ュ櫒鎼滅储鍒扮殑鐩綍涔嬪鐨勮矾寰勶紝MySQL鍦ㄥ姞杞藉嚱鏁扮殑鏃跺欐鏌ュ湪CREATE FUNCTION 涓寚瀹氱殑鍏变韩鐩爣鏂囦欢鍚嶏紝浠ュ強瀛樺湪mysql.func琛ㄤ腑鐨勬枃浠剁殑璺緞鍒嗛殧绗︺傝繖闃叉閫氳繃鐩存帴鎿嶄綔mysql.func琛ㄦ寚瀹氶潪娉曡矾寰勫悕銆傛湁鍏砋DF鍜岃繍琛屾椂閾炬帴鍣紝璇峰弬闃27.2.3.5鑺傦紝鈥滅紪璇戝拰瀹夎鑷畾涔夊嚱鏁扳

27.2.4.聽娣诲姞鏂扮殑鍥烘湁鍑芥暟

涓嬮潰浠嬬粛娣诲姞鏂板浐鏈夊嚱鏁扮殑姝ラ銆傝娉ㄦ剰浣犱笉鑳芥坊鍔犲浐鏈夊嚱鏁板埌浜岃繘鍒跺垎鍙戠増閲岋紝鍥犱负杩欎釜姝ラ鍖呭惈淇敼MySQL婧愪唬鐮併備綘蹇呴』浠庢簮鐮佸垎鍙戠増鑷繁缂栬瘧MySQL銆傚彟澶栬娉ㄦ剰锛屽鏋滀綘鎶奙ySQL绉绘鍒板彟涓涓増鏈紙姣斿鏂扮増鏈斁鍑烘潵鐨勬椂鍊欙級锛屼綘闇瑕佺敤鏂扮増鏈噸澶嶈繖涓坊鍔 姝ラ銆

閲囧彇涓嬪垪姝ラ鏉ユ坊鍔燤ySQL鏂扮殑鍥烘湁鍑芥暟锛

  1. 鍦ㄥ畾涔夊嚱鏁板悕鐨刲ex.h鏂囦欢涓殑sql_functions[]鏁板垪閲屾坊鍔犱竴琛屻

  2. 濡傛灉鍑芥暟鍘熷瀷鏄畝鍗曠殑锛堝彧鏈夐浂涓紝涓涓紝浜屼釜鎴栦笁涓弬閲忥級锛屼綘搴旇鍦╨ex.h涓寚瀹 SYM(FUNC_ARGN) 锛堝叾涓N 鏄弬閲忕殑涓暟锛変綔涓簊ql_functions[]鏁板垪涓殑绗簩涓 鍙傞噺锛屽苟娣诲姞涓涓湪item_create.cc涓垱寤哄嚱鏁扮洰鏍囩殑鍑芥暟銆傚彲浠ョ湅鐪 "ABS" 鍜 create_funcs_abs() 浣滀负涓句緥璇存槑銆

    濡傛灉鍑芥暟鍘熷瀷鏄鏉傜殑锛堜妇渚嬶紝濡傛灉鍑芥暟鏈夊绉嶅弬閲忥級锛屼綘搴旇缁檚ql_yacc.yy娣诲姞涓よ銆備竴琛岃〃绀yacc搴旇瀹氫箟鐨勯澶勭悊绋嬪簭璁板彿锛岋紙杩欏簲璇ュ湪鏂囦欢鐨勫紑濮嬫坊鍔狅級銆傜劧鍚庡畾涔夊嚱鏁 鍙傛暟锛屽苟娣诲姞涓涓甫杩欎簺鍙傛暟鐨勯」鍒simple_expr鍒嗘瀽瑙勫垯涓備妇涓涓緥瀛愶紝浣犲彲浠ユ鏌 sql_yacc.yy 涓墍鏈夊嚭鐜扮殑ATAN 鐪嬬湅杩欎釜瀹氫箟鏄粈涔堟牱瀛愮殑銆

  3. 鍦 item_func.h涓鏄庝竴涓户鎵胯嚜Item_num_func 杩樻槸 Item_str_func鐨勭被锛屽彇鍐充簬浣犵殑鍑芥暟鏄繑鍥炰竴涓暟杩樻槸涓涓瓧绗︿覆銆

  4. 鍦 item_func.cc涓槸鍚︽坊鍔犱笅鍒楄鏄庝箣涓锛屽彇鍐充簬浣犳槸瀹氫箟涓涓暟瀛楀嚱鏁拌繕鏄瓧绗﹀嚱鏁帮細

    double   Item_func_newname::val()
    longlong Item_func_newname::val_int()
    String  *Item_func_newname::Str(String *str)
    

    濡傛灉浣犱粠浠讳綍鏍囧噯椤圭户鎵夸簡浣犵殑鐩爣锛堢被浼间簬Item_num_func锛夛紝浣犳垨璁稿彧瑕佸畾涔夎繖浜涘嚱鏁颁腑鐨勪竴涓紝鐒跺悗璁╃埗鐩爣鐓х鍒殑鍑芥暟銆傛瘮濡傦紝Item_str_func 绫诲畾涔変簡涓涓 val() 鍑芥暟锛屽畠杩欎釜鍑芥暟瀵::str()杩斿洖鐨勫艰繘琛 atof()鎿嶄綔銆

  5. 浣犳垨璁镐篃瀹氫箟浜嗕笅鍒楃洰鏍囧嚱鏁帮細

    void Item_func_newname::fix_length_and_dec()
    

    杩欎釜鍑芥暟鑷冲皯搴旇璁$畻鍩轰簬缁欏畾鍙傞噺鐨刴ax_length銆 max_length 鏄嚱鏁板彲鑳借繑鍥炲瓧绗︾殑鏈澶т釜鏁般傚鏋滀富鍑芥暟涓嶈兘杩斿洖 NULL鍊硷紝杩欎釜鍑芥暟涔熷簲璇ヨ缃 maybe_null = 0銆傚嚱鏁板彲浠ラ氳繃妫鏌ュ嚱鏁扮殑maybe_null鍊兼潵妫鏌ユ槸鍚︽湁鍑芥暟 鍙傞噺鑳借繑鍥濶ULL鍊笺備綘鍙互鐪嬩竴涓婭tem_func_mod::fix_length_and_dec 浣滀负鍏稿瀷鐨勪緥瀛愭潵璇存槑杩欎釜闂銆

鎵鏈夊嚱鏁伴兘蹇呴』鏄嚎绋嬪畨鍏ㄧ殑锛屾崲鍙ヨ瘽璇村氨鏄紝濡傛灉娌℃湁浜掓枼浣撲繚鎶わ紝涓嶈鍦ㄥ嚱鏁颁腑浣跨敤浠讳綍鍏ㄥ眬鎴栭潤鎬佸彉閲忋

濡傛灉浣犳兂瑕佷粠鍑芥暟::val(), ::val_int()鎴::str()杩斿洖NULL锛屼綘搴旇璁緉ull_value涓1,骞惰繑鍥0銆

瀵逛簬鐩爣鍑芥暟 ::str() 鏈変竴浜涢渶瑕佽屽鑰冭檻涔嬪锛:

  • 瀛楃涓插弬閲*str 鎻愪緵涓涓瓧绗︿覆缂撳啿鍙互鐢ㄦ潵淇濇寔缁撴灉锛堟洿澶氬叧浜庡瓧绗︿覆绫诲瀷鐨勪俊鎭鍙傞槄 sql_string.h鏂囦欢锛夈 

  • 濡傛灉缁撴灉涓篘ULL锛::str() 鍑芥暟搴旇杩斿洖淇濇寔杩欎釜缁撴灉鐨勫瓧绗︿覆鎴(char*) 0銆

  • 闄ら潪鏈夌粷瀵瑰湴闇瑕侊紝鎵鏈夊綋鍓嶇殑瀛楃涓插嚱鏁拌閬垮厤鍒嗛厤鍐呭瓨锛

27.3. 涓篗ySQL娣诲姞鏂版楠

鍦∕ySQL涓紝浣犲彲浠ョ敤C++瀹氫箟涓涓楠わ紝鍦ㄤ竴涓煡璇㈣鍙戦佸埌瀹㈡埛绔箣鍓嶈闂拰淇敼鍏朵腑鐨勬暟鎹備慨鏀瑰彲浠ヤ竴琛屾帴涓琛屽湴鍋氾紝鎴栬呮寜鐓х骇鍒垚缁勶紙GROUP锛夊湴鍋氥

鎴戜滑鍒涘缓涓涓寖渚嬫楠ゆ潵婕旂ず浣犲彲浠ュ仛鐨勩

姝ゅ锛屾垜浠帹鑽愪綘鐪嬩竴涓媘ylua銆傞氳繃瀹冧綘鍙互鐢  LUA璇█鎶婅繍琛屾椂閲岀殑涓涓 姝ラ鍔犺浇鍒mysqld涓

27.3.1.聽姝ラ鍒嗘瀽

analyse([max_elements,[max_memory]])

杩欎釜姝ラ鍦╯ql/sql_analyse.cc瀹氫箟锛岃繖涓楠ゆ鏌ヤ綘鏌ヨ鐨勭粨鏋滐紝骞朵笖杩斿洖瀵规缁撴灉鐨勪竴涓垎鏋愶細

  • max_elements (榛樿鍊 256) 鏄痑nalyse娉ㄦ剰鍒版瘡 鍒椾笉鍚屽肩殑鏈楂樻暟鐩俛nalyse浣跨敤姝 鍙傛暟鏉ユ鏌ユ槸鍚︽渶浼樺寲鐨勫垪鐨勭被鍨嬫槸ENUM绫诲瀷銆

  • max_memory (榛樿鍊 8192) 鏄痑nalyse鍦ㄦ煡鎵炬墍鏈変笉鍚屽兼椂鍒嗛厤缁欐瘡 鍒楃殑鏈澶у唴瀛樻暟銆俰

SELECT ... FROM ... WHERE ... PROCEDURE ANALYSE([max_elements,[max_memory]])

27.3.2. 缂栧啓姝ラ

褰撳墠鏉ヨ锛岀浉鍏崇殑鏂囨。鍙湁婧愮爜銆

妫鏌ヤ笅鍒楁枃浠跺彲浠ヨ幏寰楀叧浜庢楠ょ殑鎵鏈変俊鎭細

  • sql/sql_analyse.cc

  • sql/procedure.h

  • sql/procedure.cc

  • sql/sql_select.cc


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆