{"id":21,"date":"2009-04-15T14:54:40","date_gmt":"2009-04-15T19:54:40","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=21"},"modified":"2011-07-06T09:44:29","modified_gmt":"2011-07-06T14:44:29","slug":"c-callouts-from-mysql","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2009\/04\/15\/c-callouts-from-mysql\/","title":{"rendered":"C callouts from MySQL"},"content":{"rendered":"<p>While I was trying to find a way to use the statistical functions built into MySQL for performance measurement, I discovered the ability to create user defined functions that use C callouts from within the database. I knew you could create your own functions using SQL inside the database, but this allows us to expose OS functionality\/information if we need it.  This is pretty cool, because we can extend the functionality of MySQL (subject to limitations) without changing the source code.  Below is a very simple example of getting the hostname on which the server runs. This is already this functionality in MySQL (\u201cshow variables like \u2018%host%\u2019), but as noted this is just a simple example. We may have other ways that we could use this technology, such as sending mail from within the database\u2026<\/p>\n<pre lang=\"c\">\r\n[root@linux3 lib]# cat foo.c\r\n#include <stdlib.h>\r\n#include <stdio.h>\r\n#include <string.h>\r\n#include <my_global.h>\r\n#include <my_sys.h>\r\n#include <mysql.h>\r\n#include <ctype.h>\r\n\r\ntypedef unsigned long long ulonglong;\r\ntypedef long long longlong;\r\n\r\n#ifdef  __cplusplus\r\nextern \"C\" {\r\n#endif\r\n\r\n\/\/init function is required, and a deinit function is optional.\r\n\/\/These can be used to setup for the function call, and then clean\r\n\/\/up afterwards.\r\n\r\nmy_bool my_machine_name_init(UDF_INIT *initid,\r\n                                  UDF_ARGS *args,\r\n                                  char *message){\r\n  return 0;\r\n}\r\n\r\nchar* my_machine_name(UDF_INIT *initid,\r\n                      UDF_ARGS *args,\r\n                      char* result,\r\n                      unsigned long* length,\r\n                      char *is_null,\r\n                      char *error){\r\n  gethostname(result,1000);\r\n  *length = strlen(result);\r\n  return result;\r\n}\r\n<\/pre>\n<p>We then compile the C code above into a shared library, visible in \/usr\/lib so mySQL is already aware of it (in the LD_LIBRARY_PATH, but could be anywhere mySQL \u201cknows\u201d about)\u2026<\/p>\n<pre lang=\"text\">\r\n[root@linux3 lib]# gcc -shared -o foo.so foo.c<\/pre>\n<p>\u2026and then create a function in the database that references this library.<\/p>\n<pre lang=\"sql\">\r\nmysql> drop function my_machine_name;\r\nQuery OK, 0 rows affected (0.00 sec)\r\nmysql> create function my_machine_name returns string soname 'foo.so';\r\nQuery OK, 0 rows affected (0.00 sec)\r\nmysql> select my_machine_name();\r\n+--------------------------+\r\n| my_machine_name() |\r\n+--------------------------+\r\n| linux3                   |\r\n+--------------------------+\r\n1 row in set (0.01 sec)\r\n\r\nmysql> exit\r\nBye\r\n-bash-3.00$\r\n<\/pre>\n<p>Like I said, what is above is pretty simple, but I am excited about the possibilities. The code has to be thread safe, and well tested, but it does open some doors for us if the need arises.<\/p>\n<p>You may have to download the MySQL header files.\u00a0 On Red Hat, simply issue a yum install mysql-devel.\u00a0 You may also have to adjust your include file search path to find them (under \/usr\/include\/mysql by default).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I was trying to find a way to use the statistical functions built into MySQL for performance measurement, I discovered the ability to create user defined functions that use C callouts from within the database. I knew you could&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2009\/04\/15\/c-callouts-from-mysql\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,23],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":11,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":1290,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/21\/revisions\/1290"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}