-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathmod.rs
249 lines (246 loc) · 10.1 KB
/
mod.rs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
mod destination;
mod dispatcher;
pub mod get_meta;
mod pandas_columns;
mod pystring;
mod transports;
mod typesystem;
pub use self::destination::{PandasBlockInfo, PandasDestination, PandasPartitionDestination};
use self::dispatcher::PandasDispatcher;
pub use self::transports::{
BigQueryPandasTransport, MsSQLPandasTransport, MysqlPandasTransport, OraclePandasTransport,
PostgresPandasTransport, SqlitePandasTransport, TrinoPandasTransport,
};
pub use self::typesystem::{PandasDType, PandasTypeSystem};
use crate::errors::ConnectorXPythonError;
use connectorx::source_router::{SourceConn, SourceType};
use connectorx::sources::oracle::OracleSource;
use connectorx::{
prelude::*,
sources::{
mysql::{BinaryProtocol as MySQLBinaryProtocol, TextProtocol},
postgres::{
rewrite_tls_args, BinaryProtocol as PgBinaryProtocol, CSVProtocol, CursorProtocol,
SimpleProtocol,
},
},
sql::CXQuery,
};
use fehler::throws;
use log::debug;
use postgres::NoTls;
use postgres_openssl::MakeTlsConnector;
use pyo3::prelude::*;
use std::sync::Arc;
#[throws(ConnectorXPythonError)]
pub fn write_pandas<'a, 'py: 'a>(
py: Python<'py>,
source_conn: &SourceConn,
origin_query: Option<String>,
queries: &[CXQuery<String>],
pre_execution_queries: Option<&[String]>,
) -> Bound<'py, PyAny> {
let destination = PandasDestination::new();
let protocol = source_conn.proto.as_str();
debug!("Protocol: {}", protocol);
match source_conn.ty {
SourceType::Postgres => {
let (config, tls) = rewrite_tls_args(&source_conn.conn)?;
match (protocol, tls) {
("csv", Some(tls_conn)) => {
let sb = PostgresSource::<CSVProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let mut dispatcher = PandasDispatcher::<
_,
PostgresPandasTransport<CSVProtocol, MakeTlsConnector>,
>::new(
sb, destination, queries, origin_query
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("csv", None) => {
let sb =
PostgresSource::<CSVProtocol, NoTls>::new(config, NoTls, queries.len())?;
let mut dispatcher = PandasDispatcher::<
_,
PostgresPandasTransport<CSVProtocol, NoTls>,
>::new(
sb, destination, queries, origin_query
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("binary", Some(tls_conn)) => {
let sb = PostgresSource::<PgBinaryProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let mut dispatcher =
PandasDispatcher::<
_,
PostgresPandasTransport<PgBinaryProtocol, MakeTlsConnector>,
>::new(sb, destination, queries, origin_query);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("binary", None) => {
let sb = PostgresSource::<PgBinaryProtocol, NoTls>::new(
config,
NoTls,
queries.len(),
)?;
let mut dispatcher = PandasDispatcher::<
_,
PostgresPandasTransport<PgBinaryProtocol, NoTls>,
>::new(
sb, destination, queries, origin_query
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("cursor", Some(tls_conn)) => {
let sb = PostgresSource::<CursorProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let mut dispatcher =
PandasDispatcher::<
_,
PostgresPandasTransport<CursorProtocol, MakeTlsConnector>,
>::new(sb, destination, queries, origin_query);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("cursor", None) => {
let sb =
PostgresSource::<CursorProtocol, NoTls>::new(config, NoTls, queries.len())?;
let mut dispatcher = PandasDispatcher::<
_,
PostgresPandasTransport<CursorProtocol, NoTls>,
>::new(
sb, destination, queries, origin_query
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("simple", Some(tls_conn)) => {
let sb = PostgresSource::<SimpleProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let mut dispatcher =
PandasDispatcher::<
_,
PostgresPandasTransport<SimpleProtocol, MakeTlsConnector>,
>::new(sb, destination, queries, origin_query);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
("simple", None) => {
let sb =
PostgresSource::<SimpleProtocol, NoTls>::new(config, NoTls, queries.len())?;
let mut dispatcher = PandasDispatcher::<
_,
PostgresPandasTransport<SimpleProtocol, NoTls>,
>::new(
sb, destination, queries, origin_query
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
_ => unimplemented!("{} protocol not supported", protocol),
}
}
SourceType::SQLite => {
// remove the first "sqlite://" manually since url.path is not correct for windows
let path = &source_conn.conn.as_str()[9..];
let source = SQLiteSource::new(path, queries.len())?;
let dispatcher = PandasDispatcher::<_, SqlitePandasTransport>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.run(py)?
}
SourceType::MySQL => match protocol {
"binary" => {
let source =
MySQLSource::<MySQLBinaryProtocol>::new(&source_conn.conn[..], queries.len())?;
let mut dispatcher =
PandasDispatcher::<_, MysqlPandasTransport<MySQLBinaryProtocol>>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
"text" => {
let source =
MySQLSource::<TextProtocol>::new(&source_conn.conn[..], queries.len())?;
let mut dispatcher = PandasDispatcher::<_, MysqlPandasTransport<TextProtocol>>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.set_pre_execution_queries(pre_execution_queries);
dispatcher.run(py)?
}
_ => unimplemented!("{} protocol not supported", protocol),
},
SourceType::MsSQL => {
let rt = Arc::new(tokio::runtime::Runtime::new().expect("Failed to create runtime"));
let source = MsSQLSource::new(rt, &source_conn.conn[..], queries.len())?;
let dispatcher = PandasDispatcher::<_, MsSQLPandasTransport>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.run(py)?
}
SourceType::Oracle => {
let source = OracleSource::new(&source_conn.conn[..], queries.len())?;
let dispatcher = PandasDispatcher::<_, OraclePandasTransport>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.run(py)?
}
SourceType::BigQuery => {
let rt = Arc::new(tokio::runtime::Runtime::new().expect("Failed to create runtime"));
let source = BigQuerySource::new(rt, &source_conn.conn[..])?;
let dispatcher = PandasDispatcher::<_, BigQueryPandasTransport>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.run(py)?
}
SourceType::Trino => {
let rt = Arc::new(tokio::runtime::Runtime::new().expect("Failed to create runtime"));
let source = TrinoSource::new(rt, &source_conn.conn[..])?;
let dispatcher = PandasDispatcher::<TrinoSource, TrinoPandasTransport>::new(
source,
destination,
queries,
origin_query,
);
dispatcher.run(py)?
}
_ => unimplemented!("{:?} not implemented!", source_conn.ty),
}
}