dmsc/database/
result.rs

1//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
2//!
3//! This file is part of DMSC.
4//! The DMSC project belongs to the Dunimd Team.
5//!
6//! Licensed under the Apache License, Version 2.0 (the "License");
7//! You may not use this file except in compliance with the License.
8//! You may obtain a copy of the License at
9//!
10//!     http://www.apache.org/licenses/LICENSE-2.0
11//!
12//! Unless required by applicable law or agreed to in writing, software
13//! distributed under the License is distributed on an "AS IS" BASIS,
14//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15//! See the License for the specific language governing permissions and
16//! limitations under the License.
17
18use crate::database::DMSCDBRow;
19use std::sync::Arc;
20
21#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
22#[derive(Debug, Clone)]
23pub struct DMSCDBResult {
24    rows: Arc<Vec<DMSCDBRow>>,
25    row_count: usize,
26    affected_rows: u64,
27    last_insert_id: Option<i64>,
28}
29
30impl DMSCDBResult {
31    pub fn new() -> Self {
32        Self {
33            rows: Arc::new(Vec::new()),
34            row_count: 0,
35            affected_rows: 0,
36            last_insert_id: None,
37        }
38    }
39
40    pub fn with_rows(rows: Vec<DMSCDBRow>) -> Self {
41        let row_count = rows.len();
42        Self {
43            rows: Arc::new(rows),
44            row_count,
45            affected_rows: row_count as u64,
46            last_insert_id: None,
47        }
48    }
49
50    pub fn with_affected_rows(affected_rows: u64) -> Self {
51        Self {
52            rows: Arc::new(Vec::new()),
53            row_count: 0,
54            affected_rows,
55            last_insert_id: None,
56        }
57    }
58
59    pub fn affected_rows(&self) -> u64 {
60        self.affected_rows
61    }
62
63    pub fn last_insert_id(&self) -> Option<i64> {
64        self.last_insert_id
65    }
66
67    pub fn set_last_insert_id(&mut self, id: i64) {
68        self.last_insert_id = Some(id);
69    }
70
71    pub fn is_empty(&self) -> bool {
72        self.row_count == 0
73    }
74
75    pub fn len(&self) -> usize {
76        self.row_count
77    }
78
79    pub fn row_count(&self) -> usize {
80        self.row_count
81    }
82
83    pub fn rows(&self) -> &[DMSCDBRow] {
84        &self.rows
85    }
86
87    pub fn iter(&self) -> impl Iterator<Item = &DMSCDBRow> {
88        self.rows.iter()
89    }
90
91    pub fn first(&self) -> Option<&DMSCDBRow> {
92        self.rows.first()
93    }
94
95    pub fn last(&self) -> Option<&DMSCDBRow> {
96        self.rows.last()
97    }
98
99    pub fn get(&self, index: usize) -> Option<&DMSCDBRow> {
100        self.rows.get(index)
101    }
102
103    pub fn into_rows(self) -> Vec<DMSCDBRow> {
104        Arc::try_unwrap(self.rows).unwrap_or_else(|arc| (*arc).clone())
105    }
106
107    pub fn to_vec(&self) -> Vec<DMSCDBRow> {
108        self.rows.iter().cloned().collect()
109    }
110}
111
112#[cfg(feature = "pyo3")]
113#[pyo3::prelude::pymethods]
114impl DMSCDBResult {
115    #[new]
116    fn py_new() -> Self {
117        Self::new()
118    }
119
120    fn get_affected_rows(&self) -> u64 {
121        self.affected_rows
122    }
123
124    fn get_last_insert_id(&self) -> Option<i64> {
125        self.last_insert_id
126    }
127
128    fn is_empty_result(&self) -> bool {
129        self.is_empty()
130    }
131
132    fn get_length(&self) -> usize {
133        self.len()
134    }
135
136    fn get_row_count(&self) -> usize {
137        self.row_count()
138    }
139
140    fn to_rows(&self) -> Vec<DMSCDBRow> {
141        self.to_vec()
142    }
143}
144
145impl Default for DMSCDBResult {
146    fn default() -> Self {
147        Self::new()
148    }
149}
150
151impl IntoIterator for DMSCDBResult {
152    type Item = DMSCDBRow;
153    type IntoIter = std::vec::IntoIter<DMSCDBRow>;
154
155    fn into_iter(self) -> Self::IntoIter {
156        self.into_rows().into_iter()
157    }
158}