<?php
/**
*
* QMysqlDataObject
* @package
* @subpackage
* @author Thomas Schäfer
* @since 13.07.2008 11:02:58
* @desc
*/
class QMysqlDataObject {
private $link;
private $resource;
private $sql;
private $header = array(
"AmountOfRows"=>false,
"NumOfPages" => false,
"ActivePage" => false,
"Limit" => false,
"Offset" => false,
"NumRows"=>false,
"NumFields" => false,
"FieldNames" => false,
"FieldPositions" => false,
"FieldFlags" => false,
"FieldTypes" => false,
"FieldLengths" => false,
"TableNames" => false,
"Perspectives" => false,
);
private $properties = array();
public function __construct($link, $sql) {
$this->link = $link;
$this->sql = $sql;
}
public function byLimit($offset=0, $limit=10) {
$this->header["Limit"] = $limit;
$this->header["Offset"] = $offset;
$this->init();
return $this;
}
public function byPage($page=false, $size=false) {
$this->header["ActivePage"] = empty($page) ? 1 : $page;
$this->header["Limit"] = empty($size) ? 10 : $size;
$this->init();
return $this;
}
private function prepareLimit(){
return $this->sql
. ' LIMIT '
. $this->header["Offset"]
. ","
. $this->header["Limit"]
. ";";
}
private function countRecords() {
$sql = "SELECT count(*) AS records ". substr($this->sql,strpos($this->sql,'FROM'));
$result = mysql_query($sql, $this->link);
$resource = mysql_fetch_object($result);
return $resource->records;
}
private function init() {
$this->setNumRows($this->countRecords());
if(empty($this->header["Offset"])) { // by page
$this->header["Offset"] = $offset = intval(($this->header["ActivePage"] - 1) * $this->header["Limit"]);
$limit = $this->header["Limit"];
$this->header["NumOfPages"] = empty($this->header["NumOfPages"])
? intval($this->header["NumRows"] / $this->header["Limit"])+1
: $this->header["NumOfPages"];
} else { // by limit
$offset = $this->header["Offset"];
$limit = $this->header["Limit"];
$this->header["NumOfPages"] = empty($this->header["NumOfPages"])
? intval($this->header["NumRows"] / $this->header["Limit"])+1
: $this->header["NumOfPages"];
$this->header["ActivePage"] = intval($this->header["Offset"] / $this->header["Limit"])+1;
}
$this->resource = mysql_query($this->prepareLimit(), $this->link);
if($this->resource){
$rowIncrement=0;
$i=0;
while($resource = mysql_fetch_object($this->resource)) {
$colIncrement=0;
if(empty($this->header["NumFields"])) {
$this->header["NumFields"]= count(get_object_vars($resource));
}
while(list($columName, $columnValue) = each( $resource ) ) {
$phpName = self::camelize($columName);
$tableName = mysql_field_table(($this->resource), $colIncrement);
$phpNamePerspective = self::camelize($columName) . 'Of' . self::camelize($tableName);
if($rowIncrement==0) {
$this->header["FieldNames"][$phpNamePerspective] = $columName;
$this->header["TableNames"][$phpNamePerspective] = $tableName;
$this->header["FieldPositions"][$phpNamePerspective] = $colIncrement;
$this->header["Perspectives"][$phpNamePerspective] = $columName;
$this->header["FieldTypes"][$phpNamePerspective] = mysql_field_type($this->resource, $colIncrement);
$this->header["FieldLengths"][$phpNamePerspective] = mysql_field_len($this->resource, $colIncrement);
$flags = explode(" ", mysql_field_flags($this->resource, $colIncrement));
$this->header["FieldFlags"][$phpNamePerspective] = empty($flags[0]) ? NULL : $flags;
$colIncrement++;
}
$this->properties[$i][$phpNamePerspective] = $columnValue;
}
$i++;
$this->header["AmountOfRows"] = $i;
}
$rowIncrement++;
}
mysql_free_result($this->resource);
}
private static function camelize($string) {
$replace = str_replace(" ", "", ucwords(str_replace("_", " ", $string)));
return $replace;
}
private static function underscore($string) {
$tmp = self::replace($string, array('/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2', '/([a-z\d])([A-Z])/' => '\\1_\\2'));
return strtolower($tmp);
}
private function replace($search, $replacePairs) {
return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
}
public function getListByPosition($offset) {
$array = array();
if(is_integer($offset) and $this->hasNumFields() and $offset >= 0 and $offset < $this->getNumFields()) {
$colName = QDataObject::camelize($this->getFieldNames($offset));
foreach($this->properties as $propKey => $property) {
$array[$propKey] = $property[$colName];
}
}
return $array;
}
public function getListByName($name) {
$array = array();
$name = QDataObject::camelize($name);
if(strpos($name, "Of")) {
$col = $this->hasPerspectives($name)
? $this->getPerspectives($name)
: false;
if(is_string($col) and $this->hasFieldPositions() and array_key_exists($col, $this->getFieldPositions())) {
foreach($this->properties as $propKey => $property) {
$array[$propKey] = $property[$name];
}
}
} else {
if(is_string($name) and $this->hasFieldPositions() and array_key_exists(QDataObject::underscore($name), $this->getFieldPositions())) {
foreach($this->properties as $propKey => $property) {
$array[$propKey] = $property[$name];
}
}
}
return $array;
}
public function __call($funcName, $args) {
$methodType = substr($funcName, 0, 3);
switch((substr($funcName, 0, 2))) {
case "of":
case "is":
$methodType = (substr($funcName, 0, 2));
break;
}
switch($methodType) {
case "len":
$headerPart = "FieldLengths";
$method = self::camelize(substr($funcName, 3));
break;
case "typ":
$headerPart = "FieldTypes";
$method = self::camelize(substr($funcName, 3));
break;
case "of":
$headerPart = "FieldTypes";
$method = self::camelize(substr($funcName, 2));
break;
case "is":
$headerPart = "FieldFlags";
$method = self::camelize(substr($funcName, 2));
break;
default:
$method = self::camelize(substr($funcName, 3));
break;
}
switch ($methodType)
{
case "len":
case "typ":
$arg = !is_array($this->getPerspectives($method)) ? QDataObject::camelize($this->getPerspectives($method)) : $method;
if(array_key_exists(QDataObject::underscore($arg), $this->header[$headerPart])) {
return $this->header[$headerPart][QDataObject::underscore($arg)];
} else {
return false;
}
case "of":
$arg = !is_array($this->getPerspectives($args[0])) ? QDataObject::camelize($this->getPerspectives($args[0])) : $args[0];
if(isset($arg) and array_key_exists(QDataObject::underscore($arg), $this->header[$headerPart])) {
return isset($this->header[$headerPart][QDataObject::underscore($arg)])==strtolower($method)?true:false;
} else {
return false;
}
case "is":
$arg = !is_array($this->getPerspectives($args[0])) ? $this->getPerspectives($args[0]) : $args[0];
if( isset($arg) and
isset($this->header[$headerPart][QDataObject::underscore($arg)]) and
is_array($this->header[$headerPart][QDataObject::underscore($arg)]))
{
$part = array_flip($this->header[$headerPart][QDataObject::underscore($arg)]);
$flagMethod = QDataObject::underscore($method);
if(isset($part[$flagMethod])) {
return true;
} else {
return false;
}
} else {
return false;
}
break;
case "has":
if(array_key_exists($method, $this->header)) {
if(isset($this->header[$method]) and !empty($this->header[$method])) {
return true;
} else {
return false;
}
} elseif(isset($this->properties[$method]) and !empty($this->properties[$method])) {
return true;
} else {
return false;
}
return $this;
case "add":
if(array_key_exists($method, $this->header)) {
if(count($args)>1) {
$this->header[$args[0]][$method] = $args[1];
} else {
$this->header[$method][] = $args[0];
}
} else {
if(count($args)>1) {
$this->properties[$args[0]][$method] = $args[1];
} else {
$this->properties[$method][] = $args[0];
}
}
return $this;
case "set":
if(array_key_exists($method, $this->header)) {
$this->header[$method] = $args[0];
} else {
$this->properties[$method] = $args[0];
}
return $this;
case "get":
if(array_key_exists($method, $this->header)) {
if(isset($args[0]) and isset($this->header[$method][$args[0]]) ) {
return $this->header[$method][$args[0]];
} else {
return $this->header[$method];
}
} else {
if(empty($args) and $this->getNumRows==1 and isset($this->properties[0][$method])) {
return $this->properties[0][$method];
} elseif(isset($args[0]) and isset($this->properties[$args[0]][$method]) ) {
return $this->properties[$args[0]][$method];
} else {
return $this->properties[0][$method];
}
}
break;
}
}
public function showMethods(){
$error = get_class()."'s methods";
$exception = new WorkingObjectException($error);
$properties = $this->provideMethod();
echo $exception->render($properties,true);
}
protected function provideMethod(){
$array = array();
$array["Main"][] = get_class().'::getListByName(columName)';
$array["Main"][] = get_class().'::getListByPosition(offset)';
foreach($this->header["FieldNames"] as $key => $name){
$newName = QDataObject::camelize($name);
$array[$newName][] = get_class().'::set'.$newName."()";
$array[$newName][] = get_class().'::add'.$newName."(offset=null)";
$array[$newName][] = get_class().'::get'.$newName."(offset=null)";
$array[$newName][] = get_class().'::has'.$newName."()";
$array[$newName][] = get_class().'::len'.$newName."()";
$array[$newName][] = get_class().'::typ'.$newName."(offset=null)";
}
foreach($this->header["FieldFlags"] as $name => $fields){
if(is_array($fields)) {
foreach($fields as $key => $attributes) {
$newName = QDataObject::camelize($name);
$array[$newName][] = get_class().'::is'.QDataObject::camelize($attributes)."('".$newName."')";
}
}
}
foreach($this->header["FieldTypes"] as $name => $type){
$newName = QDataObject::camelize($name);
$array[$newName][] = get_class().'::of'.QDataObject::camelize($type)."('".$newName."')";
}
return $array;
}
}
class WorkingObjectException extends Exception {
/**
* render exception
*
* @param array $array
* @return string
*/
public function render($array = array(), $mode=false){
$error = $this->getCode();
$output = '<html><head><title>WorkingObjectException</title>
<style>
*{font-family:Tahoma;font-size:10pt;}
div.page{margin:1em;padding:1em;}
div.message{border:1px solid #f5f5f5;padding:10px;position:relative;margin:30px 0 0 0}
div.message span{position:absolute;top:-18px;left:-1px;width:200px;border:1px solid #f5f5f5;padding: 0px 10px;font-weight:bold;}
ol{list-style-type:decimal }
div.sample-outer{margin:10px}
div.sample{background-color:#f5f5f5;border:1px solid:#c5c5c5;padding:10px;}
ul li{color:red}
pre{font-family:Courier New;font-size:9pt;padding:10px;}
</style>
</head><body>
<div class="page">
';
$output .= '
<div class="message">
<span>Provided Methods</span>
<div>'.$this->getMethodsAsHTML($array).'</div>
</div>
</div>
</body></html>';
return $output;
}
/**
* render provided methods
*
* @param array $array
* @return string
*/
protected function getMethodsAsHTML($array){
$output = '<ol>';
foreach($array as $section => $value){
$output .= '<li>';
$output .= '<b>'.$section.'</b>';
if(is_array($value)) {
$output .= '<ol>';
foreach($value as $k => $v){
$output .= '<div>'.$v.'</div>';
}
$output .= '</ol>';
} else {
$output .= '<div>'.$value.'</div>';
}
$output .= '</li>';
}
$output .= '</ol>';
return $output;
}
}