dataCreating/bin/DBConfig.py

137 lines
5.0 KiB
Python
Raw Normal View History

2023-10-26 04:02:41 +00:00
# -*- coding:utf-8 -*-
import faker_data
2024-01-08 06:27:58 +00:00
import yaml
2023-10-26 04:02:41 +00:00
from pathlib import Path
from datetime import datetime
# 定义公共的部分
path = Path(__file__)
etc_dir = path.parent.parent / "etc"
2024-01-08 06:27:58 +00:00
config_file_patch = path.parent.parent / "test_config.yml"
2023-10-26 04:02:41 +00:00
2024-01-08 06:27:58 +00:00
# 获取配置文件
with open(config_file_patch, "r", encoding='utf-8') as fy:
config = yaml.safe_load(fy)
# print(config)
def get_now():
week = str(datetime.now().isocalendar()[1])
return(week)
2023-10-26 04:02:41 +00:00
def mysql_config():
mysql_info_dict={}
2023-11-07 10:01:16 +00:00
mysql_info_dict['driver_name'] = "com.mysql.cj.jdbc.Driver"
2023-10-26 04:02:41 +00:00
mysql_info_dict['driver_jar'] = "mysql-connector-java-8.0.29.jar"
driver_jar_path = etc_dir / "driver" / mysql_info_dict['driver_jar']
mysql_info_dict['driver_jar_path'] = str(driver_jar_path)
#这里要连接到具体的库
2024-01-08 06:27:58 +00:00
mysql_info_dict['jdbc_url'] = "jdbc:mysql://" + config['IP'] + ":" + config['PROT'] + "/" + config['DATABASES']
mysql_info_dict['db_user'] = config['DBUSER']
mysql_info_dict['db_password'] = config['DBPASSWD']
2023-10-26 04:02:41 +00:00
#create table sql
2024-01-08 06:27:58 +00:00
week = get_now()
table_name = "mysql_test_table_w" + week
2023-10-26 04:02:41 +00:00
create_sql_string = "CREATE TABLE IF NOT EXISTS `"
create_sql_string += table_name
create_sql_string += "` ("
create_sql_string += "`uuid` varchar(50) DEFAULT NULL,"
create_sql_string += "`id` bigint DEFAULT NULL,"
create_sql_string += "`name` varchar(50) DEFAULT NULL,"
create_sql_string += "`mobile` varchar(50) DEFAULT NULL,"
create_sql_string += "`ssn` varchar(50) DEFAULT NULL,"
create_sql_string += "`sex` int DEFAULT NULL,"
create_sql_string += "`email` varchar(50) DEFAULT NULL,"
create_sql_string += "`job` varchar(50) DEFAULT NULL,"
create_sql_string += "`address` varchar(50) DEFAULT NULL,"
2023-11-07 10:01:16 +00:00
create_sql_string += "`actime_time` varchar(50) NULL DEFAULT NULL"
2023-11-14 05:44:25 +00:00
create_sql_string += ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
2023-10-26 04:02:41 +00:00
mysql_info_dict['create_sql_string'] = create_sql_string
# insert into sql
insert_list = []
2024-01-08 06:27:58 +00:00
data = faker_data.faker_data(lines=config['InsertRows'])[1:]
2023-10-26 04:02:41 +00:00
for a in data:
#拼接sql
insert_sql_string = "INSERT INTO " + table_name
insert_sql_string += "(uuid, id, name, mobile, ssn, sex, email, job, address, actime_time) VALUES("
str_data = ','.join("'{0}'".format(x) for x in a)
insert_sql_string += str_data + ");"
insert_list.append(insert_sql_string)
# 返回所有的插入语句
mysql_info_dict['insert_list'] = insert_list
# delete sql
2023-10-26 05:14:46 +00:00
del_sql = "delete FROM "+ table_name + " where 1=1 limit 100"
2023-10-26 04:02:41 +00:00
mysql_info_dict['del_sql'] = del_sql
# uptata sql
2023-11-07 10:01:16 +00:00
updata_sql = "UPDATE "+ table_name + " t SET t.job='unary_测试' WHERE t.name like '%' AND sex = 1 AND id = 1 "
2023-10-26 04:02:41 +00:00
mysql_info_dict['updata_sql'] = updata_sql
return(mysql_info_dict)
def oracle_config():
2023-10-26 05:14:46 +00:00
oracle_info_dict={}
oracle_info_dict['driver_name'] = "oracle.jdbc.OracleDriver"
oracle_info_dict['driver_jar'] = "ojdbc8-12.2.0.1.jar"
driver_jar_path = etc_dir / "driver" / oracle_info_dict['driver_jar']
oracle_info_dict['driver_jar_path'] = str(driver_jar_path)
#这里要连接到具体的实例
2024-01-08 06:27:58 +00:00
oracle_info_dict['jdbc_url'] = "jdbc:oracle:thin:@" + config['IP'] + ":" + config['PROT'] + "/" + config['DATABASES']
oracle_info_dict['db_user'] = config['DBUSER']
oracle_info_dict['db_password'] = config['DBPASSWD']
2023-10-26 04:02:41 +00:00
2023-10-26 05:14:46 +00:00
#create table sql
2024-01-08 06:27:58 +00:00
week = get_now()
table_name = "ORACLE_TEST_w" + week
2023-10-26 05:14:46 +00:00
create_sql_string = 'CREATE TABLE "'
create_sql_string += table_name + '" '
create_sql_string += '("UUID" VARCHAR2(50),'
create_sql_string += '"ID" NUMBER(38,0), '
create_sql_string += '"NAME" VARCHAR2(50),'
create_sql_string += '"MOBILE" VARCHAR2(50),'
create_sql_string += '"SSN" VARCHAR2(50),'
create_sql_string += '"SEX" NUMBER(*,0), '
create_sql_string += '"EMAIL" VARCHAR2(50), '
create_sql_string += '"JOB" VARCHAR2(50),'
create_sql_string += '"ADDRESS" VARCHAR2(100),'
create_sql_string += '"ACTIME_TIME" VARCHAR2(50))'
oracle_info_dict['create_sql_string'] = create_sql_string
# insert into sql
insert_list = []
2024-01-08 06:27:58 +00:00
data = faker_data.faker_data(lines=config['InsertRows'])[1:]
2023-10-26 05:14:46 +00:00
for a in data:
#拼接sql
insert_sql_string = "INSERT INTO " + table_name
insert_sql_string += " t (uuid, id, name, mobile, ssn, sex, email, job, address, actime_time) VALUES("
str_data = ','.join("'{0}'".format(x) for x in a)
insert_sql_string += str_data + ")"
insert_list.append(insert_sql_string)
# 返回所有的插入语句
oracle_info_dict['insert_list'] = insert_list
# delete sql
del_sql = "delete FROM "+ table_name + " WHERE rownum<=100"
oracle_info_dict['del_sql'] = del_sql
# uptata sql
2023-11-07 10:01:16 +00:00
updata_sql = "UPDATE "+ table_name + " t SET t.job='unary_测试' WHERE t.name like '%' AND sex = 1 AND id = 1 "
2023-10-26 05:14:46 +00:00
oracle_info_dict['updata_sql'] = updata_sql
return(oracle_info_dict)
2023-10-26 04:02:41 +00:00