| 1 | package Mojo::Hakkefuin::Backend; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | use Carp 'croak'; | ||
| 5 | use Mojo::Hakkefuin::Utils; | ||
| 6 | |||
| 7 | has 'dsn'; | ||
| 8 | has 'dir'; | ||
| 9 | has mhf_util => 'Mojo::Hakkefuin::Utils'; | ||
| 10 | has table_ready => 0; | ||
| 11 | |||
| 12 | # table structure | ||
| 13 | has table_name => 'mojo_hakkefuin'; | ||
| 14 | has id => 'id_auth'; | ||
| 15 | has identify => 'identify'; | ||
| 16 | has cookie => 'cookie'; | ||
| 17 | has csrf => 'csrf'; | ||
| 18 | has create_date => 'create_date'; | ||
| 19 | has expire_date => 'expire_date'; | ||
| 20 | has cookie_lock => 'cookie_lock'; | ||
| 21 | has lock => 'lock_state'; | ||
| 22 | |||
| 23 | # table interaction | ||
| 24 | sub table_query { croak 'Method "table_query" not implemented by subclass' } | ||
| 25 | sub check_table { croak 'Method "check_table" not implemented by subclass' } | ||
| 26 | sub create_table { croak 'Method "create_table" not implemented by subclass' } | ||
| 27 | sub empty_table { croak 'Method "empty_table" not implemented by subclass' } | ||
| 28 | sub drop_table { croak 'Method "drop_table" not implemented by subclass' } | ||
| 29 | |||
| 30 | sub _table_ok { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | return 1 if $self->table_ready; | ||
| 34 | |||
| 35 | my $check = $self->check_table; | ||
| 36 | my $ready = $check && $check->{result} ? 1 : 0; | ||
| 37 | $self->table_ready($ready); | ||
| 38 | return $ready; | ||
| 39 | } | ||
| 40 | |||
| 41 | # data interaction | ||
| 42 | sub create { croak 'Method "create" not implemented by subclass' } | ||
| 43 | sub read { croak 'Method "read" not implemented by subclass' } | ||
| 44 | sub update { croak 'Method "update" not implemented by subclass' } | ||
| 45 | sub update_csrf { croak 'Method "update_csrf" not implemented by subclass' } | ||
| 46 | sub update_cookie { croak 'Method "update_cookie" not implemented by subclass' } | ||
| 47 | sub upd_coolock { croak 'Method "upd_coolock" not implemented by subclass' } | ||
| 48 | sub upd_lckstate { croak 'Method "upd_lckstate" not implemented by subclass' } | ||
| 49 | sub delete { croak 'Method "delete" not implemented by subclass' } | ||
| 50 | sub check { croak 'Method "check" not implemented by subclass' } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | |||
| 54 | =encoding utf8 | ||
| 55 | |||
| 56 | =head1 NAME | ||
| 57 | |||
| 58 | Mojo::Hakkefuin::Backend - Backend base class | ||
| 59 | |||
| 60 | =head1 SYNOPSIS | ||
| 61 | |||
| 62 | package Mojo::Hakkefuin::Backend::MyBackend; | ||
| 63 | use Mojo::Base 'Mojo::Hakkefuin::Backend'; | ||
| 64 | |||
| 65 | sub table_query { ... } | ||
| 66 | sub check_table { ... } | ||
| 67 | sub create_table { ... } | ||
| 68 | sub empty_table { ... } | ||
| 69 | sub drop_table { ... } | ||
| 70 | |||
| 71 | sub create { ... } | ||
| 72 | sub read { ... } | ||
| 73 | sub update { ... } | ||
| 74 | sub update_csrf { ... } | ||
| 75 | sub update_cookie { ... } | ||
| 76 | sub upd_coolock { ... } | ||
| 77 | sub upd_lckstate { ... } | ||
| 78 | sub delete { ... } | ||
| 79 | sub check { ... } | ||
| 80 | |||
| 81 | =head1 DESCRIPTION | ||
| 82 | |||
| 83 | L<Mojo::Hakkefuin::Backend> is an abstract base class for | ||
| 84 | L<Mojo::Hakkefuin> backends, like L<Mojo::Hakkefuin::Backend::sqlite>. | ||
| 85 | |||
| 86 | =head1 ATTRIBUTES | ||
| 87 | |||
| 88 | L<Mojo::Hakkefuin::Backend> implements the following attributes. | ||
| 89 | |||
| 90 | =head2 dsn | ||
| 91 | |||
| 92 | # list of dsn support : | ||
| 93 | - mysql://username:password@hostname:port/database | ||
| 94 | - mariadb://username:password@hostname:port/database | ||
| 95 | - postgresql://username:password@hostname:port/database | ||
| 96 | |||
| 97 | # Example use as a config | ||
| 98 | my $backend = Mojo::Hakkefuin::Backend::mariadb->new( | ||
| 99 | ... | ||
| 100 | dsn => 'protocols://username:password@hostname:port/database', | ||
| 101 | ... | ||
| 102 | ); | ||
| 103 | |||
| 104 | # use as a method | ||
| 105 | my $backend = $backend->dsn; | ||
| 106 | $backend->dsn('protocols://username:password@hostname:port/database'); | ||
| 107 | |||
| 108 | This attribute for specify Data Source Name for DBMS, e.g. MariaDB/MySQL | ||
| 109 | or PostgreSQL. C<dsn> attribute can be use as a config or method. | ||
| 110 | |||
| 111 | =head2 dir | ||
| 112 | |||
| 113 | # Example use as a config | ||
| 114 | my $backend = Mojo::Hakkefuin::Backend::mariadb->new( | ||
| 115 | ... | ||
| 116 | dir => '/home/user/mojo/app/path/', | ||
| 117 | ... | ||
| 118 | ); | ||
| 119 | |||
| 120 | # use as a method | ||
| 121 | my $backend = $backend->dir; | ||
| 122 | $backend->dir('/home/user/mojo/app/path/'); | ||
| 123 | |||
| 124 | This attribute for specify path (directory address) of directory | ||
| 125 | SQLite database file or migrations configuration file. | ||
| 126 | |||
| 127 | =head1 TABLE FIELD ATTRIBUTES | ||
| 128 | |||
| 129 | The attributes for Table Field will be used on method C<table_query>. | ||
| 130 | In the future the user can determine the name dynamically | ||
| 131 | from the field table. | ||
| 132 | |||
| 133 | my $table_field = [ | ||
| 134 | $self->table_name, | ||
| 135 | $self->id, | ||
| 136 | $self->identify, | ||
| 137 | $self->cookie, | ||
| 138 | $self->csrf, | ||
| 139 | $self->create_date, | ||
| 140 | $self->expire_date, | ||
| 141 | $self->cookie_lock, | ||
| 142 | $self->lock | ||
| 143 | ]; | ||
| 144 | |||
| 145 | Now this attributes only used by method C<table_query>. | ||
| 146 | |||
| 147 | =head1 METHODS | ||
| 148 | |||
| 149 | L<Mojo::Hakkefuin::Backend> inherits all methods from L<Mojo::Base> | ||
| 150 | and implements the following new ones. In this module contains | ||
| 151 | 2 section methods that is B<Table Interaction> and B<Data Interaction>. | ||
| 152 | |||
| 153 | =head2 Table Interaction | ||
| 154 | |||
| 155 | A section for all activities related to interaction data in a database table. | ||
| 156 | |||
| 157 | =head3 table_query | ||
| 158 | |||
| 159 | my $table_query = $backend->table_query; | ||
| 160 | $backend->table_query; | ||
| 161 | |||
| 162 | This method used by the C<create_table> method to generate a query | ||
| 163 | that will be used to create a database table. | ||
| 164 | |||
| 165 | =head3 check_table | ||
| 166 | |||
| 167 | $backend->check_table(); | ||
| 168 | my $check_table = $backend->check_table; | ||
| 169 | |||
| 170 | This method is used to check whether a table in the DBMS exists or not | ||
| 171 | |||
| 172 | =head3 create_table | ||
| 173 | |||
| 174 | $backend->create_table(); | ||
| 175 | my $create_table = $backend->create_table; | ||
| 176 | |||
| 177 | This method is used to create database table. | ||
| 178 | |||
| 179 | =head3 empty_table | ||
| 180 | |||
| 181 | $backend->empty_table; | ||
| 182 | my $empty_table = $backend->empty_table; | ||
| 183 | |||
| 184 | This method is used to delete all database table | ||
| 185 | (It means to delete all data in a table). | ||
| 186 | |||
| 187 | =head3 drop_table | ||
| 188 | |||
| 189 | $backend->drop_table; | ||
| 190 | my $drop_table = $backend->drop_table; | ||
| 191 | |||
| 192 | This method is used to drop database table | ||
| 193 | (It means to delete all data in a table and its contents). | ||
| 194 | |||
| 195 | =head2 Data Interaction | ||
| 196 | |||
| 197 | A section for all activities related to data interaction in a database table. | ||
| 198 | These options are currently available: | ||
| 199 | |||
| 200 | =over 2 | ||
| 201 | |||
| 202 | =item $id | ||
| 203 | |||
| 204 | A variable containing a unique id that is generated when inserting data | ||
| 205 | into a table. | ||
| 206 | |||
| 207 | =item $identify | ||
| 208 | |||
| 209 | The variables that contain important information for data login | ||
| 210 | but not crucial information. | ||
| 211 | |||
| 212 | =item $cookie | ||
| 213 | |||
| 214 | The variable that contains the cookie hash, which hash is used | ||
| 215 | in the HTTP header. | ||
| 216 | |||
| 217 | =item $csrf | ||
| 218 | |||
| 219 | The variable that contains the csrf token hash, which hash is used | ||
| 220 | in the HTTP header. | ||
| 221 | |||
| 222 | =item $expires | ||
| 223 | |||
| 224 | The variable that contains timestamp format. e.g. C<2023-03-23 12:01:53>. | ||
| 225 | For more information see L<Mojo::Hakkefuin::Utils>. | ||
| 226 | |||
| 227 | =back | ||
| 228 | |||
| 229 | =head3 create($identify, $cookie, $csrf, $expires) | ||
| 230 | |||
| 231 | $backend->create($identify, $cookie, $csrf, $expires) | ||
| 232 | |||
| 233 | Method for insert data login. | ||
| 234 | |||
| 235 | =head3 read($identify, $cookie) | ||
| 236 | |||
| 237 | $backend->read($identify, $cookie); | ||
| 238 | |||
| 239 | Method for read data login. | ||
| 240 | |||
| 241 | =head3 update() | ||
| 242 | |||
| 243 | $backend->update($id, $cookie, $csrf); | ||
| 244 | |||
| 245 | Method for update data login. | ||
| 246 | |||
| 247 | =head3 update_csrf() | ||
| 248 | |||
| 249 | $backend->update_csrf($id, $csrf); | ||
| 250 | |||
| 251 | Method for update CSRF token login. | ||
| 252 | |||
| 253 | =head3 update_cookie() | ||
| 254 | |||
| 255 | $backend->update_cookie($id, $cookie); | ||
| 256 | |||
| 257 | Method for update cookie login. | ||
| 258 | |||
| 259 | =head3 upd_coolock() | ||
| 260 | |||
| 261 | $backend->upd_coolock(); | ||
| 262 | |||
| 263 | Method for update cookie lock session. | ||
| 264 | |||
| 265 | =head3 upd_lckstate() | ||
| 266 | |||
| 267 | $backend->upd_lckstate(); | ||
| 268 | |||
| 269 | Method for update lock state condition. | ||
| 270 | |||
| 271 | =head3 delete($identify, $cookie) | ||
| 272 | |||
| 273 | $backend->delete($identify, $cookie); | ||
| 274 | |||
| 275 | Method for delete data login. | ||
| 276 | |||
| 277 | =head3 check($identify, $cookie) | ||
| 278 | |||
| 279 | $backend->check($identify, $cookie); | ||
| 280 | |||
| 281 | Method for check data login. | ||
| 282 | |||
| 283 | =head1 SEE ALSO | ||
| 284 | |||
| 285 | =over 2 | ||
| 286 | |||
| 287 | =item * L<Mojolicious::Plugin::Hakkefuin> | ||
| 288 | |||
| 289 | =item * L<Mojo::Hakkefuin> | ||
| 290 | |||
| 291 | =item * L<Mojo::mysql> | ||
| 292 | |||
| 293 | =item * L<Mojo::Pg> | ||
| 294 | |||
| 295 | =item * L<Mojo::SQLite> | ||
| 296 | |||
| 297 | =item * L<Mojolicious::Guides> | ||
| 298 | |||
| 299 | =item * L<https://mojolicious.org> | ||
| 300 | |||
| 301 | =back | ||
| 302 | |||
| 303 | =cut |