Skip to content

Commit

Permalink
Init code
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnl committed May 8, 2019
0 parents commit 8254444
Showing 6 changed files with 287 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Build and Release Folders
vendor

# Other files and folders

# Executables
*.exe

# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## Version 1.0.0

1. Create `\koolreport\mongo\MongoDataSource` to connect to MongoDB
126 changes: 126 additions & 0 deletions MongoDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* This file contains class to pull data from MongoDB
*
* @author KoolPHP Inc (support@koolphp.net)
* @link https://www.koolphp.net
* @copyright KoolPHP Inc
* @license https://www.koolreport.com/license#mit-license
*/

/*
* 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'
* ))
*
*/

namespace koolreport\mongodb;
use \koolreport\core\DataSource;
use \koolreport\core\Utility;

class MongoDataSource extends DataSource
{
protected $connectionString;
protected $host;
protected $username;
protected $password;
protected $charset;
protected $database;
protected $collection;
protected $find;
protected $options;

protected $mongoClient;

protected function onInit()
{
$this->connectionString = Utility::get($this->params,"connectionString");
$this->host = Utility::get($this->params,"username");
$this->username = Utility::get($this->params,"username");
$this->password = Utility::get($this->params,"password");
$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
{
$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());
return $this;
}

protected function guessType($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 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);
}
$this->endInput(null);
}
}
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Introduction

This package allows you to connect and get data from MongoDB.

#Installation

1. Download the zip file.
2. Unzip
3. Copy folder `mongodb` to the folder `koolreport/packages`

# Documentation

### Settings

|Name|type|default|description|
|----------|---------|---------|----------------|
|class|string|| Must set to `'\koolreport\mongodb\MongoDataSource'`|
|connectionString|string||Define connection string to MongoDB. If you use connectionString, you do not need to use properties host, username and password.|
|host|string||MongoDB host|
|username|string||Username|
|password|string||Password|
|database|string||The name of database you want to connect|

### Example

```
<?php
class MyReport extends \koolreport\KoolReport
{
public function settings()
{
return array(
"dataSources"=>array(
"mongo_purchase"=>array(
"class"=>'\koolreport\mongodb\MongoDataSource',
"connectionString"=>"mongo://johndoe:secret_password@localhost:65432",
"database"=>"dbpurchase"
),
)
);
}
public function setup()
{
$this->src('mongo_purchase')
->query(array("collection"=>"cPurchases"))
->pipe(..)
->pipe(...)
...
->pipe($this->dataStore('mongo_purchases'));
}
}
```

## Support

Please use our forum if you need support, by this way other people can benefit as well. If the support request need privacy, you may send email to us at __support@koolreport.com__.
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "koolreport/mongodb",
"version":"1.0.0",
"description": "Allow KoolReport to work with MongoDB",
"keywords": ["PHP","Reporting Tools","Data Report","Charts","MongoDB"],
"homepage": "https://www.koolreport.com",
"type": "library",
"license": "https://www.koolreport.com/license#mit-license",
"require": {
"mongodb/mongodb": "^1.1"
}
}
76 changes: 76 additions & 0 deletions composer.lock

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

0 comments on commit 8254444

Please sign in to comment.