summaryrefslogtreecommitdiff
path: root/framework/DataAccess/TCreoleProvider.php
blob: 51b7013a279148681bbee34fa03157eafe676eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

Prado::using('System.DataAccess.TDatabaseProvider');
Prado::using('System.DataAccess.TDatabaseException');

Prado::using('System.DataAccess.creole.*');

class TCreoleProvider extends TDatabaseProvider
{
	private $_connection = null;

	public function getConnection()
	{
		if(is_null($this->_connection))
			$this->_connection = new TCreoleConnection($this);
		return $this->_connection;
	}

	public function getConnectionString()
	{
		if(strlen(parent::getConnectionString()) > 0)
			return parent::getConnectionString();
		else
			return $this->generateConnectionString();
	}

	protected function generateConnectionString()
	{
		$driver = $this->getDriver();
		$user = $this->getUsername();
		$pass = $this->getPassword();
		$host = $this->getHost();
		$database = $this->getDatabase();

		$pass = strlen($pass) > 0 ? ':'.$pass : '';
		$username_password = strlen($user) > 0 ? $user.$pass.'@' : '';
		$database = strlen($database) > 0 ? '/'.$database : '';

		return "{$driver}://{$username_password}{$host}{$database}";
	}
}

class TCreoleConnection extends TDbConnection
{
	private $_connection = null;

	protected function beginDbTransaction()
	{
	}

	/**
	 * Closes the connection to the database.
	 */
	public function close()
	{
		$this->_connection->close();
	}

	public function prepare($statement)
	{
		return $this->_connection->prepareStatement($statement);
	}

	//public function execute($sql, 

	/**
	 * Opens a database connection with settings provided in the ConnectionString.
	 */
	public function open()
	{
		if(is_null($this->_connection))
		{
			$connectionString = $this->getProvider()->getConnectionString();
			if(strlen($connectionString) < 1 || strcmp($connectionString,'://') === 0)
				throw new TDbConnectionException('db_driver_required');
			$class = 'System.DataAccess.creole.creole.Creole';
			$creole = Prado::createComponent($class);
			$this->_connection = $creole->getConnection($connectionString);				
		}
		return $this->_connection;
	}
}

?>