Doctrine 1.2.4
Doctrine_Import_Mssql Class Reference

Inherits Doctrine_Import.

Public Member Functions

 databaseExists ($database)
 
 functionExists ($function)
 
 getConnection ()
 
 getModuleName ()
 
 importSchema ($directory, array $connections=array(), array $options=array())
 
 listDatabases ()
 
 listFunctions ()
 
 listSequences ($database=null)
 
 listTableColumns ($table)
 
 listTableConstraints ($table)
 
 listTableIndexes ($table)
 
 listTableRelations ($tableName)
 
 listTables ($database=null)
 
 listTableTriggers ($table)
 
 listTableViews ($table)
 
 listTriggers ($database=null)
 
 listUsers ()
 
 listViews ($database=null)
 
 sequenceExists ($sequence, $database=null)
 
 tableColumnExists ($column, $table)
 
 tableConstraintExists ($constraint, $table)
 
 tableExists ($table, $database=null)
 
 tableIndexExists ($index, $table)
 
 tableTriggerExists ($trigger, $table)
 
 tableViewExists ($view, $table)
 
 triggerExists ($trigger, $database=null)
 
 userExists ($user)
 
 viewExists ($view, $database=null)
 

Detailed Description

Definition at line 34 of file Mssql.php.

Member Function Documentation

Doctrine_Import::databaseExists (   $database)
inherited

checks if a database exists

Parameters
string$database
Returns
boolean

Definition at line 220 of file Import.php.

{
return in_array($database, $this->listDatabases());
}
Doctrine_Import::functionExists (   $function)
inherited

checks if a function exists

Parameters
string$function
Returns
boolean

Definition at line 231 of file Import.php.

{
return in_array($function, $this->listFunctions());
}
Doctrine_Connection_Module::getConnection ( )
inherited

getConnection returns the connection object this module uses

Returns
Doctrine_Connection

Definition at line 68 of file Module.php.

{
return $this->conn;
}
Doctrine_Connection_Module::getModuleName ( )
inherited

getModuleName returns the name of this module

Returns
string the name of this module

Definition at line 79 of file Module.php.

{
return $this->moduleName;
}
Doctrine_Import::importSchema (   $directory,
array  $connections = array(),
array  $options = array() 
)
inherited

importSchema

method for importing existing schema to Doctrine_Record classes

Parameters
string$directory
array$connectionsArray of connection names to generate models for
Returns
array the names of the imported classes

Definition at line 364 of file Import.php.

{
$classes = array();
foreach ($manager as $name => $connection) {
// Limit the databases to the ones specified by $connections.
// Check only happens if array is not empty
if ( ! empty($connections) && ! in_array($name, $connections)) {
continue;
}
$builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory);
$builder->setOptions($options);
$definitions = array();
foreach ($connection->import->listTables() as $table) {
$definition = array();
$definition['tableName'] = $table;
$definition['className'] = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table));
$definition['columns'] = $connection->import->listTableColumns($table);
$definition['connection'] = $connection->getName();
$definition['connectionClassName'] = $definition['className'];
try {
$definition['relations'] = array();
$relations = $connection->import->listTableRelations($table);
$relClasses = array();
foreach ($relations as $relation) {
$table = $relation['table'];
if (in_array($class, $relClasses)) {
$alias = $class . '_' . (count($relClasses) + 1);
} else {
$alias = $class;
}
$relClasses[] = $class;
$definition['relations'][$alias] = array(
'alias' => $alias,
'class' => $class,
'local' => $relation['local'],
'foreign' => $relation['foreign']
);
}
} catch (Exception $e) {}
$definitions[strtolower($definition['className'])] = $definition;
$classes[] = $definition['className'];
}
// Build opposite end of relationships
foreach ($definitions as $definition) {
$className = $definition['className'];
$relClasses = array();
foreach ($definition['relations'] as $alias => $relation) {
if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) {
$alias = $className . '_' . (count($relClasses) + 1);
} else {
$alias = $className;
}
$relClasses[] = $relation['class'];
$definitions[strtolower($relation['class'])]['relations'][$alias] = array(
'alias' => $alias,
'class' => $className,
'local' => $relation['foreign'],
'foreign' => $relation['local']
);
}
}
// Build records
foreach ($definitions as $definition) {
$builder->buildRecord($definition);
}
}
return $classes;
}
Doctrine_Import::listDatabases ( )
inherited

lists all databases

Returns
array

Definition at line 46 of file Import.php.

