Merge pull request 'v0.5.0-add_llmutils' (#12) from v0.5.0-add_llmutils into main
Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
name = "GeneralUtils"
|
name = "GeneralUtils"
|
||||||
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
||||||
version = "0.5.0"
|
version = "0.5.1"
|
||||||
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
|
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
|
||||||
|
|
||||||
[deps]
|
[deps]
|
||||||
|
|||||||
+109
-1
@@ -3,7 +3,7 @@ module llmUtil
|
|||||||
export formatLLMtext, extractthink, checkAgentResponse_JSON, clean_json_response,
|
export formatLLMtext, extractthink, checkAgentResponse_JSON, clean_json_response,
|
||||||
extract_column_metadata, generate_embedding_payloads, resolve_semantic_cluster,
|
extract_column_metadata, generate_embedding_payloads, resolve_semantic_cluster,
|
||||||
harvest_entity_catalog, resolve_entity, harvest_db_undirected_schema_graph,
|
harvest_entity_catalog, resolve_entity, harvest_db_undirected_schema_graph,
|
||||||
get_db_table_schema
|
get_db_table_schema, get_db_table_schema_simple
|
||||||
|
|
||||||
using UUIDs, JSON, Dates, DataFrames, StringDistances, Graphs, LibPQ
|
using UUIDs, JSON, Dates, DataFrames, StringDistances, Graphs, LibPQ
|
||||||
using ..util
|
using ..util
|
||||||
@@ -1016,7 +1016,115 @@ function get_db_table_schema(conn::LibPQ.Connection, table_name::String)::DataFr
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
""" Generate simplified DDL statement for a PostgreSQL table.
|
||||||
|
|
||||||
|
Extracts table structure from PostgreSQL and returns a clean CREATE TABLE statement
|
||||||
|
without NULL/NOT NULL constraints, useful for schema documentation or migration purposes.
|
||||||
|
|
||||||
|
# Arguments
|
||||||
|
- `conn::LibPQ.Connection`
|
||||||
|
A PostgreSQL connection object created via `LibPQ.Connection()`.
|
||||||
|
- `table_name::String`
|
||||||
|
The name of the table to extract.
|
||||||
|
- `schema_name::String` (default: `"public"`)
|
||||||
|
The schema containing the table.
|
||||||
|
|
||||||
|
# Return
|
||||||
|
- `String`
|
||||||
|
A CREATE TABLE DDL statement containing:
|
||||||
|
- Column definitions with names, types, and DEFAULT values
|
||||||
|
- Table-level constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK)
|
||||||
|
- Excludes NULL/NOT NULL specifications for cleaner output
|
||||||
|
|
||||||
|
# Details
|
||||||
|
The function:
|
||||||
|
1. Queries PostgreSQL system catalogs (`pg_attribute`, `pg_class`, `pg_namespace`)
|
||||||
|
2. Extracts column names, data types, and default values
|
||||||
|
3. Captures table-level constraints via `pg_constraint`
|
||||||
|
4. Omits NULL/NOT NULL checks to produce a simplified schema definition
|
||||||
|
5. Returns properly formatted DDL with quoted identifiers
|
||||||
|
|
||||||
|
# Example
|
||||||
|
```julia
|
||||||
|
julia> using GeneralUtils, LibPQ
|
||||||
|
julia> conn = LibPQ.Connection("host=localhost port=5432 dbname=winedb user=admin password=secret")
|
||||||
|
julia> ddl = GeneralUtils.get_db_table_schema_simple(conn, "wine")
|
||||||
|
"CREATE TABLE \"public\".\"wine\" (
|
||||||
|
\"id\" integer DEFAULT nextval('wine_id_seq'::regclass),
|
||||||
|
\"wine_name\" text,
|
||||||
|
\"year\" integer,
|
||||||
|
\"price\" numeric
|
||||||
|
);"
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
function get_db_table_schema_simple(pg_conn_str::String, table_name::String;
|
||||||
|
schema_name::String="public")::String
|
||||||
|
conn = LibPQ.Connection(pg_conn_str)
|
||||||
|
return get_db_table_schema_simple(conn, table_name; schema_name=schema_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function get_db_table_schema_simple(conn, table_name::String; schema_name::String="public")::String
|
||||||
|
# 1. SQL query tailored to omit nullability checks
|
||||||
|
sql = """
|
||||||
|
SELECT
|
||||||
|
a.attname AS column_name,
|
||||||
|
format_type(a.atttypid, a.atttypmod) AS data_type,
|
||||||
|
pg_get_expr(def.adbin, def.adrelid) AS default_value,
|
||||||
|
COALESCE(
|
||||||
|
(SELECT pg_get_constraintdef(p.oid)
|
||||||
|
FROM pg_catalog.pg_constraint p
|
||||||
|
WHERE p.conrelid = c.oid AND a.attnum = ANY(p.conkey)
|
||||||
|
LIMIT 1), ''
|
||||||
|
) AS constraint_definition
|
||||||
|
FROM
|
||||||
|
pg_catalog.pg_attribute a
|
||||||
|
JOIN
|
||||||
|
pg_catalog.pg_class c ON a.attrelid = c.oid
|
||||||
|
JOIN
|
||||||
|
pg_catalog.pg_namespace n ON c.relnamespace = n.oid
|
||||||
|
LEFT JOIN
|
||||||
|
pg_catalog.pg_attrdef def ON def.adrelid = c.oid AND def.adnum = a.attnum
|
||||||
|
WHERE
|
||||||
|
c.relname = \$1
|
||||||
|
AND n.nspname = \$2
|
||||||
|
AND a.attnum > 0
|
||||||
|
AND NOT a.attisdropped
|
||||||
|
ORDER BY
|
||||||
|
a.attnum;
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = execute(conn, sql, [table_name, schema_name])
|
||||||
|
|
||||||
|
if length(result) == 0
|
||||||
|
error("Table '$schema_name.$table_name' not found.")
|
||||||
|
end
|
||||||
|
|
||||||
|
ddl_lines = String[]
|
||||||
|
constraints = String[]
|
||||||
|
|
||||||
|
for row in result
|
||||||
|
col_name = row.column_name
|
||||||
|
data_type = row.data_type
|
||||||
|
|
||||||
|
# Handle the default value if it exists
|
||||||
|
default_val = ismissing(row.default_value) ? "" : " DEFAULT " * row.default_value
|
||||||
|
|
||||||
|
# Build the column definition line (without NULL/NOT NULL)
|
||||||
|
col_def = " \"$col_name\" $data_type$default_val"
|
||||||
|
push!(ddl_lines, col_def)
|
||||||
|
|
||||||
|
# Handle table-level constraints
|
||||||
|
con_def = ismissing(row.constraint_definition) ? "" : row.constraint_definition
|
||||||
|
if !isempty(con_def) && !(con_def in constraints)
|
||||||
|
push!(constraints, " " * con_def)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
all_definitions = vcat(ddl_lines, constraints)
|
||||||
|
body = join(all_definitions, ",\n")
|
||||||
|
|
||||||
|
return "CREATE TABLE \"$schema_name\".\"$table_name\" (\n$body\n);"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user