Code

ed9544ada82b6cf1134c3d7233b5f295c224b9d6
[gosa.git] / include / class_pgsql_opengw.inc
1 <?php
3 class pgre_sql{
5   var $handle;
6   var $query_log;
7   var $user;
8   var $server;
9   var $db;
10   var $pwd;
11   var $is_connected = false;
13   function pgre_sql($user,$pwd,$server,$db)
14   {
15     $this->user   = $user;
16     $this->pwd  = $pwd;
17     $this->server = $server;
18     $this->db   = $db;
20     if($this->_connect()){
21       $this->is_connected = true;
22     }
24     return($this->is_connected);
25   }
27   function _connect()
28   {
29         error_reporting(E_ALL);
30         if(is_callable("pg_connect")){
31                 $this->handle = pg_connect("dbname=".$this->db." host=".$this->server." user=".$this->user);
32                 if(!$this->handle){
33                         print "can't connect to pgsql :";
34                         exit();
35                 }
36         }else{
37                 print "Extension fpr pgsql is missing";
38                 $this->handle = false;
39         }
40         return($this->handle);
41   }
43   function Query($a_query)
44   {
45     if(is_array($a_query)){
46       foreach($a_query as $nr => $query){
47         return($this->_query($query));
48       }
49     }else{
50       return($this->_query($a_query));
51     }
52   }
53         
54   function _query($query)
55   {
56     return(pg_query($this->handle,$query));
57   }
59   function FetchAllRows($res)
60   {
61     return(pg_fetch_all($res))  ;
62   }
64   function gen_id()
65   {
66           $tmp = $this->_query("select nextval('key_generator');");
67           $tmp = ($this->FetchAllRows($tmp));
68           return($tmp[0]['nextval']);
69   }
72   function GetTemplateUser(){
73           $data = array();
74           $qry = "SELECT description,name,company_id FROM company WHERE is_template_user=1;";
75           $res = $this->_query($qry);
76           $tmp = $this->FetchAllRows($res);
77           foreach($tmp as $attr){
78                   $data[$attr['name']] = $attr;
79           }
80           return $data;
81   }
82   function GetLocationTeam(){
83           $data = array();
84           $qry = "SELECT description,name,company_id FROM team WHERE is_location_team=1;";
85           $res = $this->_query($qry);
86           $tmp = $this->FetchAllRows($res);
87           foreach($tmp as $attr){
88                   $data[$attr['description']] = $attr;
89           }
90           return $data;
91   }
92   function GetTeams(){
93           $data = array();
94           $qry = "SELECT description,name,company_id FROM team 
95                                 WHERE (is_team=1) AND company_id 
96                                         NOT IN (SELECT company_id FROM company WHERE is_location_team=1);";
97           $res = $this->_query($qry);
98           $tmp = $this->FetchAllRows($res);
99           foreach($tmp as $attr){
100                   $data[$attr['description']] = $attr;
101           }
102           return $data;
103   }
107 function gen_syntax($array,$tablename,$act,$ist)
109         if($act == "EDIT"){
110                 $str = "UPDATE ".$tablename." SET ";
111                 $company_id = $ist[0]['company_id'];
113                 foreach($array as $name => $value){
114                         if(empty($value)) continue;
115                 
116                         if(!is_numeric($value)){
117                                 $str.= " ".$name."='".$value."', ";
118                         }else{
119                                 $str.= " ".$name."=".$value.", ";
120                         }
121                 }
122                 $str = preg_replace("/, $/","",$str);
123                 $str .= " WHERE company_id=".$company_id.";\n";
124                 return $str;
125         }
126         if($act == "ADD"){
127                 $str = "INSERT into ".$tablename." (";
128                 $attrs  = "";
129                 $values = "";
130                 foreach($array as $name => $attribute){
132                         if(empty($attribute)) continue;
134                         if(is_numeric($attribute)){
135                                 $attrs  .= $name.", ";
136                                 $values .= $attribute.", ";
137                         }else{
138                                 $attrs  .= $name.", ";
139                                 $values .= "'".$attribute."', ";
140                         }
141                 }
142                 $attrs = preg_replace("/, $/","",$attrs);
143                 $values= preg_replace("/, $/","",$values);
144                 $str .= $attrs." ) \nVALUES\n (".$values.");\n";
145                 return $str;
146         }
149 ?>