Generate inserts command considering date columns
This commit is contained in:
parent
e1c76efc03
commit
4c175a49e7
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -5,7 +5,7 @@ class SQLColumnType(Enum):
|
||||
BIGINT = 'bigint(20)'
|
||||
INT = 'int(11)'
|
||||
VARCHAR = 'varchar(255)'
|
||||
|
||||
DATE = 'date'
|
||||
|
||||
class SQLColumn:
|
||||
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user