Skip to content

Commit

Permalink
- Change data retrieval algorithm
Browse files Browse the repository at this point in the history
- Change mongodb/mongodb version
  • Loading branch information
dongnl committed Aug 26, 2019
1 parent 2ff9ae1 commit 41c0cf1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 64 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Version 1.2.0

1. Change data retrieval algorithm
2. Change mongodb/mongodb version

## Version 1.1.0

1. Change README
Expand Down
99 changes: 51 additions & 48 deletions MongoDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@
*/

/*
* The user can declare connection string
* array(
* "connectionString"="mongo://{username}:{password}@localhost:65432",
* 'database' => 'test'
* )
* or
* array(
* "host"=>"mongo://localhost:65432",
* "username":"username",
* "password":"password",
* 'database' => 'test'
* )
* ->query(array(
* 'collection' => 'sales'
* ))
*
The user can declare connection string
array(
"connectionString"="mongo://{username}:{password}@localhost:65432",
'database' => 'test'
)
or
array(
"host"=>"mongo://localhost:65432",
"username":"username",
"password":"password",
'database' => 'test'
)
->query(array(
'collection' => 'sales',
'find' => ['age' => ['$gte' => '40']],
'options' => [
'skip' => 0,
'limit' => 5,
'projection' => [
'_id' => 0,
'name' => 1,
'age' => 1,
],
],
))
*/

namespace koolreport\mongodb;
Expand Down Expand Up @@ -54,23 +64,21 @@ protected function onInit()
$this->charset = Utility::get($this->params,"charset","utf8");
$this->database = Utility::get($this->params,"database",null);

if($this->connectionString)
{
$this->mongoClient = new \MongoDB\Client($this->connectionString);
}
else
{
if($this->connectionString) {
$this->mongoClient = new \MongoDB\Client($this->connectionString);
} else {
$this->mongoClient = new \MongoDB\Client($this->host, array(
"username"=>$this->username,
"password"=>$this->password,
));
}
}

function query($params) {
$this->collection = Utility::get($params, "collection", null);
$this->find = Utility::get($params, "find", array());
$this->options = Utility::get($params, "options", array());
function query($params)
{
$this->collection = Utility::get($params, "collection", null);
$this->find = Utility::get($params, "find", array());
$this->options = Utility::get($params, "options", array());
return $this;
}

Expand All @@ -87,10 +95,8 @@ protected function guessType($value)
);

$type = strtolower(gettype($value));
foreach($map as $key=>$value)
{
if(strpos($type,$key)!==false)
{
foreach($map as $key=>$value) {
if(strpos($type,$key)!==false) {
return $value;
}
}
Expand All @@ -102,24 +108,21 @@ public function start()
$data = array();
$collection = $this->mongoClient->{$this->database}->{$this->collection};
$cursor = $collection->find($this->find, $this->options);
foreach ($cursor as $row)
array_push($data, (array)$row);
$firstRow = Utility::get($data, 0, []);
$columnNames = array_keys($firstRow);

$metaData = array("columns"=>array());
for($i=0; $i<count($columnNames); $i++) {
$metaData["columns"][$columnNames[$i]] = array(
"type"=>(isset($firstRow)) ?
$this->guessType($firstRow[$columnNames[$i]]) : "unknown");
}

$this->sendMeta($metaData, $this);
$this->startInput(null);

$rowNum = count($data);
for($i=0; $i<$rowNum; $i++) {
$this->next($data[$i], $this);
foreach ($cursor as $i => $row) {
$row = (array) $row;
if ($i === 0) {
$firstRow = $row;
$columnNames = array_keys($firstRow);
$metaData = array("columns"=>array());
for($i=0; $i<count($columnNames); $i++) {
$metaData["columns"][$columnNames[$i]] = array(
"type"=>(isset($firstRow)) ?
$this->guessType($firstRow[$columnNames[$i]]) : "unknown");
}
$this->sendMeta($metaData, $this);
$this->startInput(null);
}
$this->next($row, $this);
}
$this->endInput(null);
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "koolreport/mongodb",
"version":"1.1.0",
"version":"1.2.0",
"description": "Allow KoolReport to work with MongoDB",
"keywords": ["PHP","Reporting Tools","Data Report","Charts","MongoDB"],
"homepage": "https://www.koolreport.com",
"type": "library",
"license": "MIT",
"require": {
"mongodb/mongodb": "^1.1"
"mongodb/mongodb": "^1.4"
}
}
30 changes: 16 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 41c0cf1

Please sign in to comment.