1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{
commands::{CopyDirection, CopyFormat},
data_io::DataIO,
};
use super::{validation, SqlExecutor};
impl SqlExecutor {
pub fn handle_copy(
&mut self,
table: &str,
file_path: &str,
direction: CopyDirection,
format: CopyFormat,
) -> anyhow::Result<()> {
// Validate table name to prevent SQL injection
validation::validate_table_name(&self.db, table)?;
match direction {
CopyDirection::Export => {
// Execute SELECT * FROM table to get all data
let query = format!("SELECT * FROM {}", table);
let result = self.execute(&query)?;
// Export based on format
match format {
CopyFormat::Csv => DataIO::export_csv(&result, file_path)?,
CopyFormat::Json => DataIO::export_json(&result, file_path)?,
}
}
CopyDirection::Import => {
// Import based on format
match format {
CopyFormat::Csv => {
// Validate CSV columns before import
validation::validate_csv_columns(&self.db, file_path, table)?;
// Import CSV - generates INSERT statements
let insert_statements = DataIO::import_csv(file_path, table)?;
// Execute each INSERT statement
let mut success_count = 0;
for stmt in &insert_statements {
match self.execute(stmt) {
Ok(_) => success_count += 1,
Err(e) => {
eprintln!("Warning: Failed to insert row: {}", e);
// Continue with remaining rows
}
}
}
println!("Imported {} rows into '{}'", success_count, table);
}
CopyFormat::Json => {
// Validate JSON columns before import
validation::validate_json_columns(&self.db, file_path, table)?;
// Import JSON - generates INSERT statements
let insert_statements = DataIO::import_json(file_path, table)?;
// Execute each INSERT statement
let mut success_count = 0;
for stmt in &insert_statements {
match self.execute(stmt) {
Ok(_) => success_count += 1,
Err(e) => {
eprintln!("Warning: Failed to insert row: {}", e);
// Continue with remaining rows
}
}
}
println!("Imported {} rows into '{}'", success_count, table);
}
}
}
}
Ok(())
}
}