diff --git a/pythonProject/schema.sql b/pythonProject/schema.sql index 2e10bd7..a19e4f1 100644 --- a/pythonProject/schema.sql +++ b/pythonProject/schema.sql @@ -2,6 +2,7 @@ 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, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci diff --git a/pythonProject/sqlgenerator/SQLGenerator.py b/pythonProject/sqlgenerator/SQLGenerator.py index a4fca24..c284cff 100644 --- a/pythonProject/sqlgenerator/SQLGenerator.py +++ b/pythonProject/sqlgenerator/SQLGenerator.py @@ -1,4 +1,6 @@ +import datetime import random +import time from sqlgenerator.ColumnValue import ValueType, ColumnValue from sqlparse.SQLColumn import SQLColumn, SQLColumnType @@ -11,8 +13,12 @@ max_bigint = 9223372036854775807 min_int = -2147483648 max_int = 2147483647 +min_date = datetime.date(2023, 1, 1) +max_date = datetime.date(2024, 12, 31) + + def generate_random_insert(sql_table: SQLTable): - columnValues = [] + columnValues = [] for column in sql_table.columns: columnValues.append(generate_random_column(column)) @@ -21,8 +27,7 @@ def generate_random_insert(sql_table: SQLTable): if columnValue.valueType == ValueType.INT: sql_insert_command = sql_insert_command.replace("${i}", str(columnValue.value) + ", ${i}") else: - sql_insert_command = sql_insert_command.replace("${i}", "'"+columnValue.value+"', ${i}") - + sql_insert_command = sql_insert_command.replace("${i}", "'" + columnValue.value + "', ${i}") sql_insert_command = sql_insert_command.replace(", ${i}", "") return sql_insert_command @@ -30,10 +35,15 @@ def generate_random_insert(sql_table: SQLTable): def generate_random_column(sql_column: SQLColumn): 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) elif sql_column.columnType == SQLColumnType.INT: return ColumnValue(random.randint(min_int, max_int), ValueType.INT) elif sql_column.columnType == SQLColumnType.VARCHAR: text_legnth = random.randint(1, 255) lorem_text = lorem.sentence() return ColumnValue(lorem_text[:text_legnth], ValueType.STRING) + 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) diff --git a/pythonProject/sqlparse/SQLColumn.py b/pythonProject/sqlparse/SQLColumn.py index 916a0e8..a9f2311 100644 --- a/pythonProject/sqlparse/SQLColumn.py +++ b/pythonProject/sqlparse/SQLColumn.py @@ -5,7 +5,7 @@ class SQLColumnType(Enum): BIGINT = 'bigint(20)' INT = 'int(11)' VARCHAR = 'varchar(255)' - + DATE = 'date' class SQLColumn: diff --git a/pythonProject/sqlparse/SQLParser.py b/pythonProject/sqlparse/SQLParser.py index b2568a0..4fc96a8 100644 --- a/pythonProject/sqlparse/SQLParser.py +++ b/pythonProject/sqlparse/SQLParser.py @@ -70,6 +70,8 @@ def get_column_type(column_definition: str): return SQLColumn.SQLColumnType.INT elif column_definition_characteristic == 'varchar(255)': return SQLColumn.SQLColumnType.VARCHAR + elif column_definition_characteristic == 'date': + return SQLColumn.SQLColumnType.DATE def get_column_name(column_definition: str):