1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<?php
class pgre_sql{
var $handle;
var $query_log;
var $user;
var $server;
var $db;
var $pwd;
var $is_connected = false;
function __construct($user,$pwd,$server,$db)
{
$this->user = $user;
$this->pwd = $pwd;
$this->server = $server;
$this->db = $db;
if($this->_connect()){
$this->is_connected = true;
}else{
$this->is_connected = false;
}
}
function _connect()
{
error_reporting(E_ALL | E_STRICT);
if(is_callable("pg_connect")){
if(empty($this->pwd)){
$this->handle = @pg_connect("dbname=".$this->db." host=".$this->server." user=".$this->user);
}else{
$this->handle = @pg_connect("dbname=".$this->db." host=".$this->server." user=".$this->user." password=".$this->pwd);
}
if(!$this->handle){
$this->handle = false;
}
}else{
$this->handle = false;
}
return($this->handle);
}
function Query($a_query)
{
if(is_array($a_query)){
foreach($a_query as $nr => $query){
return($this->_query($query));
}
}else{
return($this->_query($a_query));
}
}
function _query($query)
{
return(pg_query($this->handle,$query));
}
function FetchAllRows($res)
{
return(pg_fetch_all($res)) ;
}
function gen_id()
{
$tmp = $this->_query("select nextval('key_generator');");
$tmp = ($this->FetchAllRows($tmp));
return($tmp[0]['nextval']);
}
function GetTemplateUser(){
$data = array();
$qry = "SELECT description,name,company_id FROM company WHERE is_template_user=1;";
$res = $this->_query($qry);
$tmp = $this->FetchAllRows($res);
foreach($tmp as $attr){
$data[$attr['name']] = $attr;
}
return $data;
}
function GetLocationTeam(){
$data = array();
$qry = "SELECT description,name,company_id FROM team WHERE is_location_team=1;";
$res = $this->_query($qry);
$tmp = $this->FetchAllRows($res);
if(is_array($tmp)){
foreach($tmp as $attr){
$data[$attr['description']] = $attr;
}
}
return $data;
}
function GetTeams(){
$data = array();
$qry = "SELECT description,name,company_id FROM team
WHERE (is_team=1) AND company_id
NOT IN (SELECT company_id FROM company WHERE is_location_team=1);";
$res = $this->_query($qry);
$tmp = $this->FetchAllRows($res);
foreach($tmp as $attr){
$data[$attr['description']] = $attr;
}
return $data;
}
}
function gen_syntax($array,$tablename,$act,$ist)
{
if($act == "EDIT"){
$str = "UPDATE ".$tablename." SET ";
$company_id = $ist[0]['company_id'];
foreach($array as $name => $value){
if((empty($value))&&(!preg_match("/^is_/i",$name))) continue;
if((empty($value))&&(preg_match("/^is_/i",$name))){
$value= 0;
}
if(!is_numeric($value)){
$str.= " ".$name."='".$value."', ";
}else{
$str.= " ".$name."=".$value.", ";
}
}
$str = preg_replace("/, $/","",$str);
$str .= " WHERE company_id=".$company_id.";\n";
return $str;
}
if($act == "ADD"){
$str = "INSERT into ".$tablename." (";
$attrs = "";
$values = "";
foreach($array as $name => $attribute){
if((empty($attribute))&&(!preg_match("/^is_/i",$name))) continue;
if((empty($attribute))&&(preg_match("/^is_/i",$name))){
$attribute= 0;
}
if(is_numeric($attribute)){
$attrs .= $name.", ";
$values .= $attribute.", ";
}else{
$attrs .= $name.", ";
$values .= "'".$attribute."', ";
}
}
$attrs = preg_replace("/, $/","",$attrs);
$values= preg_replace("/, $/","",$values);
$str .= $attrs." ) \nVALUES\n (".$values.");\n";
return $str;
}
}
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>
|