{
if ( ! isset($this->sql['listDatabases'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listDatabases']);
}
Doctrine_Import::listFunctions ( )
inherited

lists all availible database functions

Returns
array

Definition at line 60 of file Import.php.

{
if ( ! isset($this->sql['listFunctions'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listFunctions']);
}
Doctrine_Import_Mssql::listSequences (   $database = null)

lists all database sequences

Parameters
string | null$database
Returns
array

Definition at line 42 of file Mssql.php.

{
$query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
$tableNames = $this->conn->fetchColumn($query);
return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
}
Doctrine_Import_Mssql::listTableColumns (   $table)

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 90 of file Mssql.php.

{
$sql = 'EXEC sp_primary_keys_rowset @table_name = ' . $this->conn->quoteIdentifier($table, true);
$result = $this->conn->fetchAssoc($sql);
$primary = array();
foreach ($result as $key => $val) {
$primary[] = $val['COLUMN_NAME'];
}
$sql = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
$result = $this->conn->fetchAssoc($sql);
$columns = array();
foreach ($result as $key => $val) {
$val = array_change_key_case($val, CASE_LOWER);
if (strstr($val['type_name'], ' ')) {
list($type, $identity) = explode(' ', $val['type_name']);
} else {
$type = $val['type_name'];
$identity = '';
}
if ($type == 'varchar') {
$type .= '(' . $val['length'] . ')';
}
$val['type'] = $type;
$val['identity'] = $identity;
$decl = $this->conn->dataDict->getPortableDeclaration($val);
$isIdentity = (bool) (strtoupper(trim($identity)) == 'IDENTITY');
$isNullable = (bool) (strtoupper(trim($val['is_nullable'])) == 'NO');
$isPrimary = in_array($val['column_name'], $primary);
$description = array(
'name' => $val['column_name'],
'ntype' => $type,
'type' => $decl['type'][0],
'alltypes' => $decl['type'],
'length' => $decl['length'],
'fixed' => (bool) $decl['fixed'],
'unsigned' => (bool) $decl['unsigned'],
'notnull' => $isIdentity ? true : $isNullable,
'default' => $val['column_def'],
'primary' => $isPrimary,
'autoincrement' => $isIdentity,
);
$columns[$val['column_name']] = $description;
}
return $columns;
}
Doctrine_Import::listTableConstraints (   $table)
inherited

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 101 of file Import.php.

{
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
Doctrine_Import_Mssql::listTableIndexes (   $table)

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 151 of file Mssql.php.

{
}
Doctrine_Import_Mssql::listTableRelations (   $tableName)

lists table relations

Expects an array of this format to be returned with all the relationships in it where the key is the name of the foreign table, and the value is an array containing the local and foreign column name

Array ( [groups] => Array ( [local] => group_id [foreign] => id ) )

Parameters
string$tabledatabase table name
Returns
array

Definition at line 69 of file Mssql.php.

{
$relations = array();
$sql = 'SELECT o1.name as table_name, c1.name as column_name, o2.name as referenced_table_name, c2.name as referenced_column_name, s.name as constraint_name FROM sysforeignkeys fk inner join sysobjects o1 on fk.fkeyid = o1.id inner join sysobjects o2 on fk.rkeyid = o2.id inner join syscolumns c1 on c1.id = o1.id and c1.colid = fk.fkey inner join syscolumns c2 on c2.id = o2.id and c2.colid = fk.rkey inner join sysobjects s on fk.constid = s.id AND o1.name = \'' . $tableName . '\'';
$results = $this->conn->fetchAssoc($sql);
foreach ($results as $result)
{
$result = array_change_key_case($result, CASE_LOWER);
$relations[] = array('table' => $result['referenced_table_name'],
'local' => $result['column_name'],
'foreign' => $result['referenced_column_name']);
}
return $relations;
}
Doctrine_Import_Mssql::listTables (   $database = null)

lists tables

Parameters
string | null$database
Returns
array

Definition at line 162 of file Mssql.php.

{
$sql = "SELECT name FROM sysobjects WHERE type = 'U' AND name <> 'dtproperties' AND name <> 'sysdiagrams' ORDER BY name";
return $this->conn->fetchColumn($sql);
}
Doctrine_Import_Mssql::listTableTriggers (   $table)

lists table triggers

Parameters
string$tabledatabase table name
Returns
array

Definition at line 189 of file Mssql.php.

{
$table = $this->conn->quote($table, 'text');
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $this->conn->quoteIdentifier($table, true);
$result = $this->conn->fetchColumn($query);
return $result;
}
Doctrine_Import_Mssql::listTableViews (   $table)

lists table views

Parameters
string$tabledatabase table name
Returns
array

Definition at line 205 of file Mssql.php.

{
$keyName = 'INDEX_NAME';
$pkName = 'PK_NAME';
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
$keyName = strtolower($keyName);
$pkName = strtolower($pkName);
} else {
$keyName = strtoupper($keyName);
$pkName = strtoupper($pkName);
}
}
$table = $this->conn->quote($table, 'text');
$query = 'EXEC sp_statistics @table_name = ' . $this->conn->quoteIdentifier($table, true);
$indexes = $this->conn->fetchColumn($query, $keyName);
$query = 'EXEC sp_pkeys @table_name = ' . $this->conn->quoteIdentifier($table, true);
$pkAll = $this->conn->fetchColumn($query, $pkName);
$result = array();
foreach ($indexes as $index) {
if ( ! in_array($index, $pkAll) && $index != null) {
$result[] = $this->conn->formatter->fixIndexName($index);
}
}
return $result;
}
Doctrine_Import_Mssql::listTriggers (   $database = null)

lists all triggers

Returns
array

Definition at line 174 of file Mssql.php.

{
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
$result = $this->conn->fetchColumn($query);
return $result;
}
Doctrine_Import::listUsers ( )
inherited

lists database users

Returns
array

Definition at line 190 of file Import.php.

{
if ( ! isset($this->sql['listUsers'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listUsers']);
}
Doctrine_Import_Mssql::listViews (   $database = null)

lists database views

Parameters
string | null$database
Returns
array

Definition at line 242 of file Mssql.php.

{
$query = "SELECT name FROM sysobjects WHERE xtype = 'V'";
return $this->conn->fetchColumn($query);
}
Doctrine_Import::sequenceExists (   $sequence,
  $database = null 
)
inherited

checks if a sequence exists

Parameters
string$sequence
string | null$database
Returns
boolean

Definition at line 255 of file Import.php.

{
return in_array($sequence, $this->listSequences($database));
}
Doctrine_Import::tableColumnExists (   $column,
  $table 
)
inherited

checks if a table column exists

Parameters
string$column
string$tabledatabase table name
Returns
boolean

Definition at line 279 of file Import.php.

{
return in_array($column, $this->listTableColumns($table));
}
Doctrine_Import::tableConstraintExists (   $constraint,
  $table 
)
inherited

checks if a table constraint exists

Parameters
string$constraint
string$tabledatabase table name
Returns
boolean

Definition at line 267 of file Import.php.

{
return in_array($constraint, $this->listTableConstraints($table));
}
Doctrine_Import::tableExists (   $table,
  $database = null 
)
inherited

checks if a table exists

Parameters
string$table
string | null$database
Returns
boolean

Definition at line 303 of file Import.php.

{
return in_array($table, $this->listTables($database));
}
Doctrine_Import::tableIndexExists (   $index,
  $table 
)
inherited

checks if a table index exists

Parameters
string$index
string$tabledatabase table name
Returns
boolean

Definition at line 291 of file Import.php.

{
return in_array($index, $this->listTableIndexes($table));
}
Doctrine_Import::tableTriggerExists (   $trigger,
  $table 
)
inherited

checks if a table trigger exists

Parameters
string$trigger
string$tabledatabase table name
Returns
boolean

Definition at line 315 of file Import.php.

{
return in_array($trigger, $this->listTableTriggers($table));
}
Doctrine_Import::tableViewExists (   $view,
  $table 
)
inherited

checks if a table view exists

Parameters
string$view
string$tabledatabase table name
Returns
boolean

Definition at line 327 of file Import.php.

{
return in_array($view, $this->listTableViews($table));
}
Doctrine_Import::triggerExists (   $trigger,
  $database = null 
)
inherited

checks if a trigger exists

Parameters
string$trigger
string | null$database
Returns
boolean

Definition at line 243 of file Import.php.

{
return in_array($trigger, $this->listTriggers($database));
}
Doctrine_Import::userExists (   $user)
inherited

checks if a user exists

Parameters
string$user
Returns
boolean

Definition at line 338 of file Import.php.

{
return in_array($user, $this->listUsers());
}
Doctrine_Import::viewExists (   $view,
  $database = null 
)
inherited

checks if a view exists

Parameters
string$view
string | null$database
Returns
boolean

Definition at line 350 of file Import.php.

{
return in_array($view, $this->listViews($database));
}

The documentation for this class was generated from the following file: