use strict;
return sub
{
	my $dbh = shift;
	
	my ($sth, $sth1, $maxId, $id, $row);

	
	$sth = $dbh->prepare("select max(id) from jin_download");
	$sth->execute;
	$maxId = $sth->fetchrow + 0;
	
	$sth = $dbh->prepare("select distinct owner_id,dlv_link_url as url,dlv_link_username as username,dlv_link_password as password from jin_product where dlv_link_url!='' and dlv_downloads=''");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1 = $dbh->prepare("select id from jin_download where owner_id=? and url=? and username=? and password=?");
		$sth1->execute(map { "$row->{$_}" } qw/owner_id url username password/);
		
		if ($sth1->rows > 0)
		{
			$id = $sth1->fetch->[0] + 0;
		}
		else
		{
			$id = ++$maxId;
			$dbh->do("insert into jin_download (id,owner_id,url,username,password) values (?,?,?,?,?)", undef, $id, map { "$row->{$_}" } qw/owner_id url username password/);
		}
		
		$dbh->do("update jin_product set dlv_downloads=? where owner_id=? and dlv_link_url=? and dlv_link_username=? and dlv_link_password=?", undef, $id, map { "$row->{$_}" } qw/owner_id url username password/);
	}
	

	
	$sth = $dbh->prepare("select max(id) from jin_lmem");
	$sth->execute;
	$maxId = $sth->fetchrow + 0;
	
	$sth = $dbh->prepare("select distinct owner_id,dlv_lmem_path as path from jin_product where dlv_lmem_path!='' and dlv_lmems=''");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1 = $dbh->prepare("select id from jin_lmem where owner_id=? and path=?");
		$sth1->execute(map { "$row->{$_}" } qw/owner_id path/);
		
		if ($sth1->rows > 0)
		{
			$id = $sth1->fetch->[0] + 0;
		}
		else
		{
			$id = ++$maxId;
			$dbh->do("insert into jin_lmem (id,owner_id,path) values (?,?,?)", undef, $id, map { "$row->{$_}" } qw/owner_id path/);
		}
		
		$dbh->do("update jin_product set dlv_lmems=? where owner_id=? and dlv_lmem_path=?", undef, $id, map { "$row->{$_}" } qw/owner_id path/);
	}

	
	
	$sth = $dbh->prepare("select max(id) from jin_rmem");
	$sth->execute;
	$maxId = $sth->fetchrow + 0;
	
	$sth = $dbh->prepare("select distinct owner_id,dlv_rmem_ftp_serv as ftp_serv,dlv_rmem_ftp_user as ftp_user,dlv_rmem_ftp_pass as ftp_pass,dlv_rmem_ftp_dir as ftp_dir,dlv_rmem_url as url from jin_product where dlv_rmem_url!='' and dlv_rmems=''");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1 = $dbh->prepare("select id from jin_rmem where owner_id=? and ftp_serv=? and ftp_user=? and ftp_pass=? and ftp_dir=? and url=?");
		$sth1->execute(map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
		
		if ($sth1->rows > 0)
		{
			$id = $sth1->fetch->[0] + 0;
		}
		else
		{
			$id = ++$maxId;
			$dbh->do("insert into jin_rmem (id,owner_id,ftp_serv,ftp_user,ftp_pass,ftp_dir,url) values (?,?,?,?,?,?,?)", undef, $id, map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
		}
		
		$dbh->do("update jin_product set dlv_rmems=? where owner_id=? and dlv_rmem_ftp_serv=? and dlv_rmem_ftp_user=? and dlv_rmem_ftp_pass=? and dlv_rmem_ftp_dir=? and dlv_rmem_url=?", undef, $id, map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
	}
	

	
	$sth = $dbh->prepare("select max(id) from jin_lmem");
	$sth->execute;
	$maxId = $sth->fetchrow + 0;
	
	$sth = $dbh->prepare("select distinct owner_id,path from jin_user_lmem where lmem_id=0");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1 = $dbh->prepare("select id from jin_lmem where owner_id=? and path=?");
		$sth1->execute(map { "$row->{$_}" } qw/owner_id path/);
		
		if ($sth1->rows > 0)
		{
			$id = $sth1->fetch->[0] + 0;
		}
		else
		{
			$id = ++$maxId;
			$dbh->do("insert into jin_lmem (id,owner_id,path) values (?,?,?)", undef, $id, map { "$row->{$_}" } qw/owner_id path/);
		}
		
		$dbh->do("update jin_user_lmem set lmem_id=? where owner_id=? and path=?", undef, $id, map { "$row->{$_}" } qw/owner_id path/);
	}

	
	
	$sth = $dbh->prepare("select max(id) from jin_rmem");
	$sth->execute;
	$maxId = $sth->fetchrow + 0;
	
	$sth = $dbh->prepare("select distinct owner_id,ftp_serv,ftp_user,ftp_pass,ftp_dir,url from jin_user_rmem where rmem_id=0");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1 = $dbh->prepare("select id from jin_rmem where owner_id=? and ftp_serv=? and ftp_user=? and ftp_pass=? and ftp_dir=? and url=?");
		$sth1->execute(map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
		
		if ($sth1->rows > 0)
		{
			$id = $sth1->fetch->[0] + 0;
		}
		else
		{
			$id = ++$maxId;
			$dbh->do("insert into jin_rmem (id,owner_id,ftp_serv,ftp_user,ftp_pass,ftp_dir,url) values (?,?,?,?,?,?,?)", undef, $id, map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
		}
		
		$dbh->do("update jin_user_rmem set rmem_id=? where owner_id=? and ftp_serv=? and ftp_user=? and ftp_pass=? and ftp_dir=? and url=?", undef, $id, map { "$row->{$_}" } qw/owner_id ftp_serv ftp_user ftp_pass ftp_dir url/);
	}
	
	
	
	$sth = $dbh->prepare("select id,owner_id,u_lmems,u_rmems from jin_specusers where u_lmems!='' or u_rmems!=''");
	$sth->execute;

	while (my $row = $sth->fetchrow_hashref)
	{
		my $lm = join ',', grep { $_ } map
		{
			my $path = $_;
			$sth1 = $dbh->prepare("select id from jin_lmem where owner_id=? and path=?");
			$sth1->execute($row->{owner_id}, $path);
			$sth1->rows > 0 ? $sth1->fetch->[0] : undef;
		}
		split ',', $row->{u_lmems};

		my $rm = join ',', grep { $_ } map
		{
			my $url = $_;
			$sth1 = $dbh->prepare("select id from jin_rmem where owner_id=? and url=?");
			$sth1->execute($row->{owner_id}, $url);
			$sth1->rows > 0 ? $sth1->fetch->[0] : undef;
		}
		split ',', $row->{u_rmems};
		
		$dbh->do("update jin_specusers set u_lmems=?,u_rmems=? where id=?", undef, $lm, $rm, $row->{id});
	}

	
	$sth = $dbh->prepare("delete from zen_id_dbtable");
	$sth->execute;
	
};

