17 |
# select all posts which have been read or unread |
# select all posts which have been read or unread |
18 |
my $sql = qq{ |
my $sql = qq{ |
19 |
select |
select |
20 |
* |
items.id as _id, |
21 |
|
items.*, |
22 |
|
feeds.url as feed_url, |
23 |
|
feeds.title as feed_title, |
24 |
|
feeds.link as feed_link, |
25 |
|
feeds.description as feed_description |
26 |
from items |
from items |
27 |
-- join items_userdata on id=item_id |
join items_userdata on items.id = item_id |
28 |
-- where label = 'read' and value_numeric = ? |
-- where label = 'read' and value_numeric = ? |
29 |
-- limit 3 |
join feeds on items.feed_id = feeds.id |
30 |
order by id asc |
order by items.id asc |
31 |
|
-- limit 42 |
32 |
}; |
}; |
33 |
|
|
34 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
35 |
$sth->execute( 1 ) || die $sth->errstr(); |
$sth->execute( 1 ) || die $sth->errstr(); |
36 |
|
|
37 |
|
warn dump( $sth->{NAME} ); |
38 |
|
|
39 |
print "found ",$sth->rows," items to process..."; |
print "found ",$sth->rows," items to process..."; |
40 |
|
|
41 |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
51 |
my $pk = 'id'; |
my $pk = 'id'; |
52 |
|
|
53 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
54 |
my $_id = $row->{$pk}; |
my $_id = $row->{_id} || die "row needs _id"; |
55 |
my $doc = $db->newDoc( $_id ); |
my $doc = $db->newDoc( $_id ); |
56 |
$doc->{data} = $row; |
|
57 |
|
sub row2doc { |
58 |
|
my ( $row, $doc ) = @_; |
59 |
|
my $a = delete( $row->{xml} ); |
60 |
|
$doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a; |
61 |
|
my $a = delete( $row->{content} ); |
62 |
|
$doc->addAttachment( 'content.html', 'text/html', $a ) if $a; |
63 |
|
$doc->{data} = $row; |
64 |
|
return $doc; |
65 |
|
} |
66 |
|
|
67 |
|
row2doc( $row, $doc ); |
68 |
|
|
69 |
eval { $doc->create }; |
eval { $doc->create }; |
70 |
if ( $@ ) { |
if ( $@ ) { |
71 |
warn "can't create $_id $@"; |
$doc->retrieve; |
72 |
eval { $doc->update }; |
row2doc( $row, $doc )->update; |
73 |
warn $@ ? "can't update $_id $@" : "updated $_id"; |
# eval { $doc->update }; |
74 |
|
warn ( $@ ? "ERROR $_id $@" : "updated $_id" ), $/; |
75 |
} else { |
} else { |
76 |
warn "created ",dump( $row ); |
warn "created ",dump( $row ),$/; |
77 |
} |
} |
78 |
} |
} |
79 |
|
|