2024-04-17 10:55:01 +02:00
|
|
|
import datetime
|
2024-04-17 10:18:01 +02:00
|
|
|
import random
|
2024-04-17 10:55:01 +02:00
|
|
|
import time
|
2024-04-17 16:22:16 +02:00
|
|
|
from typing import List
|
2024-04-17 10:40:55 +02:00
|
|
|
|
|
|
|
from sqlgenerator.ColumnValue import ValueType, ColumnValue
|
2024-04-17 10:18:01 +02:00
|
|
|
from sqlparse.SQLColumn import SQLColumn, SQLColumnType
|
|
|
|
from sqlparse.SQLTable import SQLTable
|
2024-04-17 10:40:55 +02:00
|
|
|
from lorem_text import lorem
|
2024-04-17 10:18:01 +02:00
|
|
|
|
|
|
|
min_bigint = 0
|
|
|
|
max_bigint = 9223372036854775807
|
|
|
|
|
|
|
|
min_int = -2147483648
|
|
|
|
max_int = 2147483647
|
|
|
|
|
2024-04-17 10:55:01 +02:00
|
|
|
min_date = datetime.date(2023, 1, 1)
|
|
|
|
max_date = datetime.date(2024, 12, 31)
|
|
|
|
|
|
|
|
|
2024-04-17 10:18:01 +02:00
|
|
|
def generate_random_insert(sql_table: SQLTable):
|
2024-04-17 10:55:01 +02:00
|
|
|
columnValues = []
|
2024-04-17 10:18:01 +02:00
|
|
|
for column in sql_table.columns:
|
2024-04-17 16:22:16 +02:00
|
|
|
columnValues.append(generate_random_column(column, columnValues))
|
2024-04-17 10:18:01 +02:00
|
|
|
|
|
|
|
sql_insert_command = "INSERT INTO " + sql_table.table_name + " VALUES (${i});"
|
|
|
|
for columnValue in columnValues:
|
2024-04-17 10:40:55 +02:00
|
|
|
if columnValue.valueType == ValueType.INT:
|
|
|
|
sql_insert_command = sql_insert_command.replace("${i}", str(columnValue.value) + ", ${i}")
|
|
|
|
else:
|
2024-04-17 10:55:01 +02:00
|
|
|
sql_insert_command = sql_insert_command.replace("${i}", "'" + columnValue.value + "', ${i}")
|
2024-04-17 10:40:55 +02:00
|
|
|
|
2024-04-17 10:18:01 +02:00
|
|
|
sql_insert_command = sql_insert_command.replace(", ${i}", "")
|
|
|
|
return sql_insert_command
|
|
|
|
|
|
|
|
|
2024-04-17 16:22:16 +02:00
|
|
|
def generate_random_column(sql_column: SQLColumn, generated_values: List[ColumnValue]):
|
2024-04-17 10:18:01 +02:00
|
|
|
if sql_column.columnType == SQLColumnType.BIGINT:
|
2024-04-17 16:22:16 +02:00
|
|
|
return ColumnValue(random.randint(min_bigint, max_bigint), ValueType.INT, sql_column)
|
2024-04-17 10:18:01 +02:00
|
|
|
elif sql_column.columnType == SQLColumnType.INT:
|
2024-04-17 16:22:16 +02:00
|
|
|
return ColumnValue(random.randint(min_int, max_int), ValueType.INT, sql_column)
|
2024-04-17 10:40:55 +02:00
|
|
|
elif sql_column.columnType == SQLColumnType.VARCHAR:
|
|
|
|
text_legnth = random.randint(1, 255)
|
|
|
|
lorem_text = lorem.sentence()
|
2024-04-17 16:22:16 +02:00
|
|
|
return ColumnValue(lorem_text[:text_legnth], ValueType.STRING, sql_column)
|
2024-04-17 10:55:01 +02:00
|
|
|
elif sql_column.columnType == SQLColumnType.DATE:
|
2024-04-17 16:22:16 +02:00
|
|
|
if "end" in sql_column.columnName or "max" in sql_column.columnName:
|
|
|
|
for value in generated_values:
|
|
|
|
if "start" in value.sqlColumn.columnName or "min" in sql_column.columnName:
|
|
|
|
startingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d").date()
|
|
|
|
return generate_random_date(startingDate, max_date, sql_column)
|
|
|
|
elif "start" in sql_column.columnName or "min" in sql_column.columnName:
|
|
|
|
for value in generated_values:
|
|
|
|
if "end" in value.sqlColumn.columnName or "max" in sql_column.columnName:
|
|
|
|
endingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d").date()
|
|
|
|
return generate_random_date(min_date, endingDate, sql_column)
|
|
|
|
return generate_random_date(min_date, max_date, sql_column)
|
2024-04-17 16:35:11 +02:00
|
|
|
elif sql_column.columnType == SQLColumnType.TIME:
|
|
|
|
print("Time")
|
|
|
|
return generate_random_time(sql_column)
|
2024-04-17 16:22:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def generate_random_date(start_date, end_date, sql_column):
|
|
|
|
delta = end_date - start_date
|
|
|
|
random_days = random.randrange(delta.days + 1)
|
|
|
|
random_date = start_date + datetime.timedelta(days=random_days)
|
2024-04-17 16:35:11 +02:00
|
|
|
return ColumnValue(str(random_date), ValueType.STRING, sql_column)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_random_time(sql_column, start_time=datetime.time(0,0,0), end_time=datetime.time(23, 59, 59)):
|
|
|
|
hours = random.randint(start_time.hour, end_time.hour)
|
|
|
|
minutes = random.randint(start_time.minute, end_time.minute)
|
|
|
|
seconds = random.randint(start_time.second, end_time.second)
|
|
|
|
random_time = datetime.time(hours, minutes, seconds)
|
|
|
|
return ColumnValue(str(random_time), ValueType.STRING, sql_column)
|