Changeset 316
- Timestamp:
- 03/01/07 18:22:27 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator (added)
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php (added)
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorException.php (added)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilder.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilder.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableColumn.php (added)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableDescription.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilder.php
r256 r316 98 98 * creates the query to create a table for the given class 99 99 * 100 * @param st ring $fqClassName the full qualified classname of the class to create the table for100 * @param stubDatabaseTableDescription $tableDescription 101 101 * @throws stubDatabaseQueryBuilderException 102 * @todo put all reflection related stuff to persistence package,103 * introduce a class that represents a table description104 102 */ 105 public function createTable( $fqClassName)103 public function createTable(stubDatabaseTableDescription $tableDescription) 106 104 { 107 $nqClassName = stubClassLoader::getNonQualifiedClassName($fqClassName); 108 if (class_exists($nqClassName, false) == false) { 109 stubClassLoader::load($fqClassName); 110 } 111 112 $refClass = new stubReflectionClass($nqClassName); 113 if ($refClass->hasAnnotation('DBTable') == false) { 114 throw new stubDatabaseQueryBuilderException('Class ' . $refClass->getName() . ' is missing the DBTable annotation.'); 115 } 116 117 $dbTable = $refClass->getAnnotation('DBTable'); 118 $tableName = $dbTable->getName(); 119 $query = 'CREATE TABLE `' . $tableName . "` (\n"; 120 $methods = $refClass->getMethods(); 105 $query = 'CREATE TABLE `' . $tableDescription->getName() . "` (\n"; 106 $columns = $tableDescription->getColumns(); 121 107 $primaryKeys = array(); 122 108 $keys = array(); 123 foreach ($methods as $method) { 124 if ($method->hasAnnotation('DBColumn') == false || $method->getDeclaringClass()->equals($refClass) == false) { 125 continue; 126 } elseif ($method->hasAnnotation('DBTable') == true && $method->getAnnotation('DBTable')->getName() != $tableName) { 127 continue; 109 foreach ($columns as $column) { 110 $query .= ' ' . $column->getName() . ' ' . $column->getType(); 111 if (strtoupper($column->getType()) != 'TEXT') { 112 $query .= '(' . $column->getSize() . ')'; 128 113 } 129 114 130 $dbColumn = $method->getAnnotation('DBColumn'); 131 $query .= ' ' . $dbColumn->getName() . ' ' . $dbColumn->getType(); 132 if (strtoupper($dbColumn->getType()) != 'TEXT') { 133 $query .= '(' . str_replace('_', ',', $dbColumn->getSize()) . ')'; 134 } 135 136 if ($dbColumn->isUnsigned() == true) { 115 if ($column->isUnsigned() == true) { 137 116 $query .= ' UNSIGNED'; 138 117 } 139 118 140 if ($ dbColumn->isNullable() == true && $dbColumn->getDefaultValue() == null) {119 if ($column->isNullable() == true && $column->getDefaultValue() == null) { 141 120 $query .= ' DEFAULT NULL'; 142 121 } else { 143 if ($ dbColumn->isNullable() == false) {122 if ($column->isNullable() == false) { 144 123 $query .= ' NOT NULL'; 145 124 } 146 125 147 if ($ dbColumn->getDefaultValue() !== null) {148 $query .= " DEFAULT '" . $ dbColumn->getDefaultValue() . "'";126 if ($column->getDefaultValue() !== null) { 127 $query .= " DEFAULT '" . $column->getDefaultValue() . "'"; 149 128 } 150 129 } 151 130 152 if ($ dbColumn->isPrimaryKey() == true) {131 if ($column->isPrimaryKey() == true) { 153 132 $query .= ' AUTO_INCREMENT'; 154 $primaryKeys[] = $ dbColumn->getName();155 } elseif ($ dbColumn->isKey() == true) {156 $keys[] = $ dbColumn->getName();133 $primaryKeys[] = $column->getName(); 134 } elseif ($column->isKey() == true) { 135 $keys[] = $column->getName(); 157 136 } 158 137 … … 172 151 } 173 152 174 $query .= "\n) TYPE=" . $ dbTable->getType();175 if ($ dbTable->hasCharacterSet() == true) {176 $query .= ' CHARACTER SET=' . $ dbTable->getCharacterSet();153 $query .= "\n) TYPE=" . $tableDescription->getType(); 154 if ($tableDescription->hasCharacterSet() == true) { 155 $query .= ' CHARACTER SET=' . $tableDescription->getCharacterSet(); 177 156 } 178 157 179 if ($ dbTable->hasCollation() == true) {180 $query .= ' COLLATE=' . $ dbTable->getCollation();158 if ($tableDescription->hasCollation() == true) { 159 $query .= ' COLLATE=' . $tableDescription->getCollation(); 181 160 } 182 161 183 if ($ dbTable->hasComment() == true) {184 $query .= ' COMMENT=' . $ dbTable->getComment();162 if ($tableDescription->hasComment() == true) { 163 $query .= ' COMMENT=' . $tableDescription->getComment(); 185 164 } 186 165 trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilder.php
r256 r316 9 9 stubClassLoader::load('net.stubbles.rdbms.criteria.stubCriterion', 10 10 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderException', 11 'net.stubbles.rdbms.querybuilder.stubDatabaseTableDescription', 11 12 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 12 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 13 'net.stubbles.reflection.reflection' 13 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation' 14 14 ); 15 15 /** … … 49 49 * creates the query to create a table for the given class 50 50 * 51 * @param st ring $fqClassName the full qualified classname of the class to create the table for51 * @param stubDatabaseTableDescription $tableDescription 52 52 * @throws stubDatabaseQueryBuilderException 53 53 */ 54 public function createTable( $fqClassName);54 public function createTable(stubDatabaseTableDescription $tableDescription); 55 55 } 56 56 ?>
