OCI8TableInfo Class Reference

Inheritance diagram for OCI8TableInfo:

TableInfo

List of all members.

Public Member Functions

 __construct (OCI8DatabaseInfo $database, $name)

Protected Member Functions

 initColumns ()
 Loads the columns for this table.
 initPrimaryKey ()
 Loads the primary key information for this table.
 initIndexes ()
 Loads the indexes for this table.
 initForeignKeys ()
 Load foreign keys.

Private Attributes

 $schema


Detailed Description

Definition at line 32 of file OCI8TableInfo.php.


Constructor & Destructor Documentation

OCI8TableInfo::__construct ( OCI8DatabaseInfo database,
name 
)

Definition at line 36 of file OCI8TableInfo.php.

References TableInfo::$name, and OCI8DatabaseInfo::getSchema().

00037     {
00038         $this->schema = strtoupper( $database->getSchema() );
00039         parent::__construct($database, $name);
00040     $this->name = strtoupper( $this->name );
00041     }


Member Function Documentation

OCI8TableInfo::initColumns (  )  [protected]

Loads the columns for this table.

Reimplemented from TableInfo.

Definition at line 44 of file OCI8TableInfo.php.

References OCI8Types::getType().

Referenced by initForeignKeys(), initIndexes(), and initPrimaryKey().

00045     {
00046         
00047         include_once 'creole/metadata/ColumnInfo.php';
00048         include_once 'creole/drivers/oracle/OCI8Types.php';
00049         
00050 
00051         // To get all of the attributes we need, we'll actually do 
00052         // two separate queries.  The first gets names and default values
00053         // the second will fill in some more details.
00054         
00055         $sql = "
00056       SELECT column_name
00057         , data_type
00058         , data_precision
00059         , data_length
00060         , data_default
00061         , nullable 
00062         , data_scale
00063             FROM  all_tab_columns
00064             WHERE table_name = '{$this->name}'
00065                 AND OWNER = '{$this->schema}'";
00066 
00067         $statement = @oci_parse($this->conn->getResource(),$sql);
00068         $success = @oci_execute($statement,OCI_DEFAULT);
00069     if (!$success) {
00070             throw new SQLException("Could Not Get Columns");
00071         }
00072 
00073         while ( $statement && $row = oci_fetch_array( $statement
00074       , OCI_ASSOC + OCI_RETURN_NULLS ) ) {
00075             $row = array_change_key_case($row, CASE_LOWER);
00076             $this->columns[$row['column_name']] = new ColumnInfo( $this
00077         , $row['column_name']
00078         , OCI8Types::getType($row['data_type'])
00079         , $row['data_type']
00080         , $row['data_length']
00081         , $row['data_precision']
00082         , $row['data_scale']
00083         , $row['nullable']
00084         , $row['data_default']
00085       );
00086         }
00087                 
00088         $this->colsLoaded = true;
00089     }

OCI8TableInfo::initForeignKeys (  )  [protected]

Load foreign keys.

Reimplemented from TableInfo.

Definition at line 187 of file OCI8TableInfo.php.

References TableInfo::$name, ForeignKeyInfo::CASCADE, initColumns(), ForeignKeyInfo::NONE, and ForeignKeyInfo::SETNULL.

00187                                          {
00188         
00189         include_once 'creole/metadata/ForeignKeyInfo.php';    
00190 
00191         // columns have to be loaded first
00192         if (!$this->colsLoaded) $this->initColumns();        
00193         
00194         // Foreign keys
00195     // TODO resolve cross schema references
00196     // use all_cons... to do so, however, very slow queries then
00197     // optimizations are very ugly
00198         $sql          = "
00199       SELECT a.owner AS local_owner
00200         , a.table_name AS local_table
00201         , c.column_name AS local_column
00202         , a.constraint_name AS foreign_key_name
00203         , b.owner AS foreign_owner
00204         , b.table_name AS foreign_table
00205         , d.column_name AS foreign_column
00206         , b.constraint_name AS foreign_constraint_name
00207         , a.delete_rule AS on_delete
00208             FROM user_constraints a
00209         , user_constraints b
00210         , user_cons_columns c
00211         , user_cons_columns d
00212             WHERE a.r_constraint_name = b.constraint_name
00213                 AND c.constraint_name = a.constraint_name
00214                 AND d.constraint_name = b.constraint_name
00215                 AND a.r_owner = b.owner
00216                 AND a.constraint_type='R'
00217         AND a.table_name = '{$this->name}'
00218         AND a.owner = '{$this->schema}'
00219     ";
00220 
00221         $statement = @oci_parse($this->conn->getResource(),$sql);
00222         $success = @oci_execute($statement,OCI_DEFAULT);
00223         if (!$success) {
00224             throw new SQLException("Could Not Get Primary Keys");
00225         }
00226 
00227         // Loop through the returned results, grouping the same key_name
00228     // together adding each column for that key.
00229 
00230         while ( $statement && $row = oci_fetch_assoc( $statement )) {
00231             $row = array_change_key_case($row,CASE_LOWER);
00232 
00233             $name = $row['foreign_key_name'];            
00234 
00235             $foreignTable = $this->database->getTable($row['foreign_table']);
00236             $foreignColumn = $foreignTable->getColumn($row['foreign_column']);
00237 
00238             $localTable   = $this->database->getTable($row['local_table']);    
00239             $localColumn   = $localTable->getColumn($row['local_column']);
00240 
00241             if (!isset($this->foreignKeys[$name])) {
00242                 $this->foreignKeys[$name] = new ForeignKeyInfo($name);
00243             }
00244 
00245       switch ( $row[ 'on_delete' ] )
00246       {
00247         case 'CASCADE':
00248           $onDelete = ForeignKeyInfo::CASCADE;
00249           break;
00250 
00251         case 'SET NULL':
00252           $onDelete = ForeignKeyInfo::SETNULL;
00253           break;
00254 
00255         default:
00256         case 'NO ACTION':
00257           $onDelete = ForeignKeyInfo::NONE;
00258           break;
00259       }
00260 
00261       // addReference( local, foreign, onDelete, onUpdate )
00262       // Oracle doesn't support 'on update'
00263             $this->foreignKeys[ $name ]->addReference(
00264         $localColumn
00265         , $foreignColumn
00266         , $onDelete
00267       );
00268         }
00269         
00270         $this->fksLoaded = true;
00271     }

