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 if(is_array($tmp)){
90 foreach($tmp as $attr){
91 $data[$attr['description']] = $attr;
92 }
93 }
94 return $data;
95 }
96 function GetTeams(){
97 $data = array();
98 $qry = "SELECT description,name,company_id FROM team
99 WHERE (is_team=1) AND company_id
100 NOT IN (SELECT company_id FROM company WHERE is_location_team=1);";
101 $res = $this->_query($qry);
102 $tmp = $this->FetchAllRows($res);
103 foreach($tmp as $attr){
104 $data[$attr['description']] = $attr;
105 }
106 return $data;
107 }
108 }
111 function gen_syntax($array,$tablename,$act,$ist)
112 {
113 if($act == "EDIT"){
114 $str = "UPDATE ".$tablename." SET ";
115 $company_id = $ist[0]['company_id'];
117 foreach($array as $name => $value){
118 if((empty($value))&&(!preg_match("/^is_/i",$name))) continue;
120 if((empty($value))&&(preg_match("/^is_/i",$name))){
121 $value= 0;
122 }
124 if(!is_numeric($value)){
125 $str.= " ".$name."='".$value."', ";
126 }else{
127 $str.= " ".$name."=".$value.", ";
128 }
129 }
130 $str = preg_replace("/, $/","",$str);
131 $str .= " WHERE company_id=".$company_id.";\n";
132 return $str;
133 }
134 if($act == "ADD"){
135 $str = "INSERT into ".$tablename." (";
136 $attrs = "";
137 $values = "";
138 foreach($array as $name => $attribute){
139 if((empty($attribute))&&(!preg_match("/^is_/i",$name))) continue;
141 if((empty($attribute))&&(preg_match("/^is_/i",$name))){
142 $attribute= 0;
143 }
145 if(is_numeric($attribute)){
146 $attrs .= $name.", ";
147 $values .= $attribute.", ";
148 }else{
149 $attrs .= $name.", ";
150 $values .= "'".$attribute."', ";
151 }
152 }
153 $attrs = preg_replace("/, $/","",$attrs);
154 $values= preg_replace("/, $/","",$values);
155 $str .= $attrs." ) \nVALUES\n (".$values.");\n";
156 return $str;
157 }
158 }
159 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
160 ?>