]> andersk Git - sql-web.git/blame - lib/dbaccess.lib.php
updated checkQuotas
[sql-web.git] / lib / dbaccess.lib.php
CommitLineData
997305cf 1<?php
79ffa771
JP
2/*
3 (c) 2005 Joe Presbrey
4*/
997305cf 5
8988dbad 6require_once('joe/util.lib.php');
997305cf
JP
7
8function DBMaster($sql) {
9 sessTime($sql);
10 $res = mysql_query($sql);
11 sessTime();
12 return $res;
13}
14function DBSlave($sql) {
15 sessTime($sql);
16 $res = mysql_query($sql);
17 sessTime();
82ffc3b5 18 if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR);
997305cf
JP
19 return $res;
20}
21
22function DBSelect($sql) { return DBSlave($sql); }
377015e0
JP
23function DBInsert($sql) {
24 DBMaster($sql);
82ffc3b5 25 if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR);
377015e0
JP
26 return mysql_insert_id();
27}
28function DBUpdate($sql) { DBInsert($sql); }
29function DBDelete($sql) { DBInsert($sql); }
30function DBCreate($sql) { DBMaster($sql); }
31function DBDrop($sql) { DBMaster($sql); }
32function DBGrant($sql) { DBInsert($sql); }
33function DBRevoke($sql) { DBInsert($sql); }
34function DBSet($sql) { DBInsert($sql); }
997305cf
JP
35function DBShow($sql) { return DBSlave($sql); }
36
37function calcDBSize($tdb) {
e346f2b3
JP
38 $sql_result = "SHOW DATABASES LIKE '".mysql_escape_string($tdb)."'";
39 $result = DBShow($sql_result);
40 if (!mysql_num_rows($result)) return null;
41
997305cf 42 $sql_result = "SHOW TABLE STATUS FROM `" .mysql_escape_string($tdb)."`";
997305cf
JP
43 $result = DBShow($sql_result);
44
45 if($result) {
46 $size = 0;
47 while ($data = mysql_fetch_array($result)) {
48 $size += $data["Data_length"] + $data["Index_length"];
49 }
50 mysql_free_result($result);
51 return $size;
52 }
53 else {
54 return null;
55 }
56}
57
9c70b481
JP
58function checkQuotas($userId=null) {
59 if (empty($userId)) {
60 $sql = 'SELECT DatabaseId,Name FROM DB WHERE bEnabled=1';
61 } else {
6fd1f8c5
JP
62 if (is_array($userId)) {
63 $sql_userId = sprintf("UserId IN ('%s')", implode("','",array_map('mysql_escape_string',$userId)));
64 } elseif (is_numeric($userId)) {
65 $sql_userId = sprintf("UserId = '%s'", mysql_escape_string($userId));
66 } else {
67 die('userId parameter error in checkQuotas');
68 }
69 $sql = sprintf("SELECT DB.DatabaseId,Name FROM DB INNER JOIN DBOwner ON DB.DatabaseId = DBOwner.DatabaseId WHERE bEnabled=1 AND %s", $sql_userId);
9c70b481
JP
70 }
71 $databases = fetchRows(DBSelect($sql),'Name');
72 foreach($databases as $db) {
73 $DBId = $db['DatabaseId'];
74 $arr['dLastCheck'] = 'NOW()';
75 $arr['nBytes'] = calcDBSize($db['Name']);
8988dbad 76 $sql = sprintf("UPDATE DB SET %s WHERE DatabaseId = '%s'",
9c70b481
JP
77 buildSQLSet($arr),
78 mysql_escape_string($DBId));
79 DBUpdate($sql);
80 }
81 $sql = "UPDATE UserStat SET nBytes = (
82 SELECT SUM(nBytes)
83 FROM DB
84 INNER JOIN DBOwner ON DBOwner.DatabaseId = DB.DatabaseId
85 WHERE DBOwner.UserId = UserStat.UserId
86 AND DB.bEnabled=1
87 GROUP BY UserId
88 ), dLastCheck = NOW()";
6fd1f8c5 89 if (!empty($userId)) $sql .= " WHERE $sql_userId";
9c70b481 90 DBUpdate($sql);
9840d102
JP
91 $sql = "UPDATE UserStat SET nDatabases = (
92 SELECT COUNT(*)
93 FROM DB
94 INNER JOIN DBOwner ON DBOwner.DatabaseId = DB.DatabaseId
95 WHERE DBOwner.UserId = UserStat.UserId
96 AND DB.bEnabled=1
97 GROUP BY UserId
98 ), dLastCheck = NOW()";
6fd1f8c5 99 if (!empty($userId)) $sql .= " WHERE $sql_userId";
9840d102 100 DBUpdate($sql);
9c70b481
JP
101}
102
997305cf 103?>
This page took 0.057671 seconds and 5 git commands to generate.