Skip to content

Commit 8459466

Browse files
committed
Add class Mybatis
1 parent 005813e commit 8459466

File tree

9 files changed

+128
-4
lines changed

9 files changed

+128
-4
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ jobs:
1212
test:
1313
runs-on: ubuntu-latest
1414

15+
services:
16+
mysql:
17+
image: mysql:5.7
18+
env:
19+
MYSQL_ROOT_PASSWORD: mybatis
20+
MYSQL_DATABASE: mybatis
21+
MYSQL_USER: mybatis
22+
MYSQL_PASSWORD: mybatis
23+
ports:
24+
- 3306:3306
25+
1526
steps:
1627
- name: Checkout code
1728
uses: actions/checkout@v2
@@ -23,7 +34,18 @@ jobs:
2334

2435
- name: Install dependencies
2536
run: |
26-
pip install pytest pytest-cov
37+
pip install pytest pytest-cov mysql-connector-python
38+
39+
- name: Set up MySQL client
40+
run: sudo apt-get install mysql-client
41+
42+
- name: Wait for MySQL to be ready
43+
run: |
44+
until mysql -h 127.0.0.1 -u mybatis -pmybatis -e "SELECT 1"; do
45+
echo "Waiting for MySQL..."
46+
sleep 2
47+
done
48+
echo "MySQL is ready!"
2749
2850
- name: Run tests with coverage
2951
run: |

example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
cur = conn.cursor(prepared=True)
1414

15+
class A:
16+
def __init__(self):
17+
pass
18+
1519
def main():
1620
mm = MapperManager()
1721

@@ -45,5 +49,7 @@ def main():
4549
# res = cur.fetchall()
4650
# print(res)
4751

52+
print(globals()['A'])
53+
4854
if __name__ == "__main__":
4955
main()

mapper/test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
FROM
2929
fruits
3030
WHERE
31-
category = 'apple' AND
31+
category = 'A' AND
3232
<![CDATA[ price < 500 ]]>
3333
</select>
3434

mapper/test2.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper>
4+
<select id="testBasicNone">
5+
SELECT
6+
id,
7+
name,
8+
category,
9+
price
10+
FROM
11+
fruits
12+
WHERE
13+
category = 'C'
14+
</select>
15+
</mapper>

mybatis/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .mapper_manager import MapperManager
1+
from .mapper_manager import MapperManager
2+
from .mybatis import Mybatis

mybatis/mybatis.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Optional
2+
3+
import mysql.connector
4+
import sqlite3
5+
6+
from .mapper_manager import MapperManager
7+
import os
8+
9+
class Mybatis(object):
10+
def __init__(self, conn, mapper_path:str):
11+
self.conn = conn
12+
self.mapper_manager = MapperManager()
13+
14+
mapper_file_name_l = [name for name in os.listdir(mapper_path) if name.endswith(".xml")]
15+
for file_name in mapper_file_name_l:
16+
full_path = os.path.join(mapper_path, file_name)
17+
self.mapper_manager.read_mapper_xml_file(full_path)
18+
19+
def select_one(self, id:str, param:dict) -> Optional[dict]:
20+
sql, param_list = self.mapper_manager.select(id, param)
21+
with self.conn.cursor(prepared=True) as cursor:
22+
cursor.execute(sql, param_list)
23+
ret = cursor.fetchone()
24+
if ret is None:
25+
return None
26+
column_name = [item[0] for item in cursor.description]
27+
res = {}
28+
for idx, item in enumerate(column_name):
29+
res[item] = ret[idx]
30+
return res

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
url='https://github.com/ralgond/mybatis-py',
1212
packages=find_packages(),
1313
install_requires=[
14+
'mysql-connector-python>=9.0.0'
1415
],
1516
classifiers=[
1617
'Programming Language :: Python :: 3',

test/test_mapper_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import pytest
21
from mybatis import MapperManager
32

3+
44
def test_include():
55
mm = MapperManager()
66
mm.read_mapper_xml_file("mapper/test.xml")

test/test_mybatis.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
import mysql.connector
3+
4+
from mybatis import Mybatis
5+
6+
7+
@pytest.fixture(scope="function")
8+
def db_connection():
9+
# 配置数据库连接
10+
connection = mysql.connector.connect(
11+
host="localhost",
12+
user="mybatis",
13+
password="mybatis",
14+
database="mybatis"
15+
)
16+
cursor = connection.cursor()
17+
cursor.execute("DROP TABLE IF EXISTS fruits")
18+
create_table_sql = '''CREATE TABLE IF NOT EXISTS fruits (
19+
id INT AUTO_INCREMENT PRIMARY KEY,
20+
name VARCHAR(100),
21+
category VARCHAR(100),
22+
price int)
23+
'''
24+
# 在测试开始前准备数据
25+
cursor.execute(create_table_sql)
26+
cursor.execute("INSERT INTO fruits (name, category, price) VALUES ('Alice', 'A', 100)")
27+
cursor.execute("INSERT INTO fruits (name, category, price) VALUES ('Bob', 'B', 200)")
28+
connection.commit()
29+
30+
# 提供数据库连接给测试用例
31+
yield connection
32+
33+
# 清理数据和关闭连接
34+
connection.close()
35+
36+
def test_select_one(db_connection):
37+
mb = Mybatis(db_connection, "mapper")
38+
ret = mb.select_one('testBasic', {})
39+
assert ret is not None
40+
assert len(ret) == 4
41+
assert ret['id'] == 1
42+
assert ret['name'] == 'Alice'
43+
assert ret['category'] == 'A'
44+
assert ret['price'] == 100
45+
46+
def test_select_one_none(db_connection):
47+
mb = Mybatis(db_connection, "mapper")
48+
ret = mb.select_one('testBasicNone', {})
49+
assert ret is None

0 commit comments

Comments
 (0)