egroupware_official/phpgwapi/inc/adodb/datadict/datadict-mysql.inc.php

185 lines
4.6 KiB
PHP
Raw Normal View History

2003-10-19 21:05:23 +02:00
<?php
/**
2005-09-26 12:12:10 +02:00
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
2003-10-19 21:05:23 +02:00
the BSD license will take precedence.
2003-10-19 21:05:23 +02:00
Set tabs to 4 for best viewing.
2003-10-19 21:05:23 +02:00
*/
// security - hide paths
if (!defined('ADODB_DIR')) die();
2003-10-19 21:05:23 +02:00
class ADODB2_mysql extends ADODB_DataDict {
var $databaseType = 'mysql';
var $alterCol = ' MODIFY COLUMN';
var $alterTableAddIndex = true;
var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
var $dropIndex = 'DROP INDEX %s ON %s';
var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
2003-10-19 21:05:23 +02:00
function MetaType($t,$len=-1,$fieldobj=false)
{
if (is_object($t)) {
$fieldobj = $t;
$t = $fieldobj->type;
$len = $fieldobj->max_length;
}
$is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
2003-10-19 21:05:23 +02:00
$len = -1; // mysql max_length is not accurate
switch (strtoupper($t)) {
case 'STRING':
2003-10-19 21:05:23 +02:00
case 'CHAR':
case 'VARCHAR':
case 'TINYBLOB':
case 'TINYTEXT':
case 'ENUM':
2003-10-19 21:05:23 +02:00
case 'SET':
if ($len <= $this->blobSize) return 'C';
2003-10-19 21:05:23 +02:00
case 'TEXT':
case 'LONGTEXT':
2003-10-19 21:05:23 +02:00
case 'MEDIUMTEXT':
return 'X';
2003-10-19 21:05:23 +02:00
// php_mysql extension always returns 'blob' even if 'text'
// so we have to check whether binary...
case 'IMAGE':
case 'LONGBLOB':
2003-10-19 21:05:23 +02:00
case 'BLOB':
case 'MEDIUMBLOB':
return !empty($fieldobj->binary) ? 'B' : 'X';
2003-10-19 21:05:23 +02:00
case 'YEAR':
case 'DATE': return 'D';
2003-10-19 21:05:23 +02:00
case 'TIME':
case 'DATETIME':
case 'TIMESTAMP': return 'T';
2003-10-19 21:05:23 +02:00
case 'FLOAT':
case 'DOUBLE':
return 'F';
case 'INT':
case 'INTEGER': return $is_serial ? 'R' : 'I';
case 'TINYINT': return $is_serial ? 'R' : 'I1';
case 'SMALLINT': return $is_serial ? 'R' : 'I2';
case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
case 'BIGINT': return $is_serial ? 'R' : 'I8';
2003-10-19 21:05:23 +02:00
default: return 'N';
}
}
function ActualType($meta)
{
switch(strtoupper($meta)) {
case 'C': return 'VARCHAR';
2005-09-26 12:12:10 +02:00
case 'XL':return 'LONGTEXT';
case 'X': return 'TEXT';
2003-10-19 21:05:23 +02:00
case 'C2': return 'VARCHAR';
case 'X2': return 'LONGTEXT';
2003-10-19 21:05:23 +02:00
case 'B': return 'LONGBLOB';
2003-10-19 21:05:23 +02:00
case 'D': return 'DATE';
case 'T': return 'DATETIME';
case 'L': return 'TINYINT';
case 'R':
case 'I4':
2003-10-19 21:05:23 +02:00
case 'I': return 'INTEGER';
case 'I1': return 'TINYINT';
case 'I2': return 'SMALLINT';
case 'I8': return 'BIGINT';
2003-10-19 21:05:23 +02:00
case 'F': return 'DOUBLE';
case 'N': return 'NUMERIC';
default:
return $meta;
}
}
2003-10-19 21:05:23 +02:00
// return string must begin with space
function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
{
2003-10-19 21:05:23 +02:00
$suffix = '';
if (stripos($fconstraint, 'CHARACTER SET') === 0) {
$suffix .= ' '.$fconstraint;
unset($fconstraint);
}
2003-10-19 21:05:23 +02:00
if ($funsigned) $suffix .= ' UNSIGNED';
if ($fnotnull) $suffix .= ' NOT NULL';
if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
if ($fconstraint) $suffix .= ' '.$fconstraint;
return $suffix;
}
2003-10-19 21:05:23 +02:00
/*
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement]
create_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
[PRIMARY KEY] [reference_definition]
or PRIMARY KEY (index_col_name,...)
or KEY [index_name] (index_col_name,...)
or INDEX [index_name] (index_col_name,...)
or UNIQUE [INDEX] [index_name] (index_col_name,...)
or FULLTEXT [INDEX] [index_name] (index_col_name,...)
or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
[reference_definition]
or CHECK (expr)
*/
2003-10-19 21:05:23 +02:00
/*
CREATE [UNIQUE|FULLTEXT] INDEX index_name
ON tbl_name (col_name[(length)],... )
*/
2003-10-19 21:05:23 +02:00
function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
{
$sql = array();
if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
if ( isset($idxoptions['DROP']) )
return $sql;
}
if ( empty ($flds) ) {
return $sql;
}
if (isset($idxoptions['FULLTEXT'])) {
$unique = ' FULLTEXT';
} elseif (isset($idxoptions['UNIQUE'])) {
$unique = ' UNIQUE';
} else {
$unique = '';
}
if ( is_array($flds) ) $flds = implode(', ',$flds);
if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
$s .= ' (' . $flds . ')';
if ( isset($idxoptions[$this->upperName]) )
$s .= $idxoptions[$this->upperName];
2003-10-19 21:05:23 +02:00
$sql[] = $s;
2003-10-19 21:05:23 +02:00
return $sql;
}
}
?>