I posted this as a part of a larger set a ways back for feedback but got nothing, so I thought I'd go through and focus on each component.
This object is a part of a group of classes I'm working on to give a unified interface to different databases. MySQL and SQLite being the primaries. I have a base class which creates this class and hands it to a db interaction class to be populated, and then passes it back to the user.
Code: Select all
<?php
class dataset implements Iterator {
private $type;
public $error = false;
public $debug;
protected $data = NULL;
protected $detail = array('affected'=>0, 'returned'=>0);
####################################
#### MAGIC METHODS
####################################
public function __construct($sql_type){
$this->sql_type = $sql_type;
}
public function __toString() {
if($this->detail['affected'] > 0) return $this->detail['affected'];
elseif($this->detail['returned'] > 0) return $this->detail['returned'];
else return 0;
}
public function __set($name, $value) {
if(isset($this->detail[$name])) $this->detail[$name] = $value;
if($name == 'data') $this->data = $value;
}
####################################
#### PUBLIC METHODS
####################################
public function setError($debug_text) {
$this->error = true;
$this->debug = $debug_text;
}
public function num_rows() {
return $this->detail['returned'];
}
public function affected_rows() {
return $this->detail['affected'];
}
####################################
#### ITERATOR METHODS
####################################
public function rewind() {
reset($this->data);
}
public function current() {
$var = current($this->data);
return $var;
}
public function key() {
$var = key($this->data);
return $var;
}
public function next() {
$var = next($this->data);
return $var;
}
public function valid() {
$var = $this->current() !== false;
return $var;
}
}
?>
I've opted to have the implementor class pass all results to the dataset as an array. This is mainly so that I can close the query right away instead of waiting for it to reach the end of the results, and also the results can be iterated multiple times, although I rarely do this myself. Any problems with this method?
I originally wrote a function to pass the results to the dataset object, but I was tinkering with things and I rigged the __set() function to allow it to be passed that way. I have no real idea of the implications of this, apart from it not being overly intuitive as to how to populate the dataset. I think my idea was to keep the object as clean of functions as possible. Good or bad approach?
Also, I had originally mapped out this class as two separate ones ... row returning results, and non row returning results. So, one for selects and one for say, creates or inserts. I made it hybrid by once again using the __set() function to allow setting of whatever is the most appropriate. I was hesitant to put the calls in to retrieve this extra data before being called, but when I'm not 'hasty coding' I always call them to verify success anyhow, so I included them. Is there any argument for separating them into two classes, or does this method get the job done cleanly enough?
I'm really just jumping into the deep end with this, so I'm really flying blind as to if I'm doing it the best way. Anything else you would suggest?
Oh, and I just noticed that I don't use sql_type anywhere. I think that was my first attempt to make this contain result specific info. The implementor does that now. Oh, and I haven't left a way to get the last insert ID.
Thanks for any feedback