Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created September 5, 2009 02:37
Show Gist options
  • Select an option

  • Save ngerakines/181264 to your computer and use it in GitHub Desktop.

Select an option

Save ngerakines/181264 to your computer and use it in GitHub Desktop.
This is my ideal erlang MySQL client.
High Level
* Groups are process based and multiple group leaders can be spawned and named
* Supports many connections
* Connections (and groups of connections) can be named
* Supports SQL generation
* Uses records to represent statements and responses
Module layout
* emysql
-- Core application module and base functionality
* emysql_connection
-- Handles raw connection/socket function to/from mysql server
* emysql_statement
-- Handles statement building and parsing.
* emysql_pool
-- Handles group manipulation
An example use case:
{ok, IPWGroup} = emysql:start_group('iplaywow_master'), %% IPWGroup = pid()
ok = emysql:set_options(IPWGroup, [{reconnect, true}, {log_level, warning}]),
Connection = {"127.0.0.1", "TheDatabase", "TheUser", "ThePassword", "TheEncoding"},
ok = emysql:add_connection(IPWGroup, Connection),
...
ok = emysql:prepare(IPWGroup, <<"characters_by_owner">>, <<"SELECT * FROM ...">>),
ok = emysql:prepare(IPWGroup, <<"characters_all">>, <<"SELECT * FROM ...">>, [{'post-process', fun package_character/1}]),
Results = emysql:execute(IPWGroup, <<"characters_by_owner">>, [], [{timeout, 5000}, {'post-process', fun package_character/1}]), %% pid(), binary(), [term()], [term()]
Results2 = emysql:do(IPWGroup, <<"UPDATE atable ...">>, [{timeout, 5000}]), %% pid(), binary(), [term()]
... a little more detail:
* The actual `emysql` module doesn't manage a specific process or maintain any kind of state. It's just a module that is used to interact with groups and connections.
* When a group is started, it is given an explicit name and a pid that handles the internal functionally of connection group management is spawned and the name registered to that process.
* Groups can have options like the log level and if it should reconnect connections that are killed or die.
* Prepared statements can be given optional defaults like 'post-process'. The post-process function is applied to each row of the data-set returned by that function.
Things that would be nice to get back in `Results`
* The execution time of the request
* The connection that it was executed on
* The noonce of the execution, aka the query sequence id
* The number of rows affected (if applicable)
* The id created (if applicable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment