ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:dictionary:structure:addattribute

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
v5:dictionary:structure:addattribute [2016/04/07 02:01] – created mnewnhamv5:dictionary:structure:addattribute [2016/04/10 02:41] (current) mnewnham
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
 <WRAP right box> <WRAP right box>
-**From Version 5.21.0**+**From Version 5.21.0-beta-2**
 == See Also == == See Also ==
 [[v5:dictionary:structure:introduction|Structured Schema Management]]\\ [[v5:dictionary:structure:introduction|Structured Schema Management]]\\
Line 13: Line 13:
       )       )
 </WRAP> </WRAP>
-The method ''addAttribute()'' is part of the **//Structured Schema Management//** suite, and adds an attribute to the current [[v5:dictionary:stucture:metaobjectstructure|metaObjectStructure]] object. +The method ''addAttribute()'' is part of the **//Structured Schema Management//** suite, and adds an attribute to the current metaObjectStructure object. 
  
 An attribute can be added to any of the available structure types, table,column,index or index-item.  An attribute can be added to any of the available structure types, table,column,index or index-item. 
Line 25: Line 25:
 The priority parameter can optionally force the system to process the attributes in a specific order. If not provided, the attributes are processed in the order provided. The priority parameter can optionally force the system to process the attributes in a specific order. If not provided, the attributes are processed in the order provided.
  
 +===== Usage =====
 +In the following example, a table **//employees//** is to be created. The table is to be used in a portable environment, and require 2 platform specific attribute.
 +  - Under **MySQL**, an engine type needs to be specified.
 +  - Under **IBM DB2**,  a tablespace needs to be specified.
  
 <code php> <code php>
Line 44: Line 48:
   ->addAttribute(array('TABLESPACE'=>'LARGE1'),'db2');   ->addAttribute(array('TABLESPACE'=>'LARGE1'),'db2');
 </code> </code>
-<WRAP clear />+===== Understanding Attributes ===== 
 +==== Portable Attributes ==== 
 +Portable attributes are comparable to the options available to the original [[v5:dictionary:column_attributes|Column Attributes]] in **//Simple Schema Management//**.  
 + 
 +  * If a feature is **portable**, no platform designation is generally necessary but can still be used 
 +  * Portable options may have a **//priority//** automatically assigned, which may control the order of definitions. In particular, the priority will force the NOTNULL and DEFAULT attributes to conform to the ANSI SQL order. 
 + 
 +==== Non-Portable (Custom) Attributes ==== 
 +A custom attribute is one that must be defined fully in order to be handled correctly by the DBMS. In principle, any option not all ready defined as portable in the [[v5:dictionary:column_attributes|Column Attributes]] list is a custom attribute. 
 + 
 +In the following example, we create a column, and add 3 attributes to it. 1 attribute is portable and 2 are custom. 
 + 
 +<code php> 
 +/* 
 +* We define a metaObjectStructure, that represents 
 +* the table 'employees' 
 +*/ 
 + 
 +$dict = NewDataDictionary($db); 
 +$t = new metaObjectStructure($dict,'employees'); 
 + 
 +/* 
 +* We now add an object representing the column, 'somecolumn' of type Character(32) 
 +*/ 
 +$c = $t->addColumnObject('somecolumn','C(32)'); 
 + 
 +/* 
 +* now add a portable attribute DEFAULT 
 +* This could also be passed as a string "DEFAULT 'SOMEVALUE'"
 +*/ 
 +$c->addAttribute(array('DEFAULT'=>'SOMEVALUE'); 
 + 
 +/* 
 +* now add a custom attribute to column, applicable to all platforms 
 +*/ 
 +$c->addAttribute(array('CHARACTER SET'=>'"ascii"')); 
 + 
 +/* 
 +* Now add another attribute, which is only used if the database type is SQL Server 
 +*/ 
 +$c->addAttribute('SPARSE','mssqlnative'); 
 + 
 + 
 +/* 
 +* This object is now passed to the addColumnSql method 
 +*/ 
 +$sql = $dict->addColumnSql($t) 
 +</code> 
 + 
 +If ADOdb was attached to a MySQL database, the sql returned would be: 
 + 
 +  ALTER TABLE employees ADD somecolumn VARCHAR(32)  
 +                             DEFAULT 'SOME VALUE'  
 +                             CHARACTER SET "ascii" 
 + 
 +but if attached to a SQL Server database, it would appear as: 
 + 
 +  ALTER TABLE employees ADD somecolumn VARCHAR(32)  
 +                            DEFAULT "SOME VALUE"  
 +                      CHARACTER SET "ascii"  
 +                             SPARSE 
v5/dictionary/structure/addattribute.1459987291.txt.gz · Last modified: 2017/04/21 11:29 (external edit)