Compare commits
No commits in common. 'main' and '3adc9ebfdf382482d88e319bcf7d8a2c4ce84d74' have entirely different histories.
main
...
3adc9ebfdf
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1 @@
|
||||
futool-db-lite
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Pipenv (futool-db-lite)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
<option name="format" value="PLAIN" />
|
||||
<option name="myDocStringFormat" value="Plain" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,22 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredErrors">
|
||||
<list>
|
||||
<option value="N813" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredIdentifiers">
|
||||
<list>
|
||||
<option value="object.transform" />
|
||||
<option value="object.cluster_centers_" />
|
||||
<option value="object.labels_" />
|
||||
<option value="nodes" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Pipenv (futool-db-lite)" project-jdk-type="Python SDK" />
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/../futool-db-lite/.idea/futool-db-lite.iml" filepath="$PROJECT_DIR$/../futool-db-lite/.idea/futool-db-lite.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -1,9 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,12 @@
|
||||
[[source]]
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[packages]
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[requires]
|
||||
python_version = "3.8"
|
||||
python_full_version = "3.8.10"
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 19:58
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 19:58
|
||||
# @Author : old tom
|
||||
# @File : db_config.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
||||
import os
|
||||
from pydantic import BaseModel
|
||||
import toml
|
||||
|
||||
# 默认配置文件名
|
||||
DEFAULT_CONF_NAME = 'db-conf.toml'
|
||||
path = os.path.dirname(__file__)
|
||||
path = os.path.dirname(path)
|
||||
# 默认配置文件位置
|
||||
DEFAULT_CONF_PATH = os.path.join(path, DEFAULT_CONF_NAME)
|
||||
# 配置项名称
|
||||
DB_CONF_ITEM_NAME = ['dialect', 'host', 'port', 'user', 'passwd', 'database']
|
||||
|
||||
|
||||
class DbConfigNotFoundError(Exception):
|
||||
"""
|
||||
配置不存在异常
|
||||
"""
|
||||
|
||||
def __init__(self, msg):
|
||||
Exception.__init__(self, msg)
|
||||
|
||||
|
||||
class DbConf(BaseModel):
|
||||
dialect: str
|
||||
host: str
|
||||
port: int
|
||||
user: str
|
||||
passwd: str
|
||||
database: str
|
||||
pool_size: int
|
||||
pool_recycle: int
|
||||
show_sql: bool = False
|
||||
|
||||
|
||||
class DbConfigLoader(object):
|
||||
"""
|
||||
数据库连接加载
|
||||
"""
|
||||
|
||||
def __init__(self, db_name, conf_path):
|
||||
if not os.path.isfile(conf_path):
|
||||
raise DbConfigNotFoundError(f'数据配置文件{DEFAULT_CONF_NAME}不存在')
|
||||
self.db_conf = self.load(db_name, conf_path)
|
||||
|
||||
@staticmethod
|
||||
def load(dbname, conf_path) -> DbConf:
|
||||
"""
|
||||
校验并加载配置
|
||||
:return:
|
||||
"""
|
||||
conf = toml.load(conf_path)
|
||||
return DbConf(**conf[dbname])
|
@ -0,0 +1,19 @@
|
||||
[testdb]
|
||||
# 方言
|
||||
dialect = 'postgresql'
|
||||
# ip
|
||||
host = '127.0.0.1'
|
||||
# 端口
|
||||
port = 5432
|
||||
# 用户名
|
||||
user = 'postgres'
|
||||
# 密码
|
||||
passwd = 'root@123'
|
||||
# 数据库
|
||||
database = 'postgres'
|
||||
# 连接池大小
|
||||
pool_size = 15
|
||||
# 连接超时回收(秒)
|
||||
pool_recycle = 3600
|
||||
# 打印SQL
|
||||
show_sql = true
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 21:55
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 21:55
|
||||
# @Author : old tom
|
||||
# @File : sql_executor.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc : 获取游标,执行SQL
|
||||
from sqlalchemy import text, CursorResult
|
||||
|
||||
from transaction.connect_transaction import Transaction
|
||||
|
||||
|
||||
class SQLExecutorError(Exception):
|
||||
def __init__(self, msg):
|
||||
Exception.__init__(self, msg)
|
||||
|
||||
|
||||
class SQLExecutor(object):
|
||||
def __init__(self, tx: Transaction):
|
||||
self._tx = tx
|
||||
self._conn = tx.get_connection()
|
||||
|
||||
@staticmethod
|
||||
def _format_sql(sql):
|
||||
return text(sql)
|
||||
|
||||
def query(self, sql) -> CursorResult:
|
||||
try:
|
||||
return self._conn.execute(self._format_sql(sql))
|
||||
except Exception as e:
|
||||
raise SQLExecutorError(msg=f'{e}')
|
||||
|
||||
def execute_update(self, sql) -> int:
|
||||
try:
|
||||
self._tx.begin_transaction()
|
||||
rt = self._conn.execute(self._format_sql(sql))
|
||||
self._tx.commit()
|
||||
return rt.rowcount
|
||||
except Exception as e:
|
||||
self._tx.rollback()
|
||||
raise SQLExecutorError(msg=f'{e}')
|
||||
|
||||
def get_connection(self):
|
||||
return self._conn
|
||||
|
||||
def get_transaction(self):
|
||||
return self._tx
|
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 21:55
|
||||
# @Author : old tom
|
||||
# @File : main.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc : 数据库测试
|
||||
from session.engine.dbengine import DatabaseEngine
|
||||
from session.engine.dbengine import container
|
||||
|
||||
if __name__ == '__main__':
|
||||
db_engine = DatabaseEngine('testdb')
|
||||
db_engine2 = DatabaseEngine('testdb')
|
||||
print(db_engine.engine is db_engine2.engine)
|
||||
session_factory = db_engine.create_session_factory()
|
||||
session_factory2 = db_engine2.create_session_factory()
|
||||
print(session_factory is session_factory2)
|
||||
# for i in range(100):
|
||||
# # sess = session_factory.create_session()
|
||||
# sess = session_factory.open_session()
|
||||
# print(id(sess))
|
||||
print(container.engines, container.factory)
|
Binary file not shown.
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 19:52
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 19:53
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 19:53
|
||||
# @Author : old tom
|
||||
# @File : session.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
||||
import threading
|
||||
from sqlalchemy import Engine
|
||||
from executor.sql_executor import SQLExecutor
|
||||
from transaction.connect_transaction import TransactionFactory
|
||||
|
||||
|
||||
class CreateSessionError(Exception):
|
||||
def __init__(self, msg):
|
||||
Exception.__init__(self, msg)
|
||||
|
||||
|
||||
class SqlSessionCache(object):
|
||||
"""
|
||||
利用thread-local 实现线程隔离及单个线程获取到的session一致
|
||||
"""
|
||||
|
||||
def __init__(self, create_session_func):
|
||||
self.create_session_func = create_session_func
|
||||
self.cache = threading.local()
|
||||
|
||||
def __call__(self):
|
||||
try:
|
||||
if self.has_session():
|
||||
return self.cache.value
|
||||
else:
|
||||
val = self.cache.value = self.create_session_func()
|
||||
return val
|
||||
except Exception as e:
|
||||
raise CreateSessionError(msg=f'从cache获取session异常,e={e}')
|
||||
|
||||
def put_session(self, session):
|
||||
self.cache.value = session
|
||||
|
||||
def has_session(self) -> bool:
|
||||
return hasattr(self.cache, "value")
|
||||
|
||||
def remove_session(self):
|
||||
try:
|
||||
del self.cache.value
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
class SqlsessionFactory(object):
|
||||
"""
|
||||
工厂模式创建sqlSession
|
||||
"""
|
||||
|
||||
def __init__(self, engine: Engine):
|
||||
self._engine = engine
|
||||
self.cache = SqlSessionCache(self.create_session)
|
||||
|
||||
def open_session(self):
|
||||
"""
|
||||
从缓存获取,如果没有会自动调用create_session创建
|
||||
"""
|
||||
return self.cache()
|
||||
|
||||
def create_session(self):
|
||||
try:
|
||||
conn = self._engine.connect()
|
||||
tx_factory = TransactionFactory(conn)
|
||||
tx = tx_factory.create_transaction()
|
||||
executor = SQLExecutor(tx)
|
||||
return Sqlsession(executor, self.cache)
|
||||
except Exception as e:
|
||||
raise CreateSessionError(msg=f'创建sqlSession异常,e={e}')
|
||||
|
||||
|
||||
class Sqlsession(object):
|
||||
def __init__(self, executor: SQLExecutor, session_cache: SqlSessionCache):
|
||||
self.executor = executor
|
||||
self.session_cache = session_cache
|
||||
|
||||
def select_one(self, sql):
|
||||
return self.executor.query(sql).fetchone()
|
||||
|
||||
def select_many(self, sql, size):
|
||||
return self.executor.query(sql).fetchmany(size)
|
||||
|
||||
def select_all(self, sql):
|
||||
return self.executor.query(sql).fetchall()
|
||||
|
||||
def insert(self, sql):
|
||||
return self.executor.execute_update(sql)
|
||||
|
||||
def insert_batch(self, sql):
|
||||
"""
|
||||
todo implement
|
||||
"""
|
||||
pass
|
||||
|
||||
def update(self, sql):
|
||||
return self.executor.execute_update(sql)
|
||||
|
||||
def delete(self, sql):
|
||||
return self.executor.execute_update(sql)
|
||||
|
||||
def begin_transaction(self):
|
||||
"""
|
||||
开启事务
|
||||
:return:
|
||||
"""
|
||||
self.executor.get_transaction().begin_transaction()
|
||||
|
||||
def commit(self):
|
||||
"""
|
||||
提交
|
||||
:return:
|
||||
"""
|
||||
self.executor.get_transaction().commit()
|
||||
|
||||
def rollback(self):
|
||||
"""
|
||||
回滚
|
||||
:return:
|
||||
"""
|
||||
self.executor.get_transaction().rollback()
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
关闭连接
|
||||
:return:
|
||||
"""
|
||||
self.executor.get_connection().close()
|
||||
self.session_cache.remove_session()
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 21:35
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/22 21:37
|
||||
# @Author : old tom
|
||||
# @File : connect_transaction.py
|
||||
# @Project : futool-db-lite
|
||||
# @Desc :
|
||||
from sqlalchemy import Connection
|
||||
|
||||
|
||||
class TransactionFactory(object):
|
||||
|
||||
def __init__(self, conn: Connection):
|
||||
self.conn = conn
|
||||
|
||||
def create_transaction(self):
|
||||
return Transaction(self.conn)
|
||||
|
||||
|
||||
class Transaction(object):
|
||||
def __init__(self, conn: Connection):
|
||||
self.conn = conn
|
||||
|
||||
def begin_transaction(self):
|
||||
self.conn.begin()
|
||||
|
||||
def commit(self):
|
||||
self.conn.commit()
|
||||
|
||||
def rollback(self):
|
||||
self.conn.rollback()
|
||||
|
||||
def get_connection(self):
|
||||
return self.conn
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/23 17:29
|
||||
# @Author : old tom
|
||||
# @File : __init__.py.py
|
||||
# @Project : futool-db-0.1
|
||||
# @Desc :
|
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/6/23 17:30
|
||||
# @Author : old tom
|
||||
# @File : futool_lang.py
|
||||
# @Project : futool-db-0.1
|
||||
# @Desc :
|
||||
import hashlib
|
||||
|
||||
|
||||
def str_md5(content):
|
||||
"""
|
||||
字符串转MD5
|
||||
"""
|
||||
md5 = hashlib.md5()
|
||||
md5.update(content.encode(encoding='utf-8'))
|
||||
return md5.hexdigest()
|
Loading…
Reference in new issue