manager->xpdo->package; if (empty ($baseClass)) $baseClass= 'xPDOObject'; if (empty ($tablePrefix)) $tablePrefix= $this->manager->xpdo->config[xPDO::OPT_TABLE_PREFIX]; $schemaVersion = xPDO::SCHEMA_VERSION; $xmlContent = array(); $xmlContent[] = ""; $xmlContent[] = ""; //read list of tables $dbname= $this->manager->xpdo->escape($this->manager->xpdo->config['dbname']); $tableLike= ($tablePrefix && $restrictPrefix) ? " LIKE '{$tablePrefix}%'" : ''; $tablesStmt= $this->manager->xpdo->prepare("SHOW TABLES FROM {$dbname}{$tableLike}"); $tstart = microtime(true); $tablesStmt->execute(); $this->manager->xpdo->queryTime += microtime(true) - $tstart; $this->manager->xpdo->executedQueries++; $tables= $tablesStmt->fetchAll(PDO::FETCH_NUM); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, print_r($tables, true)); foreach ($tables as $table) { $xmlObject= array(); $xmlFields= array(); $xmlIndices= array(); if (!$tableName= $this->getTableName($table[0], $tablePrefix, $restrictPrefix)) { continue; } $class= $this->getClassName($tableName); $extends= $baseClass; $fieldsStmt= $this->manager->xpdo->query('SHOW COLUMNS FROM ' . $this->manager->xpdo->escape($table[0])); if ($fieldsStmt) { $fields= $fieldsStmt->fetchAll(PDO::FETCH_ASSOC); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, print_r($fields, true)); if (!empty($fields)) { foreach ($fields as $field) { $Field= ''; $Type= ''; $Null= ''; $Key= ''; $Default= ''; $Extra= ''; extract($field, EXTR_OVERWRITE); $Type= xPDO :: escSplit(' ', $Type, "'", 2); $precisionPos= strpos($Type[0], '('); $dbType= $precisionPos? substr($Type[0], 0, $precisionPos): $Type[0]; $dbType= strtolower($dbType); $Precision= $precisionPos? substr($Type[0], $precisionPos + 1, strrpos($Type[0], ')') - ($precisionPos + 1)): ''; if (!empty ($Precision)) { $Precision= ' precision="' . trim($Precision) . '"'; } $attributes= ''; if (isset ($Type[1]) && !empty ($Type[1])) { $attributes= ' attributes="' . trim($Type[1]) . '"'; } $PhpType= $this->manager->xpdo->driver->getPhpType($dbType); $Null= ' null="' . (($Null === 'NO') ? 'false' : 'true') . '"'; $Key= $this->getIndex($Key); $Default= $this->getDefault($Default); if (!empty ($Extra)) { if ($Extra === 'auto_increment') { if ($baseClass === 'xPDOObject' && $Field === 'id') { $extends= 'xPDOSimpleObject'; continue; } else { $Extra= ' generated="native"'; } } else { $Extra= ' extra="' . strtolower($Extra) . '"'; } $Extra= ' ' . $Extra; } $xmlFields[] = "\t\t"; } } else { $this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'No columns were found in table ' . $table[0]); } } else { $this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Error retrieving columns for table ' . $table[0]); } $whereClause= ($extends === 'xPDOSimpleObject' ? " WHERE `Key_name` != 'PRIMARY'" : ''); $indexesStmt= $this->manager->xpdo->query('SHOW INDEXES FROM ' . $this->manager->xpdo->escape($table[0]) . $whereClause); if ($indexesStmt) { $indexes= $indexesStmt->fetchAll(PDO::FETCH_ASSOC); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Indices for table {$table[0]}: " . print_r($indexes, true)); if (!empty($indexes)) { $indices = array(); foreach ($indexes as $index) { if (!array_key_exists($index['Key_name'], $indices)) $indices[$index['Key_name']] = array(); $indices[$index['Key_name']][$index['Seq_in_index']] = $index; } foreach ($indices as $index) { $xmlIndexCols = array(); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Details of index: " . print_r($index, true)); foreach ($index as $columnSeq => $column) { if ($columnSeq == 1) { $keyName = $column['Key_name']; $primary = $keyName == 'PRIMARY' ? 'true' : 'false'; $unique = empty($column['Non_unique']) ? 'true' : 'false'; $packed = empty($column['Packed']) ? 'false' : 'true'; $type = $column['Index_type']; } $null = $column['Null'] == 'YES' ? 'true' : 'false'; $xmlIndexCols[]= "\t\t\t"; } $xmlIndices[]= "\t\t"; $xmlIndices[]= implode("\n", $xmlIndexCols); $xmlIndices[]= "\t\t"; } } else { $this->manager->xpdo->log(xPDO::LOG_LEVEL_WARN, 'No indexes were found in table ' . $table[0]); } } else { $this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Error getting indexes for table ' . $table[0]); } $xmlObject[] = "\t"; $xmlObject[] = implode("\n", $xmlFields); if (!empty($xmlIndices)) { $xmlObject[] = ''; $xmlObject[] = implode("\n", $xmlIndices); } $xmlObject[] = "\t"; $xmlContent[] = implode("\n", $xmlObject); } $xmlContent[] = ""; if ($this->manager->xpdo->getDebug() === true) { $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, implode("\n", $xmlContent)); } $file= fopen($schemaFile, 'wb'); $written= fwrite($file, implode("\n", $xmlContent)); fclose($file); return true; } }