constraint_cnt = 0;
$this->column_cnt = 0;
$this->key_cnt = 0;
$this->name = $aName;
}
function addKey ($aKeyName, $aColumnName, $isPrimary) {
if (!strpos($aColumnName, ",")) {
// referenced column must be part of the table
if (in_array($aColumnName, $this->getColumnNames())) {
if ($this->getNumberOfKeys() == 0 || !$this->keyExists($aKeyName, $aColumnName)) {
$this->key[$this->key_cnt] = new database_table_key($aKeyName, $aColumnName, $isPrimary);
$index = $this->key_cnt;
$this->key_cnt ++;
return $index;
}
else {
echo "Key '" . $aKeyName . "' already in table!
";
}
}
else {
echo "Error when adding a new key: Column '" . $aColumnName . "' must be from table '" . $this->getName() . "'!
";
}
}
else {
$keyArray = explode(",", $aColumnName);
$inArray = true;
for ($i=0; $i < count($keyArray); $i++) {
if (!in_array($keyArray[$i], $this->getColumnNames())) {
$inArray = false;
}
}
if ($inArray) {
if ($this->getNumberOfKeys() == 0 || !$this->keyExists($aKeyName, $aColumnName)) {
$this->key[$this->key_cnt] = new database_table_key($aKeyName, $aColumnName, $isPrimary);
$index = $this->key_cnt;
$this->key_cnt ++;
return $index;
}
else {
echo "Key '" . $aKeyName . "' already in table!
";
}
}
}
}
function addConstraint($conSymbol, $conColumn, $conTableName, $conTableColumnName, $conOnDelete, $conOnUpdate) {
if (!strpos($conColumn, ",")&& !strpos($conTableColumnName, ",")) {
// referenced column must be part of the table
if (in_array($conColumn, $this->getColumnNames())) {
$newConstr = new database_table_constraint($conSymbol, $conColumn, $conTableName, $conTableColumnName, $conOnDelete, $conOnUpdate);
$conSymbol = "";
#print_r($newConstr);
#print_r($this->getAllConstraints());
if (!in_array($newConstr, $this->getAllConstraints())) {
$this->constraint[$this->constraint_cnt] = $newConstr;
$index = $this->constraint_cnt;
$this->constraint_cnt ++;
return $index;
}
}
else {
echo "Error when adding a new constraint: Column '" . $conColumn . "' must be from table '" . $this->getName() . "'!
";
}
}
else {
$inArray = true;
$keyArray = explode(",", $conColumn);
for ($i=0; $i < count($keyArray); $i++) {
if (!in_array($keyArray[$i], $this->getColumnNames())) {
$inArray = false;
}
}
$keyArray = explode(",", $conTableColumnName);
for ($i=0; $i < count($keyArray); $i++) {
if (!in_array($keyArray[$i], $this->getColumnNames())) {
$inArray = false;
}
}
if ($inArray) {
$this->constraint[$this->constraint_cnt] = $newConstr;
$index = $this->constraint_cnt;
$this->constraint_cnt ++;
return $index;
}
}
return false;
}
function addColumn ($aColumnName) {
if ($this->getNumberOfColumns() == 0 || !in_array($aColumnName, $this->getColumnNames())) {
$this->column[$this->column_cnt] = new database_table_column($aColumnName);
$index = $this->column_cnt;
$this->column_cnt ++;
return $index;
}
else {
echo "Column '" . $aColumnName . "' already in table!
";
}
}
function getName(){
return $this->name;
}
function getKeyAtIndex($anIndex){
return $this->key[$anIndex];
}
function getAllKeys(){
$keyArray = array();
for ($i=0; $i<$this->getNumberOfKeys(); $i++) {
$key_i = $this->getKeyAtIndex($i);
$keyArray[$i] = $key_i->getKey();
}
return $keyArray;
}
function getAllConstraints(){
$conArray = array();
for ($i=0; $i<$this->getNumberOfConstraints(); $i++) {
$con_i = $this->getConstraintAtIndex($i);
$conArray[$i] = $con_i->getConstraint();
}
return $conArray;
}
function getConstraintAtIndex($anIndex){
return $this->constraint[$anIndex];
}
function getColumnAtIndex($anIndex){
return $this->column[$anIndex];
}
function getColumnWithName($aName){
return $this->getColumnAtIndex($this->getColumnIndex($aName));
}
function getType() {
return $this->type;
}
function getMysql() {
$sql = "";
$sql .= "CREATE TABLE " . $this->getName() . " (\n";
// add the columns
for ($i=0; $i<$this->getNumberOfColumns(); $i++) {
$sql .= "\t" . $this->column[$i]->getName() . " ";
$sql .= $this->column[$i]->getType() . " ";
$sql .= $this->column[$i]->getAttributes();
$sql .= ",";
$sql .= "\n";
}
// add keys
for ($i=0; $i<$this->getNumberOfKeys(); $i++) {
if ($this->key[$i]->isPrimary()) {
$sql .= "\tPRIMARY KEY ";
}
else {
$sql .= "\tKEY ";
}
$sql .= $this->key[$i]->getKeyName() . " ";
$sql .= "(" . $this->key[$i]->getColumnName() . "),\n";
}
// add constraints
for ($i=0; $i<$this->getNumberOfConstraints(); $i++) {
$constr = array();
$constr = $this->constraint[$i]->getConstraint();
$sql .= "\tCONSTRAINT";
// constraint name
if ($constr[0]) {
$sql .= " " . $constr[0];
}
if ($constr[1]) {
$sql .= " FOREIGN KEY (" . $constr[1] . ")";
if ($constr[2] && $constr[3]) {
$sql .= " REFERENCES " . $constr[2] . " (" . $constr[3] . ")";
if ($constr[4]) {
$sql .= " ON DELETE " . $constr[4];
}
if ($constr[5]) {
$sql .= " ON UPDATE " . $constr[5];
}
}
}
$sql .= ",\n";
}
// remove last ","
$sql = substr($sql, 0, strlen($sql)-2) . "\n";
// add the table type
$sql .= ") TYPE = " . $this->getType() . "\n\n";
return $sql;
}
function getColumnNames() {
$columns = array();
for ($i=0; $i<$this->column_cnt; $i++) {
$columns[$i] = $this->column[$i]->getName();
}
if ($this->column_cnt>0) {
return $columns;
}
else {
return false;
}
}
function getColumnIndex($aColumnName) {
for ($i=0; $i<$this->column_cnt; $i++) {
if ($aColumnName == $this->column[$i]->getName()) {
return $i;
}
}
return false;
}
function getKeyIndex($aKeyName) {
for ($i=0; $i<$this->key_cnt; $i++) {
if ($aKeyName == $this->key[$i]->getKeyName()) {
return $i;
}
}
return false;
}
function getConstraintIndex($aConstraintName) {
for ($i=0; $i<$this->constraint_cnt; $i++) {
if ($aConstraintName == $this->constraint[$i]->getName()) {
return $i;
}
}
return false;
}
function getNumberOfColumns () {
return $this->column_cnt;
}
function getNumberOfConstraints () {
return $this->constraint_cnt;
}
function getNumberOfKeys () {
return $this->key_cnt;
}
function destroyTable() {
settype(&$this, 'null');
}
//FIXME
function keyExists ($aKeyName, $aColumnName) {
return false;
}
//FIXME
function constraintExists ($aConstraintName, $aColumnName) {
return false;
}
function setType($aType) {
$this->type = $aType;
}
function setColumnTypeAtIndex($aColumnType, $aColumnIndex) {
if ($aColumnIndex < $this->column_cnt) {
$this->column[$aColumnIndex]->setType($aColumnType);
}
else {
echo "column index out of range!";
}
}
function setColumnAttributesAtIndex($someColumnAttributes, $aColumnIndex) {
if ($aColumnIndex < $this->column_cnt) {
$this->column[$aColumnIndex]->setAttributes($someColumnAttributes);
}
else {
echo "column index out of range!";
}
}
function setColumnDefaultAtIndex($someColumnDefault, $aColumnIndex) {
if ($aColumnIndex < $this->column_cnt) {
$this->column[$aColumnIndex]->setDefaultValue($someColumnDefault);
}
else {
echo "column index out of range!";
}
}
function removeColumnWithName($aColumnName){
$index = $this->getColumnIndex($aColumnName);
$this->removeColumnAtIndex($index);
}
function removeKeyWithName($aKeyName){
$index = $this->getKeyIndex($aKeyName);
$this->removeKeyAtIndex($index);
}
function removeKeyAtIndex ($aKeyIndex) {
if ($aKeyIndex < $this->key_cnt && $aKeyIndex >= 0) {
$this->key[$aKeyIndex]->destroyKey();
//if key is not only key and not last key
if ($this->key_cnt > 1 && $aKeyIndex != $this->key_cnt-1) {
//fill gap in key array with last entry
$this->key[$aKeyIndex] = $this->key[$this->key_cnt-1];
$this->key[$this->key_cnt-1] = null;
}
$this->key_cnt--;
}
else {
echo "Key '" . $aKeyIndex . "' not in database!
";
}
}
function removeConstraintWithName($aConstraintName){
$index = $this->getConstraintIndex($aConstraintName);
$this->removeConstraintAtIndex($index);
}
function removeConstraintAtIndex ($aConstraintIndex) {
if ($aConstraintIndex < $this->constraint_cnt && $aConstraintIndex >= 0) {
$this->constraint[$aConstraintIndex]->destroyConstraint();
//if constraint is not only constraint and not last constraint
if ($this->constraint_cnt > 1 && $aConstraintIndex != $this->constraint_cnt-1) {
//fill gap in constraint array with last entry
$this->constraint[$aConstraintIndex] = $this->constraint[$this->constraint_cnt-1];
$this->constraint[$this->constraint_cnt-1] = null;
}
$this->constraint_cnt--;
}
else {
echo "Constraint '" . $aConstraintIndex . "' not in database!
";
}
}
function removeColumnAtIndex ($aColumnIndex) {
if ($aColumnIndex < $this->column_cnt && $aColumnIndex >= 0) {
$this->column[$aColumnIndex]->destroyColumn();
//if column is not only column and not last column
if ($this->column_cnt > 1 && $aColumnIndex != $this->column_cnt-1) {
//fill gap in column array with last entry
$this->column[$aColumnIndex] = $this->column[$this->column_cnt-1];
$this->column[$this->column_cnt-1] = null;
}
$this->column_cnt--;
}
else {
echo "Column '" . $aColumnIndex . "' not in database!
";
}
}
}
?>