<?php

class Version {


	public function getAppVersion($db, $platform_name, $rev_count, $short_hash) {
		syslog(LOG_WARNING,"Version::getAppVersion(\$db, $platform_name, $rev_count, $short_hash)");
		$platform = Push::getPlatformByName($db, $platform_name);
		$platform_id = $platform==NULL?0:$platform['id'];
		$db_rev_count = (int)$rev_count;
		$db_short_hash = pg_escape_string($short_hash);

		$q = "SELECT * FROM myfloat_version WHERE platform_id=${platform_id} AND rev_count=${db_rev_count} AND short_hash='${db_short_hash}'";
		syslog(LOG_WARNING,$q);
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}

	public function getNewAppVersion($db, $platform_name, $rev_count, $short_hash) {
		syslog(LOG_WARNING,"Version::getNewAppVersion(\$db, $platform_name, $rev_count, $short_hash)");
		$platform = Push::getPlatformByName($db, $platform_name);
		$platform_id = $platform==NULL?0:$platform['id'];
		$db_rev_count = (int)$rev_count;
		$db_short_hash = pg_escape_string($short_hash);

		$q = "SELECT * FROM myfloat_version WHERE platform_id=${platform_id} AND rev_count>${db_rev_count} AND short_hash<>'${db_short_hash}'";
		$q.= " ORDER BY rev_count DESC LIMIT 1";
		syslog(LOG_WARNING,$q);
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}
}

// vi:ts=2

