-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Database.pm6
30 lines (26 loc) · 1.04 KB
/
Database.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use Red::Driver;
=head2 Red::Database
proto database(|c) is export { * }
#| Accepts an SQL driver name and parameters and uses them to create
#| an instance of `Red::Driver` class.
#| The driver name is used to specify a particular driver from
#| `Red::Driver::` family of modules, so `SQLite` results in
#| constructing an instance of `Red::Driver::SQLite` class.
#| All subsequent attributes after the driver name will be
#| directly passed to the constructor of the driver.
multi sub database(Str $type, |c --> Red::Driver) is export {
my $driver-name = "Red::Driver::$type";
require ::($driver-name);
my Red::Driver $driver = ::($driver-name).new: |c;
$driver.auto-register;
$driver
}
#| Accepts an SQL driver name and a database handle, and
#| creates an instance of `Red::Driver` passing it the handle.
multi sub database(Str $type, $dbh --> Red::Driver) {
my $driver-name = "Red::Driver::$type";
require ::($driver-name);
my Red::Driver $driver = ::($driver-name).new: :$dbh;
$driver.auto-register;
$driver
}