diff --git a/fudb/dbapis/fu_db_api.py b/fudb/dbapis/fu_db_api.py index 66d275a..862ee40 100644 --- a/fudb/dbapis/fu_db_api.py +++ b/fudb/dbapis/fu_db_api.py @@ -98,7 +98,7 @@ def batch_insert(conn: Connection, db_type, sql_tpl, data, batch_size=1500): """ handler = BatchInsertHandler(db_type, sql_tpl, data, batch_size) insert_sqls = handler.build_insert() - # 整个插入都在一个事务内 + # 插入都在一个事务内 row_count = 0 try: conn.begin() @@ -142,7 +142,8 @@ class BatchInsertHandler(object): def __init__(self, db_type, sql_tpl, data, batch_size): """ :param db_type: 数据库类型 - :param sql_tpl: insert into t1 (f1,f2,f3) values %s + :param sql_tpl: pg及mysql: insert into t1 (f1,f2,f3) values %s + oracle: into t1 (f1,f2,f3) values %s :param data: [(1,'tom',29),(2,'jack',30)] :param batch_size: """ @@ -153,13 +154,6 @@ class BatchInsertHandler(object): self.data = data self.batch_size = batch_size - def gen_batch_sql(self) -> []: - """ - 生成批量插入SQL - :return: - """ - pass - def _split_data(self): return split_coll(self.data, self.batch_size) diff --git a/test/test_dialect_connector.py b/test/test_dialect_connector.py index 0e5bdce..2db250a 100644 --- a/test/test_dialect_connector.py +++ b/test/test_dialect_connector.py @@ -5,7 +5,7 @@ # @File : test_dialect_connector.py # @Project : futool-db from unittest import TestCase -from fudb.dbapis.fu_db_api import select_all, select_one, execute_update, batch_insert +from fudb.dbapis.fu_db_api import select_all, select_one, count, execute_update, batch_insert from fudb.connectors.connector_factory import ConnFactory dds = [(4, 'zr4', 34), (5, 'zr5', 35), (6, 'zr6', 36), (7, 'zr7', 37), (8, 'zr8', 38), (9, 'zr9', 39), @@ -32,10 +32,21 @@ dds = [(4, 'zr4', 34), (5, 'zr5', 35), (6, 'zr6', 36), (7, 'zr7', 37), (8, 'zr8' class TestPostgresqlConnector(TestCase): def test_get_conn(self): # factory = ConnFactory('postgresql', 'postgres', 'root@123', 'localhost', 5432, 'postgres') - factory = ConnFactory('oracle', 'zr', 'root@123', 'localhost', 1521, 'XE') + oracle_factory = ConnFactory('oracle', 'zr', 'root@123', 'localhost', 1521, 'XE') # factory2 = ConnFactory('postgresql', 'postgres', 'root@123', 'localhost', 5432, 'postgres') # print(id(factory), id(factory2)) - conn = factory.get_conn() + conn = oracle_factory.get_conn() + # 返回第一条 + select_one(oracle_factory.get_conn(), 'select * from test limit 20') + # 查全部 + select_all(oracle_factory.get_conn(), 'select * from test limit 20') + # 数据量统计 + count(oracle_factory.get_conn(), 'test') + # delete \insert \update + execute_update(oracle_factory.get_conn(),'delete from test where id=1') + # 批量插入 + batch_insert(conn, 'oracle', 'into t_user (id,name,age) values %s', dds, 10) + # print(select_all(factory.get_conn(), 'select * from test limit 20')) # print(select_all(factory.get_conn(), 'select * from t_user limit 20')) # conn.close()