OCI8TableInfo::initIndexes (  )  [protected]

Loads the indexes for this table.

Reimplemented from TableInfo.

Definition at line 133 of file OCI8TableInfo.php.

References TableInfo::$name, TableInfo::indexesLoaded(), and initColumns().

00133                                      {
00134     
00135         include_once 'creole/metadata/IndexInfo.php';    
00136 
00137         // columns have to be loaded first
00138         if (!$this->colsLoaded) $this->initColumns();        
00139 
00140         // Indexes
00141         $sql = "SELECT
00142             allind.index_name,
00143             allind.table_name,
00144             allind.index_type,
00145             allind.uniqueness,
00146             indcol.column_name
00147             FROM all_indexes allind INNER JOIN all_ind_columns indcol
00148                 ON allind.owner = indcol.index_owner
00149                 AND allind.index_name = indcol.index_name
00150             WHERE allind.table_owner = '{$this->schema}'
00151             AND allind.table_name = '{$this->name}'
00152             AND allind.index_name NOT IN (SELECT
00153                     constraint_name
00154                     FROM all_constraints
00155                     WHERE constraint_type = 'P')
00156             ORDER BY allind.index_name,
00157                 indcol.column_position";
00158 
00159         $statement = @oci_parse($this->conn->getResource(),$sql);
00160         $success = @oci_execute($statement,OCI_DEFAULT);
00161         if (!$success) {
00162             throw new SQLException("Could Not Get Primary Keys");
00163         }
00164 
00165 
00166         // Loop through the returned results, grouping the same key_name together
00167         // adding each column for that key.
00168 
00169         while ( $statement && $row = oci_fetch_assoc( $statement )) {
00170             $row = array_change_key_case($row,CASE_LOWER);
00171 
00172             $name = $row['index_name'];
00173             $index_col_name = $row['column_name'];
00174 
00175             if (!isset($this->indexes[$name])) {
00176                 $this->indexes[$name] = new IndexInfo($name);
00177             }
00178 
00179             $this->indexes[$name]->addColumn($this->columns[ $index_col_name ]);
00180         }
00181         
00182                 
00183         $this->indexesLoaded = true;
00184     }

OCI8TableInfo::initPrimaryKey (  )  [protected]

Loads the primary key information for this table.

Reimplemented from TableInfo.

Definition at line 92 of file OCI8TableInfo.php.

References TableInfo::$name, and initColumns().

00093     {
00094         include_once 'creole/metadata/PrimaryKeyInfo.php';
00095         
00096         // columns have to be loaded first
00097         if (!$this->colsLoaded) $this->initColumns();
00098         
00099 
00100         // Primary Keys Query
00101         $sql = "SELECT a.owner, a.table_name,
00102                             a.constraint_name, a.column_name
00103                         FROM all_cons_columns a, all_constraints b
00104                         WHERE b.constraint_type = 'P'
00105                         AND a.constraint_name = b.constraint_name
00106                         AND b.table_name = '{$this->name}'
00107             AND b.owner = '{$this->schema}'
00108             ";
00109 
00110 
00111         $statement = @oci_parse($this->conn->getResource(),$sql);
00112         $success = @oci_execute($statement,OCI_DEFAULT);
00113         if (!$success) {
00114             throw new SQLException("Could Not Get Primary Keys");
00115         }
00116 
00117         while ( $statement && $row = oci_fetch_assoc( $statement )) {
00118             $row = array_change_key_case($row,CASE_LOWER);
00119 
00120             $name = $row['column_name'];
00121 
00122             if (!isset($this->primaryKey)) {
00123                 $this->primaryKey = new PrimaryKeyInfo($name);
00124             }
00125 
00126             $this->primaryKey->addColumn($this->columns[$name]);
00127         }
00128         
00129         $this->pkLoaded = true;
00130     }


Member Data Documentation

OCI8TableInfo::$schema [private]

Definition at line 34 of file OCI8TableInfo.php.


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

Generated on Wed May 6 23:10:50 2009 for fareofficelib by  doxygen 1.5.8