phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


ADOdb: Data Dictionary

Tag: adodb, database, database layer   Category: PHP Database
post: 30 Oct 2007 read: 1,543


When you create table using sql statement, may be you still can not set free from sql command. For example, in mysql we can use auto_increment for id (integer as primary key). Other databases may be can not recognize auto_increment. ADOdb provide solution of this problem.

Here is example ( from manual ):


include_once('adodb.inc.php');

# First create a normal connection
$db->NewADOConnection('mysql');
$db->Connect(...);

# Then create a data dictionary object, 
#using this connection
$dict = NewDataDictionary($db);

# We have a portable declarative data 
#dictionary format in ADOdb 3.50, 
#similar to SQL.
# Field types use 1 character codes, 
#and fields are separated by commas.
# The following example creates three 
#fields: "col1", "col2" and "col3":

$flds = " 
 col1 C(32) NOTNULL DEFAULT 'abc',
 col2 I  DEFAULT 0,
 col3 N(12.2)";

# We demonstrate creating tables and indexes

$sqlarray = $dict->CreateTableSQL($tabname, $flds, $taboptarray);
$dict->ExecuteSQLArray($sqlarray);
$idxflds = 'co11, col2';
$sqlarray = $dict->CreateIndexSQL($idxname, $tabname, $idxflds);
$dict->ExecuteSQLArray($sqlarray);

Currently the following databases are supported: Well-tested: PostgreSQL, MySQL, Oracle, MSSQL.
Beta-quality: DB2, Informix, Sybase, Interbase, Firebird.
Alpha-quality: MS Access (does not support DEFAULT values) and generic ODBC.

function

function CreateDatabase($dbname, $optionsarray=false) 
function CreateTableSQL($tabname, $fldarray, $taboptarray=false) 

Legal portable type codes include:

C: varchar
X: Largest varchar size
XL: For Oracle, returns CLOB, otherwise same as 'X' above
C2: Multibyte varchar
X2: Multibyte varchar (largest size)
B: BLOB (binary large object)
D: Date (some databases do not support this, and we return a datetime type)
T: Datetime or Timestamp
L: Integer field suitable for storing booleans (0 or 1)
I: Integer (mapped to I4)
I1: 1-byte integer
I2: 2-byte integer
I4: 4-byte integer
I8: 8-byte integer
F: Floating point number
N: Numeric or decimal number

The $colsize field represents the size of the field. If a decimal number is used, then it is assumed that the number following the dot is the precision, so 6.2 means a number of size 6 digits and 2 decimal places. It is recommended that the default for number types be represented as a string to avoid any rounding errors.

The $otheroptions include the following keywords (case-insensitive):


AUTO			For autoincrement number. 
                        Emulated with triggers if not
                        available.
			Sets NOTNULL also.
AUTOINCREMENT	Same as auto.
KEY			Primary key field. 
                        Sets NOTNULL also. 
                        Compound keys are supported.
PRIMARY 		Same as KEY.
DEF			Synonym for DEFAULT for lazy typists.
DEFAULT		The default value. 
                Character strings are auto-quoted 
                unless the string begins and ends 
                with spaces, eg ' SYSDATE '.
NOTNULL		If field is not null.
DEFDATE		Set default value to call function 
                to get today's date.
DEFTIMESTAMP	Set default to call function 
                to get today's datetime.
NOQUOTE		Prevents autoquoting of default 
                string values.
CONSTRAINTS	Additional constraints defined at 
                the end of the field definition.

The Data Dictonary accepts two formats, the older array specification:

$flds = array(
	array('COLNAME', 'DECIMAL', '8.4', 'DEFAULT' => 0, 'NotNull'),
	array('ID',      'I'      , 'AUTO'),
	array('MYDATE',  'D'      , 'DEFDATE'),
	array('NAME',    'C'      ,'32', 
		  'CONSTRAINTS' => 'FOREIGN KEY 
                             REFERENCES reftable')
);


Series this article:
ADOdb: Introduction
ADOdb: Connection Statement
ADOdb: Advance Select Statement
ADOdb: How to show tables
ADOdb: How to show fields
ADOdb: How to show databases
ADOdb: Caching of Recordset
ADOdb: Recordset to HTML
ADOdb: Multi Database Connection
ADOdb: Data Dictionary
ADOdb: Quick Export Data
ADOdb: Insert data style
ADOdb: Replace Data
ADOdb: Log Query
PHP ADOdb: Understanding Pivot Table For Reporting
PHP ADOdb: Creating Pivot Table
PHP ADOdb: Creating Query to Build Pivot Table

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting