This commit is contained in:
macbook_max
2026-10-15 20:36:59 +08:00
commit 6bf949949c
7923 changed files with 1369582 additions and 0 deletions
@@ -0,0 +1,19 @@
import asyncio
import aiomysql
async def test_example(loop):
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='', db='mysql',
loop=loop)
async with conn.cursor() as cur:
await cur.execute("SELECT Host,User FROM user")
print(cur.description)
r = await cur.fetchall()
print(r)
conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
@@ -0,0 +1,26 @@
import asyncio
import aiomysql
async def test_example(loop):
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', loop=loop)
async with conn.cursor() as cur:
await cur.execute('DROP PROCEDURE IF EXISTS myinc;')
await cur.execute("""CREATE PROCEDURE myinc(p1 INT)
BEGIN
SELECT p1 + 1;
END""")
await cur.callproc('myinc', [1])
(ret, ) = await cur.fetchone()
assert 2, ret
print(ret)
conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
@@ -0,0 +1,31 @@
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def test_example():
conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', loop=loop)
cur = yield from conn.cursor()
yield from cur.execute("DROP PROCEDURE IF EXISTS myinc;")
yield from cur.execute("""CREATE PROCEDURE myinc(p1 INT)
BEGIN
SELECT p1 + 1;
END
""")
yield from cur.callproc('myinc', [1])
(ret, ) = yield from cur.fetchone()
assert 2, ret
print(ret)
yield from cur.close()
conn.close()
loop.run_until_complete(test_example())
@@ -0,0 +1,20 @@
import asyncio
import aiomysql
async def test_example(loop):
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='', db='mysql',
loop=loop)
sql = "SELECT 1 `id`, JSON_OBJECT('key1', 1, 'key2', 'abc') obj"
async with conn.cursor(aiomysql.cursors.DeserializationCursor,
aiomysql.cursors.DictCursor) as cur:
await cur.execute(sql)
print(cur.description)
r = await cur.fetchall()
print(r)
conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
@@ -0,0 +1,41 @@
import asyncio
import aiomysql
async def test_example_executemany(loop):
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', loop=loop)
cur = await conn.cursor()
async with conn.cursor() as cur:
await cur.execute("DROP TABLE IF EXISTS music_style;")
await cur.execute("""CREATE TABLE music_style
(id INT,
name VARCHAR(255),
PRIMARY KEY (id));""")
await conn.commit()
# insert 3 rows one by one
await cur.execute("INSERT INTO music_style VALUES(1,'heavy metal')")
await cur.execute("INSERT INTO music_style VALUES(2,'death metal');")
await cur.execute("INSERT INTO music_style VALUES(3,'power metal');")
await conn.commit()
# insert 3 row by one long query using *executemany* method
data = [(4, 'gothic metal'), (5, 'doom metal'), (6, 'post metal')]
await cur.executemany(
"INSERT INTO music_style (id, name)"
"values (%s,%s)", data)
await conn.commit()
# fetch all insert row from table music_style
await cur.execute("SELECT * FROM music_style;")
result = await cur.fetchall()
print(result)
conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_executemany(loop))
@@ -0,0 +1,44 @@
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def test_example_executemany():
conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', loop=loop)
cur = yield from conn.cursor()
yield from cur.execute("DROP TABLE IF EXISTS music_style;")
yield from cur.execute("""CREATE TABLE music_style
(id INT,
name VARCHAR(255),
PRIMARY KEY (id));""")
yield from conn.commit()
# insert 3 rows one by one
yield from cur.execute("INSERT INTO music_style VALUES(1,'heavy metal')")
yield from cur.execute("INSERT INTO music_style VALUES(2,'death metal');")
yield from cur.execute("INSERT INTO music_style VALUES(3,'power metal');")
yield from conn.commit()
# insert 3 row by one long query using *executemane* method
data = [(4, 'gothic metal'), (5, 'doom metal'), (6, 'post metal')]
yield from cur.executemany(
"INSERT INTO music_style (id, name)"
"values (%s,%s)", data)
yield from conn.commit()
# fetch all insert row from table music_style
yield from cur.execute("SELECT * FROM music_style;")
result = yield from cur.fetchall()
print(result)
yield from cur.close()
conn.close()
loop.run_until_complete(test_example_executemany())
@@ -0,0 +1,28 @@
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def test_example():
conn = yield from aiomysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
db='mysql',
loop=loop
)
cur = yield from conn.cursor()
yield from cur.execute("SELECT Host,User FROM user")
print(cur.description)
r = yield from cur.fetchall()
print(r)
yield from cur.close()
conn.close()
loop.run_until_complete(test_example())
@@ -0,0 +1,20 @@
import asyncio
import aiomysql
async def test_example(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
print(cur.description)
(r,) = await cur.fetchone()
assert r == 42
pool.close()
await pool.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
@@ -0,0 +1,28 @@
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def test_example():
pool = yield from aiomysql.create_pool(
host='127.0.0.1',
port=3306,
user='root',
password='',
db='mysql',
loop=loop
)
with (yield from pool) as conn:
cur = yield from conn.cursor()
yield from cur.execute("SELECT 10")
# print(cur.description)
(r,) = yield from cur.fetchone()
assert r == 10
pool.close()
yield from pool.wait_closed()
loop.run_until_complete(test_example())
@@ -0,0 +1,40 @@
import asyncio
import sqlalchemy as sa
from aiomysql.sa import create_engine
metadata = sa.MetaData()
tbl = sa.Table('tbl', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('val', sa.String(255)))
async def create_table(engine):
async with engine.acquire() as conn:
await conn.execute('DROP TABLE IF EXISTS tbl')
await conn.execute('''CREATE TABLE tbl (
id serial PRIMARY KEY,
val varchar(255))''')
async def go(loop):
engine = await create_engine(user='root', db='test_pymysql',
host='127.0.0.1', password='', loop=loop)
await create_table(engine)
async with engine.acquire() as conn:
await conn.execute(tbl.insert().values(val='abc'))
await conn.execute(tbl.insert().values(val='xyz'))
async for row in conn.execute(tbl.select()):
print(row.id, row.val)
await conn.execute("commit")
engine.close()
await engine.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(go(loop))
@@ -0,0 +1,44 @@
import asyncio
import sqlalchemy as sa
from aiomysql.sa import create_engine
metadata = sa.MetaData()
tbl = sa.Table('tbl', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('val', sa.String(255)))
@asyncio.coroutine
def create_table(engine):
with (yield from engine) as conn:
yield from conn.execute('DROP TABLE IF EXISTS tbl')
yield from conn.execute('''CREATE TABLE tbl (
id serial PRIMARY KEY,
val varchar(255))''')
@asyncio.coroutine
def go():
engine = yield from create_engine(user='root',
db='test_pymysql',
host='127.0.0.1',
password='')
yield from create_table(engine)
with (yield from engine) as conn:
yield from conn.execute(tbl.insert().values(val='abc'))
res = yield from conn.execute(tbl.select())
for row in res:
print(row.id, row.val)
yield from conn.commit()
engine.close()
yield from engine.wait_closed()
asyncio.get_event_loop().run_until_complete(go())
@@ -0,0 +1,38 @@
import asyncio
import ssl
import aiomysql
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx.check_hostname = False
ctx.load_verify_locations(cafile='../tests/ssl_resources/ssl/ca.pem')
async def main():
async with aiomysql.create_pool(
host='localhost', port=3306, user='root',
password='rootpw', ssl=ctx,
auth_plugin='mysql_clear_password') as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Run simple command
await cur.execute("SHOW DATABASES;")
value = await cur.fetchall()
values = [item[0] for item in value]
# Spot check the answers, we should at least have mysql
# and information_schema
assert 'mysql' in values, \
'Could not find the "mysql" table'
assert 'information_schema' in values, \
'Could not find the "mysql" table'
# Check TLS variables
await cur.execute("SHOW STATUS LIKE 'Ssl_version%';")
value = await cur.fetchone()
# The context has TLS
assert value[1].startswith('TLS'), \
'Not connected to the database with TLS'
asyncio.get_event_loop().run_until_complete(main())
@@ -0,0 +1,57 @@
import asyncio
import aiomysql
async def test_example_transaction(loop):
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', autocommit=False,
loop=loop)
async with conn.cursor() as cursor:
stmt_drop = "DROP TABLE IF EXISTS names"
await cursor.execute(stmt_drop)
await cursor.execute("""
CREATE TABLE names (
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) DEFAULT '' NOT NULL,
cnt TINYINT UNSIGNED DEFAULT 0,
PRIMARY KEY (id))""")
await conn.commit()
# Insert 3 records
names = (('Geert',), ('Jan',), ('Michel',))
stmt_insert = "INSERT INTO names (name) VALUES (%s)"
await cursor.executemany(stmt_insert, names)
# Roll back!!!!
await conn.rollback()
# There should be no data!
stmt_select = "SELECT id, name FROM names ORDER BY id"
await cursor.execute(stmt_select)
resp = await cursor.fetchall()
# Check there is no data
assert not resp
# Do the insert again.
await cursor.executemany(stmt_insert, names)
# Data should be already there
await cursor.execute(stmt_select)
resp = await cursor.fetchall()
print(resp)
# Do a commit
await conn.commit()
await cursor.execute(stmt_select)
print(resp)
# Cleaning up, dropping the table again
await cursor.execute(stmt_drop)
await cursor.close()
conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_transaction(loop))
@@ -0,0 +1,60 @@
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def test_example_transaction():
conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='',
db='test_pymysql', autocommit=False,
loop=loop)
cursor = yield from conn.cursor()
stmt_drop = "DROP TABLE IF EXISTS names"
yield from cursor.execute(stmt_drop)
yield from cursor.execute("""
CREATE TABLE names (
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) DEFAULT '' NOT NULL,
cnt TINYINT UNSIGNED DEFAULT 0,
PRIMARY KEY (id))""")
yield from conn.commit()
# Insert 3 records
names = (('Geert',), ('Jan',), ('Michel',))
stmt_insert = "INSERT INTO names (name) VALUES (%s)"
yield from cursor.executemany(stmt_insert, names)
# Roll back!!!!
yield from conn.rollback()
# There should be no data!
stmt_select = "SELECT id, name FROM names ORDER BY id"
yield from cursor.execute(stmt_select)
resp = yield from cursor.fetchall()
# Check there is no data
assert not resp
# Do the insert again.
yield from cursor.executemany(stmt_insert, names)
# Data should be already there
yield from cursor.execute(stmt_select)
resp = yield from cursor.fetchall()
print(resp)
# Do a commit
yield from conn.commit()
yield from cursor.execute(stmt_select)
print(resp)
# Cleaning up, dropping the table again
yield from cursor.execute(stmt_drop)
yield from cursor.close()
conn.close()
loop.run_until_complete(test_example_transaction())