|
| 1 | +use crate::describe::Describe; |
| 2 | +use crate::error::Error; |
| 3 | +use crate::executor::{Execute, Executor}; |
| 4 | +use crate::logger::QueryLogger; |
| 5 | +use crate::odbc::{Odbc, OdbcColumn, OdbcConnection, OdbcQueryResult, OdbcRow, OdbcStatement, OdbcTypeInfo}; |
| 6 | +use either::Either; |
| 7 | +use futures_core::future::BoxFuture; |
| 8 | +use futures_core::stream::BoxStream; |
| 9 | +use futures_util::TryStreamExt; |
| 10 | +use std::pin::Pin; |
| 11 | +use odbc_api::Cursor; |
| 12 | +use std::borrow::Cow; |
| 13 | + |
| 14 | +impl OdbcConnection { |
| 15 | + async fn run<'e>( |
| 16 | + &'e mut self, |
| 17 | + sql: &'e str, |
| 18 | + ) -> Result<impl futures_core::Stream<Item = Result<Either<OdbcQueryResult, OdbcRow>, Error>> + 'e, Error> { |
| 19 | + let mut logger = QueryLogger::new(sql, self.log_settings.clone()); |
| 20 | + |
| 21 | + Ok(Box::pin(try_stream! { |
| 22 | + let guard = self.worker.shared.conn.lock().await; |
| 23 | + match guard.execute(sql, (), None) { |
| 24 | + Ok(Some(mut cursor)) => { |
| 25 | + use odbc_api::ResultSetMetadata; |
| 26 | + let mut columns = Vec::new(); |
| 27 | + if let Ok(count) = cursor.num_result_cols() { |
| 28 | + for i in 1..=count { // ODBC columns are 1-based |
| 29 | + let mut cd = odbc_api::ColumnDescription::default(); |
| 30 | + let _ = cursor.describe_col(i as u16, &mut cd); |
| 31 | + let name = String::from_utf8(cd.name).unwrap_or_else(|_| format!("col{}", i-1)); |
| 32 | + columns.push(OdbcColumn { name, type_info: OdbcTypeInfo { name: format!("{:?}", cd.data_type), is_null: false }, ordinal: (i-1) as usize }); |
| 33 | + } |
| 34 | + } |
| 35 | + while let Some(mut row) = cursor.next_row().map_err(|e| Error::from(e))? { |
| 36 | + let mut values = Vec::with_capacity(columns.len()); |
| 37 | + for i in 1..=columns.len() { |
| 38 | + let mut buf = Vec::new(); |
| 39 | + let not_null = row.get_text(i as u16, &mut buf).map_err(|e| Error::from(e))?; |
| 40 | + if not_null { |
| 41 | + let ti = OdbcTypeInfo { name: "TEXT".into(), is_null: false }; |
| 42 | + values.push((ti, Some(buf))); |
| 43 | + } else { |
| 44 | + let ti = OdbcTypeInfo { name: "TEXT".into(), is_null: true }; |
| 45 | + values.push((ti, None)); |
| 46 | + } |
| 47 | + } |
| 48 | + logger.increment_rows_returned(); |
| 49 | + r#yield!(Either::Right(OdbcRow { columns: columns.clone(), values })); |
| 50 | + } |
| 51 | + r#yield!(Either::Left(OdbcQueryResult { rows_affected: 0 })); |
| 52 | + } |
| 53 | + Ok(None) => { |
| 54 | + r#yield!(Either::Left(OdbcQueryResult { rows_affected: 0 })); |
| 55 | + } |
| 56 | + Err(e) => return Err(Error::from(e)), |
| 57 | + } |
| 58 | + Ok(()) |
| 59 | + })) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<'c> Executor<'c> for &'c mut OdbcConnection { |
| 64 | + type Database = Odbc; |
| 65 | + |
| 66 | + fn fetch_many<'e, 'q: 'e, E>( |
| 67 | + self, |
| 68 | + mut query: E, |
| 69 | + ) -> BoxStream<'e, Result<Either<OdbcQueryResult, OdbcRow>, Error>> |
| 70 | + where |
| 71 | + 'c: 'e, |
| 72 | + E: Execute<'q, Self::Database> + 'q, |
| 73 | + { |
| 74 | + let sql = query.sql(); |
| 75 | + Box::pin(try_stream! { |
| 76 | + let s = self.run(sql).await?; |
| 77 | + futures_util::pin_mut!(s); |
| 78 | + while let Some(v) = s.try_next().await? { r#yield!(v); } |
| 79 | + Ok(()) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + fn fetch_optional<'e, 'q: 'e, E>( |
| 84 | + self, |
| 85 | + query: E, |
| 86 | + ) -> BoxFuture<'e, Result<Option<OdbcRow>, Error>> |
| 87 | + where |
| 88 | + 'c: 'e, |
| 89 | + E: Execute<'q, Self::Database> + 'q, |
| 90 | + { |
| 91 | + let mut s = self.fetch_many(query); |
| 92 | + Box::pin(async move { |
| 93 | + while let Some(v) = s.try_next().await? { |
| 94 | + if let Either::Right(r) = v { return Ok(Some(r)); } |
| 95 | + } |
| 96 | + Ok(None) |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + fn prepare_with<'e, 'q: 'e>( |
| 101 | + self, |
| 102 | + sql: &'q str, |
| 103 | + _parameters: &'e [OdbcTypeInfo], |
| 104 | + ) -> BoxFuture<'e, Result<OdbcStatement<'q>, Error>> |
| 105 | + where |
| 106 | + 'c: 'e, |
| 107 | + { |
| 108 | + Box::pin(async move { |
| 109 | + // Basic statement metadata: no parameter/column info without executing |
| 110 | + Ok(OdbcStatement { sql: Cow::Borrowed(sql), columns: Vec::new(), parameters: 0 }) |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + #[doc(hidden)] |
| 115 | + fn describe<'e, 'q: 'e>(self, _sql: &'q str) -> BoxFuture<'e, Result<Describe<Odbc>, Error>> |
| 116 | + where |
| 117 | + 'c: 'e, |
| 118 | + { |
| 119 | + Box::pin(async move { Err(Error::Protocol("ODBC describe not implemented".into())) }) |
| 120 | + } |
| 121 | +} |
0 commit comments