{"id":21397315,"url":"https://github.com/silviucpp/mysql_pool","last_synced_at":"2025-07-13T20:32:07.240Z","repository":{"id":46080091,"uuid":"66847740","full_name":"silviucpp/mysql_pool","owner":"silviucpp","description":"Erlang mysql pool based on mysql-otp and poolboy","archived":false,"fork":false,"pushed_at":"2024-03-19T18:50:37.000Z","size":75,"stargazers_count":11,"open_issues_count":0,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-07T17:40:43.339Z","etag":null,"topics":["erlang","mysql","pool"],"latest_commit_sha":null,"homepage":"","language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/silviucpp.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-29T13:49:18.000Z","updated_at":"2025-03-27T05:12:41.000Z","dependencies_parsed_at":"2024-11-22T14:41:45.350Z","dependency_job_id":"4fe38692-2a52-4e43-a73e-9a62791b4e02","html_url":"https://github.com/silviucpp/mysql_pool","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/silviucpp/mysql_pool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviucpp%2Fmysql_pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviucpp%2Fmysql_pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviucpp%2Fmysql_pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviucpp%2Fmysql_pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silviucpp","download_url":"https://codeload.github.com/silviucpp/mysql_pool/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviucpp%2Fmysql_pool/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265199849,"owners_count":23726722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["erlang","mysql","pool"],"created_at":"2024-11-22T14:41:38.231Z","updated_at":"2025-07-13T20:32:06.990Z","avatar_url":"https://github.com/silviucpp.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mysql_pool\n\n[![Build Status](https://travis-ci.com/silviucpp/mysql_pool.svg?branch=master)](https://travis-ci.com/github/silviucpp/mysql_pool)\n![GitHub](https://img.shields.io/github/license/silviucpp/mysql_pool?dummy=unused)\n\nmysql_pool provides connection pooling for [MySQL/OTP][1] using [Poolboy][2]. It contains convenience functions for executing SQL queries on a mysql connection in a pool.\n\nAlways Use the methods from `mysql_connection` in case you need to play with a certain connection from the pool. This module has logic to reprepare and execute the query again in case the server evicted a prepare statement. The [MySQL/OTP][1] is not treating this case at this moment.\n\n### Getting starting:\n\nUsing `rebar`, put this in your `rebar.config`:\n\n```erlang\n{deps, [\n    {mysql_pool, \".*\", {git, \"https://github.com/silviucpp/mysql_pool.git\", {tag, \"1.7\"}}}\n]}.\n```\n\nExamples:\n\n\n```erlang\nok = mysql_pool:start(),\nmysql_pool:prepare(pool_one, get_accounts, \u003c\u003c\"SELECT id, fname, lname, email FROM accounts WHERE id = ?\"\u003e\u003e).\nmysql_pool:execute(pool_one, get_accounts, [1]).\n\nmysql_pool:transaction(pool_name, fun (Pid) -\u003e\n       ok = mysql_connection:query(Pid, \"INSERT INTO foo(id, name) VALUES (?, ?)\", [1, \u003c\u003c\"banana\"\u003e\u003e]),\n       ok = mysql_connection:query(Pid, \"INSERT INTO foo(id, name) VALUES (?, ?)\", [2, \u003c\u003c\"kiwi\"\u003e\u003e])\nend).\n```\n\n### Config\n\n```erlang\n    {mysql_pool, [\n        {pools, [\n            {pool_one, [\n                {size, 10},\n                {connection_options, [\n                    {host, \"localhost\"},\n                    {user, \"root\"},\n                    {password, \"root\"},\n                    {database, \"db_name\"}\n                ]}\n            ]}\n        ]}\n    ]}\n```\n\nEach pool has the following settings:\n\n- `size`: The size of the pool, default value is `5` if omitted.\n- `connection_options` - The options that are used to start each connection. The list of available settings are:\n    - `{host, Host}` - Hostname of the MySQL server, default value is `localhost`.\n    - `{port, Port}` - Port where MySQL server listen, default value is `3306` if omitted.\n    - `{user, User}` - Username used to connect to the database\n    - `{password, Password}` - Password used to connect to the database\n    - `{database, Database}` - The name of the database AKA schema to use\n    - `{connect_mode, synchronous | asynchronous | lazy}` - Connection mode.\n    - `{allowed_local_paths, [binary()]}` - This option allows you to specify a list of directories or individual files on the client machine which the server may request, for example when executing a `LOAD DATA LOCAL INFILE' query. Only absolute paths without relative components such as `..' and `.' are allowed.\n    - `{connect_timeout, Timeout}` - The maximum time to spend for connecting to the server\n    - `{log_warnings, boolean()}` - Whether to fetch warnings and log them using error_logger, default `true`.\n    - `{log_slow_queries, boolean()}` - Whether to log slow queries using error_logger; default false.\n    - `{keep_alive, boolean() | timeout()}` - Send ping when unused for a certain time. Possible values are `true`, `false` or `integer() \u003e 0` for an explicit interval in milliseconds. The default is `false`. For `true` a default ping timeout is used.\n    - `{prepare, NamedStatements}` - Named prepared statements to be created as soon as the connection is ready. Can be created also at runtime using `mysql_pool:prepare/3`\n    - `{queries, Queries}` - Queries to be executed as soon as the connection is ready. Any results are discarded. Typically, this is used for setting time zone and other session variables.\n    - `{query_timeout, Timeout}` - The default time to wait for a response when executing a query or a prepared statement. This can be given per query using `query/3,4` and `execute/4`. The default is `infinity`.\n    - `{found_rows, boolean()}` - If set to true, the connection will be established with CLIENT_FOUND_ROWS capability. affected_rows/1 will now return the number of found rows, not the number of rows changed by the query\n    - `{query_cache_time, Timeout}` - The minimum number of milliseconds to cache prepared statements used for parametrized queries with `query/3`.\n    - `{tcp_options, Options}` - Additional options for `gen_tcp:connect/3`. You may want to set `{recbuf, Size}` and `{sndbuf, Size}` if you send or receive more than the default (typically 8K) per query.\n    - `{ssl, Options}` - Additional options for `ssl:connect/3`.\n    - `{float_as_decimal, boolean() | non_neg_integer()}` - Encode floats as decimals when sending parameters for parametrized queries and prepared statements to the server. This prevents float rounding and truncation errors from happening on the server side. If a number is specified, the float is rounded to this number of decimals. Default is `{float_as_decimal, 8}`\n\nNote: Default value for `float_as_decimal` is `{float_as_decimal, 8}` instead `false` (the default from [mysql-otp](1)).\n\n### API\n\nThe `mysql-otp` documentation is available [here][3]. `mysql_connection` is only a wrapper around `mysql` interface from `mysql-otp`, It's accepting the same arguments and returns the values in the same way.\n\nAlso it provides few additional methods like: `query_opt/3, query_opt/4, query_opt/5` and `execute_opt/4, execute_opt/5` where the last argument (`OptionFlag`) is an atom argument that can take one for the following values:\n- affected_rows - will transform all `ok` results into `{ok, NumberOfAffectedRows}`\n- insert_id - will transform all `ok` results into `{ok, LastInsertId}`\n- both - will transform all `ok` results into `{ok, {NumberOfAffectedRows, LastInsertId}}`\n\nAnother difference is that all queries via `query/2`, `query/3`, `query/4` are done via non prepared statements. In the original `mysql-otp` in case\nthe params list is not empty the queries are executed via prepared statements where query is used as id. For same functionality you can use\n`p_query/3` and `p_query/4`.\n\nBeside this the `mysql_pool` comes with the following methods\n\n- `add_pool(PoolName, Size, MaxOverflow, ConnectionOptions)` - Add a pool at runtime with a specific size and connection options\n- `remove_pool(PoolName)` - Remove a pool at runtime\n- `prepare(PoolName, Stm, Query)` - Add a prepare statement for a pool. The statement will be added to all existing connections and also on all new connections that might be created in the future\n- `unprepare(PoolName, Stm)` - Remove a prepare statement from a pool. The statement will be unprepare on all existing connections as well.\n- `query/2, query/3, query/4` - Run a query over a connection in a specific pool\n- `execute/3, execute/4` - Run a prepare statement over a connection in a specific pool\n- `query_opt/3, query_opt/4, query_opt/5` - Run a query over a connection in a specific pool and returns additional info's for `ok` results as it's described above\n- `execute_opt/4, execute_opt/5` - Run a prepare statement over a connection in a specific pool and returns additional info's for `ok` results as it's described above\n- `transaction/2, transaction/3, transaction/4` - Start a transaction over a connection in a specified pool\n- `with(PoolName, Fun)` - Pick a connection from the pool `PoolName` and call the specified method using the connection as argument : `Fun(Pid)`.\n\n### Utils\n\n#### Converting Row Data To JSON\n\nUsing `mysql_utils:as_json` you are getting back a list of erlang terms in accordance with EEP-18. For actual utf8 binary JSON string you will need external library like [jsx][4] or [jiffy][5]\n\nExample:\n\n```erl\nResult = mysql_pool:query(pool1, \u003c\u003c\"select bar, baz from foo\"\u003e\u003e),\nJSON = mysql_utils:as_json(Result).\n#[[{\u003c\u003c\"bar\"\u003e\u003e,\u003c\u003c\"bar_value\"\u003e\u003e}, {\u003c\u003c\"baz\"\u003e\u003e,\u003c\u003c\"baz_value\"\u003e\u003e}], ...]\n```\n\n[1]:https://github.com/mysql-otp/mysql-otp\n[2]:https://github.com/devinus/poolboy\n[3]:https://mysql-otp.github.io/mysql-otp/index.html\n[4]:https://github.com/talentdeficit/jsx\n[5]:https://github.com/davisp/jiffy\n[6]:https://github.com/mysql-otp/mysql-otp/issues/52\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilviucpp%2Fmysql_pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilviucpp%2Fmysql_pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilviucpp%2Fmysql_pool/lists"}