|
1:
<?php 2: if (!$SQL_CLASS_LOAD){ 3: class cSQL { 4: 5: var $sql_type; 6: var $errstr; 7: var $errno; 8: var $db; 9: var $num_fields; 10: var $num_rows; 11: var $no_queries; 12: var $result; 13: var $myrow; 14: var $sql_query; 15: 16: function connect($sql_type, $hostname, $username, $password, $db){ 17: //function connect(){ 18: //$sql_type = 'MySQL'; 19: //$hostname = 'localhost'; 20: //$username = 'root'; 21: //$password = 'xxx'; 22: //$db = 'chat'; 23: switch (strtolower($sql_type)){ 24: /* MySQL */ 25: case 'mysql' : 26: $this->sql_type = "MySQL"; 27: if (($this->db = mysql_connect($hostname, $username, $password)) === FALSE){ 28: $this->error(mysql_error(), mysql_errno()); 29: } 30: if ($db != 'null'){ 31: if ((mysql_select_db($db, $this->db)) === FALSE){ 32: $this->error(mysql_error(), mysql_errno()); 33: } 34: } 35: return TRUE; 36: break; 37: /* END MySQL */ 38: /* PostgrSQL */ 39: case 'postgresql' : 40: $this->sql_type = "PostgreSQL"; 41: if (($this->db = pg_connect("host=".$hostname." dbname=".$db." user=".$username." password=".$password)) === FALSE){ 42: $this->error(pg_errormessage(), 1); 43: } 44: return TRUE; 45: break; 46: /* END PostgreSQL */ 47: default : 48: $this->error("Unkown database type '$sql_type'", 1); 49: break; 50: } 51: $this->no_queries = 0; 52: } 53: 54: function select_db($db){ 55: switch($this->sql_type){ 56: /* MySQL */ 57: case 'MySQL' : 58: if ((mysql_select_db($db, $this->db)) === FALSE){ 59: $this->error(mysql_error(), mysql_errno()); 60: return FALSE; 61: } 62: return TRUE; 63: break; 64: /* END MySQL */ 65: } 66: } 67: 68: function query($sql){ 69: switch($this->sql_type){ 70: /* MySQL */ 71: case 'MySQL' : 72: $this->sql_query = $sql; 73: if (($this->result = mysql_query($sql)) === FALSE){ 74: $this->error(mysql_error(), mysql_errno()); 75: } 76: ++$this->no_query; 77: if (is_resource($this->result)){ 78: $this->num_fields = mysql_num_fields($this->result); 79: $this->num_rows = mysql_num_rows($this->result); 80: } 81: return TRUE; 82: break; 83: /* END MySQL */ 84: /* PostgreSQL */ 85: case 'PostgreSQL' : 86: $this->sql_query = $sql; 87: if (($this->result = pg_query($sql)) === FALSE){ 88: $this->error(pg_errormessage(), 1); 89: } 90: ++$this->no_query; 91: if (is_resource($this->result)){ 92: $this->num_fields = pg_num_fields($this->result); 93: $this->num_rows = pg_num_rows($this->result); 94: } 95: return TRUE; 96: break; 97: /* END PostgreSQL */ 98: } 99: } 100: 101: function result(){ 102: switch($this->sql_type){ 103: /* MySQL */ 104: case 'MySQL' : 105: $this->myrow = mysql_fetch_array($this->result); 106: return $this->myrow; 107: break; 108: /* END MySQL */ 109: /* PostgreSQL */ 110: case 'PostgreSQL' : 111: $this->myrow = pg_fetch_array($this->result); 112: return $this->myrow; 113: break; 114: /* END PostgreSQL */ 115: } 116: } 117: 118: function show_tables(){ 119: switch($this->sql_type){ 120: /* MySQL */ 121: case 'MySQL' : 122: $this->query("show tables;"); 123: break; 124: /* END MySQL */ 125: /* PostgreSQL */ 126: case 'PostgreSQL' : 127: $this->query("select * from \"pg_tables\""); //test 128: break; 129: /* END PostgreSQL */ 130: } 131: } 132: 133: function describe($sql_table){ 134: switch($this->sql_type){ 135: /* MySQL */ 136: case 'MySQL' : 137: $this->query("describe ".$sql_table); 138: break; 139: /* END MySQL */ 140: /* PostgreSQL */ 141: case 'PostgreSQL' : 142: $this->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod), a.attnotnull, a.atthasdef, a.attnum, col_description(a.attrelid, a.attnum) FROM pg_class c, pg_attribute a WHERE c.relname='".$sql_table."' AND a.attnum > 0 AND a.attrelid = c.oid ORDER BY a.attnum"); //test 143: break; 144: /* END PostgreSQL */ 145: } 146: } 147: 148: function optimize($table){ 149: switch($this->sql_type){ 150: /* MySQL */ 151: case 'MySQL' : 152: $this->query("optimize table `".$table."`"); 153: break; 154: /* END MySQL */ 155: /* PostgreSQL */ 156: case 'PostgreSQL' : 157: $this->query("vacuum \"".$table."\""); 158: $this->query("reindex table \"".$table."\""); 159: break; 160: /* END PostgreSQL */ 161: } 162: } 163: 164: function report(){ 165: echo $this->no_query; 166: } 167: 168: function last_insert_id(){ 169: return mysql_insert_id($this->db); 170: } 171: 172: function error($errstr, $errno){ 173: echo "<FONT FACE = \"Tahoma\" SIZE = 1 COLOR = \"#FF0000\">\n"; 174: echo $this->sql_type . " Error: <B>$errstr</B> [$errno]<BR>\n"; 175: echo "<B>SQL Query:</B><I> ".$this->sql_query."<BR>\n"; 176: echo "</FONT>\n"; 177: die(); 178: } 179: function close(){ 180: switch($this->sql_type){ 181: /* MySQL */ 182: case 'MySQL' : 183: mysql_close($this->db); 184: break; 185: /* END MySQL */ 186: /* PostgreSQL */ 187: case 'PostgreSQL' : 188: pg_close($this->db); 189: break; 190: /* END PostgreSQL */ 191: } 192: } 193: } 194: } 195: $SQL_CLASS_LOAD = 1; 196: ?> 197:
|