1use crate::row::Row;
38use crate::statement::ColumnInfo;
39
40#[derive(Debug, Clone)]
42pub struct ImplicitResult {
43 pub cursor_id: u16,
45 pub columns: Vec<ColumnInfo>,
47 pub rows: Vec<Row>,
49 pub has_more_rows: bool,
51}
52
53impl ImplicitResult {
54 pub fn new(cursor_id: u16, columns: Vec<ColumnInfo>, rows: Vec<Row>) -> Self {
56 Self {
57 cursor_id,
58 columns,
59 rows,
60 has_more_rows: false,
61 }
62 }
63
64 pub fn empty() -> Self {
66 Self {
67 cursor_id: 0,
68 columns: Vec::new(),
69 rows: Vec::new(),
70 has_more_rows: false,
71 }
72 }
73
74 pub fn column_count(&self) -> usize {
76 self.columns.len()
77 }
78
79 pub fn row_count(&self) -> usize {
81 self.rows.len()
82 }
83
84 pub fn is_empty(&self) -> bool {
86 self.rows.is_empty()
87 }
88
89 pub fn column(&self, name: &str) -> Option<&ColumnInfo> {
91 self.columns.iter().find(|c| c.name.eq_ignore_ascii_case(name))
92 }
93
94 pub fn iter(&self) -> impl Iterator<Item = &Row> {
96 self.rows.iter()
97 }
98}
99
100impl IntoIterator for ImplicitResult {
101 type Item = Row;
102 type IntoIter = std::vec::IntoIter<Row>;
103
104 fn into_iter(self) -> Self::IntoIter {
105 self.rows.into_iter()
106 }
107}
108
109#[derive(Debug, Clone, Default)]
111pub struct ImplicitResults {
112 pub results: Vec<ImplicitResult>,
114}
115
116impl ImplicitResults {
117 pub fn new() -> Self {
119 Self { results: Vec::new() }
120 }
121
122 pub fn add(&mut self, result: ImplicitResult) {
124 self.results.push(result);
125 }
126
127 pub fn len(&self) -> usize {
129 self.results.len()
130 }
131
132 pub fn is_empty(&self) -> bool {
134 self.results.is_empty()
135 }
136
137 pub fn get(&self, index: usize) -> Option<&ImplicitResult> {
139 self.results.get(index)
140 }
141
142 pub fn iter(&self) -> impl Iterator<Item = &ImplicitResult> {
144 self.results.iter()
145 }
146}
147
148impl IntoIterator for ImplicitResults {
149 type Item = ImplicitResult;
150 type IntoIter = std::vec::IntoIter<ImplicitResult>;
151
152 fn into_iter(self) -> Self::IntoIter {
153 self.results.into_iter()
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160 use crate::constants::OracleType;
161 use crate::row::Value;
162
163 #[test]
164 fn test_implicit_result_creation() {
165 let columns = vec![
166 ColumnInfo::new("ID", OracleType::Number),
167 ColumnInfo::new("NAME", OracleType::Varchar),
168 ];
169 let rows = vec![
170 Row::new(vec![Value::Integer(1), Value::String("Alice".to_string())]),
171 ];
172 let result = ImplicitResult::new(1, columns, rows);
173
174 assert_eq!(result.cursor_id, 1);
175 assert_eq!(result.column_count(), 2);
176 assert_eq!(result.row_count(), 1);
177 assert!(!result.is_empty());
178 }
179
180 #[test]
181 fn test_implicit_result_empty() {
182 let result = ImplicitResult::empty();
183 assert_eq!(result.cursor_id, 0);
184 assert!(result.is_empty());
185 }
186
187 #[test]
188 fn test_implicit_result_column_lookup() {
189 let columns = vec![
190 ColumnInfo::new("ID", OracleType::Number),
191 ColumnInfo::new("NAME", OracleType::Varchar),
192 ];
193 let result = ImplicitResult::new(1, columns, Vec::new());
194
195 assert!(result.column("ID").is_some());
196 assert!(result.column("id").is_some()); assert!(result.column("MISSING").is_none());
198 }
199
200 #[test]
201 fn test_implicit_results_collection() {
202 let mut results = ImplicitResults::new();
203 assert!(results.is_empty());
204
205 results.add(ImplicitResult::empty());
206 results.add(ImplicitResult::empty());
207
208 assert_eq!(results.len(), 2);
209 assert!(!results.is_empty());
210 assert!(results.get(0).is_some());
211 assert!(results.get(5).is_none());
212 }
213
214 #[test]
215 fn test_implicit_result_iterator() {
216 let rows = vec![
217 Row::new(vec![Value::Integer(1)]),
218 Row::new(vec![Value::Integer(2)]),
219 ];
220 let result = ImplicitResult::new(1, Vec::new(), rows);
221
222 let collected: Vec<_> = result.iter().collect();
223 assert_eq!(collected.len(), 2);
224 }
225
226 #[test]
227 fn test_implicit_results_iterator() {
228 let mut results = ImplicitResults::new();
229 results.add(ImplicitResult::new(1, Vec::new(), Vec::new()));
230 results.add(ImplicitResult::new(2, Vec::new(), Vec::new()));
231
232 let ids: Vec<_> = results.iter().map(|r| r.cursor_id).collect();
233 assert_eq!(ids, vec![1, 2]);
234 }
235}