/usr/share/perl5/DBIx/Class/Schema/KiokuDB.pm is in libkiokudb-backend-dbi-perl 1.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | package DBIx::Class::Schema::KiokuDB;
BEGIN {
$DBIx::Class::Schema::KiokuDB::AUTHORITY = 'cpan:NUFFIN';
}
# ABSTRACT: Hybrid KiokuDB/DBIx::Class::Schema schema support.
$DBIx::Class::Schema::KiokuDB::VERSION = '1.23';
use strict;
use warnings;
use Carp qw(croak);
use DBI 1.607 ();
use DBIx::Class 0.08127 ();
use DBIx::Class::KiokuDB::EntryProxy;
use DBIx::Class::ResultSource::Table;
use Scalar::Util qw(weaken refaddr);
use namespace::clean;
use base qw(Class::Accessor::Grouped);
__PACKAGE__->mk_group_accessors( inherited => "kiokudb_entries_source_name" );
sub kiokudb_handle {
my $self = shift;
croak "Can't call kiokudb_handle on unconnected schema" unless ref $self;
unless ( $self->{kiokudb_handle} ) {
require KiokuDB;
require KiokuDB::Backend::DBI;
croak "Can't vivify KiokuDB handle without KiokuDB schema bits. " .
"Add __PACKAGE__->define_kiokudb_schema() to your schema class"
unless $self->kiokudb_entries_source_name;
my $dir = KiokuDB->new(
backend => my $backend = KiokuDB::Backend::DBI->new(
connected_schema => $self,
),
);
$backend->meta->get_attribute('schema')->_weaken_value($backend); # FIXME proper MOP api?
# not weak
$self->{kiokudb_handle} = $dir;
}
$self->{kiokudb_handle};
}
sub _kiokudb_handle {
my ( $self, $handle ) = @_;
croak "Can't call _kiokudb_handle on unconnected schema" unless ref $self;
croak "Can't vivify KiokuDB handle without KiokuDB schema bits. " .
"Add __PACKAGE__->define_kiokudb_schema() to your schema class"
unless $self->kiokudb_entries_source_name;
if ( $self->{kiokudb_handle} ) {
if ( refaddr($self->{kiokudb_handle}) != refaddr($handle) ) {
croak "KiokuDB directory already registered";
}
} else {
$self->{kiokudb_handle} = $handle;
weaken($self->{kiokudb_handle});
}
return $handle;
}
sub define_kiokudb_schema {
my ( $self, @args ) = @_;
my %args = (
schema => $self,
entries_table => "entries",
gin_index_table => "gin_index",
result_class => "DBIx::Class::KiokuDB::EntryProxy",
gin_index => 1,
@args,
);
my $entries_source_name = $args{entries_source} ||= $args{entries_table};
my $gin_index_source_name = $args{gin_index_source} ||= $args{gin_index_table};
my $entries = $self->define_kiokudb_entries_resultsource(%args);
my $schema = $args{schema};
$schema->register_source( $entries_source_name => $entries );
if ($args{gin_index}) {
my $gin_index = $self->define_kiokudb_gin_index_resultsource(%args);
$schema->register_source( $gin_index_source_name => $gin_index );
}
$schema->kiokudb_entries_source_name($entries_source_name)
unless $schema->kiokudb_entries_source_name;
}
sub define_kiokudb_entries_resultsource {
my ( $self, %args ) = @_;
my $entries = DBIx::Class::ResultSource::Table->new({ name => $args{entries_table} });
$entries->add_columns(
id => { data_type => "varchar" },
data => { data_type => "blob", is_nullable => 0 }, # FIXME longblob for mysql
class => { data_type => "varchar", is_nullable => 1 },
root => { data_type => "boolean", is_nullable => 0 },
tied => { data_type => "char", size => 1, is_nullable => 1 },
@{ $args{extra_entries_columns} || [] },
);
$entries->set_primary_key("id");
$entries->sqlt_deploy_callback(sub {
my ($source, $sqlt_table) = @_;
$sqlt_table->extra->{mysql_table_type} = "InnoDB";
if ( $source->schema->storage->sqlt_type eq 'MySQL' ) {
$sqlt_table->get_field('data')->data_type('longblob');
}
});
$entries->result_class($args{result_class});
return $entries;
}
sub define_kiokudb_gin_index_resultsource {
my ( $self, %args ) = @_;
my $gin_index = DBIx::Class::ResultSource::Table->new({ name => $args{gin_index_table} });
$gin_index->add_columns(
id => { data_type => "varchar", is_foreign_key => 1 },
value => { data_type => "varchar" },
);
$gin_index->add_relationship('entry_ids', $args{entries_source}, { 'foreign.id' => 'self.id' });
$gin_index->sqlt_deploy_callback(sub {
my ($source, $sqlt_table) = @_;
$sqlt_table->extra->{mysql_table_type} = "InnoDB";
$sqlt_table->add_index( name => 'gin_index_ids', fields => ['id'] )
or die $sqlt_table->error;
$sqlt_table->add_index( name => 'gin_index_values', fields => ['value'] )
or die $sqlt_table->error;
});
return $gin_index;
}
# ex: set sw=4 et:
__PACKAGE__
__END__
=pod
=encoding UTF-8
=head1 NAME
DBIx::Class::Schema::KiokuDB - Hybrid KiokuDB/DBIx::Class::Schema schema support.
=head1 VERSION
version 1.23
=head1 SYNOPSIS
Load this component into the schema:
package MyApp::DB;
use base qw(DBIx::Class::Schema);
__PACKAGE__->load_components(qw(Schema::KiokuDB));
__PAKCAGE__->load_namespaces;
Then load the L<DBIx::Class::KiokuDB> component into every table that wants to
refer to arbitrary KiokuDB objects:
package MyApp::DB::Result::Album;
use base qw(DBIx::Class::Core);
__PACKAGE__->load_components(qw(KiokuDB));
__PACKAGE__->table('album');
__PACKAGE__->add_columns(
id => { data_type => "integer" },
title => { data_type => "varchar" },
# the foreign key for the KiokuDB object:
metadata => { data_type => "varchar" },
);
__PACKAGE__->set_primary_key('id');
# enable a KiokuDB rel on the column:
__PACKAGE__->kiokudb_column('metadata');
Connect to the DSN:
my $dir = KiokuDB->connect(
'dbi:SQLite:dbname=:memory:',
schema => "MyApp::DB",
create => 1,
);
# get the connect DBIC schema instance
my $schema = $dir->backend->schema;
Then you can freely refer to KiokuDB objects from your C<Album> class:
$dir->txn_do(scope => 1, body => sub {
$schema->resultset("Album")->create({
title => "Blah blah",
metadata => $any_object,
});
});
=head1 DESCRIPTION
This class provides the schema definition support code required for integrating
an arbitrary L<DBIx::Class::Schema> with L<KiokuDB::Backend::DBI>.
=head2 REUSING AN EXISTING DBIx::Class SCHEMA
The example in the Synopis assumes that you want to first set up a
L<KiokuDB> and than link that to some L<DBIx::Class> classes. Another
use case is that you already have a configured L<DBIx::Class> Schema
and want to tack L<KiokuDB> onto it.
The trick here is to make sure to load the L<KiokuDB> schema using
C<< __PACKAGE__->define_kiokudb_schema() >> in your Schema class:
package MyApp::DB;
use base qw(DBIx::Class::Schema);
__PACKAGE__->load_components(qw(Schema::KiokuDB));
__PACKAGE__->define_kiokudb_schema();
__PAKCAGE__->load_namespaces;
You can now get the L<KiokuDB> directory handle like so:
my $dir = $schema->kiokudb_handle;
For a complete example take a look at F<t/autovivify_handle.t>.
=head1 USAGE AND LIMITATIONS
L<KiokuDB> managed objects may hold references to row objects, resultsets
(treated as saved searches, or results or cursor state is saved), result source
handles, and the schema.
Foreign L<DBIx::Class> objects, that is ones that originated from a schema that
isn't the underlying schema are currently not supported, but this limitation
may be lifted in the future.
All DBIC operations which may implicitly cause a lookup of a L<KIokuDB> managed
object require live object scope management, just as normal.
It is reccomended to use L<KiokuDB/txn_do> because that will invoke the
appropriate transaction hooks on both layers, as opposed to just in
L<DBIx::Class>.
=head1 SEE ALSO
L<DBIx::Class::KiokuDB>, L<KiokuDB::Backend::DBI>.
=for Pod::Coverage define_kiokudb_entries_resultsource
define_kiokudb_gin_index_resultsource
define_kiokudb_schema
kiokudb_handle
=head1 AUTHOR
Yuval Kogman <nothingmuch@woobling.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Yuval Kogman, Infinity Interactive.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|