<?php

/**
 * Provides configured database access adapter
 *
 * Alter static method buildAdapter to make this class useful.
 * Then use <code>DBFactory::getAdapter()</code> to get DBAdapter.
 * There is only one instance of DBAdapter created.
 *
 * @version DR1.6
 */
final class DBFactory {



	protected static $adapter;



	/**
	 * Returns instance of DBAdapter
	 *
	 * Alter this method so it returns desired DBAdapter.
	 * See <code>lib/model/db-adapter</code> for other database access library adapters.
	 * See <code>lib/model/sql</code> for other SQL dialects.
	 *
	 * @note use try-catch or turn off error reporting to prevent exposing password in case of db connection failure
	 * @note for SQLite PDO driver both the database file and folder must be server writable
	 *
	 * @return DBAdapter
	 */
	protected static function buildAdapter() {
		try {
			$pdo = new PDO('sqlite:var/sqlite/phonebook.sqlite');
		} catch (Exception $e) {
			die($e->getMessage());
		}
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		return new PDOAdapter($pdo, new SQL92Dialect());
	}



	/**
	 * Returns instance of DBAdapter, creates it when needed
	 *
	 * @return DBAdapter
	 */
	public static function getAdapter() {
		if (!self::$adapter) {
			self::$adapter = self::buildAdapter();
		}
		return self::$adapter;
	}



} // class DBFactory

?>