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(table_name: i64) -> i32;
11	fn db_exist_schema(table_name: i64, schema: i64) -> i32;
12	fn db_exec(sql: i64) -> i32;
13	fn db_transaction(query: i64) -> i32;
14	fn db_transaction_async(query: i64, func0: i32, stack0: i64);
15	fn db_query(sql: i64, with_columns: i32) -> i64;
16	fn db_query_with_params(sql: i64, params: i64, with_columns: i32) -> i64;
17	fn db_insert(table_name: i64, values: i64);
18	fn db_exec_with_records(sql: i64, values: i64) -> i32;
19	fn db_query_with_params_async(sql: i64, params: i64, with_columns: i32, func0: i32, stack0: i64);
20	fn db_insert_async(table_name: i64, values: i64, func0: i32, stack0: i64);
21	fn db_exec_async(sql: i64, values: i64, func0: i32, stack0: i64);
22}
23use crate::dora::IObject;
24/// A struct that represents a database.
25pub struct DB { }
26impl DB {
27	/// Checks whether a table exists in the database.
28	///
29	/// # Arguments
30	///
31	/// * `table_name` - The name of the table to check.
32	///
33	/// # Returns
34	///
35	/// * `bool` - `true` if the table exists, `false` otherwise.
36	pub fn exist(table_name: &str) -> bool {
37		unsafe { return db_exist(crate::dora::from_string(table_name)) != 0; }
38	}
39	/// Checks whether a table exists in the database.
40	///
41	/// # Arguments
42	///
43	/// * `table_name` - The name of the table to check.
44	/// * `schema` - Optional. The name of the schema to check in.
45	///
46	/// # Returns
47	///
48	/// * `bool` - `true` if the table exists, `false` otherwise.
49	pub fn exist_schema(table_name: &str, schema: &str) -> bool {
50		unsafe { return db_exist_schema(crate::dora::from_string(table_name), crate::dora::from_string(schema)) != 0; }
51	}
52	/// Executes an SQL statement and returns the number of rows affected.
53	///
54	/// # Arguments
55	///
56	/// * `sql` - The SQL statement to execute.
57	///
58	/// # Returns
59	///
60	/// * `i32` - The number of rows affected by the statement.
61	pub fn exec(sql: &str) -> i32 {
62		unsafe { return db_exec(crate::dora::from_string(sql)); }
63	}
64	/// Executes a list of SQL statements as a single transaction.
65	///
66	/// # Arguments
67	///
68	/// * `query` - A list of SQL statements to execute.
69	///
70	/// # Returns
71	///
72	/// * `bool` - `true` if the transaction was successful, `false` otherwise.
73	pub fn transaction(query: crate::dora::DBQuery) -> bool {
74		unsafe { return db_transaction(query.raw()) != 0; }
75	}
76	/// Executes a list of SQL statements as a single transaction asynchronously.
77	///
78	/// # Arguments
79	///
80	/// * `sqls` - A list of SQL statements to execute.
81	/// * `callback` - A callback function that is invoked when the transaction is executed, receiving the result of the transaction.
82	///
83	/// # Returns
84	///
85	/// * `bool` - `true` if the transaction was successful, `false` otherwise.
86	pub fn transaction_async(query: crate::dora::DBQuery, mut callback: Box<dyn FnMut(bool)>) {
87		let mut stack0 = crate::dora::CallStack::new();
88		let stack_raw0 = stack0.raw();
89		let func_id0 = crate::dora::push_function(Box::new(move || {
90			callback(stack0.pop_bool().unwrap())
91		}));
92		unsafe { db_transaction_async(query.raw(), func_id0, stack_raw0); }
93	}
94	/// Executes an SQL query and returns the results as a list of rows.
95	///
96	/// # Arguments
97	///
98	/// * `sql` - The SQL statement to execute.
99	/// * `with_column` - Whether to include column names in the result.
100	///
101	/// # Returns
102	///
103	/// * `DBRecord` - A list of rows returned by the query.
104	pub fn query(sql: &str, with_columns: bool) -> crate::dora::DBRecord {
105		unsafe { return crate::dora::DBRecord::from(db_query(crate::dora::from_string(sql), if with_columns { 1 } else { 0 })); }
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	/// * `params` - A list of values to substitute into the SQL statement.
113	/// * `with_column` - Whether to include column names in the result.
114	///
115	/// # Returns
116	///
117	/// * `DBRecord` - A list of rows returned by the query.
118	pub fn query_with_params(sql: &str, params: &crate::dora::Array, with_columns: bool) -> crate::dora::DBRecord {
119		unsafe { return crate::dora::DBRecord::from(db_query_with_params(crate::dora::from_string(sql), params.raw(), if with_columns { 1 } else { 0 })); }
120	}
121	/// Inserts a row of data into a table within a transaction.
122	///
123	/// # Arguments
124	///
125	/// * `table_name` - The name of the table to insert into.
126	/// * `values` - The values to insert into the table.
127	///
128	/// # Returns
129	///
130	/// * `bool` - `true` if the insertion was successful, `false` otherwise.
131	pub fn insert(table_name: &str, values: crate::dora::DBParams) {
132		unsafe { db_insert(crate::dora::from_string(table_name), values.raw()); }
133	}
134	/// Executes an SQL statement and returns the number of rows affected.
135	///
136	/// # Arguments
137	///
138	/// * `sql` - The SQL statement to execute.
139	/// * `values` - Lists of values to substitute into the SQL statement.
140	///
141	/// # Returns
142	///
143	/// * `i32` - The number of rows affected by the statement.
144	pub fn exec_with_records(sql: &str, values: crate::dora::DBParams) -> i32 {
145		unsafe { return db_exec_with_records(crate::dora::from_string(sql), values.raw()); }
146	}
147	/// Executes an SQL query asynchronously and returns the results as a list of rows.
148	///
149	/// # Arguments
150	///
151	/// * `sql` - The SQL statement to execute.
152	/// * `params` - Optional. A list of values to substitute into the SQL statement.
153	/// * `with_column` - Optional. Whether to include column names in the result. Default is `false`.
154	/// * `callback` - A callback function that is invoked when the query is executed, receiving the results as a list of rows.
155	pub fn query_with_params_async(sql: &str, params: &crate::dora::Array, with_columns: bool, mut callback: Box<dyn FnMut(crate::dora::DBRecord)>) {
156		let mut stack0 = crate::dora::CallStack::new();
157		let stack_raw0 = stack0.raw();
158		let func_id0 = crate::dora::push_function(Box::new(move || {
159			callback(crate::dora::DBRecord::from(stack0.pop_i64().unwrap()))
160		}));
161		unsafe { db_query_with_params_async(crate::dora::from_string(sql), params.raw(), if with_columns { 1 } else { 0 }, func_id0, stack_raw0); }
162	}
163	/// Inserts a row of data into a table within a transaction asynchronously.
164	///
165	/// # Arguments
166	///
167	/// * `table_name` - The name of the table to insert into.
168	/// * `values` - The values to insert into the table.
169	/// * `callback` - A callback function that is invoked when the insertion is executed, receiving the result of the insertion.
170	pub fn insert_async(table_name: &str, values: crate::dora::DBParams, mut callback: Box<dyn FnMut(bool)>) {
171		let mut stack0 = crate::dora::CallStack::new();
172		let stack_raw0 = stack0.raw();
173		let func_id0 = crate::dora::push_function(Box::new(move || {
174			callback(stack0.pop_bool().unwrap())
175		}));
176		unsafe { db_insert_async(crate::dora::from_string(table_name), values.raw(), func_id0, stack_raw0); }
177	}
178	/// Executes an SQL statement with a list of values within a transaction asynchronously and returns the number of rows affected.
179	///
180	/// # Arguments
181	///
182	/// * `sql` - The SQL statement to execute.
183	/// * `values` - A list of values to substitute into the SQL statement.
184	/// * `callback` - A callback function that is invoked when the statement is executed, recieving the number of rows affected.
185	pub fn exec_async(sql: &str, values: crate::dora::DBParams, mut callback: Box<dyn FnMut(i64)>) {
186		let mut stack0 = crate::dora::CallStack::new();
187		let stack_raw0 = stack0.raw();
188		let func_id0 = crate::dora::push_function(Box::new(move || {
189			callback(stack0.pop_i64().unwrap())
190		}));
191		unsafe { db_exec_async(crate::dora::from_string(sql), values.raw(), func_id0, stack_raw0); }
192	}
193}