Crates.io | cql3-parser |
lib.rs | cql3-parser |
version | 0.4.2 |
source | src |
created_at | 2022-04-29 03:03:48.707667 |
updated_at | 2024-09-13 00:49:46.449329 |
description | A Rust implementation of a CQL3 Parser |
homepage | |
repository | |
max_upload_size | |
id | 577105 |
size | 241,188 |
This is a full implementation of an Apache Cassandra CQL3 query language parser. The goal of this package is to parse CQL3 statements into a structure that can be used in a multi-threaded envrionment. It uses the tree-sitter-cql3 parser to parse the original query and then constructs an editable thread safe representation of the query.
use crate::cassandra_ast::CassandraAST;
use crate::cassandra_statement::CassandraStatement;
use crate::select::{Named, SelectElement};
let ast = CassandraAST::new("select foo from myTable" );
// verify that there was no error
assert!( !ast.has_error() );
// get the parsed statement
let stmt : CassandraStatement = ast.statement;
match stmt {
CassandraStatement::Select(select) => {
select.columns.push( SelectElement::Column( Named {
name : "bar".as_string(),
alias : Some( "baz".as_string() ),
}));
select.order_clause = Some( OrderClause { name : "baz".as_string() } );
},
_ => {}
}
let edited_stmt = stmt.to_string();
The above code changes SELECT foo FROM myTable
to Select foo, bar AS baz FROM myTable ORDER BY baz ASC
.
NOTE: It is possible to create invalid statements. If in doubt reparse the new statement to verify that it is syntactically correct.
cassandra_ast
module.cassandra_statements
module.create_table
has the Create Table specific structs).common
module.Drop
statements have the same structure, it is in common_drop
.Create Role
) utilize the role_common
module.When a statement is absolutely unparsable the parser will return a CassandraStatement::Unknown
object. For example CassandraAST::new("This is an invalid statement");
yields
CassandraStatement::Unknown("This is an invalid statement")
. However, if a statement is
partially parsable multiple results are returned. For example CassandraAST::new("SELECT * FROM foo WHERE some invalid part");
yields
CassandraStatement::Select( select )
where the select is the result of parsing "SELECT * FROM foo
followed by
CassandraStatement::Unknown("SELECT * FROM foo WHERE some invalid part")
.