Files
moodle/lib/xmldb/classes/generators/mysql/mysql.class.php

214 lines
9.6 KiB
PHP

<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/// This class generate SQL code to be used against MySQL
/// It extends XMLDBgenerator so everything can be
/// overriden as needed to generate correct SQL.
class XMLDBmysql extends XMLDBGenerator {
/// Only set values that are different from the defaults present in XMLDBgenerator
var $quote_string = '`'; // String used to quote names
var $default_for_char = ''; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
var $primary_key_name = 'primary'; //To force primary key names to one string (null=no force)
var $foreign_keys = false; // Does the generator build foreign keys
var $primary_index = false;// Does the generator need to build one index for primary keys
var $unique_index = false; // Does the generator need to build one index for unique keys
var $sequence_extra_code = false; //Does the generator need to add extra code to generate the sequence fields
var $sequence_name = 'auto_increment'; //Particular name for inline sequences in this generator
var $enum_extra_code = false; //Does the generator need to add extra code to generate code for the enums in the table
/**
* Creates one new XMLDBmysql
*/
function XMLDBmysql() {
parent::XMLDBGenerator();
global $CFG;
$this->prefix = '';
$this->reserved_words = $this->getReservedWords();
}
/**
* Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type
*/
function getTypeSQL ($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
switch ($xmldb_type) {
case XMLDB_TYPE_INTEGER: // From http://mysql.com/doc/refman/5.0/en/numeric-types.html!
if (empty($xmldb_length)) {
$xmldb_length = 10;
}
if ($xmldb_length > 9) {
$dbtype = 'BIGINT';
} else if ($xmldb_length > 6) {
$dbtype = 'INT';
} else if ($xmldb_length > 4) {
$dbtype = 'MEDIUMINT';
} else if ($xmldb_length > 2) {
$dbtype = 'SMALLINT';
} else {
$dbtype = 'TINYINT';
}
$dbtype .= '(' . $xmldb_length . ')';
break;
case XMLDB_TYPE_NUMBER:
$dbtype = $this->number_type;
if (!empty($xmldb_length)) {
$dbtype .= '(' . $xmldb_length;
if (!empty($xmldb_decimals)) {
$dbtype .= ',' . $xmldb_decimals;
}
$dbtype .= ')';
}
break;
case XMLDB_TYPE_FLOAT:
$dbtype = 'FLOAT';
if (!empty($xmldb_length)) {
$dbtype .= '(' . $xmldb_length;
if (!empty($xmldb_decimals)) {
$dbtype .= ',' . $xmldb_decimals;
}
$dbtype .= ')';
}
break;
case XMLDB_TYPE_CHAR:
$dbtype = 'VARCHAR';
if (empty($xmldb_length)) {
$xmldb_length='255';
}
$dbtype .= '(' . $xmldb_length . ')';
break;
case XMLDB_TYPE_TEXT:
if (empty($xmldb_length)) {
$xmldb_length = 'small';
}
if ($xmldb_length = 'small') {
$dbtype = 'TEXT';
} else if ($xmldb_length = 'medium') {
$dbtype = 'MEDIUMTEXT';
} else {
$dbtype = 'BIGTEXT';
}
break;
case XMLDB_TYPE_BINARY:
if (empty($xmldb_length)) {
$xmldb_length = 'small';
}
if ($xmldb_length = 'small') {
$dbtype = 'BLOB';
} else if ($xmldb_length = 'medium') {
$dbtype = 'MEDIUMBLOB';
} else {
$dbtype = 'BIGBLOB';
}
break;
case XMLDB_TYPE_DATETIME:
$dbtype = 'DATETIME';
}
return $dbtype;
}
/**
* Given one XMLDB Field, return its enum SQL
*/
function getEnumSQL ($xmldb_field) {
return 'enum(' . implode(', ', $xmldb_field->getEnumValues()) . ')';
}
/**
* Returns the code needed to add one comment to the table
*/
function getCommentSQL ($xmldb_table) {
$comment = '';
if ($xmldb_table->getComment()) {
$comment .= " COMMENT='" . substr($xmldb_table->getComment(), 0, 250) . "'";
}
return $comment;
}
/**
* Returns an array of reserved words (lowercase) for this DB
*/
function getReservedWords() {
/// This file contains the reserved words for MySQL databases
/// from http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
$reserved_words = array (
'add', 'all', 'alter', 'analyze', 'and', 'as', 'asc',
'asensitive', 'before', 'between', 'bigint', 'binary',
'blob', 'both', 'by', 'call', 'cascade', 'case', 'change',
'char', 'character', 'check', 'collate', 'column',
'condition', 'connection', 'constraint', 'continue',
'convert', 'create', 'cross', 'current_date', 'current_time',
'current_timestamp', 'current_user', 'cursor', 'database',
'databases', 'day_hour', 'day_microsecond',
'day_minute', 'day_second', 'dec', 'decimal', 'declare',
'default', 'delayed', 'delete', 'desc', 'describe',
'deterministic', 'distinct', 'distinctrow', 'div', 'double',
'drop', 'dual', 'each', 'else', 'elseif', 'enclosed', 'escaped',
'exists', 'exit', 'explain', 'false', 'fetch', 'float', 'float4',
'float8', 'for', 'force', 'foreign', 'from', 'fulltext', 'grant',
'group', 'having', 'high_priority', 'hour_microsecond',
'hour_minute', 'hour_second', 'if', 'ignore', 'in', 'index',
'infile', 'inner', 'inout', 'insensitive', 'insert', 'int', 'int1',
'int2', 'int3', 'int4', 'int8', 'integer', 'interval', 'into', 'is',
'iterate', 'join', 'key', 'keys', 'kill', 'leading', 'leave', 'left',
'like', 'limit', 'lines', 'load', 'localtime', 'localtimestamp',
'lock', 'long', 'longblob', 'longtext', 'loop', 'low_priority',
'match', 'mediumblob', 'mediumint', 'mediumtext',
'middleint', 'minute_microsecond', 'minute_second',
'mod', 'modifies', 'natural', 'not', 'no_write_to_binlog',
'null', 'numeric', 'on', 'optimize', 'option', 'optionally',
'or', 'order', 'out', 'outer', 'outfile', 'precision', 'primary',
'procedure', 'purge', 'raid0', 'read', 'reads', 'real',
'references', 'regexp', 'release', 'rename', 'repeat', 'replace',
'require', 'restrict', 'return', 'revoke', 'right', 'rlike', 'schema',
'schemas', 'second_microsecond', 'select', 'sensitive',
'separator', 'set', 'show', 'smallint', 'soname', 'spatial',
'specific', 'sql', 'sqlexception', 'sqlstate', 'sqlwarning',
'sql_big_result', 'sql_calc_found_rows', 'sql_small_result',
'ssl', 'starting', 'straight_join', 'table', 'terminated', 'then',
'tinyblob', 'tinyint', 'tinytext', 'to', 'trailing', 'trigger', 'true',
'undo', 'union', 'unique', 'unlock', 'unsigned', 'update',
'upgrade', 'usage', 'use', 'using', 'utc_date', 'utc_time',
'utc_timestamp', 'values', 'varbinary', 'varchar', 'varcharacter',
'varying', 'when', 'where', 'while', 'with', 'write', 'x509',
'xor', 'year_month', 'zerofill'
);
return $reserved_words;
}
}
?>