You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letmut subtable:Option<Table> = match col["cols"].is_badvalue(){
263
279
true => None,
264
280
false => {
265
281
if norm.is_some() && col["file"].is_badvalue(){// Many-to-one relation (subtable with fkey in parent table)
266
-
letmut subtable = add_table(colname,&path, norm, settings, col["cols"].as_vec().unwrap_or_else(|| fatalerr!("Error: subtable 'cols' entry is not an array")),None);
267
-
subtable.normalized = true;
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);
268
283
Some(subtable)
269
284
}
270
-
else{// If norm.is_some(): many-to-many relation (this file will contain the crosslink table)
271
-
// Otherwise: one-to-many relation (this file will contain the subtable with the parent table fkey)
285
+
elseif norm.is_some(){// Many-to-many relation (this file will contain the crosslink table)
286
+
let filename = col["file"].as_str().unwrap_or_else(|| fatalerr!("Error: subtable {} has no 'file' entry", colname));
287
+
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))
289
+
}
290
+
else{// One-to-many relation (this file will contain the subtable with the parent table fkey)
272
291
let filename = col["file"].as_str().unwrap_or_else(|| fatalerr!("Error: subtable {} has no 'file' entry", colname));
273
292
if table.columns.is_empty(){fatalerr!("Error: table '{}' cannot have a subtable as first column", name);}
274
-
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))))
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))
table.write(&format!("COPY {} ({}, {}) FROM stdin;\n", table.name, fkey.unwrap().split(' ').next().unwrap(), cols));
404
+
if table.cardinality == Cardinality::ManyToMany{
405
+
let parent = fkey.as_ref().unwrap().split_once(' ').unwrap().0;
406
+
table.write(&format!("COPY {}_{} ({}, {}) FROM stdin;\n", parent, table.name, parent, table.name));
407
+
}
408
+
else{
409
+
let cols = table.columns.iter().filter_map(|c| {
410
+
if c.hide || (c.subtable.is_some() && c.subtable.as_ref().unwrap().cardinality != Cardinality::ManyToOne){returnNone;}
411
+
Some(String::from(&c.name))
412
+
}).collect::<Vec<String>>().join(", ");
413
+
if fkey.is_some(){
414
+
table.write(&format!("COPY {} ({}, {}) FROM stdin;\n", table.name, fkey.unwrap().split(' ').next().unwrap(), cols));
415
+
}
416
+
else{ table.write(&format!("COPY {} ({}) FROM stdin;\n", table.name, cols));}
374
417
}
375
-
else{ table.write(&format!("COPY {} ({}) FROM stdin;\n", table.name, cols));}
376
418
}
377
419
}
378
420
@@ -423,7 +465,7 @@ fn main() {
423
465
hush_notice: hush.contains("notice"),
424
466
hush_warning: hush.contains("warning")
425
467
};
426
-
let maintable = add_table(name, rowpath, outfile,&settings, colspec,None);
468
+
let maintable = add_table(name, rowpath, outfile,&settings, colspec,None,Cardinality::Default);
427
469
if !settings.skip.is_empty(){
428
470
if !settings.skip.starts_with('/'){ settings.skip.insert(0,'/');}
429
471
settings.skip.insert_str(0,&maintable.path);// Maintable path is normalized in add_table()
@@ -647,7 +689,7 @@ fn main() {
647
689
}
648
690
else{
649
691
if !tables.is_empty(){// This is a subtable
650
-
if!table.normalized{// Write the first column value of the parent table as the first column of the subtable (for use as a foreign key)
692
+
if table.cardinality != Cardinality::ManyToOne{// Write the first column value of the parent table as the first column of the subtable (for use as a foreign key)
651
693
let key = tables.last().unwrap().columns[0].value.borrow();
652
694
if key.is_empty() && !settings.hush_warning{println!("Warning: subtable {} has no foreign key for parent (you may need to add a 'seri' column)", table.name);}
653
695
table.write(&format!("{}\t", key));
@@ -705,7 +747,7 @@ fn main() {
705
747
706
748
// Now write out the other column values
707
749
for i in0..table.columns.len(){
708
-
if table.columns[i].subtable.is_some() && !table.columns[i].subtable.as_ref().unwrap().normalized{continue;}
750
+
if table.columns[i].subtable.is_some() && table.columns[i].subtable.as_ref().unwrap().cardinality != Cardinality::ManyToOne{continue;}
0 commit comments