<?php
/**
 * @copyright  2009-2011 Roberto Perpuly
 *
 * This source may not be redistributed without prior consent.  Access to the 
 * source does not imply that you have consent to redistribute either this source
 * code or the generated binary. See LICENSE file for details.
 */

class Triumph_UrlTagTable extends Zend_Db_Table_Abstract {

	protected $_name = 'url_tags';

	/**
	 * saves the given Triumph_Urls into the database.
	 * 
	 * @param Triumph_Url[] $arrUrls the urls to insert 
	 */
	function saveUrls($arrUrls, $sourceDir) {
	
		// delete the old rows
		$sourceDbTable = new Triumph_SourceTable($this->getAdapter());
		$sourceId = $sourceDbTable->getOrSave($sourceDir);
		$strWhere = $this->getAdapter()->quoteInto("source_id = ?", $sourceId);
		$this->delete($strWhere);
		
		if (!is_array($arrUrls)) {
			return;
		}
		
		// sqlite optimizes transactions really well; use transaction so that the inserts are faster
		$this->getAdapter()->beginTransaction();
		foreach ($arrUrls as $url) {
			$this->insert(array(
				'source_id' => $sourceId,
				'url' => $url->url,
				'full_path' => $url->fileName,
				'class_name' => $url->className,
				'method_name' => $url->methodName
			));
		}
		$this->getAdapter()->commit();
	}
}