ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:connection:setcustommetatype

This is an old revision of the document!


setCustomMetaType

From Version 5.22

See Also

setCustomActualType

Syntax
bool setCustomMetaType(
       string $metaType,
       mixed $handleAsType=false,
       mixed $callBack=false
       )

Description

The function setCustomMetaType() takes an input string that represents a database-specific data type, and creates an ADOdb metaType associated with it. If the physical type is already bound to an existing metaType, the new setting overrides the old. For full functionality, a corresponding setCustomActualType() command should be issued.


MetaType

An existing or new type.

ActualType

A Database specific field type, mapped to the metaType

$handleAsType

One of the standard metaTypes. This indicates what type of data the new type is.

Data Handler

The handler can be one of these types:

  1. Not set: No pre-processing of data prior to insertion of data.
  2. Object: An anonymous function that can be used to process data.

The callback function can be used to apply any changes to the data prior to the processing as type.

Examples

Using A Passthrough Handler

In this example, the system can be expanded to accept geometry type data types by creating a new data type

/*
* $db = MySQL connection assumed
*/
 
/*
* First link the MySQL geometry type to a new metaType
*/ 
$ok = $db->setCustomActualType(255,'P');
 
/*
* Now tell ADOdb how to handle the data in inserts and updates
* see the entry for setCustomMetaType for an explanation
*/
$ok = $db->setCustomMetaType('P','POINT');
 
/*
* Insert a record with a point column
*/
$ar = array();
$ar['geo_column'] = 'POINT(1,2)';

Using a closure

In this example all character fields are converted to lowercase by the anonymous function

/*
* $db = MySQL connection assumed
*/
$callback = function($val) { 
	return strtolower($val); 
	};
 
$ok = $db->setCustomMetaType('C','CHAR',$callback);

Using The Custom Type In DataDict Functions

In this example, we use a custom definition to create a POINT column in a table under MySQL

/*
* We must define the custom type before loading the data dictionary
*/
$ok = $db->setCustomActualType(255,'P');
$ok = $db->setCustomMetaType('P','POINT');
 
/*
* Then create a data dictionary object, using this connection
*/
$dict = NewDataDictionary($db);
 
$tabname = "mt_test";
$flds = " 
   COL1 I  NOTNULL AUTO PRIMARY,
   COL2 C(32) NOTNULL DEFAULT 'abc',
   COL3 N(12.2),
   COL4 P
  ";
 
$sqlarray = $dict->createTableSQL($tabname, $flds);
print_r($sqlarray);
 
/*
* Returns
CREATE TABLE mt_test (
COL1                     INTEGER NOT NULL AUTO_INCREMENT,
COL2                     VARCHAR(32) NOT NULL DEFAULT 'abc',
COL3                     NUMERIC(12,2),
COL4                     POINT,
                 PRIMARY KEY (COL1)
                 )
*/
v5/reference/connection/setcustommetatype.1610842894.txt.gz · Last modified: 2021/01/17 01:21 by mnewnham