Skip to content
Snippets Groups Projects
Commit 0b479ff5 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

#1385 - first basic code for a version class; cache is a sqlite table

parent 4869a92e
No related branches found
No related tags found
No related merge requests found
<?php
/**
* version data handler for the deployment tool
*
* @author hahn
*/
class versions {
//put your code here
public $sProject = false;
public $sPhase = false;
public $sPlace = false;
public $sVariable = false;
public $sData = false;
/**
* filename of
* @var type
*/
private $_dbfile = false;
private $_sCreate = '
CREATE TABLE "versions" (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
`time` DATETIME,
`project` TEXT,
`phase` TEXT,
`place` TEXT,
`host` TEXT,
`variable` TEXT,
`data` TEXT,
UNIQUE (project, phase, place, host, variable) ON CONFLICT REPLACE
);'
;
// ----------------------------------------------------------------------
// CONSTRUCTOR
// ----------------------------------------------------------------------
public function __construct(){
// cache dir is hardcoded to versions directory :-/
$this->_dbfile = __DIR__ . '/../data/versioncache.db';
if (!file_exists($this->_dbfile)) {
$this->_createDb();
if (!file_exists($this->_dbfile)) {
die("ERROR: unable to create sqlite database " . $this->_dbfile);
}
}
}
// ----------------------------------------------------------------------
// PRIVATE
// ----------------------------------------------------------------------
/**
* create sqlite database - called in constructor if the file does not exist
*/
private function _createDb() {
echo "try to create file $this->_dbfile ...<br>\n";
return $this->_makeQuery($this->_sCreate);
}
/**
* execute a sql statement
* @param string $sSql sql statement
* @return database object
*/
private function _makeQuery($sSql) {
// $this->_log(__FUNCTION__."($sSql)");
echo "DEBUG: executing SQL<pre>$sSql</pre>";
$db = new PDO("sqlite:" . $this->_dbfile);
$result = $db->query($sSql);
$db = NULL;
return $result;
}
// ----------------------------------------------------------------------
// PUBLIC GETTER
// ----------------------------------------------------------------------
public function getVersion(){
if (!$this->sProject || !$this->sPhase || !$this->sPlace ){
die("ERROR: you need to set a project, phase and place. use the setProject() method to do so.");
}
$aReturn=array();
$sSql="select * from `versions`
WHERE
project='" . $this->sProject . "'
AND phase='" . $this->sPhase . "'
AND place='" . $this->sPlace . "'
AND host='" . $this->sHost . "'
";
foreach ($this->_makeQuery($sSql) as $row) {
for ($i = 0; $i <= count($row); $i++) {
unset($row[$i]);
}
$aReturn[] = $row;
}
return $aReturn;
}
public function whereiam(){
return array(
'project'=>$this->sProject,
'phase'=>$this->sPhase,
'place'=>$this->sPlace,
'host'=>$this->sHost,
);
}
// ----------------------------------------------------------------------
// PUBLIC SETTER
// ----------------------------------------------------------------------
public function setProject($sProject, $sPhase=false, $sPlace=false, $sHost=false){
$this->sProject=$sProject;
$this->setPhase($sPhase, $sPlace, $sHost);
return $this->sProject;
}
public function setPhase($sPhase, $sPlace=false, $sHost=false){
if (!$this->sProject){
die("ERROR: you need to set a project. Use the setProject() method to do so.");
return false;
}
$this->sPhase=$sPhase;
$this->setPlace($sPlace, $sHost);
return $this->sPhase;
}
public function setPlace($sPlace, $sHost=false){
if (!$this->sProject || !$this->sPhase){
die("ERROR: you need to set a project and phase. Use the setProject() method to do so.");
return false;
}
if($sPlace!=='queue' && $sPlace!=='puppet' && $sPlace!=='installed'){
die("ERROR: you set a wrong place [$sPlace]");
}
$this->sPlace=$sPlace;
$this->setHost($sHost);
return $this->sPlace;
}
public function setHost($sHost=false){
if (!$this->sProject || !$this->sPhase || !$this->sPlace){
die("ERROR: you need to set a project, phase and place. Use the setProject() method to do so.");
return false;
}
if($sHost && $this->sPlace!=='installed'){
die("ERROR: a host can be set on place [installed] only.");
}
if(!$sHost && $this->sPlace==='installed'){
die("ERROR: on place [installed] a host MUST be set.");
}
$this->sHost=$sHost;
return $this->sHost;
}
/**
* update a version
* @return boolean
*/
public function updateVersion($sVersioninfos){
if (!$this->sProject || !$this->sPhase || !$this->sPlace ){
die("ERROR: you need to set a project, phase and place. use the setProject() method to do so.");
}
$sSql="
INSERT into `versions`
(`time`, `project`, `phase`, `place`, `host`, `variable`, `data` )
VALUES
(
'" . date("Y-m-d H:i:s") . "',
'" . $this->sProject . "',
'" . $this->sPhase . "',
'" . $this->sPlace . "',
'" . $this->sHost . "',
'version',
'" . $sVersioninfos . "'
);
";
return $this->_makeQuery($sSql);
}
}
<?php
echo '<pre>'.print_r($_GET, 1).'</pre>';
require './classes/versions.class.php';
$oVersion=new versions();
$sProject='ci';
$sPhase='preview';
$sPlace='installed';
$sHost='preview02.ci';
$oVersion->setProject($sProject, $sPhase, $sPlace, $sHost);
print_r($oVersion->whereiam());
/*
if ($oVersion->updateVersion('{ "revision": "000" }')){
print_r($oVersion->getVersion());
} else {
echo "insert failed. :-(";
}
*/
print_r($oVersion->getVersion());
// $oVersion->
\ 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