Skip to content
Snippets Groups Projects
Commit f692a25c authored by Carsten Karbach's avatar Carsten Karbach
Browse files

Add simple library for accessing Oracle DB via PDO

parent 792b9eee
No related branches found
No related tags found
No related merge requests found
username = "<username>"
password = "<password>"
pdo_dsn = "oci:dbname=//<hostname>:<port>/<servicename>"
\ No newline at end of file
File moved
<?php
use jards\eventsapi\DBAccessService;
/*Copyright (c) 2017, Forschungszentrum Juelich GmbH, Federal Republic of Germany.
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
require_once __DIR__.'/../vendor/autoload.php';
$dbaccess = DBAccessService::getInstance(__DIR__.'/../configs/dbaccess_jsc.cnf');
$data = $dbaccess->sql_select('select * from rev_groups');
var_dump($data);
?>
\ No newline at end of file
<?php
/*Copyright (c) 2017, Forschungszentrum Juelich GmbH, Federal Republic of Germany.
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
namespace jards\eventsapi;
/**
* Simple library for access Oracle DB
*
*/
class DBAccessService{
/**
* DB username
* @var string
*/
private $dbuser;
/**
* Connection object for PDO access
* @var unknown
*/
private $conn;
/**
* Mapping of file path for DB credentials to instance of DBAccessService
* @var array
*/
private static $instances;
/**
* Returns an instance of DBAccessService or null on failure.
* Creates a new instance if no one is present or no connection was established.
* @param string $DB_Pass_file optional filepath for DB credentials
* @return DBAccessService instance of this class
*/
public static function getInstance($DB_Pass_file=NULL){
if(empty($DB_Pass_file)){
$DB_Pass_file = __DIR__.'/../configs/dbaccess.cnf';
}
if( empty(self::$instances) || ! array_key_exists($DB_Pass_file, self::$instances)
|| self::$instances[$DB_Pass_file] == null || empty(self::$instances[$DB_Pass_file]->conn)
|| self::$instances[$DB_Pass_file]->conn == false ){
self::$instances[$DB_Pass_file] = new DBAccessService($DB_Pass_file);
if( empty(self::$instances[$DB_Pass_file]->conn)
|| self::$instances[$DB_Pass_file]->conn == false ){
return null;
}
}
return self::$instances[$DB_Pass_file];
}
/**
* Creates a new instance of DBAccessService. Attempts to establish a DB connection.
* @param string $DB_Pass_file filepath for DB credentials
*
* @throws \PDOException if connection via PDO did not work
*/
protected function __construct($DB_Pass_file){
$config = parse_ini_file($DB_Pass_file);
$this->dbuser = $config['username'];
$dbuser = $config['username'];
$pass = $config['password'];
$options = array();
$pdoDSN = $config['pdo_dsn'];
// pdo
$this->conn = new \PDO($pdoDSN, $dbuser, $pass, $options);
}
/**
* Run SQL select statement and get the result as an array of rows.
*
* @param string $sqlStatement the SQL statement to run
* @param array $input_parameters bound parameters in the sqlStatement
*
* @throws Exception if statement could not be executed
*/
public function sql_select($sqlStatement, $input_parameters = null){
$sth = $this->conn->prepare($sqlStatement);
if(!$sth->execute($input_parameters)){
throw new \Exception($sth->errorInfo()[2]);
}
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
return $results;
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment