A first stab at using DBX for database abstraction. Works with Postgres currently.
[freeradius.git] / dialup_admin / lib / sql / drivers / dbx / functions.php3
1 <?php
2 function da_sql_host_connect($server,$config)
3 {
4         if ($config[sql_use_http_credentials] == 'yes'){
5                 global $HTTP_SERVER_VARS;
6                 $SQL_user = $HTTP_SERVER_VARS["PHP_AUTH_USER"];
7                 $SQL_passwd = $HTTP_SERVER_VARS["PHP_AUTH_PW"];
8         }
9         else{
10                 $SQL_user = $config[sql_username];
11                 $SQL_passwd = $config[sql_password];
12         }
13         // FIXME: This function is still Postgres specific. Needs to be configurable.
14         return @dbx_connect(DBX_PGSQL, "$server", "$config[sql_database]",
15                         "$SQL_user", "$SQL_passwd", DBX_PERSISTENT);
16 }
17
18 function da_sql_connect($config)
19 {
20         if ($config[sql_use_http_credentials] == 'yes'){
21                 global $HTTP_SERVER_VARS;
22                 $SQL_user = $HTTP_SERVER_VARS["PHP_AUTH_USER"];
23                 $SQL_passwd = $HTTP_SERVER_VARS["PHP_AUTH_PW"];
24         }
25         else{
26                 $SQL_user = $config[sql_username];
27                 $SQL_passwd = $config[sql_password];
28         }
29         // FIXME: This function is still Postgres specific. Needs to be configurable.
30         return @dbx_connect(DBX_PGSQL, "$server", "$config[sql_database]",
31                         "$SQL_user", "$SQL_passwd");
32 }
33
34 function da_sql_pconnect($config)
35 {
36         if ($config[sql_use_http_credentials] == 'yes'){
37                 global $HTTP_SERVER_VARS;
38                 $SQL_user = $HTTP_SERVER_VARS["PHP_AUTH_USER"];
39                 $SQL_passwd = $HTTP_SERVER_VARS["PHP_AUTH_PW"];
40         }
41         else{
42                 $SQL_user = $config[sql_username];
43                 $SQL_passwd = $config[sql_password];
44         }
45         // FIXME: This function is still Postgres specific. Needs to be configurable.
46         return @dbx_connect(DBX_PGSQL, "$server", "$config[sql_database]",
47                         "$SQL_user", "$SQL_passwd", DBX_PERSISTENT);
48 }
49
50 function da_sql_close($link,$config)
51 {
52         @dbx_close($link);
53 }
54
55 function da_sql_escape_string($string)
56 {
57         return addslashes($string);
58 }
59
60 function da_sql_query($link,$config,$query)
61 {
62         if ($config[sql_debug] == 'true') {
63                 print "<b>DEBUG(SQL,PG DRIVER): Query: <i>$query</i></b><br>\n";
64         }
65         return @dbx_query($link,$query);
66 }
67
68 function da_sql_num_rows($result,$config)
69 {
70         if ($config[sql_debug] == 'true')
71                 print "<b>DEBUG(SQL,PG DRIVER): Query Result: Num rows:: " . $result->rows . "</b><br>\n";
72         return $result->rows;
73 }
74
75 $dbx_global_record_counter = array() ;
76 function da_sql_fetch_array($result,$config)
77 {
78
79         global $dbx_global_record_counter;
80         if (!$dbx_global_record_counter[$result->handle]){
81                 $dbx_global_record_counter[$result->handle] = 0;
82         }
83
84         if ($dbx_global_record_counter[$result->handle] <= $result->rows - 1 ){
85                 return $result->data[$dbx_global_record_counter[$result->handle]++];
86         } elseif ($dbx_global_record_counter[$result->handle] > $result->rows - 1 ) {
87                 $dbx_global_record_counter[$result->handle]++;
88                 return NULL;
89         } else {
90                 $dbx_global_record_counter[$result->handle]++;
91                 return FALSE;
92         }
93 }
94
95 function da_sql_affected_rows($link,$result,$config)
96 {
97         // FIXME: This function is still Postgres specific.
98         if ($config[sql_debug] == 'true')
99                 print "<b>DEBUG(SQL,PG DRIVER): Query Result: Affected rows:: " . @pg_cmdtuples($result->handle) . "</b><br>\n";
100         return @pg_cmdtuples($result->handle);
101 }
102
103 function da_sql_list_fields($table,$link,$config)
104 {
105         $res = @dbx_query($link,"SELECT * FROM ".$table." LIMIT 1 ;"); 
106         if ($res){
107                 $fields[num] = $res->cols;
108         }
109         $res = @dbx_query($link,"SELECT * FROM ".$table." LIMIT 1 ;");
110         if ($res)
111                 $fields[res] = $res->info[name];
112         else
113                 return NULL;
114
115         return $fields;
116 }
117
118 function da_sql_num_fields($fields,$config)
119 {
120         if ($fields)
121                 return $fields[num];
122 }
123
124 function da_sql_field_name($fields,$num,$config)
125 {
126         if ($fields)
127                 return $fields[res][$num];
128 }
129
130 function da_sql_error($link,$config)
131 {
132         return dbx_error($link);
133 }
134 ?>