{"id":17844376,"url":"https://github.com/winterland1989/mysql-haskell","last_synced_at":"2025-04-08T11:14:31.393Z","repository":{"id":41142700,"uuid":"63867011","full_name":"winterland1989/mysql-haskell","owner":"winterland1989","description":" Pure haskell mysql driver","archived":false,"fork":false,"pushed_at":"2024-12-18T00:27:49.000Z","size":8505,"stargazers_count":125,"open_issues_count":18,"forks_count":36,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-01T10:12:51.130Z","etag":null,"topics":["database","haskell","mysql","mysql-driver"],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/winterland1989.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","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-07-21T12:22:03.000Z","updated_at":"2024-12-20T07:51:39.000Z","dependencies_parsed_at":"2024-10-23T23:41:33.234Z","dependency_job_id":"a28b83ec-4827-4be0-82ef-7f536133c20c","html_url":"https://github.com/winterland1989/mysql-haskell","commit_stats":{"total_commits":127,"total_committers":10,"mean_commits":12.7,"dds":0.1889763779527559,"last_synced_commit":"d93caffe0991e1090a3854acff355ff0ea6075c7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterland1989%2Fmysql-haskell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterland1989%2Fmysql-haskell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterland1989%2Fmysql-haskell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterland1989%2Fmysql-haskell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/winterland1989","download_url":"https://codeload.github.com/winterland1989/mysql-haskell/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247829512,"owners_count":21002997,"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":["database","haskell","mysql","mysql-driver"],"created_at":"2024-10-27T21:30:18.962Z","updated_at":"2025-04-08T11:14:31.376Z","avatar_url":"https://github.com/winterland1989.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"mysql-haskell\n=============\n\n[![Hackage](https://img.shields.io/hackage/v/mysql-haskell.svg?style=flat)](http://hackage.haskell.org/package/mysql-haskell)\n\n`mysql-haskell` is a MySQL driver written entirely in haskell.\n\n\u003ca href=\"http://chordify.net/\"\u003e\u003cimg height=42 src='https://chordify.net/img/about/slide_250_1.jpg'\u003e\u003c/a\u003e\n\nIs it fast?\n----------\n\nIn short, `select`(decode) is about 1.5 times slower than pure c/c++ but 5 times faster than `mysql-simple`, `insert` (encode) is about 1.5 times slower than pure c/c++, there're many factors involved(tls, prepared statment, batch using multiple statement):\n\n\u003cimg src=\"https://github.com/jappeace/mysql-pure/blob/master/benchmark/result.png?raw=true\" width=\"100%\"\u003e\n\nAbove figures showed the time to:\n\n* perform a \"select * from employees\" from a [sample table](https://github.com/datacharmer/test_db)\n* insert 1000 rows into a 29-columns table per thread with auto-commit off.\n\nThe benchmarks are run by my MacBook Pro 13' 2015.\n\nMotivation\n----------\n\nWhile MySQL may not be the most advanced sql database, it's widely used among China companies, including but not limited to Baidu, Alibaba, Tecent etc., but haskell's MySQL support is not ideal, we only have a very basic MySQL binding written by Bryan O'Sullivan, and some higher level wrapper built on it, which have some problems:\n\n+ lack of prepared statment and binary protocol support.\n\n+ limited concurrency due to FFI.\n\n+ no replication protocol support.\n\n`mysql-pure` is intended to solve these problems, and provide foundation for higher level libraries such as groundhog and persistent, so that accessing MySQL is both fast and easy in haskell.\n\nGuide\n-----\n\nThe `Database.MySQL.Base` module provides everything you need to start making queries:\n\n```haskell\n{-# LANGUAGE OverloadedStrings #-}\n\nmodule Main where\n\nimport Database.MySQL.Base\nimport qualified System.IO.Streams as Streams\n\nmain :: IO () \nmain = do\n    conn \u003c- connect\n        defaultConnectInfo {ciUser = \"username\", ciPassword = \"password\", ciDatabase = \"dbname\"}\n    (defs, is) \u003c- query_ conn \"SELECT * FROM some_table\"\n    print =\u003c\u003c Streams.toList is\n```\n\n`query/query_` will return a column definition list, and an `InputStream` of rows, you should consume this stream completely before start new queries.\n\nIt's recommanded to use prepared statement to improve query speed:\n\n```haskell\n    ...\n    s \u003c- prepareStmt conn \"SELECT * FROM some_table where person_age \u003e ?\"\n    ...\n    (defs, is) \u003c- queryStmt conn s [MySQLInt32U 18]\n    ...\n```\n\nIf you want to do batch inserting/deleting/updating, you can use `executeMany` to save considerable time.\n\nThe `Database.MySQL.BinLog` module provides binlog listenning functions and row-based event decoder, following program will automatically get last binlog position, and print every row event it receives:\n\n```haskell\n{-# LANGUAGE LambdaCase #-}\nmodule Main where\n\nimport           Control.Monad         (forever)\nimport qualified Database.MySQL.BinLog as MySQL\nimport qualified System.IO.Streams     as Streams\n\nmain :: IO () \nmain = do\n    conn \u003c- MySQL.connect \n        MySQL.defaultConnectInfo\n          { MySQL.ciUser = \"username\"\n          , MySQL.ciPassword = \"password\"\n          , MySQL.ciDatabase = \"dbname\"\n          }\n    MySQL.getLastBinLogTracker conn \u003e\u003e= \\ case\n        Just tracker -\u003e do\n            es \u003c- MySQL.decodeRowBinLogEvent =\u003c\u003c MySQL.dumpBinLog conn 1024 tracker False\n            forever $ do\n                Streams.read es \u003e\u003e= \\ case\n                    Just v  -\u003e print v\n                    Nothing -\u003e return ()\n        Nothing -\u003e error \"can't get latest binlog position\"\n```\n\nBuild Test Benchmark\n--------------------\n\nJust use the old way:\n\n```bash\ngit clone https://github.com/winterland1989/mysql-pure.git\ncd mysql-pure\ncabal install --enable-tests --only-dependencies\ncabal build\n```\n\nRunning tests require:\n\n* A local MySQL server, a user `testMySQLHaskell` and a database `testMySQLHaskell`, you can do it use following script:\n\n```bash\nmysql -u root -e \"CREATE DATABASE IF NOT EXISTS testMySQLHaskell;\"\nmysql -u root -e \"CREATE USER 'testMySQLHaskell'@'localhost' IDENTIFIED BY ''\"\nmysql -u root -e \"GRANT ALL PRIVILEGES ON testMySQLHaskell.* TO 'testMySQLHaskell'@'localhost'\"\nmysql -u root -e \"FLUSH PRIVILEGES\"\n```\n\n* Enable binlog by adding `log_bin = filename` to `my.cnf` or add `--log-bin=filename` to the server, and grant replication access to `testMySQLHaskell` with:\n\n```bash\nmysql -u root -e \"GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'testMySQLHaskell'@'localhost';\"\n```\n\n* Set `binlog_format` to `ROW`.\n\n* Set `max_allowed_packet` to larger than 256M(for test large packet).\n\nNew features will be automatically tested by inspecting MySQL server's version, travis is keeping an eye on following combinations:\n\n+ CABALVER=1.18 GHCVER=7.8.4  MYSQLVER=5.5\n+ CABALVER=1.22 GHCVER=7.10.2 MYSQLVER=5.5\n+ CABALVER=1.24 GHCVER=8.0.1  MYSQLVER=5.5\n+ CABALVER=1.24 GHCVER=8.0.1  MYSQLVER=5.6\n+ CABALVER=1.24 GHCVER=8.0.1  MYSQLVER=5.7\n\nPlease reference `.travis.yml` if you have problems with setting up test environment.\n\nEnter benchmark directory and run `./bench.sh` to benchmark 1) c++ version 2) mysql-pure 3) FFI version mysql, you may need to:\n\n+ Modify `bench.sh`(change the include path) to get c++ version compiled.\n+ Modify `mysql-pure-bench.cabal`(change the openssl's lib path) to get haskell version compiled.\n+ Setup MySQL's TLS support, modify `MySQLHaskellOpenSSL.hs/MySQLHaskellTLS.hs` to change the CA file's path, and certificate's subject name.\n+ Adjust rts options `-N` to get best results.\n\nWith `-N10` on my company's 24-core machine, binary protocol performs almost identical to c version!\n\nReference\n---------\n\n[MySQL official site](https://dev.mysql.com/doc/internals/en/) provided intensive document, but without following project, `mysql-pure` may not be written at all:\n\n+ [mysql-binlog-connector-java](https://github.com/shyiko/mysql-binlog-connector-java)\n\n+ [canal](https://github.com/alibaba/canal)\n\n+ [go mysql toolkit](https://github.com/siddontang/go-mysql)\n\n+ [python binlog parser](https://github.com/noplay/python-mysql-replication)\n\nLicense\n-------\n\nCopyright (c) 2016, winterland1989\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n    * Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n\n    * Redistributions in binary form must reproduce the above\n      copyright notice, this list of conditions and the following\n      disclaimer in the documentation and/or other materials provided\n      with the distribution.\n\n    * Neither the name of winterland1989 nor the names of other\n      contributors may be used to endorse or promote products derived\n      from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwinterland1989%2Fmysql-haskell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwinterland1989%2Fmysql-haskell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwinterland1989%2Fmysql-haskell/lists"}