summaryrefslogtreecommitdiff
path: root/framework/Testing/Data/Distributed/MasterSlave/TMasterSlaveDbConnection.php
blob: e6b71931ffcba364264053d73336a72a5f5420bf (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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
 * IMasterSlaveDbConnection, ISlaveDbConnection inferface,
 * TMasterSlaveDbConnection, TSlaveDbConnection class file.
 *
 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2010 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Testing.Data.Distributed.MasterSlave
 * @todo Test with Backport of Yii's DBO
 */

	Prado::using('System.Data.TDbConnection');
	Prado::using('System.Testing.Data.Distributed.TDistributedDbConnection');
	Prado::using('System.Collections.TQueue');
	Prado::using('System.Collections.TStack');
	Prado::using('System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbTransaction');

	/**
	 * IMasterSlaveDbConnection interface
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed.MasterSlave
	 * @since 4.0
	 */
	interface IMasterSlaveDbConnection extends IDistributedDbConnection
	{
		/**
		 * @return TQueue
		 */
		public function getStatementQueue();

		/**
		 * @return ISlaveDbConnection|null
		 */
		public function getSlaveConnection();

		/**
		 * @param ISlaveDbConnection|null
		 */
		public function setSlaveConnection($conn);

		/**
		 * @return TMasterSlaveDbConnectionForceMaster
		 */
		public function getForceMaster();

		/**
		 * @param TMasterSlaveDbConnectionForceMaster
		 */
		public function setForceMaster($value);

	}

	/**
	 * ISlaveDbConnection interface
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed.MasterSlave
	 * @since 4.0
	 */
	interface ISlaveDbConnection extends IDistributedDbConnection
	{
		/**
		 * @return IMasterSlaveDbConnection|null
		 */
		public function getMasterConnection();

		/**
		 * @param IMasterSlaveDbConnection|null
		 */
		public function setMasterConnection($conn);

		/**
		 * @return TMasterSlaveDbConnectionForceMaster
		 */
		public function getForceMaster();

		/**
		 * @param TMasterSlaveDbConnectionForceMaster
		 */
		public function setForceMaster($value);
	}

	/**
	 * TMasterSlaveDbConnection class
	 *
	 * IMPORTANT!!!
	 * BETA Version - Use with care and NOT in production environment (only tested with MySql)
	 *
	 * TMasterSlaveDbConnection represents a master connection to a database in master/slave senario.
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed.MasterSlave
	 * @since 4.0
	 */
	class TMasterSlaveDbConnection extends TDistributedDbConnection implements IMasterSlaveDbConnection
	{
		/**
		 * @var TSlaveDbConnection|null
		 */
		private $_connSlave = null;

		/**
		 * @var TQueue
		 */
		private $_statementQueue = null;

		/**
		 * @var integer
		 * @see TMasterSlaveDbConnectionForceMaster
		 */
		private $_forceMaster = TMasterSlaveDbConnectionForceMaster::OFF_AUTOMATIC;

		private $_forceMasterStack = null;

		/**
		 * Constructor.
		 * Note, the DB connection is not established when this connection
		 * instance is created. Set {@link setActive Active} property to true
		 * to establish the connection.
		 *
		 * @param string The Data Source Name, or DSN, contains the information required to connect to the database.
		 * @param string The user name for the DSN string.
		 * @param string The password for the DSN string.
		 * @param string Charset used for DB Connection (MySql & pgsql only). If not set, will use the default charset of your database server
		 * @see http://www.php.net/manual/en/function.PDO-construct.php
		 */
		public function __construct($dsn='', $username='', $password='', $charset='')
		{
			parent::__construct($dsn, $username, $password, $charset);
			parent::setTransactionClass('System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbTransaction');
		}

		/**
		 * @return TQueue
		 */
		public function getStatementQueue()
		{
			if($this->_statementQueue===null)
				$this->_statementQueue = new TQueue();
			return $this->_statementQueue;
		}

		/**
		 * @return ISlaveDbConnection|null
		 */
		public function getSlaveConnection()
		{
			return $this->_connSlave;
		}

		/**
		 * @param ISlaveDbConnection|null
		 * @throws TDbConnectionException if the slave connection already exists
		 * @throws TDbConnectionException connection not instance of ISlaveDbConnection
		 */
		public function setSlaveConnection($conn)
		{
			if($this->_connSlave !== null)
				throw new TDbConnectionException('masterslavedbconnection_connection_exists', get_class($this), 'SlaveConnection');

			if($conn!==null && !$conn instanceof ISlaveDbConnection)
				throw new TDbConnectionException('masterslavedbconnection_interface_required', get_class($this), 'SlaveConnection', 'ISlaveDbConnection');

			$this->_connSlave = $conn;

			if($this->_connSlave===null) return;
			if($this->_connSlave->getMasterConnection()!==null) return;

			$this->_connSlave->setMasterConnection($this);
		}

		/**
		 * Creates a command for execution.
		 * @param string SQL statement associated with the new command.
		 * @return TDistributedDbCommand the DB command
		 * @throws TDbException if the connection is not active
		 */
		public function createCommand($sql)
		{
			$force = $this->getForceMaster();
			if($force == TMasterSlaveDbConnectionForceMaster::ON_MANUAL || $force == TMasterSlaveDbConnectionForceMaster::ON_TRANSACTION)
			{
				Prado::log('ForceMaster: ' . $force, TLogger::DEBUG, 'System.Testing.Data.Distributed.MasterSlave.TMasterSlaveDbConnection');
				return new TDistributedDbCommand($this, $sql, TDbStatementClassification::UNKNOWN);
			}

			$bEnqueue	= false;
			$bMaster	= true;

			$classification = $this->getStatementClassification($sql);

			switch($classification) {
				case TDbStatementClassification::CONTEXT:
					$bEnqueue	= true;
					$bMaster	= true;
				break;
				case TDbStatementClassification::SQL:
					$bMaster = false;
				break;
				case TDbStatementClassification::TCL:
					$this->setForceMaster(TMasterSlaveDbConnectionForceMaster::ON_TCL);
				case TDbStatementClassification::DDL:
				case TDbStatementClassification::DML:
				case TDbStatementClassification::DCL:
				case TDbStatementClassification::UNKNOWN:
				default:
					$bMaster = true;
				break;
			}

			$bMaster = $bMaster || $this->getForceMaster();

			$result = new TDistributedDbCommand(($bMaster ? $this : $this->getSlaveConnection()), $sql, $classification);
			//$result = new TDistributedDbCommand($this, $sql, $classification);

			if($bEnqueue)
				$this->getStatementQueue()->enqueue($result);

			return $result;
		}

		/**
		 * @return TMasterSlaveDbConnectionForceMaster
		 */
		public function getForceMaster()
		{
			return $this->_forceMaster;
		}

		/**
		 * @param TMasterSlaveDbConnectionForceMaster
		 */
		public function setForceMaster($value)
		{
			if($this->_forceMasterStack===null)
				$this->_forceMasterStack = new TStack();

			if($value)
			{
				$this->_forceMaster = (integer)$value;
				$this->_forceMasterStack->push((integer)$value);
			}
			elseif($this->_forceMasterStack->count() > 0)
				$this->_forceMaster = $this->_forceMasterStack->pop();
			else
				$this->_forceMaster = (integer)$value;
		}

		/**
		 * @return TDbConnectionServerRole
		 */
		public function getServerRole()
		{
			return TDbConnectionServerRole::Master;
		}
	}

	/**
	 * TSlaveDbConnection class
	 *
	 * IMPORTANT!!!
	 * BETA Version - Use with care and NOT in production environment (only tested with MySql)
	 *
	 * TSlaveDbConnection represents a readonly connection to a database in master/slave senario.
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed.MasterSlave
	 * @since 4.0
	 */
	class TSlaveDbConnection extends TDbConnection implements ISlaveDbConnection
	{
		/**
		 * @var TMasterSlaveDbConnection|null
		 */
		private $_connMaster = null;

		/**
		 * @return IMasterSlaveDbConnection|null
		 */
		public function getMasterConnection()
		{
			return $this->_connMaster;
		}

		/**
		 * @param IMasterSlaveDbConnection|null
		 * @throws TDbConnectionException if the master connection already exists
		 * @throws TDbConnectionException connection not instance of IMasterSlaveDbConnection
		 */
		public function setMasterConnection($conn)
		{
			if($this->_connMaster!==null)
				throw new TDbConnectionException('masterslavedbconnection_connection_exists', get_class($this), 'MasterConnection');

			if($conn!==null && !$conn instanceof IMasterSlaveDbConnection)
				throw new TDbConnectionException('masterslavedbconnection_interface_required', get_class($this), 'MasterConnection', 'IMasterSlaveDbConnection');

			$this->_connMaster = $conn;
		}

		/**
		 * Creates a command for execution.
		 * @param string SQL statement associated with the new command.
		 * @return TDistributedDbCommand the DB command
		 * @throws TDbException if the connection is not active
		 */
		public function createCommand($sql)
		{
			$force = $this->getForceMaster();
			if($force == TMasterSlaveDbConnectionForceMaster::ON_MANUAL || $force == TMasterSlaveDbConnectionForceMaster::ON_TRANSACTION)
			{
				Prado::log('ForceMaster: ' . $force, TLogger::DEBUG, 'System.Testing.Data.Distributed.MasterSlave.TSlaveDbConnection');
				return new TDistributedDbCommand($this->getMasterConnection(), $sql, TDbStatementClassification::UNKNOWN);
			}

			$bEnqueue	= false;
			$bMaster	= false;

			$classification = $this->getStatementClassification($sql);

			switch($classification) {
				case TDbStatementClassification::SQL:
					$bMaster = false;
				break;
				case TDbStatementClassification::CONTEXT:
					$bEnqueue	= true;
					$bMaster	= true;
				break;
				case TDbStatementClassification::TCL:
					$this->setForceMaster(TMasterSlaveDbConnectionForceMaster::ON_TCL);
				case TDbStatementClassification::DDL:
				case TDbStatementClassification::DML:
				case TDbStatementClassification::DCL:
				case TDbStatementClassification::UNKNOWN:
				default:
					$bMaster = true;
				break;
			}

			$bMaster = $bMaster || $this->getForceMaster();

			$result = new TDistributedDbCommand(($bMaster ? $this->getMasterConnection() : $this), $sql, $classification);

			if($bEnqueue)
				$this->getMasterConnection()->getStatementQueue()->enqueue($result);

			return $result;
		}

		/**
		 * Starts a transaction.
		 * @return TDbTransaction the transaction initiated
		 * @throws TDbException if no master connection exists or the connection is not active
		 */
		public function beginTransaction()
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');

			return $this->getMasterConnection()->beginTransaction();
		}

		/**
		 * @return string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}.
		 * @throws TDbException if no master connection exists
		 */
		public function getTransactionClass()
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			return $this->getMasterConnection()->getTransactionClass();
		}

		/**
		 * @param string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}.
		 * @throws TDbException if no master connection exists
		 */
		public function setTransactionClass($value)
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			$this->getMasterConnection()->setTransactionClass($value);
		}

		/**
		 * Gets the statement analyser of type given by
		 * {@link setStatementAnalyserClass StatementAnalyserClass }.
		 * @return IDbStatementAnalysis statement analyser.
		 * @throws TDbException if no master connection exists
		 */
		public function getStatementAnalyser()
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			return $this->getMasterConnection()->getStatementAnalyser();
		}

		/**
		 * The statement analyser class name to be created when {@link getStatementAnalyserClass}
		 * method is called. The {@link setStatementAnalyserClass StatementAnalyserClass}
		 * property must be set before calling {@link getStatementAnalyser} if you wish to
		 * create the connection using the  given class name.
		 * @param string Statement analyser class name.
		 * @throws TDbException if no master connection exists
		 */
		public function setStatementAnalyserClass($value)
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			$this->getMasterConnection()->setStatementAnalyserClass($value);
		}

		/**
		 * @param string Statement analyser class name to be created.
		 * @throws TDbException if no master connection exists
		 */
		public function getStatementAnalyserClass()
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			return $this->getMasterConnection()->getStatementAnalyserClass();
		}

		/**
		 * @return TMasterSlaveDbConnectionForceMaster
		 * @throws TDbException if no master connection exists
		 */
		public function getForceMaster()
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			return $this->getMasterConnection()->getForceMaster();
		}

		/**
		 * @param TMasterSlaveDbConnectionForceMaster
		 * @throws TDbException if no master connection exists
		 */
		public function setForceMaster($value)
		{
			if($this->getMasterConnection() === null)
				throw new TDbException('slavedbconnection_requires_master', getclass($this), 'MasterConnection');
			$this->getMasterConnection()->setForceMaster($value);
		}

		/**
		 * @return TDbConnectionServerRole
		 */
		public function getServerRole()
		{
			return TDbConnectionServerRole::Slave;
		}
	}

	/**
	 * TMasterSlaveDbConnectionForceMaster class
	 *
	 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
	 * @version $Id$
	 * @package System.Testing.Data.Distributed.MasterSlave
	 * @since 4.0
	 */
	class TMasterSlaveDbConnectionForceMaster extends TEnumerable
	{
		const OFF_AUTOMATIC		= 0;
		const ON_MANUAL			= 1;
		const ON_TCL			= -1;
		const ON_TRANSACTION	= -2;
	}