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,
       string $actualType,
       string $dictionayType,
       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. The type definition should be an uppercase alpha-numeric value, e.g. P

$actualType

The physical data type as provided by the database, for example the POINT data type in the mySQL returns 255.

$dictionaryType

A Database specific field type, mapped to the metaType, e.g. POINT. This tells ADOdb how to handle the data n inserts and updates.

$handleAsType

One of the standard metaTypes. This indicates what type of data the new type is. If not set, then no attempt is made to process the data.

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
*/
$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','C', $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->setCustomMetaType('P', 255, '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.1610852515.txt.gz · Last modified: 2021/01/17 04:01 by mnewnham