Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
PostgreSQL and MySQL compatibility.
Browse files Browse the repository at this point in the history
Table- and column-names are now passed into the SelectObject by a helper function, which
converts the string to lower-case e.g. for PostgreSQL or leaves it camel-case for MySQL,
so we don't need to repeat most of the code for different databases, as identifiers might
be different depending on which database you use. This is a workaround for an inconsistence
in the Bareos installation scripts relating the creation of needed database tables for
the catalogue. The helper function strdbcompat() might also be extended in future for Oracle
databases, where identifiers are handled upper-case by default and possible other databases.
  • Loading branch information
fbergkemper committed Jul 30, 2014
1 parent 6569d86 commit 11c36fb
Show file tree
Hide file tree
Showing 25 changed files with 523 additions and 117 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -11,5 +11,8 @@
"php": ">=5.3.3",
"zendframework/zendframework": "2.2.*",
"zendframework/zend-developer-tools": "dev-master"
},
"autoload": {
"psr-0": {"Bareos\\": "vendor/Bareos/library/"}
}
}
5 changes: 4 additions & 1 deletion module/Client/Module.php
Expand Up @@ -6,6 +6,7 @@
use Client\Model\ClientTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class Module
{
Expand Down Expand Up @@ -42,7 +43,9 @@ public function getServiceConfig()
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Client());
return new TableGateway('client', $dbAdapter, null, $resultSetPrototype);
$config = $sm->get('Config');
$bsqlch = new BareosSqlCompatHelper($config['db']['driver']);
return new TableGateway($bsqlch->strdbcompat("Client"), $dbAdapter, null, $resultSetPrototype);
},
),
);
Expand Down
2 changes: 2 additions & 0 deletions module/Client/src/Client/Model/Client.php
Expand Up @@ -14,6 +14,8 @@ class Client

public function exchangeArray($data)
{
$data = array_change_key_case($data, CASE_LOWER);

$this->clientid = (!empty($data['clientid'])) ? $data['clientid'] : null;
$this->name = (!empty($data['name'])) ? $data['name'] : null;
$this->uname = (!empty($data['uname'])) ? $data['uname'] : null;
Expand Down
29 changes: 24 additions & 5 deletions module/Client/src/Client/Model/ClientTable.php
Expand Up @@ -7,8 +7,11 @@
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class ClientTable
class ClientTable implements ServiceLocatorAwareInterface
{

protected $tableGateway;
Expand All @@ -18,10 +21,24 @@ public function __construct(TableGateway $tableGateway)
$this->tableGateway = $tableGateway;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
return $this->serviceLocator;
}

public function getDbDriverConfig() {
$config = $this->getServiceLocator()->get('Config');
return $config['db']['driver'];
}

public function fetchAll($paginated=false)
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('client');
$select->from($bsqlch->strdbcompat("Client"));

if($paginated) {
$resultSetPrototype = new ResultSet();
Expand All @@ -43,9 +60,10 @@ public function getClient($id)
{
$id = (int) $id;

$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('client');
$select->where('clientid = ' . $id);
$select->from($bsqlch->strdbcompat("Client"));
$select->where($bsqlch->strdbcompat("ClientId") . " = " . $id);

$rowset = $this->tableGateway->selectWith($select);
$row = $rowset->current();
Expand All @@ -59,8 +77,9 @@ public function getClient($id)

public function getClientNum()
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('client');
$select->from($bsqlch->strdbcompat("Client"));
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Client());
$rowset = new DbSelect(
Expand Down
5 changes: 4 additions & 1 deletion module/File/Module.php
Expand Up @@ -6,6 +6,7 @@
use File\Model\FileTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class Module
{
Expand Down Expand Up @@ -42,7 +43,9 @@ public function getServiceConfig()
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new File());
return new TableGateway('file', $dbAdapter, null, $resultSetPrototype);
$config = $sm->get('Config');
$bsqlch = new BareosSqlCompatHelper($config['db']['driver']);
return new TableGateway($bsqlch->strdbcompat("File"), $dbAdapter, null, $resultSetPrototype);
},
),
);
Expand Down
25 changes: 25 additions & 0 deletions module/File/src/File/Model/File.php
@@ -1,5 +1,28 @@
<?php

