Skip to content

Commit

Permalink
Bump 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteclover committed Sep 26, 2017
1 parent a7c13fe commit 3d9ce85
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Fork or download it, then run:
Compatibility
=============

Built and tested under Python 2.7
Built and tested under Python 2.7+


DB API
Expand All @@ -100,7 +100,7 @@ Have a look:
}
db.setup(config, minconn=5, maxconn=10,
adapter='mysql', key='defalut', slave=False)
adapter='mysql', key='default', slave=False)
db.execute('show tables')
Expand All @@ -111,7 +111,7 @@ setup

:config: the connection basic config, the all of arguements of MySQLDB#connect is acceptable。 the ``max_idle`` is the connect timeout setting that is used to reconnection when connection is timeout, default is 10 seconds.
:minconn: the minimum connections for the connection pool, default is 5.
:maxconn: the maximum connections for the connection pool, defalut is 10.
:maxconn: the maximum connections for the connection pool, default is 10.
:adapter: the database driver adapter name, currently supports mysql (MySQLdb, pymysql) only.
:key: the database idenfify for database, default database is "default"
:slave: if set to true, the database will be register as a slave database. make sure you setup a master firstly.
Expand Down
6 changes: 3 additions & 3 deletions README_CN.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Development
Compatibility
=============

在 Python 2.7.x 测试开发
在 Python 2.7+ 测试开发

DB API
========
Expand All @@ -84,7 +84,7 @@ DB API
}
db.setup(config, minconn=5, maxconn=10,
adapter='mysql', key='defalut', slave=False)
adapter='mysql', key='default', slave=False)
Expand All @@ -94,7 +94,7 @@ setup
:config: 是数据库连接参数,可以传入MySQLDB#connect接口中所有的可选参数。 其中``max_idle`` 相对是mysql服务端 connect_timeout配置,默认10秒。
:minconn: 为当前数据库连接池保持最小连接池,默认为5
:maxconn: 为当前数据库连接池最大连接池,默认为10
:adapter: 为适配器名,当前只支持 mysql
:adapter: 为适配器名,当前支持 mysql和pymsql
:key: 是数据库的标识符,默认为 default
:slave: 如果为true那么当前的数据库将会注册为读数据库。如果你没有做读写分离,只有一个数据库用来读写,那么setup一次就好,这样就可以读写。

Expand Down
59 changes: 51 additions & 8 deletions db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

__version__ = '0.1.2'
__version__ = '0.1.3'
VERSION = tuple(map(int, __version__.split('.')))


Expand Down Expand Up @@ -52,6 +52,17 @@ def or_():


def setup(config, minconn=5, maxconn=10, adapter='mysql', key='default', slave=False):
"""Setup database
:param config dict: is the db adapter config
:param key string: the key to identify dabtabase
:param adapter string: the dabtabase adapter current support mysql only
:param minconn int: the min connection for connection pool
:param maxconn int: the max connection for connection pool
:param slave boolean: If True the database can be read only.
"""
global __db

if '.' in key:
Expand Down Expand Up @@ -84,21 +95,27 @@ def query(sql, args=None, many=None, as_dict=False, key='default'):
"""The connection raw sql query, when select table, show table
to fetch records, it is compatible the dbi execute method::
args::
sql string: the sql stamtement like 'select * from %s'
args maybe list: Wen set None, will use dbi execute(sql), else
:param sql string: the sql stamtement like 'select * from %s'
:param args list: Wen set None, will use dbi execute(sql), else
dbi execute(sql, args), the args keep the original rules, it shuld be tuple or list of list
many maybe int: when set, the query method will return genarate an iterate
as_dict bool: when is true, the type of row will be dict, otherwise is tuple
key: a key for your dabtabase you wanna use
:param many int: when set, the query method will return genarate an iterate
:param as_dict bool: when is True, the type of row will be dict, otherwise is tuple
:param key: a key for your dabtabase you wanna use
"""
database = choice(__db[key + '.slave'])
return database.query(sql, args, many, as_dict)


def execute(sql, args=None, key='default'):
"""It is used for update, delete records::
"""It is used for update, delete records.
:param sql string: the sql stamtement like 'select * from %s'
:param args list: Wen set None, will use dbi execute(sql), else
dbi execute(sql, args), the args keep the original rules, it shuld be tuple or list of list
:param key: a key for your dabtabase you wanna use
eg::
execute('insert into users values(%s, %s)', [(1L, 'blablabla'), (2L, 'animer')])
execute('delete from users')
Expand All @@ -108,31 +125,57 @@ def execute(sql, args=None, key='default'):


def transaction(key='default'):
"""transaction wrapper
:param key: a key for your dabtabase you wanna use
"""
database = __db[key]
return database.transaction()


def select(table, key='default'):
"""Select dialect
:param key: a key for your dabtabase you wanna use
"""
database = choice(__db[key + '.slave'])
return database.select(table)


def insert(table, key='default'):
"""insert dialect
:param key: a key for your dabtabase you wanna use
"""
database = __db[key]
return database.insert(table)


def update(table, key='default'):
"""update dialect
:param key: a key for your dabtabase you wanna use
"""
database = __db[key]
return database.update(table)


def delete(table, key='default'):
"""delete dialect
:param key: a key for your dabtabase you wanna use
"""
database = __db[key]
return database.delete(table)


def database(key='default', slave=False):
"""datbase dialect
:param key: a key for your dabtabase you wanna use
:param slave boolean: If True the database can be read only, Defaults False.
"""
if slave:
key += '.slave'
return choice(__db[key])
Expand Down
33 changes: 22 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
#!/usr/bin/env python

from setuptools import setup, find_packages
import sys
from db import __version__
from codecs import open

with open('README.rst', 'r', 'utf-8') as f:
readme = f.read()

setup(
name='dbpy',
version=__version__,
author="Thomas Huang",
author_email='lyanghwy@gmail.com',
description="database abstraction layer for pythoneer",
long_description=readme,
license="GPL",
keywords="database abstraction layer for pythoneer(orm, database)",
url='https://github.com/thomashuang/dbpy',
url='https://github.com/whiteclover/dbpy',
packages=find_packages(exclude=['samples', 'tests*']),
zip_safe=False,
include_package_data=True,
install_requires=['setuptools'],
test_suite='unittests',
classifiers=(
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Operating System :: OS Independent",
"Topic :: Database"
)
test_suite='unittest',
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: GNU Affero General Public License v3',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Operating System :: OS Independent',
'Topic :: Database'
]
)

0 comments on commit 3d9ce85

Please sign in to comment.