pub fn split(sql: &str) -> Vec<String>Expand description
Split a string into individual sql statements.
SQL is an &str containing some sql statements separated by semicolons.
This func returns a Vec
This func is not smart, but it’s not completely dumb either. It
will ignore semicolons inside enclosures recognized by sqlite
(quote, double-quote, backticks, square braces). It will not be
thrown off by nested quotes. It will remove empty statements if
it finds any.
This func does not know how to parse sql, and will not verify that your sql is well-formed. If you feed it invalid sql, results are undefined. For example, it will not treat handle quotes escaped with a backslash as a special case, since sqlite does not recognize backslash escaped quotes.
Limitations:
The output from split is meant for the sqlite engine, not
humans. It seeks to preserve semantics, not form. This func will
strip comments because doing so simplifies parsing and also it is
sometimes hard to know which statement a comment should attach to.
Because we strip comments, there is one known case where sql_split’s behavior differs from sqlite’s. If you start a .command and then start a multi-line block comment before you terminate the .command with a newline, sqlite will throw an error. We just remove that error and give you the .command as its own statement.
use sql_split::split;
use rusqlite::{Connection, Result};
let conn = Connection::open_in_memory().expect("Can't open db in memory");
let sql = "CREATE TABLE foo (bar text); CREATE TABLE meep (moop text);";
for s in split(sql) {
conn.execute(&s, []).expect("Can't write to the db");
}
assert_eq!(split(sql),
vec![
"CREATE TABLE foo (bar text);",
"CREATE TABLE meep (moop text);",
]
);