1use crate::error::DbError;
13use crate::postgres::Database;
14use sqlx::postgres::PgConnection;
15use std::future::poll_fn;
16
17const COPY_SEND_BUFFER_BYTES: usize = 64 * 1024;
19
20fn validate_copy_sql(sql: &str) -> Result<&str, DbError> {
25 let trimmed = sql.trim();
26 if trimmed.is_empty() {
27 return Err(DbError::InvalidArgument(
28 "PostgreSQL COPY 语句不能为空".to_string(),
29 ));
30 }
31 Ok(trimmed)
32}
33
34fn append_csv_row(buffer: &mut Vec<u8>, row: &[u8]) {
38 buffer.extend_from_slice(row);
39 if !row.ends_with(b"\n") {
40 buffer.push(b'\n');
41 }
42}
43
44impl Database {
45 pub async fn copy_from_csv<I, R>(&self, sql: &str, rows: I) -> Result<u64, DbError>
56 where
57 I: IntoIterator<Item = R>,
58 R: AsRef<[u8]>,
59 {
60 let sql = validate_copy_sql(sql)?;
61 let mut conn = self.acquire().await?;
62 match copy_in_rows(&mut conn, sql, rows).await {
63 Ok(affected) => Ok(affected),
64 Err(error) => {
65 drop(conn.detach());
67 Err(error)
68 }
69 }
70 }
71
72 pub async fn copy_to_csv(&self, sql: &str) -> Result<Vec<u8>, DbError> {
83 let sql = validate_copy_sql(sql)?;
84 let mut conn = self.acquire().await?;
85 match copy_out_bytes(&mut conn, sql).await {
86 Ok(bytes) => Ok(bytes),
87 Err(error) => {
88 drop(conn.detach());
90 Err(error)
91 }
92 }
93 }
94}
95
96async fn copy_in_rows<I, R>(conn: &mut PgConnection, sql: &str, rows: I) -> Result<u64, DbError>
98where
99 I: IntoIterator<Item = R>,
100 R: AsRef<[u8]>,
101{
102 let mut copy = conn.copy_in_raw(sql).await?;
103 let mut buffer = Vec::with_capacity(COPY_SEND_BUFFER_BYTES);
104 for row in rows {
105 let row = row.as_ref();
106 if buffer.len() + row.len() + 1 > COPY_SEND_BUFFER_BYTES && !buffer.is_empty() {
107 copy.send(buffer.as_slice()).await?;
108 buffer.clear();
109 }
110 append_csv_row(&mut buffer, row);
111 }
112 if !buffer.is_empty() {
113 copy.send(buffer.as_slice()).await?;
114 }
115 Ok(copy.finish().await?)
116}
117
118async fn copy_out_bytes(conn: &mut PgConnection, sql: &str) -> Result<Vec<u8>, DbError> {
120 let mut stream = conn.copy_out_raw(sql).await?;
121 let mut bytes = Vec::new();
122 while let Some(chunk) = poll_fn(|cx| stream.as_mut().poll_next(cx)).await {
123 bytes.extend_from_slice(&chunk?);
124 }
125 Ok(bytes)
126}
127
128#[cfg(test)]
129#[allow(clippy::unwrap_used)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn validate_copy_sql_rejects_blank_statements() {
135 for blank in ["", " ", "\n\t"] {
136 assert!(
137 matches!(validate_copy_sql(blank), Err(DbError::InvalidArgument(_))),
138 "空语句应被拒绝: {blank:?}"
139 );
140 }
141 assert_eq!(
142 validate_copy_sql(" COPY t FROM STDIN ").unwrap(),
143 "COPY t FROM STDIN"
144 );
145 }
146
147 #[test]
148 fn append_csv_row_adds_missing_newline_and_preserves_existing() {
149 let mut buffer = Vec::new();
150 append_csv_row(&mut buffer, b"1,alice");
151 assert_eq!(buffer, b"1,alice\n");
152
153 append_csv_row(&mut buffer, b"2,bob\n");
155 assert_eq!(buffer, b"1,alice\n2,bob\n");
156
157 append_csv_row(&mut buffer, b"");
159 assert_eq!(buffer, b"1,alice\n2,bob\n\n");
160
161 let mut quoted = Vec::new();
163 append_csv_row(&mut quoted, b"\"a\nb\",c");
164 assert_eq!(quoted, b"\"a\nb\",c\n");
165 }
166}