1: <?php
2:
3: namespace webfilesframework\core\datasystem\database;
4:
5:
6:
7:
8: 9: 10: 11: 12: 13:
14: class MDatabaseTable
15: {
16:
17: var $name = null;
18:
19: var $primaryKey = null;
20: var $identifier = null;
21: var $identifierSize = null;
22:
23: var $columns = array();
24:
25: var $databaseConnection;
26:
27: 28: 29: 30: 31:
32: public function __construct(MDatabaseConnection $databaseConnection, $name)
33: {
34: $this->databaseConnection = $databaseConnection;
35: $this->name = $name;
36: }
37:
38:
39: 40: 41:
42: public function create()
43: {
44: $query = "CREATE TABLE IF NOT EXISTS `" . $this->name . "` (";
45:
46: if ($this->identifier != null && $this->identifierSize != null) {
47: $query .= "`" . $this->identifier . "` int(" . $this->identifierSize . ") NOT NULL AUTO_INCREMENT,";
48: }
49: 50: 51:
52: foreach ($this->columns as $value) {
53: $query .= $value->getStringRepresentation();
54: }
55:
56: if ($this->primaryKey != null) {
57: $query .= "PRIMARY KEY (`" . $this->primaryKey . "`)";
58: }
59: $query .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
60:
61: $this->databaseConnection->query($query);
62: }
63:
64:
65: 66: 67: 68:
69: public function drop()
70: {
71: $this->databaseConnection->query(
72: "DROP TABLE IF EXISTS `" . $this->name . "`"
73: );
74: }
75:
76:
77: 78: 79: 80: 81:
82: public function specifyIdentifier($columnName, $size)
83: {
84: $this->identifier = $columnName;
85: $this->identifierSize = $size;
86: $this->setPrimaryKey($columnName);
87: }
88:
89:
90: 91: 92: 93:
94: public function setPrimaryKey($columnName)
95: {
96: $this->primaryKey = $columnName;
97: }
98:
99:
100: 101: 102: 103: 104: 105: 106:
107: public function addColumn($name, $type, $length = null)
108: {
109: $column = new MDatabaseTableColumn($name, $type, $length);
110: array_push($this->columns, $column);
111: }
112:
113: 114: 115:
116: public function addColumnObject($column) {
117: array_push($this->columns,$column);
118: }
119:
120:
121: }