chore: add fmt for statement query (#1588)

* chore: add fmt for statement query

* chore: add test for query display
This commit is contained in:
localhost
2023-05-16 16:14:11 +08:00
committed by GitHub
parent fb1ac0cb9c
commit 3330957896
2 changed files with 159 additions and 0 deletions

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
use datatypes::prelude::ConcreteDataType;
use sqlparser::ast::Query as SpQuery;
@@ -53,3 +55,56 @@ impl Query {
&mut self.param_types
}
}
impl fmt::Display for Query {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ", self.inner)?;
write!(f, "[")?;
for i in 0..self.param_types.len() {
write!(f, "{}", self.param_types[i])?;
if i != self.param_types.len() - 1 {
write!(f, ",")?;
}
}
write!(f, "]")?;
Ok(())
}
}
#[cfg(test)]
mod test {
use sqlparser::dialect::GenericDialect;
use super::Query;
use crate::parser::ParserContext;
use crate::statements::statement::Statement;
fn create_query(sql: &str) -> Option<Box<Query>> {
match ParserContext::create_with_dialect(sql, &GenericDialect {})
.unwrap()
.remove(0)
{
Statement::Query(query) => Some(query),
_ => None,
}
}
#[test]
fn test_query_display() {
assert_eq!(
create_query("select * from abc where x = 1 and y = 7")
.unwrap()
.to_string(),
"SELECT * FROM abc WHERE x = 1 AND y = 7 []"
);
assert_eq!(
create_query(
"select * from abc left join bcd where abc.a = 1 and bcd.d = 7 and abc.id = bcd.id"
)
.unwrap()
.to_string(),
"SELECT * FROM abc LEFT JOIN bcd WHERE abc.a = 1 AND bcd.d = 7 AND abc.id = bcd.id []"
);
}
}