<?php
/**
 * @copyright  2012 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_DatabaseTagTable extends Zend_Db_Table_Abstract {

	protected $_name = 'database_tags';
	

	/**
	 * Retrieves all of the methods from the given files. Only resources that 
	 * are methods will be returned.
	 *
	 * @param Triumph_DatabaseTag[] the database tags to save
	 * @param string $sourceDir the source directory for this database tag
	 */
	public function saveDatabaseTags($databaseTags, $sourceDir) {
		
		// delete all old database tags
		$sourceDbTable = new Triumph_SourceTable($this->getAdapter());
		$sourceId = $sourceDbTable->getOrSave($sourceDir);
		$strWhere = $this->getAdapter()->quoteInto("source_id = ?", $sourceId);
		$this->delete($strWhere);
		
		// sqlite optimizes transactions really well; use transaction so that the inserts are faster
		$this->getAdapter()->beginTransaction();
		foreach ($databaseTags as $databaseTag) {
			$this->insert(array(
				'source_id' => $sourceId,
				'label' => $databaseTag->label,
				'schema' => $databaseTag->schema,
				'driver' => $databaseTag->driver,
				'host' => $databaseTag->host,
				'port' => $databaseTag->port,
				'user' => $databaseTag->user,
				'password' => $databaseTag->password
			));
		}
		$this->getAdapter()->commit();
 	}
	
}
