use crate::parser::mysql_insert::{
parse_insert_tuple, FkRef, InsertRowContext, ParsedRow, PkTuple, PkValue, RowExtraction,
};
use crate::parser::postgres_copy::{parse_copy_columns, CopyParser, ParsedCopyRow};
use crate::parser::{ContentFilter, Parser, ParserEvent, SqlDialect};
use crate::schema::{SchemaBuilder, SchemaGraph, TableSchema};
use crate::splitter::{Splitter, Stats as SplitStats};
use indicatif::{ProgressBar, ProgressStyle};
use std::fs::{self, File};
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use tempfile::TempDir;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RowFormat {
Insert,
Copy,
}
impl RowFormat {
fn tag(self) -> u8 {
match self {
RowFormat::Insert => 0,
RowFormat::Copy => 1,
}
}
fn from_tag(tag: u8) -> Self {
if tag == 0 {
RowFormat::Insert
} else {
RowFormat::Copy
}
}
}
pub enum UnifiedRow {
Insert(ParsedRow),
Copy(ParsedCopyRow),
}
impl UnifiedRow {
pub fn pk(&self) -> Option<&PkTuple> {
match self {
UnifiedRow::Insert(r) => r.pk.as_ref(),
UnifiedRow::Copy(r) => r.pk.as_ref(),
}
}
pub fn fk_values(&self) -> &[(FkRef, PkTuple)] {
match self {
UnifiedRow::Insert(r) => &r.fk_values,
UnifiedRow::Copy(r) => &r.fk_values,
}
}
pub fn get_column_value(&self, idx: usize) -> Option<&PkValue> {
match self {
UnifiedRow::Insert(r) => r.get_column_value(idx),
UnifiedRow::Copy(r) => r.get_column_value(idx),
}
}
pub fn format(&self) -> RowFormat {
match self {
UnifiedRow::Insert(_) => RowFormat::Insert,
UnifiedRow::Copy(_) => RowFormat::Copy,
}
}
pub fn raw(&self) -> &[u8] {
match self {
UnifiedRow::Insert(r) => &r.raw,
UnifiedRow::Copy(r) => &r.raw,
}
}
}
pub use crate::parser::RowFlow;
pub fn is_copy_data_block(stmt: &[u8]) -> bool {
stmt.ends_with(b"\\.\n") || stmt.ends_with(b"\\.\r\n")
}
pub fn for_each_data_row<F>(
table_file: &Path,
table_schema: &TableSchema,
dialect: SqlDialect,
extraction: RowExtraction,
mut f: F,
) -> anyhow::Result<()>
where
F: FnMut(UnifiedRow) -> anyhow::Result<RowFlow>,
{
let file = File::open(table_file)?;
let mut parser = Parser::with_dialect(file, 64 * 1024, dialect);
let mut insert_ctx: Option<InsertRowContext> = None;
let mut copy_ctx: Option<(CopyParser<'_>, bool)> = None;
parser.visit_events(|event| match event {
ParserEvent::Statement(_) => Ok(RowFlow::Continue),
ParserEvent::InsertRow {
header,
row,
first_in_statement,
} => {
if first_in_statement || insert_ctx.is_none() {
insert_ctx = Some(InsertRowContext::from_header(header, table_schema));
}
let ctx = insert_ctx.as_ref().expect("insert_ctx set above");
match parse_insert_tuple(row, table_schema, ctx, dialect, extraction) {
Some(parsed) => f(UnifiedRow::Insert(parsed)),
None => Ok(RowFlow::Continue),
}
}
ParserEvent::CopyStart(header) => {
let columns = parse_copy_columns(&String::from_utf8_lossy(header));
let (parser, empty_line_is_row) = CopyParser::new(&[])
.with_schema(table_schema)
.with_column_order(columns)
.with_extraction(extraction)
.prepared();
copy_ctx = Some((parser, empty_line_is_row));
Ok(RowFlow::Continue)
}
ParserEvent::CopyRow(line) => match copy_ctx.as_ref() {
Some((cp, empty_line_is_row)) => match cp.parse_line(line, *empty_line_is_row) {
Some(parsed) => f(UnifiedRow::Copy(parsed)),
None => Ok(RowFlow::Continue),
},
None => Ok(RowFlow::Continue),
},
ParserEvent::CopyEnd => Ok(RowFlow::Continue),
})
}
pub struct RowSpillWriter {
writer: BufWriter<File>,
}
impl RowSpillWriter {
pub fn create(path: &Path) -> io::Result<Self> {
Ok(Self {
writer: BufWriter::new(File::create(path)?),
})
}
pub fn write_row(&mut self, format: RowFormat, raw: &[u8]) -> io::Result<()> {
self.writer.write_all(&[format.tag()])?;
self.writer.write_all(&(raw.len() as u32).to_le_bytes())?;
self.writer.write_all(raw)
}
pub fn finish(mut self) -> io::Result<()> {
self.writer.flush()
}
}
pub struct RowSpillReader {
reader: BufReader<File>,
}
impl RowSpillReader {
pub fn open(path: &Path) -> io::Result<Self> {
Ok(Self {
reader: BufReader::new(File::open(path)?),
})
}
pub fn next_row(&mut self) -> io::Result<Option<(RowFormat, Vec<u8>)>> {
let mut tag = [0u8; 1];
match self.reader.read_exact(&mut tag) {
Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e),
}
let mut len_buf = [0u8; 4];
self.reader.read_exact(&mut len_buf)?;
let len = u32::from_le_bytes(len_buf) as usize;
let mut raw = vec![0u8; len];
self.reader.read_exact(&mut raw)?;
Ok(Some((RowFormat::from_tag(tag[0]), raw)))
}
}
pub struct SplitPhase {
pub temp_dir: TempDir,
pub tables_dir: PathBuf,
pub stats: SplitStats,
}
pub fn split_to_temp_tables(
input: &Path,
dialect: SqlDialect,
progress: bool,
) -> anyhow::Result<SplitPhase> {
let file_size = std::fs::metadata(input)?.len();
let progress_bar = if progress {
let pb = ProgressBar::new(file_size);
pb.set_style(
ProgressStyle::with_template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({percent}%) {msg}",
)
.unwrap()
.progress_chars("█▓▒░ ")
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
);
pb.enable_steady_tick(std::time::Duration::from_millis(100));
pb.set_message("Splitting dump...");
Some(pb)
} else {
None
};
let temp_dir = TempDir::new()?;
let tables_dir = temp_dir.path().join("tables");
let mut splitter = Splitter::new(input.to_path_buf(), tables_dir.clone())
.with_dialect(dialect)
.with_content_filter(ContentFilter::All);
if let Some(ref pb) = progress_bar {
let pb_clone = pb.clone();
splitter = splitter.with_progress(move |bytes| {
pb_clone.set_position(bytes);
});
}
let stats = splitter.split()?;
if let Some(ref pb) = progress_bar {
pb.finish_and_clear();
}
if progress {
eprintln!(
"Split complete: {} tables, {} statements",
stats.tables_found, stats.statements_processed
);
}
Ok(SplitPhase {
temp_dir,
tables_dir,
stats,
})
}
pub fn build_schema_graph(tables_dir: &Path, dialect: SqlDialect) -> anyhow::Result<SchemaGraph> {
let mut builder = SchemaBuilder::new();
for entry in fs::read_dir(tables_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().is_some_and(|e| e == "sql") {
let file = File::open(&path)?;
let mut parser = Parser::with_dialect(file, 64 * 1024, dialect);
while let Some(stmt) = parser.read_statement()? {
builder.ingest_statement(&stmt, dialect);
}
}
}
Ok(SchemaGraph::from_schema(builder.build()))
}
pub fn quote_ident(dialect: SqlDialect, name: &str) -> String {
match dialect {
SqlDialect::MySql => format!("`{}`", name.replace('`', "``")),
SqlDialect::Postgres | SqlDialect::Sqlite => format!("\"{}\"", name.replace('"', "\"\"")),
SqlDialect::Mssql => format!("[{}]", name.replace(']', "]]")),
}
}
pub fn write_dialect_header<W: Write>(writer: &mut W, dialect: SqlDialect) -> io::Result<()> {
match dialect {
SqlDialect::MySql => {
writeln!(writer, "SET NAMES utf8mb4;")?;
writeln!(writer, "SET FOREIGN_KEY_CHECKS = 0;")?;
}
SqlDialect::Postgres => {
writeln!(writer, "SET client_encoding = 'UTF8';")?;
writeln!(writer, "SET session_replication_role = replica;")?;
}
SqlDialect::Sqlite => {
writeln!(writer, "PRAGMA foreign_keys = OFF;")?;
}
SqlDialect::Mssql => {
writeln!(writer, "SET ANSI_NULLS ON;")?;
writeln!(writer, "SET QUOTED_IDENTIFIER ON;")?;
writeln!(writer, "SET NOCOUNT ON;")?;
}
}
writeln!(writer)?;
Ok(())
}
pub fn write_dialect_footer<W: Write>(writer: &mut W, dialect: SqlDialect) -> io::Result<()> {
writeln!(writer)?;
match dialect {
SqlDialect::MySql => {
writeln!(writer, "SET FOREIGN_KEY_CHECKS = 1;")?;
}
SqlDialect::Postgres => {
writeln!(writer, "SET session_replication_role = DEFAULT;")?;
}
SqlDialect::Sqlite => {
writeln!(writer, "PRAGMA foreign_keys = ON;")?;
}
SqlDialect::Mssql => {
}
}
Ok(())
}
pub fn write_insert_chunk<W: Write>(
writer: &mut W,
quoted_name: &str,
chunk: &[(RowFormat, Vec<u8>)],
dialect: SqlDialect,
) -> io::Result<()> {
writeln!(writer, "INSERT INTO {} VALUES", quoted_name)?;
for (i, (format, row_bytes)) in chunk.iter().enumerate() {
if i > 0 {
writer.write_all(b",\n")?;
}
let values = match format {
RowFormat::Insert => match dialect {
SqlDialect::Postgres => convert_row_to_postgres(row_bytes),
_ => row_bytes.clone(),
},
RowFormat::Copy => convert_copy_to_insert_values(row_bytes, dialect),
};
writer.write_all(&values)?;
}
writer.write_all(b";\n")?;
Ok(())
}
pub fn convert_row_to_postgres(row: &[u8]) -> Vec<u8> {
let mut result = Vec::with_capacity(row.len());
let mut i = 0;
while i < row.len() {
if row[i] == b'\\' && i + 1 < row.len() && row[i + 1] == b'\'' {
result.push(b'\'');
result.push(b'\'');
i += 2;
} else {
result.push(row[i]);
i += 1;
}
}
result
}
pub fn convert_copy_to_insert_values(row: &[u8], dialect: SqlDialect) -> Vec<u8> {
let mut result = Vec::with_capacity(row.len() + 20);
result.push(b'(');
let fields: Vec<&[u8]> = row.split(|&b| b == b'\t').collect();
for (i, field) in fields.iter().enumerate() {
if i > 0 {
result.extend_from_slice(b", ");
}
if *field == b"\\N" {
result.extend_from_slice(b"NULL");
} else if field.is_empty() {
result.extend_from_slice(b"''");
} else if is_numeric(field) {
result.extend_from_slice(field);
} else {
result.push(b'\'');
for &b in *field {
match b {
b'\'' => {
match dialect {
SqlDialect::MySql => result.extend_from_slice(b"\\'"),
SqlDialect::Postgres | SqlDialect::Sqlite | SqlDialect::Mssql => {
result.extend_from_slice(b"''")
}
}
}
b'\\' if dialect == SqlDialect::MySql => {
result.extend_from_slice(b"\\\\");
}
_ => result.push(b),
}
}
result.push(b'\'');
}
}
result.push(b')');
result
}
pub fn is_numeric(s: &[u8]) -> bool {
if s.is_empty() {
return false;
}
let mut has_digit = false;
let mut has_dot = false;
let mut start = 0;
if s[0] == b'-' || s[0] == b'+' {
start = 1;
}
for &b in &s[start..] {
match b {
b'0'..=b'9' => has_digit = true,
b'.' if !has_dot => has_dot = true,
b'e' | b'E' => {
continue;
}
_ => return false,
}
}
has_digit
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spill_roundtrip_preserves_newlines_and_binary() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("t.rows");
let rows: Vec<(RowFormat, Vec<u8>)> = vec![
(RowFormat::Insert, b"(1, 'a\nb')".to_vec()),
(RowFormat::Copy, vec![0xFF, 0xFE, b'\t', b'x', b'\n']),
(RowFormat::Insert, Vec::new()),
];
let mut w = RowSpillWriter::create(&path).unwrap();
for (f, raw) in &rows {
w.write_row(*f, raw).unwrap();
}
w.finish().unwrap();
let mut r = RowSpillReader::open(&path).unwrap();
for (f, raw) in &rows {
let (rf, rraw) = r.next_row().unwrap().unwrap();
assert_eq!(rf, *f);
assert_eq!(&rraw, raw);
}
assert!(r.next_row().unwrap().is_none());
}
#[test]
fn quote_ident_per_dialect() {
assert_eq!(quote_ident(SqlDialect::MySql, "t"), "`t`");
assert_eq!(quote_ident(SqlDialect::Postgres, "t"), "\"t\"");
assert_eq!(quote_ident(SqlDialect::Sqlite, "t"), "\"t\"");
assert_eq!(quote_ident(SqlDialect::Mssql, "t"), "[t]");
}
#[test]
fn quote_ident_escapes_the_closing_delimiter() {
assert_eq!(quote_ident(SqlDialect::MySql, "a`b"), "`a``b`");
assert_eq!(quote_ident(SqlDialect::Postgres, "a\"b"), "\"a\"\"b\"");
assert_eq!(quote_ident(SqlDialect::Sqlite, "a\"b"), "\"a\"\"b\"");
assert_eq!(quote_ident(SqlDialect::Mssql, "a]b"), "[a]]b]");
}
#[test]
fn is_numeric_basics() {
assert!(is_numeric(b"123"));
assert!(is_numeric(b"-1.5"));
assert!(is_numeric(b"1e10"));
assert!(!is_numeric(b""));
assert!(!is_numeric(b"abc"));
assert!(!is_numeric(b"1.2.3"));
}
}