As an Ubuntu 18.04 user, I also needed to install some C libraries in order to get DBIish to install using zef. If you encounter this problem too, run the following command and try again:
sudo apt -y install libffi-dev libtommath-dev libatomic-ops-dev libuv-dev
I'm using my MessageRepository here as an example. In my schema, there is a table called "message", so in order to connect to it, I have a repository class for that table with a reference to the $table
variable from the abstract, so it knows where the queries should be applied. You may also notice a Service file that simply provides the database connection information. Also that if an exception is caught it is just returned. In my project, I use an entity system to manage errors and data travelling between classes so if you want to see how I did that you can find a working example in my Telegram-Relay-Bot-Perl6 project.
MessageRepository.pm6
#usr/bin/perl6
use v6;
need Repository::AbstractRepository;
class MessageRepository does AbstractRepository
{
method new ()
{
self.bless(table => "message");
}
}
MessageService.pm6
...
need Module::Message::MessageRepository;
class MessageService
{
has $!messageRepository = MessageRepository.new;
}
This will return the ID of the added row in an array, even if there's only one
-
INSERT INTO message (column) VALUES (0)
$!messageRepository.insert('column', 0); $!messageRepository.insert(%(column => 0)); $!messageRepository.insert(['column'], [0]);
-
INSERT INTO message (columnA, columnB) VALUES (1, 'string')
$!messageRepository.insert(%(columnA => 1, columnB => 'string')); $!messageRepository.insert(['columnA', 'columnB'], [1, 'string']);
Multiple INSERTs can be done by sending an array of hashes
This will also add the FROM <table name>
('message' in this case)
-
SELECT column
$!messageRepository.select('column'); $!messageRepository.select(['column']);
-
SELECT columnA, columnB
$!messageRepository.select('columnA', 'columnB'); $!messageRepository.select(['columnA', 'columnB']);
-
WHERE column = 0
$!messageRepository.where('column', 0); $!messageRepository.where('column', '=', 0); $!messageRepository.where(%(column => 0)); $!messageRepository.where(['column' => 0]);
-
WHERE columnA = 1 AND columnB = 2
$!messageRepository.where(%(columnA => 1, columnB => 2)); $!messageRepository.where(['columnA', 'columnB'], [1, 2]); $!messageRepository.where(['columnA', 'columnB'], ['=', '='], [1, 2]);
-
WHERE (columnA = 1 AND columnB = 2) OR columnC = 3
$!messageRepository.where([%(columnA => 1, columnB => 2), %(columnC => 3)]); $!messageRepository.where(['columnA', 'columnB'], [1, 2]); $!messageRepository.orWhere(['columnC'], [3]); $!messageRepository.where(['columnA', 'columnB'], ['=', '='], [1, 2]); $!messageRepository.orWhere(['columnC'], ['='], [3]);
orWhere
performs the same operation where
but prefixes the string with 'OR'