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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::error::ProtonClientError;
use crate::{alias::Result, ProtonClient};
use clickhouse::{insert, inserter, Row};

impl ProtonClient {
    /// Inserts a new data item into Proton.
    ///
    /// Pass in the data to insert to add it to Proton.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name to insert into
    ///
    /// # Errors
    ///
    /// This method will return an error if:
    ///
    /// - The API call fails
    ///
    /// # Example
    /// ```no_run
    ///  use proton_client::ProtonClient;
    ///  use proton_client::prelude::{Result, Row};
    ///  use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Row, Serialize, Deserialize)]
    /// pub struct MyRow<'a> {
    ///     no: u32,
    ///     name: &'a str,
    /// }
    ///
    /// impl<'a> MyRow<'a> {
    ///     pub fn new(no: u32, name: &'a str) -> Self {
    ///         Self { no, name }
    ///     }
    /// }
    ///
    ///  async fn example() -> Result<()> {
    ///
    ///     let client = ProtonClient::new("http://localhost:8123");
    ///     let mut insert = client.insert("table_name").await?;
    ///
    ///    insert
    ///     .write(&MyRow::new(42, "foo"))
    ///     .await
    ///     .expect(" Failed to insert row into table some");
    ///
    ///     insert.end().await.expect("Failed to end insert");
    ///
    ///     Ok(())
    ///  }
    ///
    ///
    pub async fn insert<T: Row>(&self, table: &str) -> Result<insert::Insert<T>> {
        match self.client.insert(table) {
            Ok(insert) => Ok(insert),
            Err(err) => Err(ProtonClientError::InsertFailed(err.to_string())),
        }
    }
}

impl ProtonClient {
    /// Inserts bulk data into Proton.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name to insert into
    ///
    /// # Errors
    ///
    /// This method will return an error if:
    ///
    /// - The API call fails
    ///
    /// # Example
    /// ```no_run
    ///  use proton_client::prelude::{ProtonClient,Result, Row};
    ///  use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Row, Serialize, Deserialize)]
    /// pub struct MyRow<'a> {
    ///     no: u32,
    ///     name: &'a str,
    /// }
    ///
    ///  async fn example() -> Result<()> {
    ///
    ///     let client = ProtonClient::new("http://localhost:8123");
    ///      let mut inserter = client
    ///         .inserter("table_name")
    ///         .await
    ///         .expect("Failed to create inserter")
    ///         .with_max_entries(100_000); //  The maximum number of rows in one INSERT statement.
    ///
    ///     for i in 0..1000 {
    ///         inserter.write(&MyRow { no: i, name: "foo" }).await.expect("Failed to insert row");
    ///         inserter.commit().await.expect("Failed to commit"); // Checks limits and ends a current INSERT if they are reached.
    ///     }
    ///
    ///     inserter.end().await.expect("Failed to end inserter"); // Ends a current INSERT and whole Inserter unconditionally.
    ///
    ///     Ok(())
    ///  }
    ///
    pub async fn inserter<T: Row>(&self, table: &str) -> Result<inserter::Inserter<T>> {
        match self.client.inserter(table) {
            Ok(inserter) => Ok(inserter),
            Err(err) => Err(ProtonClientError::InserterFailed(err.to_string())),
        }
    }
}