dora_ssr/dora/
db.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn db_exist_db(db_name: i64) -> i32;
11	fn db_exist(table_name: i64) -> i32;
12	fn db_exist_schema(table_name: i64, schema: i64) -> i32;
13	fn db_exec(sql: i64) -> i32;
14	fn db_transaction(query: i64) -> i32;
15	fn db_transaction_async(query: i64, func0: i32, stack0: i64);
16	fn db_query(sql: i64, with_columns: i32) -> i64;
17	fn db_query_with_params(sql: i64, params: i64, with_columns: i32) -> i64;
18	fn db_insert(table_name: i64, values: i64);
19	fn db_exec_with_records(sql: i64, values: i64) -> i32;
20	fn db_query_with_params_async(sql: i64, params: i64, with_columns: i32, func0: i32, stack0: i64);
21	fn db_insert_async(table_name: i64, values: i64, func0: i32, stack0: i64);
22	fn db_exec_async(sql: i64, values: i64, func0: i32, stack0: i64);
23}
24use crate::dora::IObject;
25/// A struct that represents a database.
26pub struct DB { }
27impl DB {
28	/// Checks whether a database exists.
29	///
30	/// # Arguments
31	///
32	/// * `db_name` - The name of the database to check.
33	///
34	/// # Returns
35	///
36	/// * `bool` - `true` if the database exists, `false` otherwise.
37	pub fn exist_db(db_name: &str) -> bool {
38		unsafe { return db_exist_db(crate::dora::from_string(db_name)) != 0; }
39	}
40	/// Checks whether a table exists in the database.
41	///
42	/// # Arguments
43	///
44	/// * `table_name` - The name of the table to check.
45	///
46	/// # Returns
47	///
48	/// * `bool` - `true` if the table exists, `false` otherwise.
49	pub fn exist(table_name: &str) -> bool {
50		unsafe { return db_exist(crate::dora::from_string(table_name)) != 0; }
51	}
52	/// Checks whether a table exists in the database.
53	///
54	/// # Arguments
55	///
56	/// * `table_name` - The name of the table to check.
57	/// * `schema` - Optional. The name of the schema to check in.
58	///
59	/// # Returns
60	///
61	/// * `bool` - `true` if the table exists, `false` otherwise.
62	pub fn exist_schema(table_name: &str, schema: &str) -> bool {
63		unsafe { return db_exist_schema(crate::dora::from_string(table_name), crate::dora::from_string(schema)) != 0; }
64	}
65	/// Executes an SQL statement and returns the number of rows affected.
66	///
67	/// # Arguments
68	///
69	/// * `sql` - The SQL statement to execute.
70	///
71	/// # Returns
72	///
73	/// * `i32` - The number of rows affected by the statement.
74	pub fn exec(sql: &str) -> i32 {
75		unsafe { return db_exec(crate::dora::from_string(sql)); }
76	}
77	/// Executes a list of SQL statements as a single transaction.
78	///
79	/// # Arguments
80	///
81	/// * `query` - A list of SQL statements to execute.
82	///
83	/// # Returns
84	///
85	/// * `bool` - `true` if the transaction was successful, `false` otherwise.
86	pub fn transaction(query: crate::dora::DBQuery) -> bool {
87		unsafe { return db_transaction(query.raw()) != 0; }
88	}
89	/// Executes a list of SQL statements as a single transaction asynchronously.
90	///
91	/// # Arguments
92	///
93	/// * `sqls` - A list of SQL statements to execute.
94	/// * `callback` - A callback function that is invoked when the transaction is executed, receiving the result of the transaction.
95	///
96	/// # Returns
97	///
98	/// * `bool` - `true` if the transaction was successful, `false` otherwise.
99	pub fn transaction_async(query: crate::dora::DBQuery, mut callback: Box<dyn FnMut(bool)>) {
100		let mut stack0 = crate::dora::CallStack::new();
101		let stack_raw0 = stack0.raw();
102		let func_id0 = crate::dora::push_function(Box::new(move || {
103			callback(stack0.pop_bool().unwrap())
104		}));
105		unsafe { db_transaction_async(query.raw(), func_id0, stack_raw0); }
106	}
107	/// Executes an SQL query and returns the results as a list of rows.
108	///
109	/// # Arguments
110	///
111	/// * `sql` - The SQL statement to execute.
112	/// * `with_column` - Whether to include column names in the result.
113	///
114	/// # Returns
115	///
116	/// * `DBRecord` - A list of rows returned by the query.
117	pub fn query(sql: &str, with_columns: bool) -> crate::dora::DBRecord {
118		unsafe { return crate::dora::DBRecord::from(db_query(crate::dora::from_string(sql), if with_columns { 1 } else { 0 })); }
119	}
120	/// Executes an SQL query and returns the results as a list of rows.
121	///
122	/// # Arguments
123	///
124	/// * `sql` - The SQL statement to execute.
125	/// * `params` - A list of values to substitute into the SQL statement.
126	/// * `with_column` - Whether to include column names in the result.
127	///
128	/// # Returns
129	///
130	/// * `DBRecord` - A list of rows returned by the query.
131	pub fn query_with_params(sql: &str, params: &crate::dora::Array, with_columns: bool) -> crate::dora::DBRecord {
132		unsafe { return crate::dora::DBRecord::from(db_query_with_params(crate::dora::from_string(sql), params.raw(), if with_columns { 1 } else { 0 })); }
133	}
134	/// Inserts a row of data into a table within a transaction.
135	///
136	/// # Arguments
137	///
138	/// * `table_name` - The name of the table to insert into.
139	/// * `values` - The values to insert into the table.
140	///
141	/// # Returns
142	///
143	/// * `bool` - `true` if the insertion was successful, `false` otherwise.
144	pub fn insert(table_name: &str, values: crate::dora::DBParams) {
145		unsafe { db_insert(crate::dora::from_string(table_name), values.raw()); }
146	}
147	/// Executes an SQL statement and returns the number of rows affected.
148	///
149	/// # Arguments
150	///
151	/// * `sql` - The SQL statement to execute.
152	/// * `values` - Lists of values to substitute into the SQL statement.
153	///
154	/// # Returns
155	///
156	/// * `i32` - The number of rows affected by the statement.
157	pub fn exec_with_records(sql: &str, values: crate::dora::DBParams) -> i32 {
158		unsafe { return db_exec_with_records(crate::dora::from_string(sql), values.raw()); }
159	}
160	/// Executes an SQL query asynchronously and returns the results as a list of rows.
161	///
162	/// # Arguments
163	///
164	/// * `sql` - The SQL statement to execute.
165	/// * `params` - Optional. A list of values to substitute into the SQL statement.
166	/// * `with_column` - Optional. Whether to include column names in the result. Default is `false`.
167	/// * `callback` - A callback function that is invoked when the query is executed, receiving the results as a list of rows.
168	pub fn query_with_params_async(sql: &str, params: &crate::dora::Array, with_columns: bool, mut callback: Box<dyn FnMut(crate::dora::DBRecord)>) {
169		let mut stack0 = crate::dora::CallStack::new();
170		let stack_raw0 = stack0.raw();
171		let func_id0 = crate::dora::push_function(Box::new(move || {
172			callback(crate::dora::DBRecord::from(stack0.pop_i64().unwrap()))
173		}));
174		unsafe { db_query_with_params_async(crate::dora::from_string(sql), params.raw(), if with_columns { 1 } else { 0 }, func_id0, stack_raw0); }
175	}
176	/// Inserts a row of data into a table within a transaction asynchronously.
177	///
178	/// # Arguments
179	///
180	/// * `table_name` - The name of the table to insert into.
181	/// * `values` - The values to insert into the table.
182	/// * `callback` - A callback function that is invoked when the insertion is executed, receiving the result of the insertion.
183	pub fn insert_async(table_name: &str, values: crate::dora::DBParams, mut callback: Box<dyn FnMut(bool)>) {
184		let mut stack0 = crate::dora::CallStack::new();
185		let stack_raw0 = stack0.raw();
186		let func_id0 = crate::dora::push_function(Box::new(move || {
187			callback(stack0.pop_bool().unwrap())
188		}));
189		unsafe { db_insert_async(crate::dora::from_string(table_name), values.raw(), func_id0, stack_raw0); }
190	}
191	/// Executes an SQL statement with a list of values within a transaction asynchronously and returns the number of rows affected.
192	///
193	/// # Arguments
194	///
195	/// * `sql` - The SQL statement to execute.
196	/// * `values` - A list of values to substitute into the SQL statement.
197	/// * `callback` - A callback function that is invoked when the statement is executed, recieving the number of rows affected.
198	pub fn exec_async(sql: &str, values: crate::dora::DBParams, mut callback: Box<dyn FnMut(i64)>) {
199		let mut stack0 = crate::dora::CallStack::new();
200		let stack_raw0 = stack0.raw();
201		let func_id0 = crate::dora::push_function(Box::new(move || {
202			callback(stack0.pop_i64().unwrap())
203		}));
204		unsafe { db_exec_async(crate::dora::from_string(sql), values.raw(), func_id0, stack_raw0); }
205	}
206}