aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: d3c286b9de174d84eb426f5ab9343decb6fd9738 (plain)
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
80
81
82
83
84
85
86
87
use clap::Parser;
use rusqlite;
use yaml_rust::yaml;


#[derive(clap::Parser, Debug)]
#[clap(version)]
struct Args {
    #[clap(subcommand)]
    command: Command,
}

#[derive(clap::Subcommand, Debug)]
enum Command {
    Insert {
        #[clap(long)]
        database: String,

        table_name: String,

        input_file: Option<String>,
    },

    Select {
        #[clap(long)]
        database: String,

        table_name: String,

        #[clap(long)]
        primary_key: Option<String>,
        record_id: String,

        #[clap(long)]
        exclude_column: Vec<String>,
    },
}


fn main() {
    let args = Args::parse();

    match args.command {
        Command::Insert {
            database,
            table_name,
            input_file,
        } => {
            let input_file = match &input_file {
                Some(f) => f,
                None => "-",
            };

            let mut dbconn = rusqlite::Connection::open(database).unwrap();

            let mut text_data;
            if input_file == "-" {
                use std::io::Read;

                text_data = String::new();
                std::io::stdin().read_to_string(&mut text_data).unwrap();
            } else {
                text_data = std::fs::read_to_string(input_file).unwrap();
            }

            let mut yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap();

            yaqlite::insert(&mut dbconn, &table_name, &mut yaml_data).unwrap();

            dbconn.close().unwrap();
        },

        Command::Select {
            database,
            table_name,
            primary_key,
            record_id,
            exclude_column,
        } => {
            let dbconn = rusqlite::Connection::open(database).unwrap();

            yaqlite::select(&dbconn, &table_name, &record_id).unwrap();

            dbconn.close().unwrap();
        },
    };
}