use std::io;
use msql_srv::*;

pub struct Backend;
impl<W: io::Write> MysqlShim<W> for Backend {
    type Error = io::Error;

    fn on_prepare(&mut self, query: &str, info: StatementMetaWriter<W>) -> io::Result<()> {
        println!("Backend on prepare, query: {}", query);
        info.reply(42, &[], &[])
    }

    fn on_execute(&mut self, id: u32, _params: ParamParser, results: QueryResultWriter<W>, ) -> io::Result<()> {
        println!("Backend on execute, id: {}", id);
        results.completed(0, 0)
    }

    fn on_close(&mut self, stmt: u32) {
        println!("Backend on close, stmt: {}", stmt);
    }

    fn on_init(&mut self, _unknown: &str, _writer: InitWriter<W>) -> io::Result<()> {
        println!("Backend on init, param2: {}", _unknown);
        Ok(())
    }

    fn on_query(&mut self, query: &str, results: QueryResultWriter<W>) -> io::Result<()> {
        println!("Backend on query, query: {}", query);
        let cols = [
            Column {
                table: "foo".to_string(),
                column: "a".to_string(),
                coltype: ColumnType::MYSQL_TYPE_LONGLONG,
                colflags: ColumnFlags::empty(),
            },
            Column {
                table: "foo".to_string(),
                column: "b".to_string(),
                coltype: ColumnType::MYSQL_TYPE_STRING,
                colflags: ColumnFlags::empty(),
            },
        ];

        let mut rw = results.start(&cols)?;
        rw.write_col(42)?;
        rw.write_col("b's value")?;
        rw.finish()
    }
}
