| 1 | package Yancy::Backend::Dbic; | ||
| 2 | our $VERSION = '1.088'; | ||
| 3 | # ABSTRACT: A backend for DBIx::Class schemas | ||
| 4 | |||
| 5 | #pod =head1 SYNOPSIS | ||
| 6 | #pod | ||
| 7 | #pod ### URL string | ||
| 8 | #pod use Mojolicious::Lite; | ||
| 9 | #pod plugin Yancy => { | ||
| 10 | #pod backend => 'dbic://My::Schema/dbi:Pg:localhost', | ||
| 11 | #pod read_schema => 1, | ||
| 12 | #pod }; | ||
| 13 | #pod | ||
| 14 | #pod ### DBIx::Class::Schema object | ||
| 15 | #pod use Mojolicious::Lite; | ||
| 16 | #pod use My::Schema; | ||
| 17 | #pod plugin Yancy => { | ||
| 18 | #pod backend => { Dbic => My::Schema->connect( 'dbi:SQLite:myapp.db' ) }, | ||
| 19 | #pod read_schema => 1, | ||
| 20 | #pod }; | ||
| 21 | #pod | ||
| 22 | #pod ### Arrayref | ||
| 23 | #pod use Mojolicious::Lite; | ||
| 24 | #pod use My::Schema; | ||
| 25 | #pod plugin Yancy => { | ||
| 26 | #pod backend => { | ||
| 27 | #pod Dbic => [ | ||
| 28 | #pod 'My::Schema', | ||
| 29 | #pod 'dbi:SQLite:mysql.db', | ||
| 30 | #pod undef, undef, | ||
| 31 | #pod { PrintError => 1 }, | ||
| 32 | #pod ], | ||
| 33 | #pod }, | ||
| 34 | #pod read_schema => 1, | ||
| 35 | #pod }; | ||
| 36 | #pod | ||
| 37 | #pod =head1 DESCRIPTION | ||
| 38 | #pod | ||
| 39 | #pod This Yancy backend allows you to connect to a L<DBIx::Class> schema to | ||
| 40 | #pod manage the data inside. | ||
| 41 | #pod | ||
| 42 | #pod =head1 METHODS | ||
| 43 | #pod | ||
| 44 | #pod See L<Yancy::Backend> for the methods this backend has and their return | ||
| 45 | #pod values. | ||
| 46 | #pod | ||
| 47 | #pod =head2 read_schema | ||
| 48 | #pod | ||
| 49 | #pod While reading the various sources, this method will check each source's | ||
| 50 | #pod C<result_class> for the existence of a C<yancy> method. If it exists, | ||
| 51 | #pod that will be called, and must return the initial JSON schema for Yancy. | ||
| 52 | #pod | ||
| 53 | #pod A very useful possibility is for that JSON schema to just contain | ||
| 54 | #pod C<<{ 'x-ignore' => 1 }>>. | ||
| 55 | #pod | ||
| 56 | #pod =head2 Backend URL | ||
| 57 | #pod | ||
| 58 | #pod The URL for this backend takes the form C<< dbic://<schema_class>/<dbi_dsn> >> | ||
| 59 | #pod where C<schema_class> is the DBIx::Class schema module name and C<dbi_dsn> is | ||
| 60 | #pod the full L<DBI> data source name (DSN) used to connect to the database. | ||
| 61 | #pod | ||
| 62 | #pod =head2 Schema Names | ||
| 63 | #pod | ||
| 64 | #pod The schema names for this backend are the names of the | ||
| 65 | #pod L<DBIx::Class::Row> classes in your schema, just as DBIx::Class allows | ||
| 66 | #pod in the C<< $schema->resultset >> method. | ||
| 67 | #pod | ||
| 68 | #pod So, if you have the following schema: | ||
| 69 | #pod | ||
| 70 | #pod package My::Schema; | ||
| 71 | #pod use base 'DBIx::Class::Schema'; | ||
| 72 | #pod __PACKAGE__->load_namespaces; | ||
| 73 | #pod | ||
| 74 | #pod package My::Schema::Result::People; | ||
| 75 | #pod __PACKAGE__->table( 'people' ); | ||
| 76 | #pod __PACKAGE__->add_columns( qw/ id name email / ); | ||
| 77 | #pod | ||
| 78 | #pod package My::Schema::Result::Business | ||
| 79 | #pod __PACKAGE__->table( 'business' ); | ||
| 80 | #pod __PACKAGE__->add_columns( qw/ id name email / ); | ||
| 81 | #pod | ||
| 82 | #pod You could map that to the following schema names: | ||
| 83 | #pod | ||
| 84 | #pod { | ||
| 85 | #pod backend => 'dbic://My::Schema/dbi:SQLite:test.db', | ||
| 86 | #pod schema => { | ||
| 87 | #pod People => { | ||
| 88 | #pod properties => { | ||
| 89 | #pod id => { | ||
| 90 | #pod type => 'integer', | ||
| 91 | #pod readOnly => 1, | ||
| 92 | #pod }, | ||
| 93 | #pod name => { type => 'string' }, | ||
| 94 | #pod email => { type => 'string' }, | ||
| 95 | #pod }, | ||
| 96 | #pod }, | ||
| 97 | #pod Business => { | ||
| 98 | #pod properties => { | ||
| 99 | #pod id => { | ||
| 100 | #pod type => 'integer', | ||
| 101 | #pod readOnly => 1, | ||
| 102 | #pod }, | ||
| 103 | #pod name => { type => 'string' }, | ||
| 104 | #pod email => { type => 'string' }, | ||
| 105 | #pod }, | ||
| 106 | #pod }, | ||
| 107 | #pod }, | ||
| 108 | #pod } | ||
| 109 | #pod | ||
| 110 | #pod =head1 SEE ALSO | ||
| 111 | #pod | ||
| 112 | #pod L<Yancy::Backend>, L<DBIx::Class>, L<Yancy> | ||
| 113 | #pod | ||
| 114 | #pod =cut | ||
| 115 | |||
| 116 | use Mojo::Base 'Yancy::Backend'; | ||
| 117 | use Role::Tiny qw( with ); | ||
| 118 | with 'Yancy::Backend::Role::Sync'; | ||
| 119 | use Scalar::Util qw( looks_like_number blessed ); | ||
| 120 | use Mojo::Loader qw( load_class ); | ||
| 121 | use Mojo::JSON qw( true encode_json ); | ||
| 122 | |||
| 123 | |||
| 124 | BEGIN { | ||
| 125 | eval { require DBIx::Class; DBIx::Class->VERSION( 0.082842 ); 1 } | ||
| 126 | or die "Could not load Dbic backend: DBIx::Class version 0.08242 or higher required\n"; | ||
| 127 | } | ||
| 128 | |||
| 129 | has driver =>; | ||
| 130 | |||
| 131 | sub new { | ||
| 132 | my ( $class, $backend, $schema ) = @_; | ||
| 133 | if ( !ref $backend ) { | ||
| 134 | my ( $dbic_class, $dsn, $optstr ) = $backend =~ m{^[^:]+://([^/]+)/([^?]+)(?:\?(.+))?$}; | ||
| 135 | if ( my $e = load_class( $dbic_class ) ) { | ||
| 136 | die ref $e ? "Could not load class $dbic_class: $e" : "Could not find class $dbic_class"; | ||
| 137 | } | ||
| 138 | $backend = $dbic_class->connect( $dsn, undef, undef, {}, { quote_names => 1 } ); | ||
| 139 | } | ||
| 140 | elsif ( !blessed $backend ) { | ||
| 141 | my $dbic_class = shift @$backend; | ||
| 142 | if ( my $e = load_class( $dbic_class ) ) { | ||
| 143 | die ref $e ? "Could not load class $dbic_class: $e" : "Could not find class $dbic_class"; | ||
| 144 | } | ||
| 145 | if ( my $extra_attrs = $backend->[4] ||= {} ) { | ||
| 146 | $extra_attrs->{ quote_names } = 1; | ||
| 147 | } | ||
| 148 | $backend = $dbic_class->connect( @$backend ); | ||
| 149 | } | ||
| 150 | return $class->SUPER::new( $backend, $schema ); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _rs { | ||
| 154 | my ( $self, $schema_name, $params, $opt ) = @_; | ||
| 155 | $params ||= {}; $opt ||= {}; | ||
| 156 | my $schema = $self->schema->{ $schema_name }; | ||
| 157 | my $real_schema = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||
| 158 | my $rs = $self->driver->resultset( $real_schema )->search( $params, $opt ); | ||
| 159 | $rs->result_class( 'DBIx::Class::ResultClass::HashRefInflator' ); | ||
| 160 | return $rs; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _find { | ||
| 164 | my ( $self, $schema_name, $id ) = @_; | ||
| 165 | my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id'; | ||
| 166 | my %id; | ||
| 167 | if ( ref $id_field eq 'ARRAY' ) { | ||
| 168 | %id = %$id; | ||
| 169 | die "Missing composite ID parts" if @$id_field > keys %$id; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | %id = ( $id_field => $id ); | ||
| 173 | } | ||
| 174 | return $self->driver->resultset( $schema_name )->find( \%id ); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub create { | ||
| 178 | my ( $self, $schema_name, $params ) = @_; | ||
| 179 | $params = $self->normalize( $schema_name, $params ); | ||
| 180 | die "No refs allowed in '$schema_name': " . encode_json $params | ||
| 181 | if grep ref && ref ne 'SCALAR', values %$params; | ||
| 182 | my $created = $self->driver->resultset( $schema_name )->create( $params ); | ||
| 183 | my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id'; | ||
| 184 | return ref $id_field eq 'ARRAY' | ||
| 185 | ? { map { $_ => $created->$_ } @$id_field } | ||
| 186 | : $created->$id_field | ||
| 187 | ; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub get { | ||
| 191 | my ( $self, $schema_name, $id, %opt ) = @_; | ||
| 192 | my $schema = $self->schema->{ $schema_name }; | ||
| 193 | my $real_schema = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||
| 194 | my $props = $schema->{properties} | ||
| 195 | || $self->schema->{ $real_schema }{properties}; | ||
| 196 | my $id_field = $schema->{ 'x-id-field' } || 'id'; | ||
| 197 | my %id; | ||
| 198 | if ( ref $id_field eq 'ARRAY' ) { | ||
| 199 | %id = %$id; | ||
| 200 | die "Missing composite ID parts" if @$id_field > keys %$id; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | %id = ( $id_field => $id ); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Prefetch the data so HashRefInflator does the right thing | ||
| 207 | if ( $opt{join} ) { | ||
| 208 | $opt{prefetch} = $opt{join}; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $ret = $self->_rs( | ||
| 212 | $real_schema, | ||
| 213 | undef, | ||
| 214 | { select => [ keys %$props ], %opt }, | ||
| 215 | )->find( \%id ); | ||
| 216 | return $self->normalize( $schema_name, $ret ); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub list { | ||
| 220 | my ( $self, $schema_name, $params, @opt ) = @_; | ||
| 221 | my $opt = @opt % 2 == 0 ? {@opt} : $opt[0]; | ||
| 222 | $params ||= {}; | ||
| 223 | my $schema = $self->schema->{ $schema_name }; | ||
| 224 | my $real_schema = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||
| 225 | my $props = $schema->{properties} | ||
| 226 | || $self->schema->{ $real_schema }{properties}; | ||
| 227 | my %rs_opt = ( | ||
| 228 | order_by => $opt->{order_by}, | ||
| 229 | select => [ keys %$props ], | ||
| 230 | ); | ||
| 231 | # Prefetch the data so HashRefInflator does the right thing | ||
| 232 | if ( $opt->{join} ) { | ||
| 233 | $rs_opt{join} = $opt->{join}; | ||
| 234 | $rs_opt{prefetch} = $opt->{join}; | ||
| 235 | } | ||
| 236 | my $count_rs = $self->_rs( $schema_name, $params, \%rs_opt ); | ||
| 237 | if ( $opt->{limit} ) { | ||
| 238 | die "Limit must be number" if !looks_like_number $opt->{limit}; | ||
| 239 | $rs_opt{ rows } = $opt->{limit}; | ||
| 240 | } | ||
| 241 | if ( $opt->{offset} ) { | ||
| 242 | die "Offset must be number" if !looks_like_number $opt->{offset}; | ||
| 243 | $rs_opt{ offset } = $opt->{offset}; | ||
| 244 | } | ||
| 245 | my $rs = $self->_rs( $schema_name, $params, \%rs_opt ); | ||
| 246 | return { | ||
| 247 | items => [ map $self->normalize( $schema_name, $_ ), $rs->all ], | ||
| 248 | total => $count_rs->count, | ||
| 249 | }; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub set { | ||
| 253 | my ( $self, $schema_name, $id, $params ) = @_; | ||
| 254 | $params = $self->normalize( $schema_name, $params ); | ||
| 255 | die "No refs allowed in '$schema_name'($id): " . encode_json $params | ||
| 256 | if grep ref && ref ne 'SCALAR', values %$params; | ||
| 257 | if ( my $row = $self->_find( $schema_name, $id ) ) { | ||
| 258 | $row->set_columns( $params ); | ||
| 259 | if ( $row->is_changed ) { | ||
| 260 | $row->update; | ||
| 261 | return 1; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | return 0; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub delete { | ||
| 268 | my ( $self, $schema_name, $id ) = @_; | ||
| 269 | # We assume that if we can find the row by ID, that the delete will | ||
| 270 | # succeed | ||
| 271 | if ( my $row = $self->_find( $schema_name, $id ) ) { | ||
| 272 | $row->delete; | ||
| 273 | return 1; | ||
| 274 | } | ||
| 275 | return 0; | ||
| 276 | } | ||
| 277 | |||
| 278 | my %fix_default = ( | ||
| 279 | current_timestamp => "now", | ||
| 280 | current_time => "now", | ||
| 281 | current_date => "now", | ||
| 282 | ); | ||
| 283 | |||
| 284 | sub read_schema { | ||
| 285 | my ( $self, @schema_names ) = @_; | ||
| 286 | my %schema; | ||
| 287 | |||
| 288 | my @schemas = @schema_names ? @schema_names : $self->driver->sources; | ||
| 289 | my %classes; | ||
| 290 | for my $schema_name ( @schemas ) { | ||
| 291 | # ; say "Got schema $schema_name"; | ||
| 292 | my $source = $self->driver->source( $schema_name ); | ||
| 293 | my $result_class = $source->result_class; | ||
| 294 | # ; say "Adding class: $result_class ($schema_name)"; | ||
| 295 | $classes{ $result_class } = $source; | ||
| 296 | $schema{ $schema_name } = $result_class->yancy if $result_class->can('yancy'); | ||
| 297 | $schema{ $schema_name }{type} = 'object'; | ||
| 298 | my @columns = $source->columns; | ||
| 299 | for my $i ( 0..$#columns ) { | ||
| 300 | my $column = $columns[ $i ]; | ||
| 301 | my $c = $source->column_info( $column ); | ||
| 302 | # ; use Data::Dumper; | ||
| 303 | # ; say Dumper $c; | ||
| 304 | my $is_auto = $c->{is_auto_increment}; | ||
| 305 | my $default = ref $c->{default_value} eq 'SCALAR' | ||
| 306 | ? ${ $c->{default_value} } | ||
| 307 | : $c->{default_value }; | ||
| 308 | $schema{ $schema_name }{ properties }{ $column } = { | ||
| 309 | $self->_map_type( $c ), | ||
| 310 | $is_auto ? ( readOnly => true ) : (), | ||
| 311 | defined $default ? ( | ||
| 312 | default => exists $fix_default{ $default } | ||
| 313 | ? $fix_default{ $default } | ||
| 314 | : $default | ||
| 315 | ) : (), | ||
| 316 | 'x-order' => $i + 1, | ||
| 317 | }; | ||
| 318 | if ( !$c->{is_nullable} && !$is_auto && !defined $c->{default_value} ) { | ||
| 319 | push @{ $schema{ $schema_name }{ required } }, $column; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | my %is_pk = map {$_=>1} $source->primary_columns; | ||
| 324 | my @unique_columns = | ||
| 325 | grep !$is_pk{$_}, # we know about those already | ||
| 326 | map @$_, grep scalar( @$_ ) == 1, | ||
| 327 | map [ $source->unique_constraint_columns( $_ ) ], | ||
| 328 | $source->unique_constraint_names; | ||
| 329 | my ( $pk ) = keys %is_pk; | ||
| 330 | if ( @unique_columns == 1 and $unique_columns[0] ne 'id' ) { | ||
| 331 | # favour "natural" key over "surrogate" integer one, if exists | ||
| 332 | $schema{ $schema_name }{ 'x-id-field' } = $unique_columns[0]; | ||
| 333 | } | ||
| 334 | elsif ( $pk && $pk ne 'id' ) { | ||
| 335 | $schema{ $schema_name }{ 'x-id-field' } = $pk; | ||
| 336 | } | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | # Link foreign keys | ||
| 341 | for my $source ( values %classes ) { | ||
| 342 | for my $rel_name ( $source->relationships ) { | ||
| 343 | my $rel = $source->relationship_info( $rel_name ); | ||
| 344 | next unless $rel->{attrs}{accessor} eq 'single'; # Only belongs_to | ||
| 345 | # ; use Data::Dumper; | ||
| 346 | # ; say Dumper $rel; | ||
| 347 | my $self_schema = $source->source_name; | ||
| 348 | my $foreign_class = $rel->{source}; | ||
| 349 | # XXX Only very simple joins are possible here right now | ||
| 350 | my @self_cols = map /^[^.]+\.(.+)$/, grep /^self[.]/, %{ $rel->{cond} }; | ||
| 351 | my @foreign_cols = map /^[^.]+\.(.+)$/, grep /^foreign[.]/, %{ $rel->{cond} }; | ||
| 352 | if ( @self_cols > 1 || @foreign_cols > 1 ) { | ||
| 353 | warn sprintf | ||
| 354 | 'Cannot do foreign key with multiple columns yet on table %s, relationship %s', | ||
| 355 | $source->source_name, $rel_name, | ||
| 356 | ; | ||
| 357 | next; | ||
| 358 | } | ||
| 359 | # ; say "Looking for foreign class: $foreign_class"; | ||
| 360 | next unless $classes{ $foreign_class }; | ||
| 361 | my $foreign_schema = $classes{ $foreign_class }->source_name; | ||
| 362 | my $foreign_id = $schema{ $foreign_schema }{'x-id-field'} // 'id'; | ||
| 363 | $schema{ $self_schema }{ properties }{ $self_cols[0] }{ 'x-foreign-key' } = join '.', $foreign_schema, $foreign_id; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | return @schema_names ? @schema{ @schema_names } : \%schema; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub _map_type { | ||
| 371 | my ( $self, $column ) = @_; | ||
| 372 | my %conf; | ||
| 373 | my $db_type = $column->{data_type} // 'varchar'; | ||
| 374 | |||
| 375 | if ( $column->{extra}{list} ) { | ||
| 376 | %conf = ( enum => $column->{extra}{list} ); | ||
| 377 | } | ||
| 378 | |||
| 379 | if ( $db_type =~ /^(?:text|varchar)/i ) { | ||
| 380 | %conf = ( %conf, type => 'string' ); | ||
| 381 | } | ||
| 382 | elsif ( $db_type =~ /^(?:boolean)/i ) { | ||
| 383 | %conf = ( %conf, type => 'boolean' ); | ||
| 384 | } | ||
| 385 | elsif ( $db_type =~ /^(?:int|integer|smallint|bigint|tinyint|rowid)/i ) { | ||
| 386 | %conf = ( %conf, type => 'integer' ); | ||
| 387 | } | ||
| 388 | elsif ( $db_type =~ /^(?:double|float|money|numeric|real)/i ) { | ||
| 389 | %conf = ( %conf, type => 'number' ); | ||
| 390 | } | ||
| 391 | elsif ( $db_type =~ /^(?:timestamp|datetime)/i ) { | ||
| 392 | %conf = ( %conf, type => 'string', format => 'date-time' ); | ||
| 393 | } | ||
| 394 | elsif ( $db_type =~ /(?:blob|bytea)/i ) { | ||
| 395 | %conf = ( %conf, type => 'string', format => 'binary' ); | ||
| 396 | } | ||
| 397 | else { | ||
| 398 | # Default to string | ||
| 399 | %conf = ( %conf, type => 'string' ); | ||
| 400 | } | ||
| 401 | |||
| 402 | if ( $column->{is_nullable} ) { | ||
| 403 | $conf{ type } = [ $conf{ type }, 'null' ]; | ||
| 404 | } | ||
| 405 | |||
| 406 | #; use Data::Dumper; | ||
| 407 | #; say "Field: " . Dumper $column; | ||
| 408 | #; say "Conf: " . Dumper \%conf; | ||
| 409 | |||
| 410 | return %conf; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub supports { 0 } | ||
| 414 | |||
| 415 | 1; | ||
| 416 | |||
| 417 | __END__ |