Generate inserts command considering string columns
This commit is contained in:
parent
5ba3fad6cc
commit
e1c76efc03
@ -1,5 +1,7 @@
|
||||
CREATE TABLE `test` (
|
||||
`id` bigint(20) NOT NULL,
|
||||
`test_value` int(11) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
`id` bigint(20) NOT NULL,
|
||||
`test_value` int(11) NOT NULL,
|
||||
`test_string` varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
|
||||
|
||||
|
12
pythonProject/sqlgenerator/ColumnValue.py
Normal file
12
pythonProject/sqlgenerator/ColumnValue.py
Normal file
@ -0,0 +1,12 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ValueType(Enum):
|
||||
INT = 0,
|
||||
STRING = 1
|
||||
|
||||
|
||||
class ColumnValue:
|
||||
def __init__(self, value, value_type: ValueType):
|
||||
self.value = value
|
||||
self.valueType = value_type
|
@ -1,6 +1,9 @@
|
||||
import random
|
||||
|
||||
from sqlgenerator.ColumnValue import ValueType, ColumnValue
|
||||
from sqlparse.SQLColumn import SQLColumn, SQLColumnType
|
||||
from sqlparse.SQLTable import SQLTable
|
||||
from lorem_text import lorem
|
||||
|
||||
min_bigint = 0
|
||||
max_bigint = 9223372036854775807
|
||||
@ -15,13 +18,22 @@ def generate_random_insert(sql_table: SQLTable):
|
||||
|
||||
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}")
|
||||
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}", "")
|
||||
return sql_insert_command
|
||||
|
||||
|
||||
def generate_random_column(sql_column: SQLColumn):
|
||||
if sql_column.columnType == SQLColumnType.BIGINT:
|
||||
return random.randint(min_bigint, max_bigint)
|
||||
return ColumnValue( random.randint(min_bigint, max_bigint), ValueType.INT)
|
||||
elif sql_column.columnType == SQLColumnType.INT:
|
||||
return random.randint(min_int, max_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)
|
||||
|
@ -4,6 +4,7 @@ from enum import Enum
|
||||
class SQLColumnType(Enum):
|
||||
BIGINT = 'bigint(20)'
|
||||
INT = 'int(11)'
|
||||
VARCHAR = 'varchar(255)'
|
||||
|
||||
|
||||
class SQLColumn:
|
||||
|
@ -68,6 +68,8 @@ def get_column_type(column_definition: str):
|
||||
return SQLColumn.SQLColumnType.BIGINT
|
||||
elif column_definition_characteristic == 'int(11)':
|
||||
return SQLColumn.SQLColumnType.INT
|
||||
elif column_definition_characteristic == 'varchar(255)':
|
||||
return SQLColumn.SQLColumnType.VARCHAR
|
||||
|
||||
|
||||
def get_column_name(column_definition: str):
|
||||
|
Loading…
Reference in New Issue
Block a user