Skip to content

Commit

Permalink
Comply to PSR
Browse files Browse the repository at this point in the history
  • Loading branch information
tuananhnghiem committed Jun 3, 2019
1 parent 78ad399 commit 2534ed0
Showing 1 changed file with 62 additions and 80 deletions.
142 changes: 62 additions & 80 deletions CIDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,111 @@
use \koolreport\core\DataSource;
use \koolreport\core\Utility;


class CIDataSource extends DataSource
{
static $databases;
protected $db;
protected $query;
protected $sqlParams;
protected $query;
protected $sqlParams;
protected function onInit()
{
$name = Utility::get($this->params,"name");
if (CIDataSource::$databases==null)
{
$name = Utility::get($this->params, "name");
if (CIDataSource::$databases == null) {
CIDataSource::$databases = array();
}

if(isset(CIDataSource::$databases[$name]))
{
if (isset(CIDataSource::$databases[$name])) {
$this->db = CIDataSource::$databases[$name];
}
else
{
require_once(BASEPATH.'database/DB.php');
} else {
require_once BASEPATH . 'database/DB.php';
$this->db = DB($name, null);
CIDataSource::$databases[$name] =$this->db;
CIDataSource::$databases[$name] = $this->db;
}
}

protected function guessTypeFromValue($value)
{
$map = array(
"float"=>"number",
"double"=>"number",
"int"=>"number",
"integer"=>"number",
"bool"=>"number",
"numeric"=>"number",
"string"=>"string",
);

$type = strtolower(gettype($value));
foreach($map as $key=>$value)
{
if(strpos($type,$key)!==false)
{
return $value;
}
}
return "unknown";
}
protected function guessTypeFromValue($value)
{
$map = array(
"float" => "number",
"double" => "number",
"int" => "number",
"integer" => "number",
"bool" => "number",
"numeric" => "number",
"string" => "string",
);

$type = strtolower(gettype($value));
foreach ($map as $key => $value) {
if (strpos($type, $key) !== false) {
return $value;
}
}
return "unknown";
}

public function query($query,$sqlParams=null)
{
$this->query = (string)$query;
if($sqlParams!=null)
{
$this->sqlParams = $sqlParams;
}
return $this;
}
public function query($query, $sqlParams = null)
{
$this->query = (string) $query;
if ($sqlParams != null) {
$this->sqlParams = $sqlParams;
}
return $this;
}

public function params($sqlParams)
{
$this->sqlParams = $sqlParams;
return $this;
$this->sqlParams = $sqlParams;
return $this;
}

protected function bindParams($query, $sqlParams)
{
if(empty($sqlParams)) return $query;

foreach($sqlParams as $key=>$value)
{
if (empty($sqlParams)) {
return $query;
}

if(gettype($value)==="array")
{
foreach ($sqlParams as $key => $value) {

if (gettype($value) === "array") {
$tmp = array();
foreach($value as $item)
{
array_push($tmp,$this->db->escape($item));
foreach ($value as $item) {
array_push($tmp, $this->db->escape($item));
}
$query = str_replace($key,"(".implode(",",$tmp).")",$query);
}
else
{
$query = str_replace($key,$this->db->escape($value),$query);
$query = str_replace($key, "(" . implode(",", $tmp) . ")", $query);
} else {
$query = str_replace($key, $this->db->escape($value), $query);
}
}
return $query;
}


public function start()
{
$queryString = $this->bindParams($this->query,$this->sqlParams);
$queryString = $this->bindParams($this->query, $this->sqlParams);
$query = $this->db->query($queryString);

$first_row = $query->unbuffered_row('array');
if($first_row!=null)
{
$metaData = array("columns"=>array());
foreach($first_row as $cName=>$cValue)
{
if ($first_row != null) {
$metaData = array("columns" => array());
foreach ($first_row as $cName => $cValue) {
$metaData["columns"][$cName] = array(
"type"=>$this->guessTypeFromValue($cValue)
"type" => $this->guessTypeFromValue($cValue),
);
}
$this->sendMeta($metaData,$this);
$this->sendMeta($metaData, $this);
$this->startInput(null);
$this->next($first_row,$this);
while ($row = $query->unbuffered_row('array'))
{
$this->next($row,$this);
}
$this->next($first_row, $this);
while ($row = $query->unbuffered_row('array')) {
$this->next($row, $this);
}
$this->endInput(null);
}
else
{
} else {
//No data
$this->sendMeta(array("columns"=>array()),$this);
$this->sendMeta(array("columns" => array()), $this);
$this->startInput(null);
$this->endInput(null);
$this->endInput(null);
}

}
}
}

0 comments on commit 2534ed0

Please sign in to comment.