<?php
/* Classes for database schema.
*
* Written by: Chris Studholme
* Copyright: GPL (http://www.fsf.org/copyleft/gpl.html)
* $Id: db_schema.php,v 1.1 2003/12/23 06:07:15 cstudhol Exp $
*/
class db_field {
var $name; // field name
var $type; // associative array of types (one per db type)
function db_field($name,$type) {
$this->name = $name;
if (is_array($type))
$this->type = $type;
else
$this->type = array($type);
}
function get_type() {
global $db_type;
if ($this->type[$db_type])
return $this->type[$db_type];
return $this->type[0];
}
}
class db_table {
var $name; // table name
var $fields; // array of fields
var $pkey; // array primary key fields
var $index; // array of other fields to index
function db_table($name,$pkey,$index,$fields) {
$this->name = $name;
$this->fields = $fields;
$this->pkey = $pkey;
$this->index = $index;
}
function find_field($aname) {
for ($i=0; $i<count($this->fields); ++$i) {
$field=$this->fields[$i];
if (strcasecmp($field->name,$aname)==0)
return $field;
}
return false;
}
function create_query() {
$result = "CREATE TABLE ".$this->name." (";
for ($i=0; $i<count($this->fields); ++$i) {
$field=$this->fields[$i];
$result .= $field->name." ".$field->get_type().", ";
}
$result .= "PRIMARY KEY (".$this->pkey[0];
for ($i=1; $i<count($this->pkey); ++$i)
$result .= ",".$this->pkey[$i];
$result .= "));";
return $result;
}
function index_queries() {
$result=array();
for ($i=0; $i<count($this->index); ++$i)
$result[] = "CREATE INDEX ".$this->name."_index_".$this->index[$i].
" ON ".$this->name." (".$this->index[$i].");";
return $result;
}
function add_field_query($field) {
return "ALTER TABLE ".$this->name.
" ADD COLUMN ".$field->name." ".$field->get_type().";";
}
}
?>