<commit_before># lib\_mysqludf\_amqp

`lib_mysqludf_amqp` is a [MySQL](http://www.mysql.com/) [user-defined function](http://dev.mysql.com/doc/refman/5.7/en/udf-features.html)
library for sending [AMQP](https://www.amqp.org/) messages.

## Status

The project is currently in its infancy. It should not be used in production until the dust has settled and proper testing is complete.

## Requirements

* C compiler and standard build tools (make, sh, ...).
* Autotools and friends (autoconf, automake, libtool, pkg-config, ...).
* [MySQL](http://www.mysql.com/)
* [rabbitmq-c](https://github.com/alanxz/rabbitmq-c)

## Building

```
$ ./autogen
$ ./make
# sudo make install
# service mysql-server restart
$ make installdb
```

## API

### `lib_mysqludf_amqp_info()`

Returns an informational message denoting the package name and version. For example, `lib_mysqludf_amqp 0.0.0`.

#### Example

```
SELECT lib_mysqludf_amqp_info();
```

### `lib_mysqludf_amqp_sendstring(hostname, port, username, password, exchange, routingKey, message)`

Sends a plain text `message` to the given `exchange` on the provided `hostname` and `port` with the supplied `routingKey` as `username` identified by `password`.

#### Example
<commit_msg>README.md: add trigger example<commit_after># lib\_mysqludf\_amqp

`lib_mysqludf_amqp` is a [MySQL](http://www.mysql.com/) [user-defined function](http://dev.mysql.com/doc/refman/5.7/en/udf-features.html)
library for sending [AMQP](https://www.amqp.org/) messages.

## Status

The project is currently in its infancy. It should not be used in production until the dust has settled and proper testing is complete.

## Requirements

* C compiler and standard build tools (make, sh, ...).
* Autotools and friends (autoconf, automake, libtool, pkg-config, ...).
* [MySQL](http://www.mysql.com/)
* [rabbitmq-c](https://github.com/alanxz/rabbitmq-c)

## Building

```
$ ./autogen
$ ./make
# sudo make install
# service mysql-server restart
$ make installdb
```

## Example

```
DROP TABLE IF EXISTS `accounts`;
CREATE TABLE `accounts` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `username` varchar(64) NOT NULL,
     PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='Customer Accounts';

DELIMITER ;;

DROP TRIGGER IF EXISTS `after_insert_on_accounts`;
CREATE DEFINER=`root`@`localhost` TRIGGER `after_insert_on_accounts` AFTER INSERT ON `accounts` FOR EACH ROW BEGIN
    SET @amqp_result = (SELECT lib_mysqludf_amqp_sendjson('localhost', 5672, 'guest', 'guest', 'udf', 'test', json_object('table', 'accounts', 'op', 'create', 'payload', json_object('id', NEW.id, 'username', NEW.username))));
END ;;

DROP TRIGGER IF EXISTS `after_update_on_accounts`;
CREATE DEFINER=`root`@`localhost` TRIGGER `after_update_on_accounts` AFTER UPDATE ON `accounts` FOR EACH ROW BEGIN
    SET @amqp_result = (SELECT lib_mysqludf_amqp_sendjson('localhost', 5672, 'guest', 'guest', 'udf', 'test', json_object('table', 'accounts', 'op', 'update', 'payload', json_object('id', NEW.id, 'username', NEW.username))));
END ;;

DROP TRIGGER IF EXISTS `after_delete_on_accounts`;
CREATE DEFINER=`root`@`localhost` TRIGGER `after_delete_on_accounts` AFTER DELETE ON `accounts` FOR EACH ROW BEGIN
    SET @amqp_result = (SELECT lib_mysqludf_amqp_sendjson('localhost', 5672, 'guest', 'guest', 'udf', 'test', json_object('table', 'accounts', 'op', 'delete', 'payload', json_object('id', OLD.id, 'username', OLD.username))));
END ;;

DELIMITER ;

INSERT INTO accounts (username) values ('jdoe');
UPDATE accounts SET username = 'jsmith';
DELETE FROM accounts WHERE id = last_insert_id();
```

## API

### `lib_mysqludf_amqp_info()`

Returns an informational message denoting the package name and version. For example, `lib_mysqludf_amqp 0.0.0`.

#### Example

```
SELECT lib_mysqludf_amqp_info();
```

### `lib_mysqludf_amqp_sendstring(hostname, port, username, password, exchange, routingKey, message)`

Sends a plain text `message` to the given `exchange` on the provided `hostname` and `port` with the supplied `routingKey` as `username` identified by `password`.

#### Example

{'fraction_non_alphanumeric': 0.14778804083616917, 'fraction_numerical': 0.006806028196402528, 'mean_word_length': 5.3897515527950315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1102419', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1383, 'n_words': 394}