|
513 | 513 | // } |
514 | 514 | // } |
515 | 515 |
|
516 | | -use std::future::IntoFuture; |
517 | 516 | use std::sync::Arc; |
518 | 517 |
|
519 | 518 | use deadpool_postgres::Object; |
520 | | -use pyo3::coroutine::Coroutine; |
521 | | -use pyo3::{exceptions::PyStopAsyncIteration, pyclass, pymethods, Py, PyAny, PyErr, Python}; |
522 | | -use pyo3::{pyfunction, PyObject}; |
| 519 | +use pyo3::{ |
| 520 | + exceptions::PyStopAsyncIteration, pyclass, pymethods, Py, PyAny, PyErr, PyObject, Python, |
| 521 | +}; |
523 | 522 |
|
524 | 523 | use crate::runtime::rustdriver_future; |
525 | 524 | use crate::{ |
526 | 525 | common::ObjectQueryTrait, |
527 | 526 | exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult}, |
528 | 527 | query_result::PSQLDriverPyQueryResult, |
529 | | - runtime::tokio_runtime, |
530 | 528 | }; |
531 | 529 |
|
532 | 530 | trait CursorObjectTrait { |
@@ -591,11 +589,6 @@ impl CursorObjectTrait for Object { |
591 | 589 | } |
592 | 590 | } |
593 | 591 |
|
594 | | -#[pyfunction] |
595 | | -async fn test() -> usize { |
596 | | - 42 |
597 | | -} |
598 | | - |
599 | 592 | #[pyclass] |
600 | 593 | pub struct Cursor { |
601 | 594 | db_transaction: Arc<Object>, |
@@ -710,7 +703,7 @@ impl Cursor { |
710 | 703 | let future = rustdriver_future(gil, async move { |
711 | 704 | let result = db_transaction |
712 | 705 | .psqlpy_query( |
713 | | - format!("FETCH {} FROM {}", fetch_number, cursor_name), |
| 706 | + format!("FETCH {fetch_number} FROM {cursor_name}"), |
714 | 707 | None, |
715 | 708 | Some(false), |
716 | 709 | ) |
@@ -792,7 +785,202 @@ impl Cursor { |
792 | 785 |
|
793 | 786 | let result = db_transaction |
794 | 787 | .psqlpy_query( |
795 | | - format!("FETCH {fetch_number} FROM {}", cursor_name), |
| 788 | + format!("FETCH {fetch_number} FROM {cursor_name}"), |
| 789 | + None, |
| 790 | + Some(false), |
| 791 | + ) |
| 792 | + .await?; |
| 793 | + Ok(result) |
| 794 | + } |
| 795 | + |
| 796 | + /// Fetch row from cursor. |
| 797 | + /// |
| 798 | + /// Execute FETCH NEXT. |
| 799 | + /// |
| 800 | + /// # Errors |
| 801 | + /// May return Err Result if cannot execute query. |
| 802 | + pub async fn fetch_next<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 803 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 804 | + let self_ = slf.borrow(gil); |
| 805 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 806 | + }); |
| 807 | + |
| 808 | + let result = db_transaction |
| 809 | + .psqlpy_query(format!("FETCH NEXT FROM {cursor_name}"), None, Some(false)) |
| 810 | + .await?; |
| 811 | + Ok(result) |
| 812 | + } |
| 813 | + |
| 814 | + /// Fetch previous from cursor. |
| 815 | + /// |
| 816 | + /// Execute FETCH PRIOR. |
| 817 | + /// |
| 818 | + /// # Errors |
| 819 | + /// May return Err Result if cannot execute query. |
| 820 | + pub async fn fetch_prior<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 821 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 822 | + let self_ = slf.borrow(gil); |
| 823 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 824 | + }); |
| 825 | + |
| 826 | + let result = db_transaction |
| 827 | + .psqlpy_query(format!("FETCH PRIOR FROM {cursor_name}"), None, Some(false)) |
| 828 | + .await?; |
| 829 | + Ok(result) |
| 830 | + } |
| 831 | + |
| 832 | + /// Fetch first row from cursor. |
| 833 | + /// |
| 834 | + /// Execute FETCH FIRST (same as ABSOLUTE 1) |
| 835 | + /// |
| 836 | + /// # Errors |
| 837 | + /// May return Err Result if cannot execute query. |
| 838 | + pub async fn fetch_first<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 839 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 840 | + let self_ = slf.borrow(gil); |
| 841 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 842 | + }); |
| 843 | + |
| 844 | + let result = db_transaction |
| 845 | + .psqlpy_query(format!("FETCH FIRST FROM {cursor_name}"), None, Some(false)) |
| 846 | + .await?; |
| 847 | + Ok(result) |
| 848 | + } |
| 849 | + |
| 850 | + /// Fetch last row from cursor. |
| 851 | + /// |
| 852 | + /// Execute FETCH LAST (same as ABSOLUTE -1) |
| 853 | + /// |
| 854 | + /// # Errors |
| 855 | + /// May return Err Result if cannot execute query. |
| 856 | + pub async fn fetch_last<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 857 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 858 | + let self_ = slf.borrow(gil); |
| 859 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 860 | + }); |
| 861 | + |
| 862 | + let result = db_transaction |
| 863 | + .psqlpy_query(format!("FETCH LAST FROM {cursor_name}"), None, Some(false)) |
| 864 | + .await?; |
| 865 | + Ok(result) |
| 866 | + } |
| 867 | + |
| 868 | + /// Fetch absolute row from cursor. |
| 869 | + /// |
| 870 | + /// Execute FETCH ABSOLUTE<absolute_number>. |
| 871 | + /// |
| 872 | + /// # Errors |
| 873 | + /// May return Err Result if cannot execute query. |
| 874 | + pub async fn fetch_absolute<'a>( |
| 875 | + slf: Py<Self>, |
| 876 | + absolute_number: i64, |
| 877 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 878 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 879 | + let self_ = slf.borrow(gil); |
| 880 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 881 | + }); |
| 882 | + |
| 883 | + let result = db_transaction |
| 884 | + .psqlpy_query( |
| 885 | + format!("FETCH ABSOLUTE {absolute_number} FROM {cursor_name}"), |
| 886 | + None, |
| 887 | + Some(false), |
| 888 | + ) |
| 889 | + .await?; |
| 890 | + Ok(result) |
| 891 | + } |
| 892 | + |
| 893 | + /// Fetch absolute row from cursor. |
| 894 | + /// |
| 895 | + /// Execute FETCH ABSOLUTE<absolute_number>. |
| 896 | + /// |
| 897 | + /// # Errors |
| 898 | + /// May return Err Result if cannot execute query. |
| 899 | + pub async fn fetch_relative<'a>( |
| 900 | + slf: Py<Self>, |
| 901 | + relative_number: i64, |
| 902 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 903 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 904 | + let self_ = slf.borrow(gil); |
| 905 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 906 | + }); |
| 907 | + |
| 908 | + let result = db_transaction |
| 909 | + .psqlpy_query( |
| 910 | + format!("FETCH RELATIVE {relative_number} FROM {cursor_name}"), |
| 911 | + None, |
| 912 | + Some(false), |
| 913 | + ) |
| 914 | + .await?; |
| 915 | + Ok(result) |
| 916 | + } |
| 917 | + |
| 918 | + /// Fetch forward all from cursor. |
| 919 | + /// |
| 920 | + /// Execute FORWARD ALL. |
| 921 | + /// |
| 922 | + /// # Errors |
| 923 | + /// May return Err Result if cannot execute query. |
| 924 | + pub async fn fetch_forward_all<'a>( |
| 925 | + slf: Py<Self>, |
| 926 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 927 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 928 | + let self_ = slf.borrow(gil); |
| 929 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 930 | + }); |
| 931 | + |
| 932 | + let result = db_transaction |
| 933 | + .psqlpy_query( |
| 934 | + format!("FETCH FORWARD ALL FROM {cursor_name}"), |
| 935 | + None, |
| 936 | + Some(false), |
| 937 | + ) |
| 938 | + .await?; |
| 939 | + Ok(result) |
| 940 | + } |
| 941 | + |
| 942 | + /// Fetch backward from cursor. |
| 943 | + /// |
| 944 | + /// Execute BACKWARD <backward_count>. |
| 945 | + /// |
| 946 | + /// # Errors |
| 947 | + /// May return Err Result if cannot execute query. |
| 948 | + pub async fn fetch_backward<'a>( |
| 949 | + slf: Py<Self>, |
| 950 | + backward_count: i64, |
| 951 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 952 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 953 | + let self_ = slf.borrow(gil); |
| 954 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 955 | + }); |
| 956 | + |
| 957 | + let result = db_transaction |
| 958 | + .psqlpy_query( |
| 959 | + format!("FETCH BACKWARD {backward_count} FROM {cursor_name}",), |
| 960 | + None, |
| 961 | + Some(false), |
| 962 | + ) |
| 963 | + .await?; |
| 964 | + Ok(result) |
| 965 | + } |
| 966 | + |
| 967 | + /// Fetch backward from cursor. |
| 968 | + /// |
| 969 | + /// Execute BACKWARD <backward_count>. |
| 970 | + /// |
| 971 | + /// # Errors |
| 972 | + /// May return Err Result if cannot execute query. |
| 973 | + pub async fn fetch_backward_all<'a>( |
| 974 | + slf: Py<Self>, |
| 975 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 976 | + let (db_transaction, cursor_name) = Python::with_gil(|gil| { |
| 977 | + let self_ = slf.borrow(gil); |
| 978 | + return (self_.db_transaction.clone(), self_.cursor_name.clone()); |
| 979 | + }); |
| 980 | + |
| 981 | + let result = db_transaction |
| 982 | + .psqlpy_query( |
| 983 | + format!("FETCH BACKWARD ALL FROM {cursor_name}"), |
796 | 984 | None, |
797 | 985 | Some(false), |
798 | 986 | ) |
|
0 commit comments