1- use pyo3:: { prelude:: * , pyclass, pymethods, types:: PyDict , IntoPyObjectExt , Py , PyAny , Python } ;
1+ use pyo3:: {
2+ prelude:: * ,
3+ pyclass, pymethods,
4+ types:: { PyDict , PyTuple } ,
5+ IntoPyObjectExt , Py , PyAny , Python ,
6+ } ;
27use tokio_postgres:: Row ;
38
49use crate :: { exceptions:: rust_errors:: PSQLPyResult , value_converter:: to_python:: postgres_to_py} ;
@@ -15,7 +20,7 @@ fn row_to_dict<'a>(
1520 py : Python < ' a > ,
1621 postgres_row : & ' a Row ,
1722 custom_decoders : & Option < Py < PyDict > > ,
18- ) -> PSQLPyResult < pyo3 :: Bound < ' a , PyDict > > {
23+ ) -> PSQLPyResult < Bound < ' a , PyDict > > {
1924 let python_dict = PyDict :: new ( py) ;
2025 for ( column_idx, column) in postgres_row. columns ( ) . iter ( ) . enumerate ( ) {
2126 let python_type = postgres_to_py ( py, postgres_row, column, column_idx, custom_decoders) ?;
@@ -24,6 +29,29 @@ fn row_to_dict<'a>(
2429 Ok ( python_dict)
2530}
2631
32+ /// Convert postgres `Row` into Python Tuple.
33+ ///
34+ /// # Errors
35+ ///
36+ /// May return Err Result if can not convert
37+ /// postgres type to python or set new key-value pair
38+ /// in python dict.
39+ #[ allow( clippy:: ref_option) ]
40+ fn row_to_tuple < ' a > (
41+ py : Python < ' a > ,
42+ postgres_row : & ' a Row ,
43+ custom_decoders : & Option < Py < PyDict > > ,
44+ ) -> PSQLPyResult < Bound < ' a , PyTuple > > {
45+ let mut rows: Vec < Bound < ' _ , PyTuple > > = vec ! [ ] ;
46+
47+ for ( column_idx, column) in postgres_row. columns ( ) . iter ( ) . enumerate ( ) {
48+ let python_type = postgres_to_py ( py, postgres_row, column, column_idx, custom_decoders) ?;
49+ let timed_tuple = PyTuple :: new ( py, vec ! [ column. name( ) . into_py_any( py) ?, python_type] ) ?;
50+ rows. push ( timed_tuple) ;
51+ }
52+ Ok ( PyTuple :: new ( py, rows) ?)
53+ }
54+
2755#[ pyclass( name = "QueryResult" ) ]
2856#[ allow( clippy:: module_name_repetitions) ]
2957pub struct PSQLDriverPyQueryResult {
@@ -56,18 +84,29 @@ impl PSQLDriverPyQueryResult {
5684 /// May return Err Result if can not convert
5785 /// postgres type to python or set new key-value pair
5886 /// in python dict.
59- #[ pyo3( signature = ( custom_decoders=None ) ) ]
87+ #[ pyo3( signature = ( custom_decoders=None , as_tuple= None ) ) ]
6088 #[ allow( clippy:: needless_pass_by_value) ]
6189 pub fn result (
6290 & self ,
6391 py : Python < ' _ > ,
6492 custom_decoders : Option < Py < PyDict > > ,
93+ as_tuple : Option < bool > ,
6594 ) -> PSQLPyResult < Py < PyAny > > {
66- let mut result: Vec < pyo3:: Bound < ' _ , PyDict > > = vec ! [ ] ;
95+ let as_tuple = as_tuple. unwrap_or ( false ) ;
96+
97+ if as_tuple {
98+ let mut tuple_rows: Vec < Bound < ' _ , PyTuple > > = vec ! [ ] ;
99+ for row in & self . inner {
100+ tuple_rows. push ( row_to_tuple ( py, row, & custom_decoders) ?) ;
101+ }
102+ return Ok ( tuple_rows. into_py_any ( py) ?) ;
103+ }
104+
105+ let mut dict_rows: Vec < Bound < ' _ , PyDict > > = vec ! [ ] ;
67106 for row in & self . inner {
68- result . push ( row_to_dict ( py, row, & custom_decoders) ?) ;
107+ dict_rows . push ( row_to_dict ( py, row, & custom_decoders) ?) ;
69108 }
70- Ok ( result . into_py_any ( py) ?)
109+ Ok ( dict_rows . into_py_any ( py) ?)
71110 }
72111
73112 /// Convert result from database to any class passed from Python.
@@ -143,12 +182,19 @@ impl PSQLDriverSinglePyQueryResult {
143182 /// postgres type to python, can not set new key-value pair
144183 /// in python dict or there are no result.
145184 #[ allow( clippy:: needless_pass_by_value) ]
146- #[ pyo3( signature = ( custom_decoders=None ) ) ]
185+ #[ pyo3( signature = ( custom_decoders=None , as_tuple= None ) ) ]
147186 pub fn result (
148187 & self ,
149188 py : Python < ' _ > ,
150189 custom_decoders : Option < Py < PyDict > > ,
190+ as_tuple : Option < bool > ,
151191 ) -> PSQLPyResult < Py < PyAny > > {
192+ let as_tuple = as_tuple. unwrap_or ( false ) ;
193+
194+ if as_tuple {
195+ return Ok ( row_to_tuple ( py, & self . inner , & custom_decoders) ?. into_py_any ( py) ?) ;
196+ }
197+
152198 Ok ( row_to_dict ( py, & self . inner , & custom_decoders) ?. into_py_any ( py) ?)
153199 }
154200
0 commit comments