Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit dbabf38

Browse files
authored
Merge pull request #96 from RedBeardLab/debug_channel_close
Debug channel close
2 parents 9888fca + 3dc7cf2 commit dbabf38

File tree

7 files changed

+250
-132
lines changed

7 files changed

+250
-132
lines changed

parser/src/statement.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use redisql_lib::redis::Command;
2+
use redisql_lib::redis::ReturnMethod;
23
use redisql_lib::redis_type::BlockedClient;
34
use redisql_lib::redisql_error::RediSQLError;
45

@@ -10,13 +11,14 @@ pub enum Action {
1011
Update,
1112
New,
1213
Show,
14+
List,
1315
}
1416

1517
#[derive(Debug, PartialEq, Clone)]
1618
pub struct Statement<'s> {
1719
database: &'s str,
1820
action: Action,
19-
stmt_name: &'s str,
21+
stmt_name: Option<&'s str>,
2022
stmt_query: Option<&'s str>,
2123
now: bool,
2224
can_update: bool,
@@ -27,22 +29,26 @@ impl Statement<'static> {
2729
pub fn get_command(self, client: BlockedClient) -> Command {
2830
match self.action {
2931
Action::Delete => Command::DeleteStatement {
30-
identifier: self.stmt_name,
32+
identifier: self.stmt_name.unwrap(),
3133
client,
3234
},
3335
Action::Update => Command::UpdateStatement {
34-
identifier: self.stmt_name,
36+
identifier: self.stmt_name.unwrap(),
3537
statement: self.stmt_query.unwrap(),
3638
can_create: self.can_create,
3739
client,
3840
},
3941
Action::New => Command::CompileStatement {
40-
identifier: self.stmt_name,
42+
identifier: self.stmt_name.unwrap(),
4143
statement: self.stmt_query.unwrap(),
4244
can_update: self.can_update,
4345
client,
4446
},
4547
Action::Show => todo!(),
48+
Action::List => Command::ListStatements {
49+
return_method: ReturnMethod::ReplyWithHeader,
50+
client,
51+
},
4652
}
4753
}
4854
pub fn is_now(&self) -> bool {
@@ -52,7 +58,7 @@ impl Statement<'static> {
5258
self.action
5359
}
5460
pub fn identifier(&self) -> &'static str {
55-
self.stmt_name
61+
self.stmt_name.unwrap()
5662
}
5763
pub fn statement(&self) -> &'static str {
5864
self.stmt_query.unwrap()
@@ -83,15 +89,24 @@ impl<'s> CommandV2<'s> for Statement<'s> {
8389
"UPDATE" => Action::Update,
8490
"NEW" => Action::New,
8591
"SHOW" => Action::Show,
92+
"LIST" => Action::List,
8693
_ => return Err(RediSQLError::with_code(23,
8794
"You provide a command for the statement that is not supported".to_string(),
8895
"Statement command unknow".to_string()))
8996
}
9097
}
9198
};
92-
let stmt_name = match args_iter.next() {
93-
Some(s) => s,
94-
None => return Err(RediSQLError::with_code(19, "You should provide the name of the statement to operate with".to_string(), "Statement command with statement name".to_string())),
99+
let stmt_name = match action {
100+
Action::New
101+
| Action::Update
102+
| Action::Delete
103+
| Action::Show => match args_iter.next() {
104+
Some(s) => Some(*s),
105+
None => {
106+
return Err(RediSQLError::with_code(19, "You should provide the name of the statement to operate with".to_string(), "Statement command with statement name".to_string()));
107+
}
108+
},
109+
Action::List => None,
95110
};
96111
let stmt_query = match action {
97112
Action::Update | Action::New => match args_iter.next() {

redisql_lib/src/community_statement.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ unsafe impl Sync for InternalStatement {}
5656

5757
impl<'a> fmt::Display for Statement {
5858
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59-
let sql = unsafe {
60-
CStr::from_ptr(ffi::sqlite3_sql(self.as_ptr()))
61-
.to_string_lossy()
62-
.into_owned()
63-
};
59+
let sql = self.sql();
6460
writeln!(f, "{}", sql)
6561
}
6662
}
@@ -250,6 +246,19 @@ impl<'a> StatementTrait<'a> for Statement {
250246
let v = unsafe { ffi::sqlite3_stmt_readonly(self.as_ptr()) };
251247
v != 0
252248
}
249+
250+
fn parameters_count(&self) -> u32 {
251+
unsafe {
252+
ffi::sqlite3_bind_parameter_count(self.as_ptr()) as u32
253+
}
254+
}
255+
fn sql(&self) -> String {
256+
unsafe {
257+
CStr::from_ptr(ffi::sqlite3_sql(self.as_ptr()))
258+
.to_string_lossy()
259+
.into_owned()
260+
}
261+
}
253262
}
254263

255264
impl<'a> StatementTrait<'a> for MultiStatement {
@@ -351,6 +360,22 @@ impl<'a> StatementTrait<'a> for MultiStatement {
351360
}
352361
true
353362
}
363+
fn parameters_count(&self) -> u32 {
364+
self.stmts
365+
.iter()
366+
.map(|s| s.parameters_count())
367+
.max()
368+
.unwrap_or(0)
369+
}
370+
fn sql(&self) -> String {
371+
let sqls = self.stmts.iter().map(|s| s.sql());
372+
let n: usize = sqls.clone().map(|s| s.len()).sum();
373+
let mut s = String::with_capacity(n);
374+
for sql in sqls {
375+
s.push_str(&sql);
376+
}
377+
s
378+
}
354379
}
355380

356381
fn count_parameters(

0 commit comments

Comments
 (0)