### ----------------------------------------------------
### -- SQL::Builder
### -- Licenses: Artistic-2.0
### -- Authors: Adrian Kreher
### -- File: t/20-insert.rakutest
### ----------------------------------------------------
use Test;
use SQL::Builder;

my $sql = SQL::Builder.new;

subtest 'basic insert', {
    given $sql.insert-into('table').data(:a(1)).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?)';
        is .bind, [1];
    }

    given $sql.insert-into('table').data([:a(1)]).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?)';
        is .bind, [1];
    }

    given $sql.insert-into('table').data([:a(1), :b(2)]).build {
        is .sql, 'INSERT INTO "table" ("a", "b") VALUES (?, ?)';
        is .bind, [1, 2];
    }
};

subtest 'insert with columns/values', {
    given $sql.insert-into('table').columns('a').values(1).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?)';
        is .bind, [1];
    }

    given $sql.insert-into('table').columns(['a',]).values([1,]).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?)';
        is .bind, [1];
    }

    given $sql.insert-into('table').columns(['a', 'b']).values([1, 2]).build {
        is .sql, 'INSERT INTO "table" ("a", "b") VALUES (?, ?)';
        is .bind, [1, 2];
    }
};

subtest 'insert with multi-values', {
    given $sql.insert-into('table').columns('a').multi-values([[1,], [2,]]).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?), (?)';
        is .bind, [1, 2];
    }

    given $sql.insert-into('table').columns(['a',]).multi-values([[1,], [2,]]).build {
        is .sql, 'INSERT INTO "table" ("a") VALUES (?), (?)';
        is .bind, [1, 2];
    }

    given $sql.insert-into('table').columns(['a', 'b']).multi-values([[1, 2], [3, 4]]).build {
        is .sql, 'INSERT INTO "table" ("a", "b") VALUES (?, ?), (?, ?)';
        is .bind, [1, 2, 3, 4];
    }
};

subtest 'insert from select', {
    my $select = $sql.from('t1').select(<a b>).where(["a", ">", 1]);
    given $sql.insert-into('t2').columns(<a b>).query($select).build {
        is .sql, 'INSERT INTO "t2" ("a", "b") SELECT "a", "b" FROM "t1" WHERE "a" > ?';
        is .bind, [1,];
    }
};

done-testing;


