summaryrefslogtreecommitdiff
path: root/lib/VNDB/DB/Chars.pm
blob: 923d1e94f2bff2846e6ccba1bb16ab76624cbc4f (plain)
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

package VNDB::DB::Chars;

use strict;
use warnings;
use Exporter 'import';

our @EXPORT = qw|dbCharGet dbCharGetRev dbCharRevisionInsert dbCharImageId|;


# options: id instance tagspoil trait_inc trait_exc char what results page gender bloodt
#   bust_min bust_max waist_min waist_max hip_min hip_max height_min height_max weight_min weight_max role
# what: extended traits vns changes
sub dbCharGet {
  my $self = shift;
  my %o = (
    page => 1,
    results => 10,
    what => '',
    tagspoil => 0,
    @_
  );

  $o{search} =~ s/%//g if $o{search};

  my %where = (
    !$o{id} ? ( 'c.hidden = FALSE' => 1 ) : (),
    $o{id}  ? ( 'c.id IN(!l)' => [ ref $o{id} ? $o{id} : [$o{id}] ] ) : (),
    $o{notid}    ? ( 'c.id <> ?'   => $o{notid} ) : (),
    $o{instance} ? ( 'c.main = ?' => $o{instance} ) : (),
    $o{vid}      ? ( 'c.id IN(SELECT id FROM chars_vns WHERE vid = ?)' => $o{vid} ) : (),
    defined $o{gender}     ? ( 'c.gender IN(!l)' => [ ref $o{gender} ? $o{gender} : [$o{gender}] ]) : (),
    defined $o{bloodt}     ? ( 'c.bloodt IN(!l)' => [ ref $o{bloodt} ? $o{bloodt} : [$o{bloodt}] ]) : (),
    defined $o{bust_min}   ? ( 'c.s_bust >= ?' => $o{bust_min} ) : (),
    defined $o{bust_max}   ? ( 'c.s_bust <= ? AND c.s_bust > 0' => $o{bust_max} ) : (),
    defined $o{waist_min}  ? ( 'c.s_waist >= ?' => $o{waist_min} ) : (),
    defined $o{waist_max}  ? ( 'c.s_waist <= ? AND c.s_waist > 0' => $o{waist_max} ) : (),
    defined $o{hip_min}    ? ( 'c.s_hip >= ?' => $o{hip_min} ) : (),
    defined $o{hip_max}    ? ( 'c.s_hip <= ? AND c.s_hip > 0' => $o{hip_max} ) : (),
    defined $o{height_min} ? ( 'c.height >= ?' => $o{height_min} ) : (),
    defined $o{height_max} ? ( 'c.height <= ? AND c.height > 0' => $o{height_max} ) : (),
    defined $o{weight_min} ? ( 'c.weight >= ?' => $o{weight_min} ) : (),
    defined $o{weight_max} ? ( 'c.weight <= ? AND c.weight > 0' => $o{weight_max} ) : (),
    $o{search} ? (
      "(c.name ILIKE ? OR translate(c.original,' ','') ILIKE translate(?,' ','') OR c.alias ILIKE ?)", [ map '%'.$o{search}.'%', 1..3 ] ) : (),
    $o{char} ? (
      'LOWER(SUBSTR(c.name, 1, 1)) = ?' => $o{char} ) : (),
    defined $o{char} && !$o{char} ? (
      '(ASCII(c.name) < 97 OR ASCII(c.name) > 122) AND (ASCII(c.name) < 65 OR ASCII(c.name) > 90)' => 1 ) : (),
    $o{role} ? (
      'EXISTS(SELECT 1 FROM chars_vns cvi WHERE cvi.id = c.id AND cvi.role IN(!l))',
      [ ref $o{role} ? $o{role} : [$o{role}] ] ) : (),
    $o{trait_inc} ? (
      'c.id IN(SELECT cid FROM traits_chars WHERE tid IN(!l) AND spoil <= ? GROUP BY cid HAVING COUNT(tid) = ?)',
      [ ref $o{trait_inc} ? $o{trait_inc} : [$o{trait_inc}], $o{tagspoil}, ref $o{trait_inc} ? $#{$o{trait_inc}}+1 : 1 ]) : (),
    $o{trait_exc} ? (
      'c.id NOT IN(SELECT cid FROM traits_chars WHERE tid IN(!l))' => [ ref $o{trait_exc} ? $o{trait_exc} : [$o{trait_exc}] ] ) : (),
  );

  my @select = (qw|c.id c.name c.original c.gender|);
  push @select, qw|c.hidden c.locked c.alias c.desc c.image c.b_month c.b_day c.s_bust c.s_waist c.s_hip c.height c.weight c.bloodt c.main c.main_spoil| if $o{what} =~ /extended/;

  my($r, $np) = $self->dbPage(\%o, q|
    SELECT !s
      FROM chars c
      !W
      ORDER BY c.name|,
    join(', ', @select), \%where
  );

  return _enrich($self, $r, $np, 0, $o{what}, $o{vid});
}


sub dbCharGetRev {
  my $self = shift;
  my %o = (what => '', @_);

  $o{rev} ||= $self->dbRow('SELECT MAX(rev) AS rev FROM changes WHERE type = \'c\' AND itemid = ?', $o{id})->{rev};

  my $select = 'c.itemid AS id, ch.name, ch.original, ch.gender';
  $select .= ', extract(\'epoch\' from c.added) as added, c.requester, c.comments, u.username, c.rev, c.ihid, c.ilock';
  $select .= ', c.id AS cid, NOT EXISTS(SELECT 1 FROM changes c2 WHERE c2.type = c.type AND c2.itemid = c.itemid AND c2.rev = c.rev+1) AS lastrev';
  $select .= ', ch.alias, ch.desc, ch.image, ch.b_month, ch.b_day, ch.s_bust, ch.s_waist, ch.s_hip, ch.height, ch.weight, ch.bloodt, ch.main, ch.main_spoil, co.hidden, co.locked' if $o{what} =~ /extended/;

  my $r = $self->dbAll(q|
    SELECT !s
      FROM changes c
      JOIN chars co ON co.id = c.itemid
      JOIN chars_hist ch ON ch.chid = c.id
      JOIN users u ON u.id = c.requester
      WHERE c.type = 'c' AND c.itemid = ? AND c.rev = ?|,
    $select, $o{id}, $o{rev}
  );

  return _enrich($self, $r, 0, 1, $o{what});
}


sub _enrich {
  my($self, $r, $np, $rev, $what, $vid) = @_;

  if(@$r && $what =~ /vns|traits/) {
    my($col, $hist, $colname) = $rev ? ('cid', '_hist', 'chid') : ('id', '', 'id');
    my %r = map {
      $_->{traits} = [];
      $_->{vns} = [];
      ($_->{$col}, $_)
    } @$r;

    if($what =~ /traits/) {
      push @{$r{ delete $_->{xid} }{traits}}, $_ for (@{$self->dbAll(qq|
        SELECT ct.$colname AS xid, ct.tid, ct.spoil, t.name, t.sexual, t."group", tg.name AS groupname
          FROM chars_traits$hist ct
          JOIN traits t ON t.id = ct.tid
          JOIN traits tg ON tg.id = t."group"
         WHERE ct.$colname IN(!l)
         ORDER BY tg."order", t.name|, [ keys %r ]
      )});
    }

    if($what =~ /vns(?:\((\d+)\))?/) {
      push @{$r{ delete $_->{xid} }{vns}}, $_ for (@{$self->dbAll("
        SELECT cv.$colname AS xid, cv.vid, cv.rid, cv.spoil, cv.role, v.title AS vntitle, r.title AS rtitle
          FROM chars_vns$hist cv
          JOIN vn v ON cv.vid = v.id
          LEFT JOIN releases r ON cv.rid = r.id
          !W
          ORDER BY v.c_released",
        { "cv.$colname IN(!l)" => [[keys %r]], $1 ? ('cv.vid = ?', $1) : () }
      )});
    }
  }

  # Depends on the VN revision rather than char revision
  if(@$r && $what =~ /seiyuu/) {
    my %r = map {
      $_->{seiyuu} = [];
      ($_->{id}, $_)
    } @$r;

    push @{$r{ delete $_->{cid} }{seiyuu}}, $_ for (@{$self->dbAll(q|
      SELECT vs.cid, s.id AS sid, sa.name, sa.original, vs.note, v.id AS vid, v.title AS vntitle
        FROM vn_seiyuu vs
        JOIN staff_alias sa ON sa.aid = vs.aid
        JOIN staff s ON s.id = sa.id
        JOIN vn v ON v.id = vs.id
        !W
        ORDER BY v.c_released, sa.name|, {
          's.hidden = FALSE' => 1,
          'vs.cid IN(!l)' => [[ keys %r ]],
          $vid ? ('v.id = ?' => $vid) : (),
        }
    )});
  }

  return wantarray ? ($r, $np) : $r;
}


# Updates the edit_* tables, used from dbItemEdit()
# Arguments: { columns in chars_rev + traits + vns },
sub dbCharRevisionInsert {
  my($self, $o) = @_;

  my %set = map exists($o->{$_}) ? (qq|"$_" = ?|, $o->{$_}) : (),
    qw|name original alias desc image b_month b_day s_bust s_waist s_hip height weight bloodt gender main main_spoil|;
  $self->dbExec('UPDATE edit_chars !H', \%set) if keys %set;

  if($o->{traits}) {
    $self->dbExec('DELETE FROM edit_chars_traits');
    $self->dbExec('INSERT INTO edit_chars_traits (tid, spoil) VALUES (?,?)', $_->[0],$_->[1]) for (@{$o->{traits}});
  }
  if($o->{vns}) {
    $self->dbExec('DELETE FROM edit_chars_vns');
    $self->dbExec('INSERT INTO edit_chars_vns (vid, rid, spoil, role) VALUES(!l)', $_) for (@{$o->{vns}});
  }
}


# fetches an ID for a new image
sub dbCharImageId {
  return shift->dbRow("SELECT nextval('charimg_seq') AS ni")->{ni};
}


1;