table_name = $table_name; $this->db = &ADONewConnection($this->db_type); $result = $this->db->Pconnect($this->db_host_name,$this->db_user_name,$this->db_user_pass,$this->db_name); } /* * Turns on debug messages */ function debug_on() { $this->db->debug = true; } /* * Turns off debug messages */ function debug_off() { $this->db->debug = false; } /* * Sets a key -> value pair representing an aspect of the current record */ function set_attribute($attribute, $value) { if ( $attribute == 'id' ) { $this->id = $value; } else { $this->attr[$attribute] = $value; } } /* * Commit changes to the database */ function execute($sql) { $results = $this->db->Execute($sql); return $results; } /* * Get a record matching a given id or return a blank record. Used by save() method. */ function get_recordset($id='') { if ( $id == '' ) { $sql = "SELECT * FROM " . $this->table_name . " WHERE (id=-1)"; } else { $sql = "SELECT * FROM " . $this->table_name . " WHERE (id=$id)"; } $results = $this->db->Execute($sql); return $results; } /* * Get the results of a SQL statement as a simple array */ function return_array($sql) { global $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $results = $this->db->Execute($sql); if ($results) { return $results->GetArray(); } else { return false; } } /* * Get the results of a SQL statement as an associative array */ function return_assoc($sql) { global $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $results = $this->db->Execute($sql); if ($results) { return $results->GetAssoc(); } else { return false; } } /* * Returns the count of records in a table with optional where clause limit */ function count_records($field='',$value='') { $sql = "SELECT COUNT(*) FROM " . $this->table_name; if ($field != '' && $value != '') { if (ereg("^[0-9]+$",$value)) { $sql .= " WHERE ($field=$value)"; } else { $sql .= " WHERE ($field='$value')"; } } $results = $this->db->Execute($sql); $results = $results->GetRows(); return $results[0]['COUNT(*)']; } /* * Return a single record (as opposed to set of records) by id as an * associative array. Used in the login function in 'master.inc' file. */ function get_record_by_id($id) { $sql = "SELECT * FROM " . $this->table_name . " WHERE (id=$id)"; $results = $this->return_array($sql); return $results[0]; } /* * Returns a single record with optional where clause limit */ function get_record($field='',$value='') { global $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $sql = "SELECT * FROM " . $this->table_name; if ($field != '' && $value != '') { if (ereg("^[0-9]+$",$value)) { $sql .= " WHERE ($field=$value)"; } else { $sql .= " WHERE ($field='$value')"; } } $results = $this->return_array($sql); return $results[0]; } /* * Returns an array of records with optional where clause limit */ function get_records($field='',$value='',$order='') { $sql = "SELECT * FROM " . $this->table_name; if ($field != '' && $value != '') { if (ereg("^[0-9]+$",$value)) { $sql .= " WHERE ($field=$value)"; } else { $sql .= " WHERE ($field='$value')"; } } if ($order != '') { $sql .= " ORDER BY $order"; } $results = $this->return_array($sql); return $results; } /* * Returns an associative array of records with optional where clause limit */ function get_records_assoc($field='',$value='',$order='') { $sql = "SELECT * FROM " . $this->table_name; if ($field != '' && $value != '') { if (ereg("^[0-9]+$",$value)) { $sql .= " WHERE ($field=$value)"; } else { $sql .= " WHERE ($field='$value')"; } } if ($order != '') { $sql .= " ORDER BY $order"; } $results = $this->return_assoc($sql); return $results; } /* * Returns an array with a where clause that calculates based on year for * the given time/date field in the database */ function get_records_by_year($field='',$year='') { $sql = "SELECT * FROM " . $this->table_name; $sql .= " WHERE (EXTRACT(YEAR FROM $field)=$year)"; $sql .= " ORDER BY $field DESC"; $results = $this->return_array($sql); return $results; } /* * Saves the data in the current recordset */ function save() { if ($this->id) { $rs = $this->get_recordset($this->id); $sql = $this->db->GetUpdateSQL($rs, $this->attr); } else { $rs = $this->get_recordset(); $sql = $this->db->GetInsertSQL($rs, $this->attr); } if ($sql) { $results = $this->db->Execute($sql); return $results; } return true; } /* * Deletes a record by id */ function delete_record_by_id($id) { $sql = "DELETE FROM " . $this->table_name . " WHERE (id=$id)"; $result = $this->db->execute($sql); if ($result) { return true; } else { return $result; } } /* * Deletes records based on where clause matching */ function delete_records($field,$value) { $sql = "DELETE FROM " . $this->table_name . ""; if ($field != '' && $value != '') { if (ereg("^[0-9]+$",$value)) { $sql .= " WHERE ($field=$value)"; } else { $sql .= " WHERE ($field='$value')"; } } $result = $this->db->execute($sql); if ($result) { return true; } else { return $result; } } } ?>