Skip to content

Commit 7c2fad8

Browse files
committed
Allow the use of 'file' without 'cols'
This generates a one-to-many subtable for that single column.
1 parent fc86ca3 commit 7c2fad8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Diff for: src/main.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn gml_to_ewkb(cell: &RefCell<String>, coll: &[Geometry], bbox: Option<&BBox>, m
247247
true
248248
}
249249

250-
fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Settings, colspec: &'a [Yaml], fkey: Option<String>, cardinality: Cardinality) -> Table<'a> {
250+
fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Settings, colspec: &'a [Yaml], cardinality: Cardinality) -> Table<'a> {
251251
let mut table = Table::new(name, rowpath, outfile, settings, cardinality);
252252
for col in colspec {
253253
let colname = col["name"].as_str().unwrap_or_else(|| fatalerr!("Error: column has no 'name' entry in configuration file"));
@@ -276,21 +276,36 @@ fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Se
276276
(Some(_), Some(_)) => Cardinality::ManyToMany
277277
};
278278
let mut subtable: Option<Table> = match col["cols"].is_badvalue() {
279-
true => None,
279+
true => match cardinality {
280+
Cardinality::OneToMany => {
281+
let filename = col["file"].as_str().unwrap();
282+
if table.columns.is_empty() { fatalerr!("Error: table '{}' cannot have a subtable as first column", name); }
283+
let mut subtable = add_table(colname, &path, Some(filename), settings, &[], cardinality);
284+
subtable.columns.push(Column { name: colname.to_string(), path: path.clone(), datatype: datatype.to_string(), ..Default::default() });
285+
emit_preamble(&subtable, settings, Some(format!("{} {}", name, table.columns[0].datatype)));
286+
Some(subtable)
287+
},
288+
_ => None
289+
},
280290
false => {
281291
if norm.is_some() && col["file"].is_badvalue() { // Many-to-one relation (subtable with fkey in parent table)
282-
let subtable = add_table(colname, &path, norm, settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), None, cardinality);
292+
let subtable = add_table(colname, &path, norm, settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), cardinality);
293+
emit_preamble(&subtable, settings, None);
283294
Some(subtable)
284295
}
285296
else if norm.is_some() { // Many-to-many relation (this file will contain the crosslink table)
286297
let filename = col["file"].as_str().unwrap_or_else(|| fatalerr!("Error: subtable {} has no 'file' entry", colname));
287298
if table.columns.is_empty() { fatalerr!("Error: table '{}' cannot have a subtable as first column", name); }
288-
Some(add_table(colname, &path, Some(filename), settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), Some(format!("{} {}", name, table.columns[0].datatype)), cardinality))
299+
let subtable = add_table(colname, &path, Some(filename), settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), cardinality);
300+
emit_preamble(&subtable, settings, Some(format!("{} {}", name, table.columns[0].datatype)));
301+
Some(subtable)
289302
}
290303
else { // One-to-many relation (this file will contain the subtable with the parent table fkey)
291304
let filename = col["file"].as_str().unwrap_or_else(|| fatalerr!("Error: subtable {} has no 'file' entry", colname));
292305
if table.columns.is_empty() { fatalerr!("Error: table '{}' cannot have a subtable as first column", name); }
293-
Some(add_table(colname, &path, Some(filename), settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), Some(format!("{} {}", name, table.columns[0].datatype)), cardinality))
306+
let subtable = add_table(colname, &path, Some(filename), settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")), cardinality);
307+
emit_preamble(&subtable, settings, Some(format!("{} {}", name, table.columns[0].datatype)));
308+
Some(subtable)
294309
}
295310
}
296311
};
@@ -370,7 +385,6 @@ fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Se
370385
table.columns.push(column);
371386
}
372387

373-
emit_preamble(&table, settings, fkey);
374388
table
375389
}
376390
fn emit_preamble(table: &Table, settings: &Settings, fkey: Option<String>) {
@@ -465,7 +479,8 @@ fn main() {
465479
hush_notice: hush.contains("notice"),
466480
hush_warning: hush.contains("warning")
467481
};
468-
let maintable = add_table(name, rowpath, outfile, &settings, colspec, None, Cardinality::Default);
482+
let maintable = add_table(name, rowpath, outfile, &settings, colspec, Cardinality::Default);
483+
emit_preamble(&maintable, &settings, None);
469484
if !settings.skip.is_empty() {
470485
if !settings.skip.starts_with('/') { settings.skip.insert(0, '/'); }
471486
settings.skip.insert_str(0, &maintable.path); // Maintable path is normalized in add_table()

0 commit comments

Comments
 (0)