Skip to content

Commit

Permalink
Merge pull request #1 from priatmoko/dev
Browse files Browse the repository at this point in the history
Release Method to Generate Simple select Queries
  • Loading branch information
priatmoko authored Dec 17, 2018
2 parents f7b49d4 + 776f5ee commit f4d6549
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 22 deletions.
39 changes: 21 additions & 18 deletions src/Db2.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,43 @@ class Db2{
* @var object of connection
*/

private $con;
protected $con;

/**
* The registered host database, IP Address of server database Db2 As400 (RDBMS)
* @var string
*/
private $host;
protected $host;

/**
* The registered username of database Db2 As400 (RDBMS)
* @var string
*/
private $username;
protected $username;

/**
* The registered password of database Db2 As400 (RDBMS)
* @var string
*/
private $password;
protected $password;

/**
* The registered catalog of database Db2 As400 (RDBMS). It is only exist on driver COM driver
* @var string
*/
private $catalog;
protected $catalog;

/**
* The registed column of Assigned table Db2 As400 (RDBMS)
* @var array
*/
private $columns;
protected $columns;

/**
* The retrieved records of table Db2 As400 (RDBMS)
* @var object of records
*/
private $records;
protected $records;

/**
* Initialization method
Expand Down Expand Up @@ -141,7 +141,7 @@ public function __construct($env=NULL)
* Create instance of Db2 As400 connection using COM DOT NET driver
* @return void
*/
private function com()
protected function com()
{
try{
$this->con = new \COM('ADODB.Connection');
Expand All @@ -160,7 +160,7 @@ private function com()
* Create instance of Db2 As400 connection using ODBC driver
* @return void
*/
private function odbc()
protected function odbc()
{
try{
$dsn="Driver={iSeries Access ODBC Driver};system=".$this->host.";";
Expand All @@ -183,7 +183,7 @@ private function odbc()
public function query($sql)
{
if (env('DB2_DRIVER')!="" && method_exists($this, "execute".ucfirst(strtolower(env('DB2_DRIVER'))))){
$method = "exec".ucfirst(strtolower(env('DB2_DRIVER')));
$method = "execute".ucfirst(strtolower(env('DB2_DRIVER')));
$this->$method($sql);
}
}
Expand All @@ -193,7 +193,7 @@ public function query($sql)
* @param string $sql
* @return void
*/
private function executeCom($sql)
protected function executeCom($sql)
{
try {
$row = $this->con->execute($sql);
Expand All @@ -213,7 +213,7 @@ private function executeCom($sql)
* @param string $sql
* @return void
*/
private function executeOdbc($sql)
protected function executeOdbc($sql)
{
try {
$row = odbc_exec($this->con, $sql);
Expand All @@ -232,7 +232,7 @@ private function executeOdbc($sql)
* @param mixed $obj
* @return void
*/
private function setColumns($obj)
protected function setColumns($obj)
{
$number_of_column=$obj->fields->count();
for ($i=0; $i<$number_of_column; $i++){
Expand Down Expand Up @@ -292,8 +292,9 @@ public function first()
* @return array
* @return false
*/
private function getCom()
protected function getCom()
{
if (!is_object($this->records)) $this->query($this->query);
if (is_object($this->records)){
$rs = $this->records;
$col = $this->columns;
Expand All @@ -319,8 +320,9 @@ private function getCom()
* @return array
* @return false
*/
private function getOdbc()
protected function getOdbc()
{
if (is_null($this->records)) $this->query($this->query);
if (!is_null($this->records)){
$rs = $this->records;
while(odbc_fetch_row($rs)){
Expand All @@ -343,8 +345,9 @@ private function getOdbc()
* @return array
* @return false
*/
private function firstCom()
protected function firstCom()
{
if (!is_object($this->records)) $this->query($this->query);
if (is_object($this->records)){
$rs = $this->records;
$col = $this->columns;
Expand All @@ -365,9 +368,9 @@ private function firstCom()
* @return array
* @return false
*/
private function firstOdbc()
protected function firstOdbc()
{

if (is_null($this->records)) $this->query($this->query);
if (!is_null($this->records)){
$rs = $this->records;
if(odbc_fetch_row($rs)){
Expand Down
167 changes: 163 additions & 4 deletions src/Model.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Masterpis\Db2As400;
namespace Masterpis\Db2as400;

use Masterpis\Db2As400\Db2;
use Masterpis\Db2as400\Db2;

/**
* Db2As400 is a collection of attributes and methods to simplify connection to DB2 As400.
Expand All @@ -21,7 +21,8 @@
*
*/

class Model extends Db2{
class Model extends Db2
{

/**
* Filtering query result using LIKE clause
Expand All @@ -34,18 +35,176 @@ class Model extends Db2{
* @var array
*/
protected $order;

/**
* Limit query result
* @var array
*/
protected $limit;

/**
* Server RDBMS
* @var string
*/
protected $server;

/**
* Fields of query result
* @var array
*/
protected $fields;

/**
* User login
* @var string
*/
protected $user;

/**
* generated Query
* @var string
*/
public $query;

/**
* Instance object Model for generating simple query
* @return void
*/
public function __construct()
{
parent::__construct();
$this->where(NULL, 'ASPARAM');
}

/**
* The query clause like.
* @param array
* @return void
*/
public function like($like){

if (is_array($this->like) && count($this->like)>0){
$used_like=array_merge($like, $this->like);
}else{
$used_like = $like;
}
$this->like=$used_like;

}

public function get(){
/**
* The query clause order
* @param array
* @return void
*/
public function orderBy($order){
$this->order = $order;
}

/**
* The query clause limit
* @param int
* @return void
*/
public function limit($limit){
$this->limit=$limit;
}

/**
* The server RDBMS, for multiple env only
* @param int
* @return void
*/
public function setServer($server){
$this->server=$server;
}

/**
* The customized fields in query.
* If you dont set it, query will generate all fealds (*) as result
* @param @array
* @return void
*/
public function setFields($fields){
$this->fields = $fields;
}

/**
* The user in charge. Registered user running commands
* @param @array
* @return void
*/
public function setUsid($usid){
$this->usid=$usid;
}

/**
* The query clause where
* @param array $where
* Simple clause where ex. ["field"=>"keyword"]
* Complex clause on where ex
* [""=>"(field1='".$keyword1."' or field2='".$keyword2."') and field2 like '%".keyword3."%'"]
* @param string $console
* NULL, CONSOLE will display query generated, ASPARAM will assign query to var $query
* @return void
*/
public function where($where=NULL, $console=NULL)
{
if ($where!=NULL){
//Looping array clause where
foreach($where as $k=>$v) {
//Validate index array clause. If index array is empty, We use all value as clause.
//It is complex clause. if index array is not empty, It is simple clause.
if ($k=="") $pf[] = $v; else $pf[] = $k."='".str_replace("'","''", $v)."'";
}
}

//Looping like clause
if (is_array($this->like) && count($this->like)>0){
foreach($this->like as $lk=>$lv) {
//Validate index array clause. If index array is empty, We use all value as clause.
//It is complex clause.
//if index array is not empty, It is simple clause.
if ($lk=="") $pf[] = $v; else $pf[] = $lk." like '".str_replace("'","''", $lv)."'";
}
}

//Combine all filter
$filter = "";
if (isset($pf) && count($pf)>0) $filter = " where ".implode(" and ", $pf);

//Looping array order clause
$order = "";
if (is_array($this->order) && count($this->order)>0){

$orders = $this->order;
foreach($orders as $ok=>$ov) $porder[] = $ok." ".$ov." ";

if (isset($porder) && is_array($porder) && count($porder)>0)
$order = "order by ".implode(",", $porder); else $order ="";

}

//validate param limit
$limit = "";
if (isset($this->limit) && $this->limit>0)
$limit = "fetch first ".$this->limit." rows only";

//validate field selected
$fields = "*";
if (is_array($this->fields) && count($this->fields)>0)
$fields = implode(",", $this->fields);

//Combine all things here
$sql="select ".$fields." from ".$this->table." ".$filter." ".$order." ".$limit;

if (strtoupper($console)=='CONSOLE'){
dd($sql);
}else if (strtoupper($console)=='ASPARAM'){
$this->query=$sql;
}else{
$this->query($sql);
}
}

}

0 comments on commit f4d6549

Please sign in to comment.