-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
48 lines (41 loc) · 1.26 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
from faker import Faker
faker = Faker()
def create_table(table,fields):
sql = ''
sql += f'CREATE TABLE {table} (\n'
sql += '\tid SERIAL PRIMARY KEY,\n'
i = 0
for field in fields:
cmd = f'type(faker.{field}())'
value = eval(cmd)
if value not in [list,set,tuple]:
dtype = 'INT' if value == int else 'VARCHAR(255)' if value == str else 'FLOAT'
sql += f'\t{field} {dtype}\n' if len(fields) -1 == i else f'\t{field} {dtype},\n'
i += 1
sql += ');\n'
return sql
def insert_into(table,fields_):
fields = []
for field in fields_:
cmd = f'faker.{field}()'
value = eval(cmd)
dtype = type(value)
if dtype not in [set,list,tuple]:
fields.append(field)
sql = f'INSERT INTO {table} {tuple(fields)} VALUES ('
i = 0
for field in fields:
cmd = f'faker.{field}()'
value = eval(cmd)
value = f"'{value}'" if type(value) == str else value
sql += f'{value}' if len(fields) -1 == i else f'{value},'
i += 1
sql += ');\n'
return sql
def gen_sql(table,qty,fields):
sql = ''
sql += create_table(table,fields)
for i in range(qty):
sql += insert_into(table,fields)
return sql