|
/**
* Database class
*
* @ Version: 2.2
* @ Revised: 27 may 2007
*
**/
Class Database {
Var $ host;
Var $ name;
Var $ user;
Var $ pass;
Var $ prefix;
Var $ linkId;
Function Database ($ mysql ){
Foreach ($ mysql as $ k => $ v ){
$ This-> $ k = $ v;
}
If (strlen ($ this-> prefix)> 0 & substr ($ this-> prefix,-1 )! = "_")
$ Prefix. = "_";
$ This-> prefix = $ prefix;
}
Function getLastID (){
$ Id = mysql_fetch_row (mysql_query ("SELECT LAST_INSERT_ID ()", $ this-> linkId ));
Return $ id [0];
}
Function getPossibleValues ($ tableA, $ whereA ){
If (gettype ($ tableA) = "array "){
$ Table = "";
Foreach ($ tableA as $ t ){
$ Table. = $ this-> prefix. $ t .",";
}
$ Table = substr ($ table, 0,-2 );
} Else $ table = $ this-> prefix. $ tableA;
If (gettype ($ whereA )! = "Array "){
$ WhereA = array ($ whereA );
}
$ Return = array ();
Foreach ($ whereA as $ where ){
$ SQL = mysql_query ("SHOW COLUMNS FROM". $ table. "LIKE '%". $ where. "% '");
While ($ arr = mysql_fetch_array ($ SQL )){
If (strpos ($ arr ['type'], 'enum') === 0 ){
$ Vals = substr ($ arr ['type'], 5,-1 );
} Else {
$ Vals = substr ($ arr ['type'], 4,-1 );
}
$ Vals = str_replace ("'", "", $ vals );
$ Vals = explode (",", $ vals );
$ I = 1;
Foreach ($ vals as $ val ){
$ Return [$ arr ['field'] [$ I ++] = $ val;
}
$ Return [$ arr ['field'] ['default'] = $ arr ['default'];
If ($ arr ['null']! = "NO") $ return [$ arr ['field'] [0] = NULL;
}
}
Return $ return;
}
Function connect (){
$ This-> linkId = mysql_connect ($ this-> host, $ this-> user, $ this-> pass );
If (! $ This-> linkId ){
Return false;
}
If (mysql_select_db ($ this-> name, $ this-> linkId) return true;
Mysql_close ($ this-> linkId );
Return false;
}
Function runSelect ($ tables, $ where = "1", $ fieldsA = "*", $ order = false, $ limit = false, $ offset = false, $ group = false) {
If (gettype ($ tables) = "array "){
$ Table = "";
Foreach ($ tables as $ t ){
$ Table. = $ this-> prefix. $ t .",";
}
$ Table = substr ($ table, 0,-2 );
} Else $ table = $ this-> prefix. $ tables;
If (gettype ($ fieldsA) = "array "){
$ Fields = "";
$ Keys = array_keys ($ fieldsA );
If ($ keys [0]! = '0 '){
Foreach ($ keys as $ key ){
$ Fields. = $ key. 'as'. $ fieldsA [$ key]. ',';
}
} Else {
Foreach ($ fieldsA as $ field ){
$ Fields. = $ field .',';
}
}
$ Fields = substr ($ fields, 0,-2 );
} Else $ fields = $ fieldsA;
$ Query = "SELECT". $ fields. "FROM". $ table. "WHERE". $ where.
($ Order! = False? "Order by". $ order :( $ group! = False? "Group by". $ group :"")).
($ Limit! = False? "LIMIT". $ limit :"").
($ Offset! = False? "OFFSET". $ offset :"");
Return mysql_query ($ query, $ this-> linkId );
}
Function runUpdate ($ table, $ valuesA, $ where = "1 "){
If (gettype ($ valuesA) = "array "){
$ Fields = "";
$ Values = "";
$ Keys = array_keys ($ valuesA );
Foreach ($ keys as $ key ){
If ($ valuesA [$ key]! = NULL)
$ Values. = ""'. $ key. "'= '". str_replace ("'",' \ '', $ valuesA [$ key]). "',";
Else
$ Values. = $ key. "= NULL ,";
}
$ Fields = substr ($ fields, 0,-1 );
$ Values = substr ($ values, 0,-1 );
} Else $ values = $ valuesA;
$ Query = "UPDATE". $ this-> prefix. $ table. "SET". $ values. "WHERE". $ where;
If (mysql_query ($ query,
$ This-> linkId ))
Return mysql_affected_rows ($ this-> linkId );
Return false;
}
Function runDelete ($ table, $ where = "1 "){
If (mysql_query ("delete from". $ this-> prefix. $ table. "WHERE". $ where, $ this-> linkId ))
Return mysql_affected_rows ($ this-> linkId );
Return false;
}
Function runInsert ($ table, $ valuesA, $ onDuplicate = NULL ){
If (gettype ($ valuesA) = "array "){
$ Fields = "";
$ Values = "";
$ Keys = array_keys ($ valuesA );
Foreach ($ keys as $ key ){
$ Fields. = "" '. $ key ."',";
$ Values. = ($ valuesA [$ key] === NULL? "NULL,": "'". str_replace ("'", '\ '', $ valuesA [$ key])."', ");
}
$ Fields = substr ($ fields, 0,-2 );
$ Values = substr ($ values, 0,-2 );
}
$ OnDup = "";
If ($ onDuplicate! = NULL ){
$ OnDup = "on duplicate key update ";
If (gettype ($ onDuplicate) = "array "){
$ Keys = array_keys ($ onDuplicate );
Foreach ($ keys as $ key ){
$ OnDup. = '''. $ key. ''= '. ($ onDuplicate [$ key] = NULL? "NULL,": "'". str_replace ("'", '\ '', $ onDuplicate [$ key])."', ");
}
$ OnDup = substr ($ onDup, 0,-2 );
} Else $ onDup. = $ onDuplicate;
}
$ Query = "insert into". $ this-> prefix. $ table. ($ fields! = NULL? "(". $ Fields .")":"").
"VALUES (". $ values. ")". $ onDup;
If (mysql_query ($ query, $ this-> linkId ))
Return mysql_affected_rows ($ this-> linkId );
Return false;
}
Function getCells ($ table ){
$ Query = "show columns from" '. $ table .""';
$ Fields = mysql_query ($ query, $ this-> linkId) or die ('hej ');
Return $ fields;
}
Function translateCellName ($ cellName ){
$ SQL = $ this-> runSelect ("mysql_cell_translation", "mysql_name = '". $ cellName ."'");
$ Row = mysql_fetch_assoc ($ SQL );
Return $ row ['human _ name']? $ Row ['human _ name']: '['. $ cellName. ']';
}
Function getError (){
Return mysql_error ($ this-> linkId );
}
Function close ()
{
Mysql_close ($ this-> linkId );
}
}
?>
|