Skip to content

Commit

Permalink
sqlsrv
Browse files Browse the repository at this point in the history
  • Loading branch information
Fi1osof committed Mar 9, 2015
1 parent a8028c2 commit 58516f0
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
class CmpGenerator extends xPDOSimpleObject {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
require_once (dirname(dirname(__FILE__)) . '/cmpgenerator.class.php');
class CmpGenerator_sqlsrv extends CmpGenerator {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
$xpdo_meta_map['CmpGenerator']= array (
'package' => 'cmpgenerator',
'table' => 'cmpgenerator',
'fields' =>
array (
'package' => '',
'database' => '',
'tables' => '',
'table_prefix' => '',
'build_scheme' => '1',
'build_package' => '1',
'create_date' => NULL,
'last_ran' => NULL,
),
'fieldMeta' =>
array (
'package' =>
array (
'dbtype' => 'nvarchar',
'precision' => '255',
'phptype' => 'string',
'null' => true,
'default' => '',
),
'database' =>
array (
'dbtype' => 'nvarchar',
'precision' => '128',
'phptype' => 'string',
'null' => true,
'default' => '',
),
'tables' =>
array (
'dbtype' => 'nvarchar',
'precision' => 'max',
'phptype' => 'string',
'null' => true,
'default' => '',
),
'table_prefix' =>
array (
'dbtype' => 'nvarchar',
'precision' => '255',
'phptype' => 'string',
'null' => true,
'default' => '',
),
'build_scheme' =>
array (
'dbtype' => 'int',
'precision' => '1',
'phptype' => 'boolean',
'null' => true,
'default' => '1',
),
'build_package' =>
array (
'dbtype' => 'int',
'precision' => '1',
'phptype' => 'boolean',
'null' => true,
'default' => '1',
),
'create_date' =>
array (
'dbtype' => 'datetime',
'phptype' => 'datetime',
'null' => true,
),
'last_ran' =>
array (
'dbtype' => 'datetime',
'phptype' => 'datetime',
'null' => true,
),
),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/**
* MySQL classes for generating xPDOObject classes and maps from an xPDO schema.
*
* @package xpdo
* @subpackage om.mysql
*/

/**
* Include the parent {@link xPDOGenerator} class.
*/
include_once(XPDO_CORE_PATH . 'om/' . $modx->config['dbtype'] . '/xpdogenerator.class.php');

/**
* An extension for generating {@link xPDOObject} class and map files for MySQL.
*
* A MySQL-specific extension to an {@link xPDOManager} instance that can
* generate class stub and meta-data map files from a provided XML schema of a
* database structure.
*
* @package xpdo
* @subpackage om.mysql
*/
class my_xPDOGenerator_sqlsrv extends xPDOGenerator_sqlsrv {
/**
* an array of allowed tables
* @var array
*/
protected $allowed_tables;

/**
* active data base to connect to
* @var (String) $database
*/
protected $databaseName;

/**
* set the database
*
*/
public function setDatabase($database=NULL) {
if (empty($database) ) {
$this->databaseName = $this->manager->xpdo->escape($this->manager->xpdo->config['dbname']);
} else {
$this->databaseName = $database;
}
}

/**
* set the allowed tables
*
*/
public function setAllowedTables(array $tables=array()) {
$this->allowed_tables = $tables;
/*
echo '<br>Table Array: ';
print_r($tables);
echo '<br>';
*/
}
/**
* This only generates scheme files for specified tables rather then the entire database
*
* Write an xPDO XML Schema from your database.
*
* @param string $schemaFile The name (including path) of the schemaFile you
* want to write.
* @param string $package Name of the package to generate the classes in.
* @param string $baseClass The class which all classes in the package will
* extend; by default this is set to {@link xPDOObject} and any
* auto_increment fields with the column name 'id' will extend {@link
* xPDOSimpleObject} automatically.
* @param string $tablePrefix The table prefix for the current connection,
* which will be removed from all of the generated class and table names.
* Specify a prefix when creating a new {@link xPDO} instance to recreate
* the tables with the same prefix, but still use the generic class names.
* @param boolean $restrictPrefix Only reverse-engineer tables that have the
* specified tablePrefix; if tablePrefix is empty, this is ignored.
* @return boolean True on success, false on failure.
*/
public function writeTableSchema($schemaFile, $package= '', $baseClass= '', $tablePrefix= '', $restrictPrefix= false) {


//print $schemaFile;

// print $baseClass;

// exit;
//$tablePrefix = 'modx_user';
// $restrictPrefix = false;
// $tablePrefix = '';
return parent::writeSchema($schemaFile, $package, $baseClass, $tablePrefix, $restrictPrefix);


// writeSchema
exit;
if (empty ($package))
$package= $this->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[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xmlContent[] = "<model package=\"{$package}\" baseClass=\"{$baseClass}\" platform=\"mysql\" defaultEngine=\"MyISAM\" version=\"{$schemaVersion}\">";
//read list of tables
$dbname = $this->databaseName;
//$this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Database name: ' . $dbname);
$tableLike= ($tablePrefix && $restrictPrefix) ? " LIKE '{$tablePrefix}%'" : '';
$tablesStmt= $this->manager->xpdo->prepare("SHOW TABLES FROM {$dbname}{$tableLike}");
$tablesStmt->execute();
$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();
// the only thing added to this function the rest is copied:
if ( !in_array($table[0],$this->allowed_tables) ) {
//echo '<br>No Table: '.$table[0];
//$this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'CMPGenerator->my_oPDO0->writeTableSchema -> No Table: '.$table[0]);
continue;
}
//echo '<br>Table: '. $table[0];
//$this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'CMPGenerator->my_oPDO0->writeTableSchema -> Table: '.$table[0].' - Pre: '.$tablePrefix.' - Restrict: '.$restrictPrefix );

// End custom
if (!$tableName= $this->getTableName($table[0], $tablePrefix, $restrictPrefix)) {
continue;
}
$class= $this->getClassName($tableName);
$extends= $baseClass;
$sql = 'SHOW COLUMNS FROM '.$this->manager->xpdo->escape($dbname).'.'.$this->manager->xpdo->escape($table[0]);
//$this->manager->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Line: '.__LINE__.' Sql: '.$sql);
$fieldsStmt= $this->manager->xpdo->query($sql);
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<field key=\"{$Field}\" dbtype=\"{$dbType}\"{$Precision}{$attributes} phptype=\"{$PhpType}\"{$Null}{$Default}{$Key}{$Extra} />";
}
} 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($dbname).'.'.$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<column key=\"{$column['Column_name']}\" length=\"{$column['Sub_part']}\" collation=\"{$column['Collation']}\" null=\"{$null}\" />";
}
$xmlIndices[]= "\t\t<index alias=\"{$keyName}\" name=\"{$keyName}\" primary=\"{$primary}\" unique=\"{$unique}\" type=\"{$type}\" >";
$xmlIndices[]= implode("\n", $xmlIndexCols);
$xmlIndices[]= "\t\t</index>";
}
} 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<object class=\"{$class}\" table=\"{$tableName}\" extends=\"{$extends}\">";
$xmlObject[] = implode("\n", $xmlFields);
if (!empty($xmlIndices)) {
$xmlObject[] = '';
$xmlObject[] = implode("\n", $xmlIndices);
}
$xmlObject[] = "\t</object>";
$xmlContent[] = implode("\n", $xmlObject);
}
$xmlContent[] = "</model>";
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function rmdir_files($dir) {
'en' => MODX_CORE_PATH.'components/'.$package_name.'/lexicon/en/',
'model' => MODX_CORE_PATH.'components/'.$package_name.'/model/',
'my_model' => MODX_CORE_PATH.'components/'.$package_name.'/model/'.$package_name.'/',
'mysql' => MODX_CORE_PATH.'components/'.$package_name.'/model/'.$package_name.'/mysql',
$modx->config['dbtype'] => MODX_CORE_PATH.'components/'.$package_name.'/model/'.$package_name.'/'.$modx->config['dbtype'],
'request' => MODX_CORE_PATH.'components/'.$package_name.'/model/request/',
'processors' => MODX_CORE_PATH.'components/'.$package_name.'/processors/',
'processors_mgr' => MODX_CORE_PATH.'components/'.$package_name.'/processors/mgr/',
Expand Down Expand Up @@ -168,7 +168,7 @@ function rmdir_files($dir) {
$generator->setAllowedTables($my_tables);


$xml_schema_file = $directories['my_model'].$package_name.'.mysql.schema.xml';
$xml_schema_file = $directories['my_model'].$package_name.'.'.$modx->config['dbtype'].'.schema.xml';
// (re)Build the schema file
// echo 'Scheme: '.$cmp->get('build_scheme');
if ( $cmp->get('build_scheme') ) {
Expand All @@ -185,7 +185,7 @@ function rmdir_files($dir) {
// (re)Build the table classes(package):
if ( $cmp->get('build_package') ) { // package
// delete any old/current class and map keys:
rmdir_files($directories['mysql']);
rmdir_files($directories[$modx->config['dbtype']]);
// now create the class and map keys:
$generator->parseSchema($xml_schema_file, $directories['model']);
}
Expand Down

0 comments on commit 58516f0

Please sign in to comment.