8 releases

0.4.2 Sep 13, 2024
0.4.1 Jul 22, 2024
0.4.0 Aug 29, 2023
0.3.2 Feb 23, 2023
0.1.1 Apr 29, 2022

#636 in Database interfaces

Download history 215/week @ 2024-07-29 108/week @ 2024-08-05 22/week @ 2024-08-12 270/week @ 2024-08-19 161/week @ 2024-08-26 140/week @ 2024-09-02 315/week @ 2024-09-09 124/week @ 2024-09-16 133/week @ 2024-09-23 142/week @ 2024-09-30 187/week @ 2024-10-07 293/week @ 2024-10-14 148/week @ 2024-10-21 220/week @ 2024-10-28 168/week @ 2024-11-04 390/week @ 2024-11-11

943 downloads per month
Used in shotover

Apache-2.0

230KB
5K SLoC

CQL3 Parser

dependency status Crates.io Released API docs

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.

Common usage

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.

Package Structure

  • The parser is in the cassandra_ast module.
  • The Statements are in the cassandra_statements module.
  • The data for the statements are found in various modules named for the statement (e.g. create_table has the Create Table specific structs).
  • Structures that are common to several packages are found in the common module.
  • Many of the Drop statements have the same structure, it is in common_drop.
  • The statements dealing with Roles (e.g. Create Role) utilize the role_common module.

A Note on Errors

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").

Dependencies

~5.5–7.5MB
~161K SLoC