Re: common source for CREATE TABLE and HTML INPUT widgets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here's some code to do just that.
You will probably want to hand-tweak it.
<?php

class type
{
	var $type;
	function type($ty)
	{
		$this->type = $ty;
	}
	function create_sql()
	{
		return $this->type;
	}
	function create_html()
	{
		return "text";
	}
	function get_length()
	{
		return 30;
	}
}

class char extends type
{
	var $length;
	function char($len, $ty = "varchar")
	{
		$this->length = $len;
		$this->type($ty);
	}
	function create_sql()
	{
		return type::create_sql() . "(" . $this->length . ")";
	}
	function get_length()
	{
		return $this->length;
	}
}

class text extends type
{
	var $length;
	function text($ty = "text")
	{
		switch ($ty)
		{
			case 'text':
				$len = 65536;
				break;
			default:
				die("Unknown text type $ty\n");
		}
		$this->length = $len;
		$this->type($ty);
	}
	function create_sql()
	{
		return type::create_sql();
	}
	function get_length()
	{
		return $this->length;
	}
}

class scalar extends type
{
	var $field_width;
	var $unsigned;
	function scalar($ty = "int", $fw = 6, $un = 0)
	{
		$this->type($ty);
		$this->field_width = $fw;
		$this->unsigned = $un;
	}
	function create_sql()
	{
		return type::create_sql() . "(" . $this->field_width . ")" .
			($this->unsigned ? " UNSIGNED" : "");
	}
}

class enum extends type
{
	var $values;
	function enum($vals)
	{
		$this->values = $vals;
		$this->type("enum");
	}
	function create_sql()
	{
		function todq($val) { return '"' . quotemeta($val) . '"'; }
		$a = array_map("todq", $this->values);
		$j = implode(", ", $a);
		return type::create_sql() . "($j)";
	}
}

class column
{
	var $form_name;
	var $db_name;
	var $type;
	var $primary_key;
	var $null;
	# NOTE: column order preserved in primary keys
	function column($ty, $dn, $fn = 0, $pk = 0, $n = 1)
	{
		$this->type = $ty;
		if (empty($fn))
		{
			$fn = strtr($dn, "_", " ");
			$fn = ucwords($fn);
		}
		$this->form_name = $fn;
		$this->db_name = $dn;
		if (($pk == 1) && ($n == 1))
			die ("Primary keys cannot be null");
		$this->primary_key = $pk;
		$this->null = $n;
	}
	function create_sql()
	{
		return($this->db_name . " " . $this->type->create_sql() .
		       ($this->null ? "" : " NOT NULL"));
	}
	function create_html()
	{
		return "$this->form_name<BR><INPUT NAME=$this->db_name TYPE=" .
		       $this->type->create_html() . " MAXLENGTH=" .
		       $this->type->get_length() . ">";
	}
	function create_php()
	{
		return "clean(\$HTTP_POST_VARS[\"$this->db_name\"], " .
		       $this->type->get_length() . ");\n";
	}
}

class table
{
	var $columns;
	var $name;
	function table($nm, $cols)
	{
		$this->name = $nm;
		$this->columns = $cols;
	}
	function create_sql()
	{
		$j = "";
		function tosql($ob) { return $ob->create_sql(); }
		$a = array_map("tosql", $this->columns);
		$j .= implode(", ", $a);
		# NOTE: column order preserved in primary keys
		function findpk($col) { return $col->primary_key; }
		$a = array_filter($this->columns, "findpk");
		if (count($a) > 0) {
			function pknames($col) { return $col->db_name; }
			$a = array_map("pknames", $a);
			$j .= ", PRIMARY_KEY(" . implode(", ", $a) . ")";
		}
		return "CREATE TABLE $this->name ($j);";
	}
	function create_html($script, $submit_val, $numcols = 3)
	{
		$h = "<FORM method=post action=\"$script\">\n";
		$h .= "<TABLE border=1>";
		$column = 0;
		foreach ($this->columns as $col)
		{
			if (($column % $numcols) == 0)
				$h .= "\t<TR>\n";
			$h .= "\t\t<TD>" . $col->create_html() . "</TD>\n";
			if (($column % $numcols) == ($numcols - 1))
				$h .= "\t</TR>\n";
			$column = (($column + 1) % $numcols);
		}
		$h .= "</TABLE>";
		$h .= "<INPUT type=submit name=submit value=$submit_val>";
		$h .= "</FORM>";
		return $h;
	}
	function create_php()
	{
		$p = "";
		foreach($this->columns as $col)
		{
			$p .= $col->create_php();
		}
		return $p;
	}
}

$sample_table = new table("sample", array(
	new column (new scalar("int", 5, 1), "id", "ID", 1, 0),
	new column (new char(30), "name"),
	new column (new text("text"), "comments")
	));

echo $sample_table->create_html("sample_add.php", "add");
echo "<BR>";
echo $sample_table->create_php();

?>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux