| 1 | package Bot::Backbone::Service::Role::Storage; | ||
| 2 | $Bot::Backbone::Service::Role::Storage::VERSION = '0.160630'; | ||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | use DBIx::Connector; | ||
| 6 | |||
| 7 | # ABSTRACT: Helper for adding storage to standard modules | ||
| 8 | |||
| 9 | |||
| 10 | has db_dsn => ( | ||
| 11 | is => 'ro', | ||
| 12 | isa => 'Str', | ||
| 13 | required => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | |||
| 17 | has db_user => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'Str', | ||
| 20 | predicate => 'has_db_user', | ||
| 21 | ); | ||
| 22 | |||
| 23 | |||
| 24 | has db_password => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'Str', | ||
| 27 | predicate => 'has_db_password', | ||
| 28 | ); | ||
| 29 | |||
| 30 | |||
| 31 | has db_conn => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'DBIx::Connector', | ||
| 34 | lazy_build => 1, | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub _build_db_conn { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | my $conn = DBIx::Connector->new( | ||
| 41 | $self->db_dsn, $self->db_user, $self->db_password, { | ||
| 42 | RaiseError => 1, | ||
| 43 | }, | ||
| 44 | ); | ||
| 45 | |||
| 46 | $self->load_schema($conn); | ||
| 47 | |||
| 48 | return $conn; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | requires 'load_schema'; | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__ | ||
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | =encoding UTF-8 | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | Bot::Backbone::Service::Role::Storage - Helper for adding storage to standard modules | ||
| 65 | |||
| 66 | =head1 VERSION | ||
| 67 | |||
| 68 | version 0.160630 | ||
| 69 | |||
| 70 | =head1 SYNOPSIS | ||
| 71 | |||
| 72 | package MyBot::Service::Thing; | ||
| 73 | use Bot::Backbone::Service; | ||
| 74 | |||
| 75 | with qw( | ||
| 76 | Bot::Backbone::Service::Role::Service | ||
| 77 | Bot::Backbone::Service::Role::Responder | ||
| 78 | Bot::Backbone::Service::Role::Storage | ||
| 79 | ); | ||
| 80 | |||
| 81 | sub load_schema { | ||
| 82 | my ($self, $conn) = @_; | ||
| 83 | |||
| 84 | $conn->run(fixup => sub { | ||
| 85 | $_->do(q[ | ||
| 86 | CREATE TABLE IF NOT EXISTS things( | ||
| 87 | name varchar(255), | ||
| 88 | PRIMARY KEY (name) | ||
| 89 | ) | ||
| 90 | ]); | ||
| 91 | }); | ||
| 92 | } | ||
| 93 | |||
| 94 | # More bot service stuff... | ||
| 95 | |||
| 96 | =head1 DESCRIPTION | ||
| 97 | |||
| 98 | Uses L<DBIx::Connector> to deliver a database handle to a bot service. It | ||
| 99 | provides attributes for configuration the DBI DSN, username, and password and | ||
| 100 | automatically manages the connection to the database using L<DBIx::Connector>. | ||
| 101 | |||
| 102 | It also provides a sort of callback for loading the schema, which happens when | ||
| 103 | the connection is first used. | ||
| 104 | |||
| 105 | =head1 ATTRIBUTES | ||
| 106 | |||
| 107 | =head2 db_dsn | ||
| 108 | |||
| 109 | This is the DSN to use when connecting to DBI. See your favorite DBD driver for | ||
| 110 | details. | ||
| 111 | |||
| 112 | =head2 db_user | ||
| 113 | |||
| 114 | This is the username to use to connect. | ||
| 115 | |||
| 116 | =head2 db_password | ||
| 117 | |||
| 118 | This is the password to use when connecting. | ||
| 119 | |||
| 120 | =head2 db_conn | ||
| 121 | |||
| 122 | Use this to get at the L<DBIx::Connector> object used to connect to your database. | ||
| 123 | |||
| 124 | =head1 REQUIRED METHODS | ||
| 125 | |||
| 126 | =head2 load_schema | ||
| 127 | |||
| 128 | $service->load_schema($db_conn); | ||
| 129 | |||
| 130 | This is called to create the schema for your database. It is passed a reference | ||
| 131 | to the newly created L<DBIx::Connector> object. You should use it to run any DDL | ||
| 132 | statements required to setup your schema. It does nothing to help you handle | ||
| 133 | different flavors of SQL so if you need to do that, the way you do that is | ||
| 134 | completely up to you. | ||
| 135 | |||
| 136 | B<DO NOT> try to call L</db_conn> on the invocant or your bot will lock up, just | ||
| 137 | use the one passed in. | ||
| 138 | |||
| 139 | =head1 AUTHOR | ||
| 140 | |||
| 141 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 142 | |||
| 143 | =head1 COPYRIGHT AND LICENSE | ||
| 144 | |||
| 145 | This software is copyright (c) 2016 by Qubling Software LLC. | ||
| 146 | |||
| 147 | This is free software; you can redistribute it and/or modify it under | ||
| 148 | the same terms as the Perl 5 programming language system itself. | ||
| 149 | |||
| 150 | =cut |