options["Port"]) ? $this->options["Port"] : ""); if($this->connection!=0) { if(!strcmp($this->connected_host,$this->host) && !strcmp($this->connected_user,$this->user) && !strcmp($this->connected_password,$this->password) && !strcmp($this->connected_port,$port) && $this->opened_persistent==$this->persistent) return(1); mysql_Close($this->connection); $this->connection=0; $this->affected_rows=-1; } $this->fixed_float=30; $function=($this->persistent ? "mysql_pconnect" : "mysql_connect"); if(($this->connection=@$function($this->host.(!strcmp($port,"") ? "" : ":".$port),$this->user,$this->password))<=0) { $this->last_error=(IsSet($php_errormsg) ? $php_errormsg : "Could not connect to MySQL server"); return(0); } if(IsSet($this->options["FixedFloat"])) $this->fixed_float=$this->options["FixedFloat"]; else { if(($result=mysql_query("SELECT VERSION()",$this->connection))) { $version=explode(".",mysql_result($result,0,0)); $major=intval($version[0]); $minor=intval($version[1]); $revision=intval($version[2]); if($major>3 || ($major==3 && $minor>=23 && ($minor>23 || $revision>=6))) $this->fixed_float=0; mysql_free_result($result); } } if(IsSet($this->supported["Transactions"]) && !$this->auto_commit) { if(!mysql_query("SET AUTOCOMMIT=0",$this->connection)) { mysql_Close($this->connection); $this->connection=0; $this->affected_rows=-1; return(0); } } $this->connected_host=$this->host; $this->connected_user=$this->user; $this->connected_password=$this->password; $this->connected_port=$port; $this->opened_persistent=$this->persistent; return(1); } Function Close() { if($this->connection!=0) { if(IsSet($this->supported["Transactions"]) && !$this->auto_commit) $this->AutoCommitTransactions(1); mysql_Close($this->connection); $this->connection=0; $this->affected_rows=-1; } } Function Query($query) { $first=$this->first_selected_row; $limit=$this->selected_row_limit; $this->first_selected_row=$this->selected_row_limit=0; if(!strcmp($this->database_name,"")) { $this->last_error="It was not specified a valid database name to select"; return(0); } if(!$this->Connect()) return(0); if(($select=(substr(strtolower(ltrim($query)),0,strlen("select"))=="select")) && $limit>0) $query.=" LIMIT $first,$limit"; if(mysql_select_db($this->database_name,$this->connection) && ($result=mysql_query($query,$this->connection))) { if($select) $this->highest_fetched_row[$result]=-1; else $this->affected_rows=mysql_affected_rows($this->connection); } else $this->last_error=mysql_error($this->connection); return($result); } Function EndOfResult($result) { if(!IsSet($this->highest_fetched_row[$result])) { $this->last_error="attempted to check the end of an unknown result"; return(-1); } return($this->highest_fetched_row[$result]>=$this->NumberOfRows($result)-1); } Function FetchResult($result,$row,$field) { $this->highest_fetched_row[$result]=max($this->highest_fetched_row[$result],$row); return(mysql_result($result,$row,$field)); } Function FetchCLOBResult($result,$row,$field) { return($this->FetchLOBResult($result,$row,$field)); } Function FetchBLOBResult($result,$row,$field) { return($this->FetchLOBResult($result,$row,$field)); } Function FetchDecimalResult($result,$row,$field) { return(sprintf("%.".$this->decimal_places."f",doubleval($this->FetchResult($result,$row,$field))/$this->decimal_factor)); } Function NumberOfRows($result) { return(mysql_num_rows($result)); } Function FreeResult($result) { UnSet($this->highest_fetched_row[$result]); UnSet($this->columns[$result]); return(mysql_free_result($result)); } Function Error() { return($this->last_error); } Function CreateDatabase($name) { if(!$this->Connect()) return(0); if(!mysql_create_db($name,$this->connection)) { $this->last_error=mysql_error($this->connection); return(0); } return(1); } Function DropDatabase($name) { if(!$this->Connect()) return(0); if(!mysql_drop_db($name,$this->connection)) { $this->last_error=mysql_error($this->connection); return(0); } return(1); } Function GetCLOBFieldTypeDeclaration($name,&$field) { if(IsSet($field["length"])) { $length=$field["length"]; if($length<=255) $type="TINYTEXT"; else { if($length<=65535) $type="TEXT"; else { if($length<=16777215) $type="MEDIUMTEXT"; else $type="LONGTEXT"; } } } else $type="LONGTEXT"; return("$name $type".(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetBLOBFieldTypeDeclaration($name,&$field) { if(IsSet($field["length"])) { $length=$field["length"]; if($length<=255) $type="TINYBLOB"; else { if($length<=65535) $type="BLOB"; else { if($length<=16777215) $type="MEDIUMBLOB"; else $type="LONGBLOB"; } } } else $type="LONGBLOB"; return("$name $type".(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetIntegerFieldTypeDeclaration($name,&$field) { return("$name ".(IsSet($field["unsigned"]) ? "INT UNSIGNED" : "INT").(IsSet($field["default"]) ? " DEFAULT ".$field["default"] : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetDateFieldTypeDeclaration($name,&$field) { return($name." DATE".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetTimestampFieldTypeDeclaration($name,&$field) { return($name." DATETIME".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetTimeFieldTypeDeclaration($name,&$field) { return($name." TIME".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetFloatFieldTypeDeclaration($name,&$field) { if(IsSet($this->options["FixedFloat"])) $this->fixed_float=$this->options["FixedFloat"]; else { if($this->connection==0) $this->Connect(); } return("$name DOUBLE".($this->fixed_float ? "(".($this->fixed_float+2).",".$this->fixed_float.")" : "").(IsSet($field["default"]) ? " DEFAULT ".$this->GetFloatFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetDecimalFieldTypeDeclaration($name,&$field) { return("$name BIGINT".(IsSet($field["default"]) ? " DEFAULT ".$this->GetDecimalFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); } Function GetCLOBFieldValue($prepared_query,$parameter,$clob,&$value) { for($value="'";!MetabaseEndOfLOB($clob);) { if(MetabaseReadLOB($clob,$data,$this->lob_buffer_length)<0) { $value=""; $this->last_error=MetabaseLOBError($clob); return(0); } $this->EscapeText($data); $value.=$data; } $value.="'"; return(1); } Function FreeCLOBValue($prepared_query,$clob,&$value) { Unset($value); } Function GetBLOBFieldValue($prepared_query,$parameter,$blob,&$value) { for($value="'";!MetabaseEndOfLOB($blob);) { if(!MetabaseReadLOB($blob,$data,$this->lob_buffer_length)) { $value=""; $this->last_error=MetabaseLOBError($blob); return(0); } $value.=AddSlashes($data); } $value.="'"; return(1); } Function FreeBLOBValue($prepared_query,$blob,&$value) { Unset($value); } Function GetFloatFieldValue($value) { return(!strcmp($value,"NULL") ? "NULL" : "$value"); } Function GetDecimalFieldValue($value) { return(!strcmp($value,"NULL") ? "NULL" : strval(round(doubleval($value)*$this->decimal_factor))); } Function GetColumnNames($result,&$column_names) { $result_value=intval($result); if(!IsSet($this->highest_fetched_row[$result_value])) { $this->last_error="it was specified an inexisting result set"; return(0); } if(!IsSet($this->columns[$result_value])) { $this->columns[$result_value]=array(); $columns=mysql_num_fields($result); for($column=0;$column<$columns;$column++) $this->columns[$result_value][strtolower(mysql_field_name($result,$column))]=$column; } $column_names=$this->columns[$result_value]; return(1); } Function NumberOfColumns($result) { if(!IsSet($this->highest_fetched_row[intval($result)])) { $this->last_error="it was specified an inexisting result set"; return(-1); } return(mysql_num_fields($result)); } Function CreateTable($name,&$fields) { if(!IsSet($name) || !strcmp($name,"")) return("it was not specified a valid table name"); if(count($fields)==0) return("it were not specified any fields for table \"$name\""); $query_fields=""; if(!$this->GetFieldList($fields,$query_fields)) return(0); if(IsSet($this->supported["Transactions"])) $query_fields.=", dummy_primary_key INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (dummy_primary_key)"; return($this->Query("CREATE TABLE $name ($query_fields)".(IsSet($this->supported["Transactions"]) ? " TYPE=BDB" : ""))); } Function AlterTable($name,&$changes,$check) { if($check) { for($change=0,Reset($changes);$changelast_error="change type \"".Key($changes)."\" not yet supported"; return(0); } } return(1); } else { $query=(IsSet($changes["name"]) ? "RENAME AS ".$changes["name"] : ""); if(IsSet($changes["AddedFields"])) { $fields=$changes["AddedFields"]; for($field=0,Reset($fields);$fieldQuery("ALTER TABLE $name $query")); } } Function CreateSequence($name,$start) { if(!$this->Query("CREATE TABLE _sequence_$name (sequence INT DEFAULT 0 NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))")) return(0); if($start==1 || $this->Query("INSERT INTO _sequence_$name (sequence) VALUES (".($start-1).")")) return(1); $error=$this->last_error; if(!$this->Query("DROP TABLE _sequence_$name")) $this->warning="could not drop inconsistent sequence table"; $this->last_error=$error; return(0); } Function DropSequence($name) { return($this->Query("DROP TABLE _sequence_$name")); } Function GetSequenceNextValue($name,&$value) { if(!$this->Query("INSERT INTO _sequence_$name (sequence) VALUES (NULL)")) return(0); $value=intval(mysql_insert_id()); if(!$this->Query("DELETE FROM _sequence_$name WHERE sequence<$value")) $this->warning="could delete previous sequence table values"; return(1); } Function GetSequenceCurrentValue($name,&$value) { if(($result=$this->Query("SELECT MAX(sequence) FROM _sequence_$name"))==0) return(0); $value=intval($this->FetchResult($result,0,0)); $this->FreeResult($result); return(1); } Function CreateIndex($table,$name,$definition) { $query="ALTER TABLE $table ADD ".(IsSet($definition["unique"]) ? "UNIQUE" : "INDEX")." $name ("; for($field=0,Reset($definition["FIELDS"]);$field0) $query.=","; $query.=Key($definition["FIELDS"]); } $query.=")"; return($this->Query($query)); } Function DropIndex($table,$name) { return($this->Query("ALTER TABLE $table DROP INDEX $name")); } Function AutoCommitTransactions($auto_commit) { if(!IsSet($this->supported["Transactions"])) { $this->last_error="transactions are not in use"; return(0); } if(((!$this->auto_commit)==(!$auto_commit))) return(1); if($this->connection) { if($auto_commit) { if(!$this->Query("COMMIT") || !$this->Query("SET AUTOCOMMIT=1")) return(0); } else { if(!$this->Query("SET AUTOCOMMIT=0")) return(0); } } $this->auto_commit=$auto_commit; return(1); } Function CommitTransaction() { if(!IsSet($this->supported["Transactions"])) { $this->last_error="transactions are not in use"; return(0); } if($this->auto_commit) { $this->last_error="transaction changes are being auto commited"; return(0); } return($this->Query("COMMIT")); } Function RollbackTransaction() { if(!IsSet($this->supported["Transactions"])) { $this->last_error="transactions are not in use"; return(0); } if($this->auto_commit) { $this->last_error="transactions can not be rolled back when changes are auto commited"; return(0); } return($this->Query("ROLLBACK")); } Function Setup() { $this->supported["Sequences"]= $this->supported["Indexes"]= $this->supported["AffectedRows"]= $this->supported["SummaryFunctions"]= $this->supported["OrderByText"]= $this->supported["GetSequenceCurrentValue"]= $this->supported["SelectRowRanges"]= $this->supported["LOBs"]= 1; if(IsSet($this->options["UseTransactions"]) && $this->options["UseTransactions"]) $this->supported["Transactions"]=1; $this->decimal_factor=pow(10.0,$this->decimal_places); return(""); } }; } ?>