diff --git a/configs/dbaccess.cnf b/configs/dbaccess.cnf new file mode 100644 index 0000000000000000000000000000000000000000..8aeafba33ab5229ab744290e26cb2482cb36ce2a --- /dev/null +++ b/configs/dbaccess.cnf @@ -0,0 +1,3 @@ +username = "<username>" +password = "<password>" +pdo_dsn = "oci:dbname=//<hostname>:<port>/<servicename>" \ No newline at end of file diff --git a/examples/.htaccess b/examples/.htaccess_public similarity index 100% rename from examples/.htaccess rename to examples/.htaccess_public diff --git a/examples/dbaccesstest.php b/examples/dbaccesstest.php new file mode 100644 index 0000000000000000000000000000000000000000..d356c16542c565c4c00764a73333fffa9d1b9514 --- /dev/null +++ b/examples/dbaccesstest.php @@ -0,0 +1,25 @@ +<?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 diff --git a/lib/DBAccessService.php b/lib/DBAccessService.php new file mode 100644 index 0000000000000000000000000000000000000000..cb0fe652bc62735a19c4ee90a303308945b927c5 --- /dev/null +++ b/lib/DBAccessService.php @@ -0,0 +1,105 @@ +<?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