File: IcingaApiConnectionDatabase.php

package info (click to toggle)
icinga 1.0.2-2%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 33,964 kB
  • ctags: 13,294
  • sloc: xml: 154,821; ansic: 99,198; sh: 14,761; sql: 5,852; php: 5,126; perl: 2,838; makefile: 1,268
file content (222 lines) | stat: -rw-r--r-- 5,418 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

/**
 * 
 * @author Christian Doebler <christian.doebler@netways.de>
 *
 */
abstract class IcingaApiConnectionDatabase
	extends IcingaApiConnection
	implements IcingaApiConnectionInterface {

	/*
	 * VARIABLES
	 */

	private $databaseDsn = false;

	public $connectionStatement = false;

	/*
	 * METHODS
	 */

	/**
	 * class constructor
	 *
	 * @param	void
	 * @return	void
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function __construct () {}

	/**
	 * calls initialization method for new search
	 *
	 * @param	void
	 * @return	mixed							search object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	abstract public function createSearch ();

	/**
	 * checks the database configuration
	 *
	 * @param	array		$config				associative array storing configuration
	 * @return	boolean							true if configuration is OK, false on error(s)
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function checkConfig (array $config) {

		$configOk = true;

		if (!array_key_exists('type', $config)) {
			throw new IcingaApiConnectionDatabaseException('Configuration error: No database type defined!');
			$configOk = false;
		}

		if (!array_key_exists('host', $config)) {
			throw new IcingaApiConnectionDatabaseException('Configuration error: No database host defined!');
			$configOk = false;
		}

		if (array_key_exists('port', $config)) {
			$port = (int)$config['port'];
			if ($port < 1 || $port > 65535) {
				throw new IcingaApiConnectionDatabaseException('Configuration error: Invalid database port!');
				$configOk = false;
			}
		}

		if (!array_key_exists('database', $config)) {
			throw new IcingaApiConnectionDatabaseException('Configuration error: No database defined!');
			$configOk = false;
		}

		if (!array_key_exists('user', $config)) {
			throw new IcingaApiConnectionDatabaseException('Configuration error: No database user defined!');
			$configOk = false;
		}

		if (!array_key_exists('password', $config)) {
			throw new IcingaApiConnectionDatabaseException('Configuration error: No database password defined!');
			$configOk = false;
		}

		$configOkParent = parent::checkConfig($config);
		if (!$configOkParent && $configOk) {
			$configOk = false;
		}

		return $configOk;

	}

	/**
	 * assembles the database DSN
	 *
	 * @param	array		$config				associative array storing database connection settings
	 * @return	mixed							DSN string on success otherwise boolean false
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function setConfig (array $config) {

		if ($this->databaseDsn === false) {

			if (!$this->checkConfig($config)) {
				return false;
			}

			if (!array_key_exists('table_prefix', $config)) {
				$config['table_prefix'] = null;
			}
			$this->config = $config;

			if ($this->config['type'] != 'oci') {

				$this->databaseDsn = sprintf(
					'%s:host=%s;dbname=%s',
					$this->config['type'],
					$this->config['host'],
					$this->config['database']
				);
				if (array_key_exists('port', $this->config)) {
					$this->databaseDsn .= ';port=' . (int)$this->config['port'];
				}

			} else {

				if (array_key_exists('port', $this->config)) {
					$dbPort = ':' . $this->config['port'];
				} else {
					$dbPort = null;
				}
				$this->databaseDsn = sprintf(
					'%s:dbname=//%s%s/%s',
					$this->config['type'],
					$this->config['host'],
					$dbPort,
					$this->config['database']
				);

			}

		}

		return $this;

	}

	/**
	 * connects to database
	 *
	 * @param	array		$config				associative array storing database connection settings
	 * @return	IcingaApiConnectionDatabase		database object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function connect () {

		if ($this->databaseDsn !== false) {

			if (array_key_exists('persistent', $this->config)) {
				$connectionAttributes = array (
					PDO::ATTR_PERSISTENT	=> true,
				);
			} else {
				$connectionAttributes = false;
			}

			try {

				if ($connectionAttributes !== false) {
					$this->connectionObject = new PDO($this->databaseDsn, $this->config['user'], $this->config['password'], $connectionAttributes);
				} else {
					$this->connectionObject = new PDO($this->databaseDsn, $this->config['user'], $this->config['password']);
				}

			} catch (PDOException $e) {

				throw new IcingaApiConnectionDatabaseException('Database connection failed: ' . $e->getMessage());

			}

		}

		return $this;

	}

	/**
	 * queries database using PDOs
	 *
	 * @param	string			$query				query to execute
	 * @param	array			$queryParams		query parameters to use with query
	 * @return	IcingaApiConnectionDatabase			database object
	 * @author	Christian Doebler <christian.doebler@netways.de>
	 */
	public function query ($query, $queryParams = array()) {

		try {
			$this->connectionStatement = $this->connectionObject->prepare($query);			
		} catch (PDOException $e) {
			throw new IcingaApiConnectionDatabaseException('Database-query prepare failed: ' . $e->getMessage());
		}

		if ($this->connectionStatement !== false) {
			try {
				$this->connectionStatement->execute($queryParams);
			} catch (PDOException $e) {
				throw new IcingaApiConnectionDatabaseException('Database-query execute failed: ' . $e->getMessage());
			}
		}

		return $this;

	}

}

// extend exceptions
class IcingaApiConnectionDatabaseException extends IcingaApiConnectionException {}

?>