RandomDBGenerator/pythonProject/sqlgenerator/SQLGenerator.py

27 lines
943 B
Python
Raw Normal View History

import random
from sqlparse.SQLColumn import SQLColumn, SQLColumnType
from sqlparse.SQLTable import SQLTable
min_bigint = 0
max_bigint = 9223372036854775807
min_int = -2147483648
max_int = 2147483647
def generate_random_insert(sql_table: SQLTable):
columnValues = []
for column in sql_table.columns:
columnValues.append(generate_random_column(column))
sql_insert_command = "INSERT INTO " + sql_table.table_name + " VALUES (${i});"
for columnValue in columnValues:
sql_insert_command = sql_insert_command.replace("${i}", str(columnValue) + ", ${i}")
sql_insert_command = sql_insert_command.replace(", ${i}", "")
return sql_insert_command
def generate_random_column(sql_column: SQLColumn):
if sql_column.columnType == SQLColumnType.BIGINT:
return random.randint(min_bigint, max_bigint)
elif sql_column.columnType == SQLColumnType.INT:
return random.randint(min_int, max_int)