|
| 1 | +=============================================================================== |
| 2 | +MySQL .NET UDF - A UDF plugin that allows you to use .NET code to execute |
| 3 | +custom code just as you were to write a native UDF. |
| 4 | + |
| 5 | +Written by James Davis. |
| 6 | +www.debugthings.com |
| 7 | +=============================================================================== |
| 8 | + |
| 9 | +INTRODUCTION |
| 10 | +------------ |
| 11 | +While looking around at the plugins available for MySQL I noticed there wasn't |
| 12 | +much in the way of things for Windows. Even more scare were any items relating |
| 13 | +to .NET. |
| 14 | + |
| 15 | +I wrote a wrapper around the .NET Hosting APIs to allow a developer to code in |
| 16 | +.NET and avoid all of the issues surrounding memory management, threading, and |
| 17 | +isolation of potentially harmful code. |
| 18 | + |
| 19 | +You now have the power of .NET at your control and can write some very crazy |
| 20 | +albeit possibly not practical custom functions. Some examples would include |
| 21 | +using WebClient to download webpages directly into a database without having to |
| 22 | +write an application to do so. |
| 23 | + |
| 24 | +QUICKSTART |
| 25 | +---------- |
| 26 | +If you want to jump in using the included examples here is what you need to do. |
| 27 | + |
| 28 | +* Rename mysqld.exe.config_full to mysqld.exe.config |
| 29 | + - Delete old config file |
| 30 | +* Open your favorite MySQL client and execute install_samples.sql |
| 31 | + |
| 32 | +Execute one of the following stored procedures. Descriptions included. |
| 33 | + |
| 34 | +simple_add3toint(int) -- Adds 3 to the input number |
| 35 | + |
| 36 | +simple_add3toreal(real) -- Adds 3 to the input number |
| 37 | + |
| 38 | +simple_addtostring(string) -- Adds "SIMPLE EXAMPLE" to the end of the input |
| 39 | + |
| 40 | +adv_isinradius(LatCenter, LongCenter, LatPoint, LongPoint, radius) -- Calculates |
| 41 | +to see if the point is inside of the specified radius from the center. |
| 42 | + |
| 43 | +adv_getwebpage(webpage) -- Uses System.WebClient to pull the raw HTML back from |
| 44 | +the URL in the function. |
| 45 | + |
| 46 | + |
| 47 | +WHY .NET? |
| 48 | +--------- |
| 49 | +The choice for me was simple. I work in .NET all the time. |
| 50 | + |
| 51 | +WHY MYSQL? |
| 52 | +---------- |
| 53 | +I have always had a soft spot for MySQL. It was my first RDBMS I used to create |
| 54 | +a few websites. I started using it in 2000 and dabbled with it off and on for |
| 55 | +the past 14 years. |
| 56 | + |
| 57 | +MySQL fell by the wayside once I got into a corporate environment and started |
| 58 | +using Microsoft SQL(MSSQL) and Oracle. However, one day I started thinking |
| 59 | +about ways I could use external code in MySQL similar to MSSQL. |
| 60 | + |
| 61 | +The natural fit was to use .NET as that is what MSSQL used. |
| 62 | + |
| 63 | +------------------------------------------------------------------------------- |
| 64 | +INSTALLATION |
| 65 | +------------------------------------------------------------------------------- |
| 66 | +This README is shown after the installation has completed. But should also be |
| 67 | +found in the "MySQL .NET UDF" installation location. Below are steps listed |
| 68 | +to do a manual installation if you happen to build your own copy or do not |
| 69 | +use the installer. |
| 70 | + |
| 71 | +BASE PLUGIN INSTALL (MANUAL) |
| 72 | +---------------------------- |
| 73 | + * Extract all files into a final destination folder |
| 74 | + * Install DOTNET20\MySQLHostManager.dll into the GAC |
| 75 | + - You can do this by dragging/dropping the files into %WINDIR%\assembly\ |
| 76 | + or using %WINDIR%\Microsoft.NET\Framework\v2.0.50727\gacutil.exe |
| 77 | + * Install DOTNET40\MySQLHostManager.dll into the GAC |
| 78 | + - There is only one option. You can do this by using |
| 79 | + %WINDIR%\Microsoft.NET\Framework\v2.0.50727\gacutil.exe |
| 80 | + * Copy bin\mysqld.exe.config to %MYSQLHOME%\bin |
| 81 | + * Copy plugin\MySQLDotNet.dll to %MYSQLHOME%\lib\plugin |
| 82 | + * Open your favorite MySQL client and execute sql\install.sql |
| 83 | + |
| 84 | +.NET CUSTOM PLUGIN INSTALL (MANUAL) |
| 85 | +----------------------------------- |
| 86 | + * Copy your custom plugin dll to %MYSQLHOME%\lib\plugin |
| 87 | + - Optionally you may copy it to %MYSQLHOME%\lib\plugin\<DLLNAME> |
| 88 | + - See section on custom assemblies |
| 89 | + * Edit %MYSQLHOME%\bin\mysqld.exe.config to include your assembly |
| 90 | + - Examples are inside of the config file |
| 91 | + |
| 92 | + |
| 93 | +EXECUTING CUSTOM CODE (MINI VERSION) |
| 94 | +------------------------------------ |
| 95 | +If the install went well |
| 96 | + |
| 97 | +------------------------------------------------------------------------------- |
| 98 | +HOW TO USE |
| 99 | +------------------------------------------------------------------------------- |
| 100 | +Once both the base plugin and your custom plugin are installed you need to tell |
| 101 | +MySQL what assemblies it is allowed to load. To do this you need to edit the |
| 102 | +.config file to include a new config section. Once the config section is added |
| 103 | +you need to tell the application the name of your assembly to load. |
| 104 | + |
| 105 | +This assembly will get picked up from the GAC or from lib\plugin or |
| 106 | +lib\plugin\ASSEMBLYNAME. |
| 107 | + |
| 108 | +CUSTOM SECTION DECLARATION |
| 109 | +-------------------------- |
| 110 | +<configSections> |
| 111 | +<section name ="mysqlassemblies" type="MySQLHostManager.MySQLAssemblyList, |
| 112 | + MySQLHostManager, Version=2.0.0.0, Culture=neutral, |
| 113 | + PublicKeyToken=71c4a5d4270bd29c"/> |
| 114 | +</configSections> |
| 115 | + |
| 116 | +This is a standard custom section declaration. You can find out more |
| 117 | +information from MSDN. |
| 118 | + |
| 119 | +ASSEMBLY CONFIG DEFINITIION |
| 120 | +--------------------------- |
| 121 | +<mysqlassemblies> |
| 122 | +<assemblies> |
| 123 | + <assembly name="MySQLCustomClass.CustomMySQLClass" |
| 124 | + fullname ="MySQLCustomClass, |
| 125 | + Version=1.0.0.0, PublicKeyToken=a55d172c54d273f4" |
| 126 | + clrversion="4.0" lifetime="02:00:00"/> |
| 127 | +</assemblies> |
| 128 | +</mysqlassemblies> |
| 129 | + |
| 130 | +This is a custom section that defines the assembly to be loaded. You must |
| 131 | +sign your assembly and use a (partial) strong name. This is a safety feature |
| 132 | +and protects against rogue code. |
| 133 | + |
| 134 | +ATTRIBUTES |
| 135 | +---------- |
| 136 | +- name: The fully qualified name of the class that you wish to use |
| 137 | +- fullname: The strong name of your assembly |
| 138 | +- clrversion: The required version of the CLR. Uses default if not specified |
| 139 | +- lifetime: How long the appdomains live for this assembly |
| 140 | + |
| 141 | +APPDOMAIN CONFIG DEFINITIION |
| 142 | +---------------------------- |
| 143 | +<mysqlassemblies> |
| 144 | +<appDomainCleanup |
| 145 | + interval="0.00:05:00" |
| 146 | + forcedInterval="1.00:00:00" /> |
| 147 | +</mysqlassemblies> |
| 148 | + |
| 149 | +All of the domains are entered into a pool and reused. After the specified |
| 150 | +lifetime the domains are released from the pool and set to "die." This custom |
| 151 | +section controls intervals the removed domains are unloaded from the |
| 152 | +application. |
| 153 | + |
| 154 | +The only time domains are checked for life is when they are being pulled from |
| 155 | +the pool and being released from active use. The forcedInterval makes sure that |
| 156 | +upon the next unload check the system will iterate through all appdomains and |
| 157 | +force the timeout check. |
| 158 | + |
| 159 | +ATTRIBUTES |
| 160 | +---------- |
| 161 | +- interval: The timespan between checking to unload. |
| 162 | +- forcedInterval: All domains are scanned and cleared if they meet the criteria |
| 163 | + |
| 164 | +PERMISSION SET CONFIG DEFINITION |
| 165 | +-------------------------------- |
| 166 | +<mysqlassemblies> |
| 167 | + <permissionsets> |
| 168 | + <permissionset name="MySQLPartial"> |
| 169 | + <permissions> |
| 170 | + <add name="FileIOPermission" /> |
| 171 | + </permissions> |
| 172 | + </permissionset> |
| 173 | + <!----> |
| 174 | + </permissionsets> |
| 175 | +<mysqlassemblies> |
| 176 | + |
| 177 | +In order to secure the host and to secure the code the AppDomain manager |
| 178 | +enforces Code Access Security. In order to run specific methods inside of |
| 179 | +MySQL we need to grant specific code access. |
| 180 | + |
| 181 | +These names come from the standard code access security classes inside of |
| 182 | +.NET. Check MSDN for a description of each class. Some common permissions: |
| 183 | + |
| 184 | +- FileIOPermission |
| 185 | +- WebPermission |
| 186 | +- RegistryPermission |
| 187 | +- EventLogPermission |
| 188 | + |
0 commit comments