#!/usr/bin/perl

use strict;
use warnings;

# 20050423
#
# Silly hack to deal with the fact that MySQL 3.23.58 didn't
# support doing UPDATE against multiple tables.
# http://dev.mysql.com/doc/mysql/en/update.html
# Works fine on my copy of MysQL 4.0.x though.
#
# Here foreach entry in temp_views we update the
# associated table g2_ItemAttributesMap such that
# g_viewCount is once again reset to the number
# of views we actually had.  Why this information
# is really worth three hours of hacking,
# I don't know.

use DBI;

# omg what's teh pass?

my $d = DBI->connect(
	'dbi:mysql:dbname=g2db;host=localhost;port=3306',
	'myuser',
	'mypassword'
) or die;

my $s = $d->prepare(
	qq{SELECT g_itemId, g_viewCount FROM temp_views}
);

eval {
	$s->execute;
};
if( $@ ) { warn $@ };

while ( my $row = $s->fetchrow_arrayref ) {
	my $upd = $d->prepare(
		qq{
			UPDATE g2_ItemAttributesMap SET
			g_viewCount = ? WHERE g_itemID = ?
		}
	);

	$upd->bind_param( 1, $row->[1] );
	$upd->bind_param( 2, $row->[0] );

	eval {
		$upd->execute;
	};
	if( $@ ) {
		warn $@;
	}

	$upd->finish;

}

$s->finish;

$d->disconnect;


