This post is the third post of a series of articles on extending MySQL with the Component Infrastructure:
- Extending MySQL using the Component Infrastructure – part 1
- Extending MySQL using the Component Infrastructure – part 2: building the server
- Extending MySQL using the Component Infrastructure – part 3: component services
- Extending MySQL using the Component Infrastructure – part 4: error logging
- Extending MySQL using the Component Infrastructure – part 5: privileges
- Extending MySQL using the Component Infrastructure – part 6: functions
- Extending MySQL using the Component Infrastructure – part 7: messages to users
It’s time to start coding our new component \o/
In the MySQL Server’s source directory (see part 2), we will create a new directory for our component:
[fred@imac ~workspace/mysql-server/BIN-DEBUG]$ cd .. [fred@imac ~workspace/mysql-server]$ mkdir components/viruscan [fred@imac ~workspace/mysql-server]$ cd components/viruscan
We will create 3 files:
Our component will of course use some services from the Component Infrastructure.
All services are listed in the documentation page Component Services Inventory.
We also need others components like the log
The required header for the services we need are:
- component_implementation (this is for the macros that every component implementation must include)
- log_builtins (to log messages in error log and performance_schema.error_log table)
- mysql_current_thread_reader (to get some user context)
- dynamic_privileges and security_context
- udf_registration (to create the UDFs)
- status_variable_registration (to create the new status variables)
First Step in extending MySQL’s world
We will now create a component that we can load. It won’t do anything yet, just have the possibility to be installed and removed.
We start with
scan.h that will be minimal:
As you can see, nothing special, the Copyright header and then we define a tag for our component
And finally we include the header for the component implementation.
Then we create our component code (which is still minimal for the moment),
We include our previously created header (
scan.h) ant then we create two functions that are called when installing and uninstalling the component.
If the component provides services or requires services we also need to add them in the following sections. Currently those are empty, we will use them later
After, we also add some metadata for the component.
And we finish with the declaration of the component, referencing the previously created function to called at install (init) and uninstall (deinit).
The library could contain multiple components, ours contains only one.
Building our component
Before building our component we still need one file
CMakeLists.txt with the following content:
DISABLE_MISSING_PROFILE_WARNING() MYSQL_ADD_COMPONENT(viruscan scan.cc MODULE_ONLY TEST_ONLY )
For the moment we are not interested in profiling our test component. Once we will require to link the component with ClamAV, we will also update this file.
Now we can run
cmake again (same command as in part 2):
[fred@imac ~workspace/mysql-server]$ cd BIN-DEBUG [fred@imac ~workspace/mysql-server/BIN-DEBUG]$ cmake .. -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=../downloads
Now we can build our component only:
[fred@imac ~workspace/mysql-server/BIN-DEBUG]$ make component_viruscan
Testing our component
We can now test our component, we use again mtr to run MySQL:
$ cd mysql-test $ perl mtr --mem --start
On another terminal (I usually keep it dedicated for testing with MySQL Shell), we can try to install and uninstall our freshly build component:
We already created our first component, the first step in extending MySQL Server, congratulations !
In the next articles, we will of course extend the capabilities of our component to make it useful.
The next step will be about using the component logging service to log messages in error log.
Stay tuned and as usual, enjoy and extend MySQL with code !