line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Otogiri; |
2
|
11
|
|
|
11
|
|
1118194
|
use 5.008005; |
|
11
|
|
|
|
|
138
|
|
3
|
11
|
|
|
11
|
|
61
|
use strict; |
|
11
|
|
|
|
|
30
|
|
|
11
|
|
|
|
|
271
|
|
4
|
11
|
|
|
11
|
|
54
|
use warnings; |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
596
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.21"; |
7
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
5186
|
use parent 'Exporter'; |
|
11
|
|
|
|
|
3405
|
|
|
11
|
|
|
|
|
67
|
|
9
|
11
|
|
|
11
|
|
6061
|
use SQL::QueryMaker; |
|
11
|
|
|
|
|
34413
|
|
|
11
|
|
|
|
|
1107
|
|
10
|
11
|
|
|
11
|
|
4883
|
use DBIx::Otogiri; |
|
11
|
|
|
|
|
40
|
|
|
11
|
|
|
|
|
1247
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = map {"sql_$_"} qw/ |
13
|
|
|
|
|
|
|
eq like lt gt le ge |
14
|
|
|
|
|
|
|
is_null is_not_null |
15
|
|
|
|
|
|
|
between not_between |
16
|
|
|
|
|
|
|
in not_in |
17
|
|
|
|
|
|
|
and or not |
18
|
|
|
|
|
|
|
op raw |
19
|
|
|
|
|
|
|
/; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
10
|
|
|
10
|
1
|
17133
|
my ($class, %opts) = @_; |
23
|
10
|
|
|
|
|
100
|
DBIx::Otogiri->new(%opts); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
1; |
27
|
|
|
|
|
|
|
__END__ |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=encoding utf-8 |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 NAME |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Otogiri - A lightweight medicine for using database |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 SYNOPSIS |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use Otogiri; |
38
|
|
|
|
|
|
|
my $db = Otogiri->new(connect_info => ['dbi:SQLite:...', '', '']); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$db->insert(book => {title => 'mybook1', author => 'me', ...}); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $book_id = $db->last_insert_id; |
43
|
|
|
|
|
|
|
my $row = $db->single(book => {id => $book_id}); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
print 'Title: '. $row->{title}. "\n"; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my @rows = $db->select(book => sql_ge(price => 500)); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# or non-strict mode |
50
|
|
|
|
|
|
|
my @rows = $db->select(book => {price => {'>=' => 500}}); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
for my $r (@rows) { |
53
|
|
|
|
|
|
|
printf "Title: %s \nPrice: %s yen\n", $r->{title}, $r->{price}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# or using iterator |
57
|
|
|
|
|
|
|
my $iter = $db->select(book => {price => {'>=' => 500}}); |
58
|
|
|
|
|
|
|
while (my $row = $iter->next) { |
59
|
|
|
|
|
|
|
printf "Title: %s \nPrice: %s yen\n", $row->{title}, $row->{price}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# If you using perl 5.38 or later, you can use class feature. |
63
|
|
|
|
|
|
|
class Book { |
64
|
|
|
|
|
|
|
field $id :param; |
65
|
|
|
|
|
|
|
field $title :param; |
66
|
|
|
|
|
|
|
field $author :param; |
67
|
|
|
|
|
|
|
field $price :param; |
68
|
|
|
|
|
|
|
field $created_at :param; |
69
|
|
|
|
|
|
|
field $updated_at :param; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
method title { |
72
|
|
|
|
|
|
|
return $title; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
my $book = $db->row_class('Book')->single(book => {id => 1}); # $book is Book object. |
76
|
|
|
|
|
|
|
say $book->title; # => say book title. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $hash = $db->no_row_class->single(book => {id => 1}); # $hash is HASH reference. |
79
|
|
|
|
|
|
|
say $hash->{title}; # => say book title. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$db->update(book => [author => 'oreore'], {author => 'me'}); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$db->delete(book => {author => 'me'}); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# using transaction |
86
|
|
|
|
|
|
|
do { |
87
|
|
|
|
|
|
|
my $txn = $db->txn_scope; |
88
|
|
|
|
|
|
|
$db->insert(book => ...); |
89
|
|
|
|
|
|
|
$db->insert(store => ...); |
90
|
|
|
|
|
|
|
$txn->commit; |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 DESCRIPTION |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Otogiri is a thing that like as ORM. A slogan is "Schema-less, Fat-less". |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Please see ATTRIBUTES section of L<DBIx::Otogiri> documentation. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 METHODS |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 new |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $db = Otogiri->new( connect_info => [$dsn, $dbuser, $dbpass] ); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Instantiate and connect to db. Then, it returns L<DBIx::Otogiri> object. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 EXPORT FUNCTIONS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Otogiri exports each SQL::QueryMaker::sql_* functions. (ex. sql_ge(), sql_like() and more...) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
For more information, please see FUNCTIONS section of L<SQL::QueryMaker>'s documentation. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 INFORMATION ABOUT INCOMPATIBILITY |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 version 0.11 |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
An insert() method is removed, and it was become a synonym of fast_insert() method. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
If you want to use previous style insert() method, please try L<Otogiri::Plugin::InsertAndFetch> . |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 LICENSE |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Copyright (C) ytnobody. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
128
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
ytnobody E<lt>ytnobody@gmail.comE<gt> |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 SEE ALSO |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
L<DBIx::Otogiri> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L<DBIx::Sunny> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
L<SQL::Maker> |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L<SQL::QueryMaker> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|