项目作者: RebirthLee

项目描述 :
MySQL(or MariaDB) UDF(User-Defined Functions) Http Client Plugin.
高级语言: Go
项目地址: git://github.com/RebirthLee/mysql_udf_http_golang.git
创建时间: 2019-02-26T00:12:40Z
项目社区:https://github.com/RebirthLee/mysql_udf_http_golang

开源协议:Other

下载


mysql_udf_http_golang

MySQL UDF MariaDB UDF

MySQL or MariaDB UDF(User-Defined Functions) HTTP Client Plugin

Setup

  • Clone Source

    1. git clone https://github.com/2rebi/mysql_udf_http_golang.git udf
    2. cd udf
  • Auto Build

    1. bash ./install.sh {username} {password}

{username} replace your MySQL or MariaDB Username.
{password} replace your MySQL or MariaDB Password(Optional).

  • Manual Build
    1. bash ./build.sh
    Build output is http.so, move file to plugin_dir path.
    if you don’t know plugin_dir path.
    Command input this on MySQL, MariaDB connection.
  1. SHOW VARIABLES LIKE 'plugin_dir';

Ex)

  1. $ mysql -u root -p
  2. Enter password:

And

  1. MariaDB [(none)]> SHOW VARIABLES LIKE 'plugin_dir';
  2. +---------------+-----------------------------------------------+
  3. | Variable_name | Value |
  4. +---------------+-----------------------------------------------+
  5. | plugin_dir | /usr/local/Cellar/mariadb/10.3.12/lib/plugin/ |
  6. +---------------+-----------------------------------------------+
  7. 1 row in set (0.001 sec)

and http.so move to Value path.

  1. mv ./http.so /usr/local/Cellar/mariadb/10.3.12/lib/plugin/

Finally, execute query

  • Http Help
    1. CREATE FUNCTION http_help RETURNS STRING SONAME 'http.so';
  • Http Get Method
    1. CREATE FUNCTION http_get RETURNS STRING SONAME 'http.so';
  • Http Post Method
    1. CREATE FUNCTION http_post RETURNS STRING SONAME 'http.so';

Usage

- Help

  1. SELECT http_help();

- GET Method

  • Prototype

    1. SELECT http_get(url, options...);
  • Simple Request

    1. SELECT http_get('http://example.com');

    Return

    1. {
    2. "Body" : String(HTML(Default), Base64, Hexdecimal)
    3. }
  • Output Option

  1. SELECT http_get('http://example.com', '-O FULL');

Return

  1. {
  2. "Proto" : String(Http Version, HTTP/1.0, HTTP/1.1, HTTP/2.0),
  3. "Status" : String(Status Code, 200 OK, 404 NOT FOUND...),
  4. "Header" : JSON(`{Key : Array, ...}`),
  5. "Body" : String(HTML(Default), Base64, Hexdecimal)
  6. }

-O {outputType} Define kind of result.
PROTO, STATUS or STATUS_CODE, HEADER, BODY(default), FULL
-O PROTO|STATUS|HEADER|BODY same this -O FULL.

  • Custom Header
  1. SELECT http_get('http://example.com', '-O FULL', '-H CustomKey:CustomValue', '-H Authorization:Bearer a1b2c3d4-123e-5678-9fgh-ijk098765432')

Like this

  1. GET / HTTP/1.1
  2. Host: example.com
  3. CustomKey: CustomValue
  4. Authorization: Bearer a1b2c3d4-123e-5678-9fgh-ijk098765432
  5. User-Agent: Go-http-client/1.1
  6. Accept-Encoding: gzip

Option param input -H {key}:{value}.

- POST Method

  • Prototype
    1. SELECT http_post(url, contentType, body, options...)
  • Simple Request(No Body)
    1. SELECT http_post('http://example.com', '', '');
  • Simple Request(Json Body)
    1. SELECT http_post('http://example.com', 'application/json', '{"Hello":"World"}');
    Like this
    ```http
    POST / HTTP/1.1
    Host: example.com
    Content-Type: application/json
    Content-Length: 17
    User-Agent: Go-http-client/1.1
    Accept-Encoding: gzip

{“Hello”:”World”}

  1. ### - Raw Method
  2. - **Prototype**
  3. ```sql
  4. SELECT http_raw(method, url, body, options...)
  • PUT
    1. SELECT http_raw('PUT', url, body, options...)
  • PATCH
    1. SELECT http_raw('PATCH', url, body, options...)
  • DELETE
    1. SELECT http_raw('DELETE', url, body, options...)

License

THE BEER-WARE LICENSE (Revision 42)