Generate insert commands for isolated number-only tables

This commit is contained in:
Sebastian Böckelmann 2024-04-17 10:18:01 +02:00
parent db0cea9de7
commit 5ba3fad6cc
4 changed files with 42 additions and 10 deletions

View File

@ -1,5 +1,6 @@
# This is a sample Python script. # This is a sample Python script.
from sqlparse import SQLParser from sqlparse import SQLParser
from sqlgenerator import SQLGenerator
# Press Umschalt+F10 to execute it or replace it with your code. # Press Umschalt+F10 to execute it or replace it with your code.
@ -7,6 +8,16 @@ from sqlparse import SQLParser
def main(): def main():
sql_tables = SQLParser.parse_sql_file("schema.sql") sql_tables = SQLParser.parse_sql_file("schema.sql")
inserts = []
for table in sql_tables:
for i in range(0, 100):
inserts.append(SQLGenerator.generate_random_insert(table))
with open("inserts.sql", "w") as file:
for insert in inserts:
file.write(insert + "\n")

View File

@ -3,10 +3,3 @@ CREATE TABLE `test` (
`test_value` int(11) NOT NULL, `test_value` int(11) NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
CREATE TABLE `test2` (
`id` bigint(20) NOT NULL,
`test_value` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

View File

@ -0,0 +1,27 @@
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)

View File

@ -22,7 +22,7 @@ def split_table_definition(sql_string: str):
def parse_sql_file(sql_path: str): def parse_sql_file(sql_path: str):
sql_string = read_sql_file(sql_path) sql_string = read_sql_file(sql_path)
sql_tables = split_table_definition(sql_string) sql_tables = split_table_definition(sql_string)
processed_tables = [SQLTable] processed_tables = []
for sql_table in sql_tables: for sql_table in sql_tables:
table = parse_table_sql(sql_table) table = parse_table_sql(sql_table)
processed_tables.append(table) processed_tables.append(table)
@ -46,7 +46,8 @@ def process_table_content(table_content: str, table_name: str):
else: else:
column = parse_column_definition(column_definition) column = parse_column_definition(column_definition)
columns.append(column) columns.append(column)
return SQLTable(table_name, columns, primary_key) table = SQLTable(table_name, columns, primary_key)
return table
def parse_column_definition(column_definition: str): def parse_column_definition(column_definition: str):