Skip to content

Commit

Permalink
Merge branch 'mysqlclient' into 'master'
Browse files Browse the repository at this point in the history
Version 2.1 - Mysqlclient (MySQLdb) support added

See merge request mastizada/dbConnect!19
  • Loading branch information
mastizada committed Jun 14, 2018
2 parents b927738 + f117682 commit 6bbab9a
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 50 deletions.
14 changes: 14 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test:
services:
- mysql:5.7
variables:
MYSQL_DATABASE: dbconnect_test
MYSQL_ROOT_PASSWORD: mysqlpass
CI_ENV: Gitlab
script:
- apt-get update -qy
- apt-get install -y python3-dev python3-pip mysql-client default-libmysqlclient-dev
- pip3 install -r requirements.txt
- pip3 install mysqlclient
- "./test/mysql-ci-setup -hmysql -uroot -pmysqlpass"
- python3 tests.py
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ language: python
addons:
apt:
packages:
- python-mysql.connector
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6
python:
- "2.7"
- "3.6"
services:
- mysql
install:
- pip install -r requirements.txt
- pip install mysqlclient
before_script:
- "./travis-ci-setup -h127.0.0.1 -uroot"
- "./test/mysql-ci-setup -h127.0.0.1 -uroot"
virtualenv:
system_site_packages: true
script: python tests.py
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@
* Added SUM function
* Added support for Postgresql
* Minor fixes

** v 2.1 **:

* Addded `mysqlclient` as supported libraries for mysql
* Flake8 added for PEP8 syntax check
* Changed Github repo to mirror and moved main repo to Gitlab
10 changes: 8 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Installation
=============
requirements:
^^^^^^^^^^^^^
dbConnect uses mysql.connector module for mysql, install it using:
dbConnect uses `mysql.connector` and `mysqlclient` modules for mysql, install **one** of them using:

.. code-block:: bash
Expand All @@ -33,6 +33,12 @@ dbConnect uses mysql.connector module for mysql, install it using:
Or using offical site: `https://dev.mysql.com/downloads/connector/python/`

.. code-block:: bash
$ pip install mysqlclient
More details on `mysqlclient`: `https://github.com/PyMySQL/mysqlclient-python`

For PostgreSQL install psycopg2 module:

.. code-block:: bash
Expand All @@ -51,7 +57,7 @@ from source code:

.. code-block:: bash
$ git clone git@github.com:mastizada/dbConnect.git
$ git clone git@gitlab.com:mastizada/dbConnect.git
$ cd dbConnect
$ sudo python setup.py install
Expand Down
2 changes: 1 addition & 1 deletion dbConnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__description__ = 'Database for Humans'
__author__ = "Emin Mastizada <emin@linux.com>"
__version__ = '2.0'
__version__ = '2.1'
__license__ = "MPL 2.0"
__help__ = """Database for Humans"""

Expand Down
40 changes: 28 additions & 12 deletions dbConnect/dbConnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class DBConnect:
"""Light database connection object."""
settings = {}

