in utils/alchemy_db_client.py [0:0]
def format_schema_dsl(schema: dict[str, Any], with_type: bool = True, with_comment: bool = False) -> str:
"""
Compress database table structure into DSL format
:param schema: Structure returned by get_db_schema
:param with_type: Whether to keep field types
:param with_comment: Whether to keep field comments
:return: Compressed DSL string
"""
type_aliases = {
'INTEGER': 'i', 'INT': 'i', 'BIGINT': 'i', 'SMALLINT': 'i', 'TINYINT': 'i',
'VARCHAR': 's', 'TEXT': 's', 'CHAR': 's',
'DATETIME': 'dt', 'TIMESTAMP': 'dt', 'DATE': 'dt',
'DECIMAL': 'f', 'NUMERIC': 'f', 'FLOAT': 'f', 'DOUBLE': 'f',
'BOOLEAN': 'b', 'BOOL': 'b',
'JSON': 'j'
}
lines = []
for table_name, table_data in schema.items():
column_parts = []
for col in table_data['columns']:
parts = [col['name']]
if with_type:
raw_type = col['type'].split('(')[0].upper()
col_type = type_aliases.get(raw_type, raw_type.lower())
parts.append(col_type)
if with_comment and col.get('comment'):
parts.append(f"# {col['comment']}")
column_parts.append(":".join(parts))
# Build table comment
if with_comment and table_data.get('comment'):
lines.append(f"# {table_data['comment']}")
lines.append(f"T:{table_name}({', '.join(column_parts)})")
return "\n".join(lines)