/**
*
* bareos-webui - Bareos Web-Frontend
*
* @link https://github.com/bareos/bareos-webui for the canonical source repository
* @copyright Copyright (c) 2013-2014 dass-IT GmbH (http://www.dass-it.de/)
* @license GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace File\Model;

class File
Expand All @@ -13,6 +36,8 @@ class File

public function exchangeArray($data)
{
$data = array_change_key_case($data, CASE_LOWER);

$this->fileid = (!empty($data['fileid'])) ? $data['fileid'] : null;
$this->jobid = (!empty($data['jobid'])) ? $data['jobid'] : null;
$this->pathid = (!empty($data['pathid'])) ? $data['pathid'] : null;
Expand Down
62 changes: 55 additions & 7 deletions module/File/src/File/Model/FileTable.php
@@ -1,28 +1,69 @@
<?php

/**
*
* bareos-webui - Bareos Web-Frontend
*
* @link https://github.com/bareos/bareos-webui for the canonical source repository
* @copyright Copyright (c) 2013-2014 dass-IT GmbH (http://www.dass-it.de/)
* @license GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace File\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class FileTable
class FileTable implements ServiceLocatorAwareInterface
{

protected $tableGateway;
protected $serviceLocator;

public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
return $this->serviceLocator;
}

public function getDbDriverConfig() {
$config = $this->getServiceLocator()->get('Config');
return $config['db']['driver'];
}

public function fetchAll($paginated=false)
{
if($paginated) {
$select = new Select('file');
$select->order('fileid DESC');
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select($bsqlch->strdbcompat("File"));
$select->order($bsqlch->strdbcompat("FileId") . " DESC");
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new File());
$paginatorAdapter = new DbSelect(
Expand All @@ -40,7 +81,8 @@ public function fetchAll($paginated=false)
public function getFile($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('fileid' => $id));
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$rowset = $this->tableGateway->select(array($bsqlch->strdbcompat("FileId") => $id));
$row = $rowset->current();
if(!$row) {
throw new \Exception("Could not find row $id");
Expand All @@ -53,9 +95,14 @@ public function getFileByJobId($id)
$paginated = true;
$jobid = (int) $id;
if($paginated) {
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('file')->join('filename', 'file.filenameid = filename.filenameid');
$select->where('file.jobid = ' . $jobid);
$select->from($bsqlch->strdbcompat("File"));
$select->join(
$bsqlch->strdbcompat("Filename"),
$bsqlch->strdbcompat("File.FilenameId = Filename.FilenameId")
);
$select->where($bsqlch->strdbcompat("File.JobId") . " = '" . $jobid . "'");
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new File());
$paginatorAdapter = new DbSelect(
Expand All @@ -72,8 +119,9 @@ public function getFileByJobId($id)

public function getFileNum()
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('file');
$select->from($bsqlch->strdbcompat("File"));
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new File());
$rowset = new DbSelect(
Expand Down
5 changes: 4 additions & 1 deletion module/Fileset/Module.php
Expand Up @@ -6,6 +6,7 @@
use Fileset\Model\FilesetTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class Module
{
Expand Down Expand Up @@ -42,7 +43,9 @@ public function getServiceConfig()
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Fileset());
return new TableGateway('fileset', $dbAdapter, null, $resultSetPrototype);
$config = $sm->get('Config');
$bsqlch = new BareosSqlCompatHelper($config['db']['driver']);
return new TableGateway($bsqlch->strdbcompat("FileSet"), $dbAdapter, null, $resultSetPrototype);
},
),
);
Expand Down
2 changes: 2 additions & 0 deletions module/Fileset/src/Fileset/Model/Fileset.php
Expand Up @@ -35,6 +35,8 @@ class Fileset

public function exchangeArray($data)
{
$data = array_change_key_case($data, CASE_LOWER);

$this->filesetid = (!empty($data['filesetid'])) ? $data['filesetid'] : null;
$this->fileset = (!empty($data['fileset'])) ? $data['fileset'] : null;
$this->md5 = (!empty($data['md5'])) ? $data['md5'] : null;
Expand Down
37 changes: 28 additions & 9 deletions module/Fileset/src/Fileset/Model/FilesetTable.php
Expand Up @@ -30,8 +30,11 @@
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class FilesetTable
class FilesetTable implements ServiceLocatorAwareInterface
{

protected $tableGateway;
Expand All @@ -41,11 +44,24 @@ public function __construct(TableGateway $tableGateway)
$this->tableGateway = $tableGateway;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
return $this->serviceLocator;
}

public function getDbDriverConfig() {
$config = $this->getServiceLocator()->get('Config');
return $config['db']['driver'];
}

public function fetchAll($paginated=false)
{

$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('fileset');
$select->from($bsqlch->strdbcompat("FileSet"));

if($paginated) {
$resultSetPrototype = new ResultSet();
Expand All @@ -66,8 +82,10 @@ public function fetchAll($paginated=false)

public function getFileset($id)
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$id = (int) $id;
$rowset = $this->tableGateway->select(array('filesetid' => $id));
$rowset = $this->tableGateway->select(array(
$bsqlch->strdbcompat("FileSetId") => $id));
$row = $rowset->current();
if(!$row) {
throw new \Exception("Could not find row $id");
Expand All @@ -78,11 +96,11 @@ public function getFileset($id)
public function getFilesetHistory($id)
{
$fset = $this->getFileSet($id);

$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('fileset');
$select->where("fileset = '". $fset->fileset ."'");
$select->order('createtime DESC');
$select->from($bsqlch->strdbcompat("FileSet"));
$select->where($bsqlch->strdbcompat("FileSet") . " = '". $fset->fileset . "'");
$select->order($bsqlch->strdbcompat("CreateTime") . " DESC");

$resultSet = $this->tableGateway->selectWith($select);

Expand All @@ -91,8 +109,9 @@ public function getFilesetHistory($id)

public function getFilesetNum()
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from('fileset');
$select->from($bsqlch->strdbcompat("FileSet"));
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Fileset());
$rowset = new DbSelect(
Expand Down
5 changes: 4 additions & 1 deletion module/Job/Module.php
Expand Up @@ -6,6 +6,7 @@
use Job\Model\JobTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class Module
{
Expand All @@ -23,7 +24,9 @@ public function getServiceConfig()
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Job());
return new TableGateway('job', $dbAdapter, null, $resultSetPrototype);
$config = $sm->get('Config');
$bsqlch = new BareosSqlCompatHelper($config['db']['driver']);
return new TableGateway($bsqlch->strdbcompat("Job"), $dbAdapter, null, $resultSetPrototype);
},
),
);
Expand Down

0 comments on commit 11c36fb

Please sign in to comment.