Generate pairs of date (start and end)
This commit is contained in:
parent
4c175a49e7
commit
ce1e594913
@ -10,7 +10,7 @@ def main():
|
||||
sql_tables = SQLParser.parse_sql_file("schema.sql")
|
||||
inserts = []
|
||||
for table in sql_tables:
|
||||
for i in range(0, 100):
|
||||
for i in range(0, 5):
|
||||
inserts.append(SQLGenerator.generate_random_insert(table))
|
||||
|
||||
with open("inserts.sql", "w") as file:
|
||||
|
@ -2,7 +2,8 @@ CREATE TABLE `test` (
|
||||
`id` bigint(20) NOT NULL,
|
||||
`test_value` int(11) NOT NULL,
|
||||
`test_string` varchar(255) DEFAULT NULL,
|
||||
`local_date` date DEFAULT NULL,
|
||||
`starting_date` date DEFAULT NULL,
|
||||
`ending_date` date DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
from enum import Enum
|
||||
|
||||
from sqlparse.SQLColumn import SQLColumn
|
||||
|
||||
|
||||
class ValueType(Enum):
|
||||
INT = 0,
|
||||
@ -7,6 +9,7 @@ class ValueType(Enum):
|
||||
|
||||
|
||||
class ColumnValue:
|
||||
def __init__(self, value, value_type: ValueType):
|
||||
def __init__(self, value, value_type: ValueType, sql_column: SQLColumn):
|
||||
self.value = value
|
||||
self.valueType = value_type
|
||||
self.valueType = value_type
|
||||
self.sqlColumn = sql_column
|
@ -1,6 +1,7 @@
|
||||
import datetime
|
||||
import random
|
||||
import time
|
||||
from typing import List
|
||||
|
||||
from sqlgenerator.ColumnValue import ValueType, ColumnValue
|
||||
from sqlparse.SQLColumn import SQLColumn, SQLColumnType
|
||||
@ -20,7 +21,7 @@ max_date = datetime.date(2024, 12, 31)
|
||||
def generate_random_insert(sql_table: SQLTable):
|
||||
columnValues = []
|
||||
for column in sql_table.columns:
|
||||
columnValues.append(generate_random_column(column))
|
||||
columnValues.append(generate_random_column(column, columnValues))
|
||||
|
||||
sql_insert_command = "INSERT INTO " + sql_table.table_name + " VALUES (${i});"
|
||||
for columnValue in columnValues:
|
||||
@ -33,17 +34,31 @@ def generate_random_insert(sql_table: SQLTable):
|
||||
return sql_insert_command
|
||||
|
||||
|
||||
def generate_random_column(sql_column: SQLColumn):
|
||||
def generate_random_column(sql_column: SQLColumn, generated_values: List[ColumnValue]):
|
||||
if sql_column.columnType == SQLColumnType.BIGINT:
|
||||
return ColumnValue(random.randint(min_bigint, max_bigint), ValueType.INT)
|
||||
return ColumnValue(random.randint(min_bigint, max_bigint), ValueType.INT, sql_column)
|
||||
elif sql_column.columnType == SQLColumnType.INT:
|
||||
return ColumnValue(random.randint(min_int, max_int), ValueType.INT)
|
||||
return ColumnValue(random.randint(min_int, max_int), ValueType.INT, sql_column)
|
||||
elif sql_column.columnType == SQLColumnType.VARCHAR:
|
||||
text_legnth = random.randint(1, 255)
|
||||
lorem_text = lorem.sentence()
|
||||
return ColumnValue(lorem_text[:text_legnth], ValueType.STRING)
|
||||
return ColumnValue(lorem_text[:text_legnth], ValueType.STRING, sql_column)
|
||||
elif sql_column.columnType == SQLColumnType.DATE:
|
||||
delta = max_date - min_date
|
||||
random_days = random.randrange(delta.days + 1)
|
||||
random_date = min_date + datetime.timedelta(days=random_days)
|
||||
return ColumnValue(str(random_date), ValueType.STRING)
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
return ColumnValue(str(random_date), ValueType.STRING, sql_column)
|
Loading…
Reference in New Issue
Block a user