@@ -76,11 +76,14 @@ function build_field_name(str_arr::Vector{String},
7676 return field_name
7777end
7878
79- function get_fieldtype_from_coltype (coltype:: String ,elttype:: String
79+ function get_fieldtype_from_coltype (coltype:: String ,
80+ elttype:: String ,
81+ customtypes:: Dict ,
8082 ;tablename:: String = " " ,
8183 colname:: String = " " )
8284
8385 attrtype = missing
86+ customtypes_names = keys (customtypes) |> collect |> n -> string .(n)
8487
8588 if (coltype == " character"
8689 || coltype == " character varying"
@@ -110,6 +113,9 @@ function get_fieldtype_from_coltype(coltype::String,elttype::String
110113 elseif (coltype == " ARRAY" )
111114 if (elttype == " _text" || elttype == " _varchar" )
112115 attrtype = " Vector{String}"
116+ elseif elttype[2 : end ] in customtypes_names
117+ elttype = elttype[2 : end ] # remove the leading underscore
118+ attrtype = " Vector{$(build_enum_name_w_module (elttype)) }"
113119 else
114120 error (" Unknown array type[$elttype ] for table[$tablename ] column[$colname ]" )
115121 end
@@ -140,6 +146,7 @@ function generate_julia_code(dbconn::LibPQ.Connection,
140146
141147end
142148
149+
143150function generate_object_model (
144151 dbconn:: LibPQ.Connection ,
145152 lang_code:: String
@@ -222,6 +229,7 @@ function generate_object_model(
222229 manytoone_field[:is_manytoone ] = true
223230 end
224231 manytoone_field[:is_onetomany ] = false
232+ manytoone_field[:is_enum ] = false
225233
226234 # Build a field name by one of the following options:
227235 # Case 1: The FK is composed of one column only. In this case we use
@@ -304,12 +312,19 @@ function generate_object_model(
304312 id_field[:is_manytoone ] = false
305313 id_field[:is_onetoone ] = false
306314 id_field[:is_onetomany ] = false
315+ id_field[:is_enum ] = false
307316 field_name = build_field_name (pkcol,lang_code)
308317 id_field[:name ] = field_name
309318
310319 field_type =
311320 get_fieldtype_from_coltype (tabledef[:cols ][pkcol][:type ],
312- tabledef[:cols ][pkcol][:elttype_if_array ])
321+ tabledef[:cols ][pkcol][:elttype_if_array ],
322+ custom_types)
323+
324+ # Check if it is an enum
325+ if tabledef[:cols ][pkcol][:type ] == " USER-DEFINED"
326+ id_field[:is_enum ] = true
327+ end
313328
314329 id_field[:field_type ] = field_type
315330 push! (struct_id_fields, id_field)
@@ -329,14 +344,21 @@ function generate_object_model(
329344 basic_field[:is_manytoone ] = false
330345 basic_field[:is_onetoone ] = false
331346 basic_field[:is_onetomany ] = false
347+ basic_field[:is_enum ] = false
332348 field_name = build_field_name (colname,lang_code)
333349 basic_field[:name ] = field_name
334350
335351 field_type =
336352 get_fieldtype_from_coltype (coldef[:type ],
337- coldef[:elttype_if_array ]
353+ coldef[:elttype_if_array ],
354+ custom_types
338355 ;tablename = table, colname = colname)
339356
357+ # Check if it is an enum
358+ if coldef[:type ] == " USER-DEFINED"
359+ basic_field[:is_enum ] = true
360+ end
361+
340362 basic_field[:field_type ] = field_type
341363 push! (struct_basic_fields, basic_field)
342364
@@ -362,6 +384,7 @@ function generate_object_model(
362384 onetomany_field[:is_manytoone ] = false
363385 onetomany_field[:is_onetoone ] = false
364386 onetomany_field[:is_onetomany ] = true
387+ onetomany_field[:is_enum ] = false
365388 onetomany_type_name_w_module = manytoone_field[:field_type ] # Public.Staff
366389
367390 # Build a field_name using one of the following options:
@@ -496,6 +519,11 @@ function generate_structs_from_object_model(object_model::Dict, outdir::String)
496519 f[:field_type ]
497520 end
498521 str = " $field_name ::Union{Missing,$field_type }\n "
522+
523+ if f[:name ] == " policeOfficerRequests"
524+ @info " $(f[:name ]) -> $str "
525+ end
526+
499527 _struct[:struct_content ] *= str
500528 end
501529
0 commit comments