Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2011, 10:31 AM   PM User | #1
kairog
New Coder

 
Join Date: Dec 2007
Posts: 68
Thanks: 25
Thanked 2 Times in 2 Posts
kairog is on a distinguished road
Need Critics On This OOP Code For MySQL Transactional

Hello PHP friends,

I am very new to OOP and transactional database and I'm just starting to apply them to some of my coding.

I appreciate if you could comment or tell me how I can improve my code below.

Here's my code....

Code:
class myTransaction {
	
	var $connection;
	
	function __construct($dbconnection) {           
		$this->connection = $dbconnection;
                @mysql_query("SET AUTOCOMMIT=0",$this->connection);         
        }  

	function begin(){
		@mysql_query("BEGIN", $this->connection);
	}
	
	function commit() {
		@mysql_query("COMMIT", $this->connection);
		@mysql_query("SET AUTOCOMMIT=1",$this->connection);
	}
	
	function rollback()	{
		@mysql_query("ROLLBACK", $this->connection);
		@mysql_query("SET AUTOCOMMIT=1",$this->connection);
	}
	
	/*
	 * use FOR UPDATE at the end of SELECT statement
	 * or use LOCK IN SHARE MODE at the end of SELECT statement
	 */
	function query($query, $lock_mode=''){
		print $query;
		if ($result = @mysql_query($query . $lock_mode )){
			return $result;
		} else {
			return false;
		}
	}
	
}
and here's how I apply it...

Code:
$connection = mysql_pconnect($dhost,$duser,$dpass) or die ('unable to connect');
mysql_select_db($dname) or die ('unable to select database');

$new_entry = new myTransaction($connection);
$error = 0;

$new_entry->begin();

// query #1
if ($result = @mysql_query('insert....')){
} else {
     $error++;
}

// query #2
if ($result = @mysql_query('insert....')){
} else {
     $error++;
}

// query #3
if ($result = @mysql_query('insert....')){
} else {
     $error++;
}

if ($error){
     $new_entry->rollback();
} else {
     $new_entry->commit();
}
Any comment will be highly appreciated.

Cheers,
kairog is offline   Reply With Quote
Old 05-23-2011, 06:02 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It has a long way to go before it becomes functional in the big picture. One of the primary purposes of OOP is to allow reusability, and a close second on that is to allow a great deal of abstraction. What you have is locked both into mysql and in both procedural and OO code.
MyTransaction should be an interface, and a potential abstract would be placed between the OOP driver and the interface. You then create factories to construct the necessary classes for you. I have chosen to un-name the createTransaction as it should be inferred from the type of driver in use for the Database::getInstance class. The MyTransaction implementation should also extend an interface for the Database, so that each transaction can be treated as a Database type.
The idea is to completely centralize the code, so that all of your second block would become this:
PHP Code:
$connection Database::getInstance('MySQL');
$connection->open($dhost,$duser,$dpass$dbname);

$new_entry $connection->createTransaction();
$error 0;

$new_entry->begin();

// query #1
if ($result $new_entry->query(...)){
} else {
     
$error++;
}

// query #2
if ($result $new_entry->query('insert....')){
} else {
     
$error++;
}

// query #3
if ($result $new_entry->query('insert....')){
} else {
     
$error++;
}

if (
$error){
     
$new_entry->rollback();
} else {
     
$new_entry->commit();

Keeping control at an aggregate level of the class will allow you to write transaction drivers for databases that do not support them (it just takes more work).

Now the only thing that needs to change to work on other databases is the use of the factory to get the instance type. Of course, you'd write the drivers to access sqlserver or oracle for example, but usage wise only the 'mySQL' would change to something like 'SQLServer' - no other usage code would need to change.

Proper OO development does take a lot of work, but the reward of re-usability and portability is well worth the effort.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
kairog (05-24-2011)
Old 05-24-2011, 12:58 AM   PM User | #3
kairog
New Coder

 
Join Date: Dec 2007
Posts: 68
Thanks: 25
Thanked 2 Times in 2 Posts
kairog is on a distinguished road
That's great Fou-Lu. As always, you're such a great help. I'll be working on improving the class based on your suggestions. Thanks a lot!
kairog is offline   Reply With Quote
Old 05-24-2011, 06:41 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by Fou-Lu View Post
PHP Code:
$connection Database::getInstance('MySQL');
$connection->open($dhost,$duser,$dpass$dbname);

$new_entry $connection->createTransaction();
$error 0;

$new_entry->begin();

// query #1
if ($result $new_entry->query(...)){
} else {
     
$error++;
}

// query #2
if ($result $new_entry->query('insert....')){
} else {
     
$error++;
}

// query #3
if ($result $new_entry->query('insert....')){
} else {
     
$error++;
}

if (
$error){
     
$new_entry->rollback();
} else {
     
$new_entry->commit();

looks like PDO code …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
oop, transactional mysql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 PM.


Advertisement
Log in to turn off these ads.