From: Joe Presbrey Date: Thu, 2 Nov 2006 18:08:56 +0000 (+0000) Subject: move SQL to use external joe libraries X-Git-Url: http://andersk.mit.edu/gitweb/sql-web.git/commitdiff_plain/da4cf7c99c1129ee9dafe10c02fa787ed94ed0aa?hp=79ffa7719bedd98ba94a37c1217dbafe56ea1a72 move SQL to use external joe libraries git-svn-id: svn://presbrey.mit.edu/sql/web/dev@120 a142d4bd-2cfb-0310-9673-cb33a7e74f58 --- diff --git a/lib/joe/errors.lib.php b/lib/joe/errors.lib.php new file mode 100644 index 0000000..3fe1ca7 --- /dev/null +++ b/lib/joe/errors.lib.php @@ -0,0 +1,96 @@ + 64) ? '...' : ''); + $args .= "\"$a\""; + break; + case 'array': + //$args .= 'Array('.count($a).')'; + $args .= print_r($a,1); + break; + case 'object': + $args .= 'Object('.get_class($a).')'; + break; + case 'resource': + $args .= 'Resource('.strstr($a, '#').')'; + break; + case 'boolean': + $args .= $a ? 'True' : 'False'; + break; + case 'NULL': + $args .= 'Null'; + break; + default: + $args .= 'Unknown'; + } + } + empty($bt['class']) && $bt['class'] = ''; + empty($bt['type']) && $bt['type'] = ''; + empty($bt['function']) && $bt['function'] = ''; + $output .= "\n"; + $output .= "file: {$bt['line']} - {$bt['file']}\n"; + $output .= "call: {$bt['class']}{$bt['type']}{$bt['function']}($args)\n"; + } + return $output; +} + +set_error_handler('ErrorHandler'); + +?> diff --git a/lib/joe/joe.lib.php b/lib/joe/joe.lib.php new file mode 120000 index 0000000..42c1507 --- /dev/null +++ b/lib/joe/joe.lib.php @@ -0,0 +1 @@ +util.lib.php \ No newline at end of file diff --git a/lib/joe/mysql.lib.php b/lib/joe/mysql.lib.php new file mode 100644 index 0000000..bc52148 --- /dev/null +++ b/lib/joe/mysql.lib.php @@ -0,0 +1,55 @@ +\n".mysql_error(),E_USER_ERROR); + return $res; +} + +function DBSelect($sql) { return DBSlave($sql); } +function DBInsert($sql) { + DBMaster($sql); + if (mysql_error()) trigger_error($sql."
\n".mysql_error(),E_USER_ERROR); + return mysql_insert_id(); +} +function DBUpdate($sql) { DBInsert($sql); } +function DBDelete($sql) { DBInsert($sql); } +function DBCreate($sql) { DBMaster($sql); } +function DBDrop($sql) { DBMaster($sql); } +function DBGrant($sql) { DBInsert($sql); } +function DBRevoke($sql) { DBInsert($sql); } +function DBSet($sql) { DBInsert($sql); } +function DBShow($sql) { return DBSlave($sql); } + +function calcDBSize($tdb) { + $sql_result = "SHOW DATABASES LIKE '".mysql_escape_string($tdb)."'"; + $result = DBShow($sql_result); + if (!mysql_num_rows($result)) return null; + + $sql_result = "SHOW TABLE STATUS FROM `" .mysql_escape_string($tdb)."`"; + $result = DBShow($sql_result); + + if($result) { + $size = 0; + while ($data = mysql_fetch_array($result)) { + $size += $data["Data_length"] + $data["Index_length"]; + } + mysql_free_result($result); + return $size; + } + else { + return null; + } +} + +?> diff --git a/lib/joe/util.lib.php b/lib/joe/util.lib.php new file mode 100644 index 0000000..dd7be0a --- /dev/null +++ b/lib/joe/util.lib.php @@ -0,0 +1,211 @@ + 0) { + $arr = array(); + if (is_null($key)) { + while ($r = mysql_fetch_assoc($rs)) { + $arr[] = $r; + } + } elseif (is_numeric($key)) { + while ($r = mysql_fetch_row($rs)) { + $arr[$r[$key]] = $r; + } + } else { + while ($r = mysql_fetch_assoc($rs)) { + $arr[$r[$key]] = $r; + } + } + mysql_free_result($rs); + return $arr; + } else { + mysql_free_result($rs); + return array(); + } +} + +function printErrors($err) { printList('err', $err); } +function printMsgs($err) { printList('msg', $err); } + +function printList($class,$err) { + if (is_array($err) && count($err)) { + echo '
',(count($err)>1?'':''),'
'; + } +} + +function buildSQLSet($fields, $values=null, $safeFields=false) { + $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); + $sql = ''; + $c = 0; + if (!is_null($values)) { + foreach($fields as $field) { + if ($c++) $sql .= ','; + $value = array_shift($values); + if (is_numeric($value)) + $sql .= " `$field`=".mysql_real_escape_string($value); + else + $sql .= " `$field`='".mysql_real_escape_string($value)."'"; + } + } else { + foreach($fields as $field=>$value) { + if ($c++) $sql .= ','; + if (in_array($value,$ex) || (is_array($safeFields) && in_array($field,$safeFields))) { + $sql .= " `$field`=$value"; + } elseif (is_numeric($value)) { + $sql .= " `$field`=".mysql_real_escape_string($value); + } else { + $sql .= " `$field`='".mysql_real_escape_string($value)."'"; + } + } + } + return $sql; +} + +function buildSQLInsert($array, $table=null, $safeFields=false) { + $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); + $sql = '('; + $c = 0; + foreach($array as $field=>$value) { + if ($c++) $sql .= ','; + $sql .= " `$field` "; + } + $sql .= ') VALUES ('; + $c = 0; + foreach($array as $field=>$value) { + if ($c++) $sql .= ','; + if (in_array($value, $ex) || (is_array($safeFields) && in_array($field,$safeFields))) { + $sql .= " $value "; + } else { + $value = mysql_real_escape_string($value); + if (is_numeric($value)) { + $sql .= " $value "; + } else { + $sql .= " '$value' "; + } + } + } + $sql .= ')'; + return (is_null($table)?$sql:('INSERT INTO `'.$table.'` '.$sql)); +} + +function build_str($query_array) { + $query_string = array(); + foreach ($query_array as $k => $v) { + $new = $k; + if (strlen($v)) + $new .= '='.$v; + $query_string[] = $new; + } + return join('&', $query_string); +} + +function newQS($key, $val=null) { + return newQSA(array($key=>$val)); +} + +function newQSA($array=array()) { + parse_str($_SERVER['QUERY_STRING'], $arr); + $s = count($arr); + foreach($array as $key=>$val) { + $arr[$key] = $val; + if (is_null($val)) + unset($arr[$key]); + } + return (count($arr)||$s)?'?'.build_str($arr):''; +} + +function formQSA($array=array()) { + if (!count($array)) $array = $_SERVER['QUERY_STRING']; + parse_str($array, $arr); + $text = ''; + foreach($arr as $key=>$val) { + $text .= sprintf('', $key, $val); + } + return $text; +} + +?>