def _check_settings(self):
"""
Check configuration file
Expand All @@ -31,23 +32,37 @@ def connect(self):
if that happens you can use this function to reconnect to database
"""
if self.engine == "mysql":
found_connector = False
try:
import mysql.connector # MySQL Connector
# Import official mysql connector if exists
import mysql.connector as mysql_module
from mysql.connector import errorcode
self.connection = mysql.connector.connect(**self.settings)
found_connector = True
except ImportError:
raise ValueError(
'Please, install mysql-connector module before using plugin.'
)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
raise ValueError("Wrong credentials, ACCESS DENIED")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
pass
if not found_connector:
# Check MySQLdb as secondary option
try:
import MySQLdb as mysql_module
except ImportError:
raise ValueError(
"Database %s does not exists" % (self.settings['database'])
'Please, install mysql-connector or mysqlclient module before using this library.'
)
else:
raise ValueError(err)
# Connect to db
try:
self.connection = mysql_module.connect(**self.settings)
except mysql_module.Error as err:
if found_connector:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
raise ValueError("Wrong credentials, ACCESS DENIED")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
raise ValueError(
"Database %s does not exists" % (self.settings['database'])
)
else:
raise ValueError(err)
# @TODO Add detailed errors for MySQLdb
raise err
elif self.engine == "postgres":
try:
import psycopg2
Expand Down Expand Up @@ -397,5 +412,6 @@ def commit(self):
"""
self.connection.commit()


if __name__ == '__main__':
pass
17 changes: 6 additions & 11 deletions docs/_templates/sidebarlogo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
<img class="logo" src="{{ pathto('_static/dbConnect-logo.png', 1) }}" title="dbConnect Logo"/>
</a>
</p>
<p>
<iframe src="http://ghbtns.com/github-btn.html?user=mastizada&repo=dbConnect&type=watch&count=true&size=large" allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>

<p>
<b>dbConnect</b> is Light MySQL and PostgreSQL Database Module. If ORM is too big for your project and MySQL module looks ugly and difficult then dbConnect is for you.
</p>

<p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="GMKFYKH7LFWY6">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - Donate!" style="border: 0px !important">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<pre>
ETH: 0x9d54Ce8B65Cafe15F00F37C8FCC02e9D999F1246
BTC: 3LwMKkbedm4bEAP6rkQvA17kKK7RWBKrnC
</pre>
</p>

<h3>Source Code</h3>
<p>Its just beginning, make suggestions and contribute to the project...</p>
<p>dbConnect is Free & Open Source Software and I would love your ideas...</p>

<p><a href="https://github.com/mastizada/dbConnect">Github Repo</a></p>
<p><a href="https://gitlab.com/mastizada/dbConnect">Gitlab Repo</a></p>
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import sys
import os
import shlex
__version__ = '2.0'
__version__ = '2.1'

sys.path.insert(0, os.path.abspath('../dbConnect'))

Expand Down
12 changes: 7 additions & 5 deletions docs/user/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ This part of the documentation covers the installation of dbConnect.
Requirements
------------

dbConnect uses mysql.connector for mysql, install it using::
dbConnect uses `mysql.connector` and `mysqlclient` for mysql, install **one** of them using::

$ apt-get install python3-mysql.connector
$ apt-get install python-mysql.connector

$ pip install mysqlclient

For PostgreSQL install psycopg2 module::

$ pip install psycopg2
Expand All @@ -36,14 +38,14 @@ But, you really `shouldn't do that <https://stackoverflow.com/questions/3220404/
Get the Code
------------

dbConnect is actively developed on GitHub, where the code is
`always available <https://github.com/mastizada/dbConnect>`_.
dbConnect is actively developed on Gitlab, where the code is
`always available <https://gitlab.com/mastizada/dbConnect>`_.

You can either clone the public repository::

$ git clone git@github.com:mastizada/dbConnect.git
$ git clone git@gitlab.com:mastizada/dbConnect.git

Once you have a copy of the source, you can embed it in your Python package,
or install it into your site-packages easily::
or install it into your site-packages using::

$ python setup.py install
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[flake8]
max-line-length = 119
exclude = .git,__pycache__,docs/conf.py,old,build,dist,venv,dbConnect.egg-info,.tox
[bdist_wheel]
universal = 1
12 changes: 8 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

setup(
name='dbConnect',
version='2.0',
version='2.1',
description='Database for Humans',
long_description=readme,
keywords='dbConnect, mysql, postgresql, postgres, simple, easy, light, module',
url='https://github.com/mastizada/dbConnect',
keywords='dbConnect, mysql, postgresql, postgres, simple, easy, light, module, mysqlclient',
url='https://gitlab.com/mastizada/dbConnect',
author='Emin Mastizada',
author_email='emin@linux.com',
license='MPLv2',
Expand All @@ -22,7 +22,6 @@
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'Operating System :: OS Independent',
'Intended Audience :: Developers',
'Natural Language :: English',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
Expand All @@ -37,5 +36,10 @@
'Environment :: Plugins',
],
scripts=['dbConnect/dbConnect.py'],
project_urls={
"Bug Reports": "https://gitlab.com/mastizada/dbConnect/issues",
"Source": "https://gitlab.com/mastizada/dbConnect",
"Say Thanks!": "https://saythanks.io/to/mastizada"
},
zip_safe=False
)
7 changes: 7 additions & 0 deletions test/gitlab_credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"host": "mysql",
"user": "root",
"password": "mysqlpass",
"database": "dbconnect_test",
"port": 3306
}
File renamed without changes.
File renamed without changes.
28 changes: 16 additions & 12 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@

from unittest import TestCase, main
from dbConnect import DBConnect
import os

USERS = [
{'name': 'Linus Torvals', 'email': 'linux@test.local'},
{'name': 'Guido van Rossum', 'email': 'python@test.local'},
{'name': 'Kenneth Reitz', 'email': 'requests@test.local'},
]

class dbTest(TestCase):

class DBTest(TestCase):
def setUp(self):
"""Prepare for Test."""
self.database = DBConnect('travis_credentials.json')
environment = os.environ.get('CI_ENV', "local")
if environment == "local":
self.database = DBConnect('test/test_credentials.json')
elif environment == "Gitlab":
self.database = DBConnect('test/gitlab_credentials.json')
elif environment == "Travis":
self.database = DBConnect('test/travis_credentials.json')

def tearDown(self):
"""Finish Testing."""
Expand All @@ -31,15 +39,13 @@ def test_insert(self):
'views': 6,
}
result = self.database.insert(new_user, 'test')
self.assertTrue(result["status"],
"Insert Failed with message %s" % result["message"])
self.assertTrue(result["status"], "Insert Failed with message %s" % result["message"])

def test_commit(self):
"""Test committing all users at once."""
for user in USERS:
result = self.database.insert(user, 'test', commit=False)
self.assertTrue(result["status"],
"Insert Failed with message %s" % result["message"])
self.assertTrue(result["status"], "Insert Failed with message %s" % result["message"])
self.database.commit()
# Now there should be 3 users in table with views=0
result = self.database.fetch(
Expand All @@ -49,23 +55,21 @@ def test_commit(self):
)
self.assertTrue(len(result), "Fetch returned empty results")
if len(result):
self.assertTrue('count(id)' in result[0].keys(),
"Requested field 'count(id)' missing in result keys")
self.assertTrue('count(id)' in result[0].keys(), "Requested field 'count(id)' missing in result keys")
if 'count(id)' in result[0].keys():
self.assertEqual(result[0]['count(id)'], 3,
"Number of new users in table should be 3")
self.assertEqual(result[0]['count(id)'], 3, "Number of new users in table should be 3")

def test_fetch(self):
"""Test fetching information from database."""
pass

def test_sum(self):
"""Test value_sum functionality."""
counter=1
counter = 1
for user in USERS[:3]:
user['views'] = counter
self.database.insert(user, 'test')
counter+=1
counter += 1
sum_result = self.database.value_sum(
'test',
fields=['views']
Expand Down

0 comments on commit 6bbab9a

Please sign in to comment.