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