1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
<?php
/**
* Data Dictionary for Firebird.
*
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
*
* @package ADOdb
* @link https://adodb.org Project's web site and documentation
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
*
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
* any later version. This means you can use it in proprietary products.
* See the LICENSE.md file distributed with this source code for details.
* @license BSD-3-Clause
* @license LGPL-2.1-or-later
*
* @copyright 2000-2013 John Lim
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
*/
// security - hide paths
if (!defined('ADODB_DIR')) die();
class ADODB2_firebird extends ADODB_DataDict
{
var $databaseType = 'firebird';
var $seqField = false;
var $seqPrefix = 's_';
var $blobSize = 40000;
var $renameColumn = 'ALTER TABLE %s ALTER %s TO %s';
var $alterCol = ' ALTER';
var $dropCol = ' DROP';
function actualType($meta)
{
$meta = strtoupper($meta);
// Add support for custom meta types.
// We do this first, that allows us to override existing types
if (isset($this->connection->customMetaTypes[$meta])) {
return $this->connection->customMetaTypes[$meta]['actual'];
}
switch($meta) {
case 'C':
return 'VARCHAR';
case 'XL':
return 'BLOB SUB_TYPE BINARY';
case 'X':
return 'BLOB SUB_TYPE TEXT';
case 'C2':
return 'VARCHAR(32765)'; // up to 32K
case 'X2':
return 'VARCHAR(4096)';
case 'V':
return 'CHAR';
case 'C1':
return 'CHAR(1)';
case 'B':
return 'BLOB';
case 'D':
return 'DATE';
case 'TS':
case 'T':
return 'TIMESTAMP';
case 'L':
case 'I1':
case 'I2':
return 'SMALLINT';
case 'I':
case 'I4':
return 'INTEGER';
case 'I8':
return 'BIGINT';
case 'F':
return 'DOUBLE PRECISION';
case 'N':
return 'DECIMAL';
default:
return $meta;
}
}
function nameQuote($name = null, $allowBrackets = false)
{
if (!is_string($name)) {
return false;
}
$name = trim($name);
if (!is_object($this->connection)) {
return $name;
}
$quote = $this->connection->nameQuote;
// if name is of the form `name`, quote it
if (preg_match('/^`(.+)`$/', $name, $matches)) {
return $quote . $matches[1] . $quote;
}
// if name contains special characters, quote it
if (!preg_match('/^[' . $this->nameRegex . ']+$/', $name)) {
return $quote . $name . $quote;
}
return $quote . $name . $quote;
}
function createDatabase($dbname, $options = false)
{
$sql = array();
$sql[] = "DECLARE EXTERNAL FUNCTION LOWER CSTRING(80) RETURNS CSTRING(80) FREE_IT ENTRY_POINT 'IB_UDF_lower' MODULE_NAME 'ib_udf'";
return $sql;
}
function _dropAutoIncrement($tabname)
{
if (strpos($tabname, '.') !== false) {
$tarr = explode('.', $tabname);
return 'DROP SEQUENCE ' . $tarr[0] . '."s_' . $tarr[1] . '"';
}
return 'DROP SEQUENCE s_' . $tabname;
}
function _createSuffix($fname, &$ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint, $funsigned, $fprimary, &$pkey)
{
$suffix = '';
if (strlen($fdefault)) {
$suffix .= " DEFAULT $fdefault";
}
if ($fnotnull) {
$suffix .= ' NOT NULL';
}
if ($fautoinc) {
$this->seqField = $fname;
}
$fconstraint = preg_replace("/``/", "\"", $fconstraint);
if ($fconstraint) {
$suffix .= ' ' . $fconstraint;
}
return $suffix;
}
/**
* Generate the SQL to create table. Returns an array of sql strings.
*/
function createTableSQL($tabname, $flds, $tableoptions = array())
{
list($lines, $pkey, $idxs) = $this->_GenFields($flds, true);
// genfields can return FALSE at times
if ($lines == null) {
$lines = array();
}
$taboptions = $this->_Options($tableoptions);
$tabname = $this->TableName($tabname);
$sql = $this->_TableSQL($tabname, $lines, $pkey, $taboptions);
if ($this->autoIncrement && !isset($taboptions['DROP'])) {
$tsql = $this->_Triggers($tabname, $taboptions);
foreach ($tsql as $s) {
$sql[] = $s;
}
}
if (is_array($idxs)) {
foreach ($idxs as $idx => $idxdef) {
$sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
$sql = array_merge($sql, $sql_idxs);
}
}
return $sql;
}
/*
CREATE or replace TRIGGER jaddress_insert
before insert on jaddress
for each row
begin
IF ( NEW."seqField" IS NULL OR NEW."seqField" = 0 ) THEN
NEW."seqField" = GEN_ID("GEN_tabname", 1);
end;
*/
function _triggers($tabname, $taboptions)
{
if (!$this->seqField) {
return array();
}
$tab1 = preg_replace('/"/', '', $tabname);
if ($this->schema) {
$t = strpos($tab1, '.');
if ($t !== false) {
$tab = substr($tab1, $t + 1);
} else {
$tab = $tab1;
}
$seqField = $this->seqField;
$seqname = $this->schema . '.' . $this->seqPrefix . $tab;
$trigname = $this->schema . '.t_' . $this->seqPrefix . $tab;
} else {
$seqField = $this->seqField;
$seqname = $this->seqPrefix . $tab1;
$trigname = 't_' . $seqname;
}
if (isset($taboptions['DROP'])) {
$sql[] = "DROP SEQUENCE $seqname";
} elseif (isset($taboptions['REPLACE'])) {
$sql[] = "DROP SEQUENCE \"$seqname\"";
$sql[] = "CREATE SEQUENCE \"$seqname\"";
$sql[] = "ALTER TRIGGER \"$trigname\" BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID(\"$seqname\", 1); END";
} else {
$sql[] = "CREATE SEQUENCE $seqname";
$sql[] = "CREATE TRIGGER $trigname FOR $tabname BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID($seqname, 1); END";
}
$this->seqField = false;
return $sql;
}
/**
* Change the definition of one column
*
* @param string $tabname table-name
* @param string $flds column-name and type for the changed column
* @param string $tableflds Unused
* @param array|string $tableoptions Unused
*
* @return array with SQL strings
*/
public function alterColumnSQL($tabname, $flds, $tableflds = '', $tableoptions = '')
{
$tabname = $this->TableName($tabname);
$sql = array();
list($lines, , $idxs) = $this->_GenFields($flds);
// genfields can return FALSE at times
if ($lines == null) {
$lines = array();
}
$alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
foreach ($lines as $v) {
/*
* The type must be preceded by the keyword 'TYPE'
*/
$vExplode = explode(' ', $v);
$vExplode = array_filter($vExplode);
array_splice($vExplode, 1, 0, array('TYPE'));
$v = implode(' ', $vExplode);
$sql[] = $alter . $v;
}
if (is_array($idxs)) {
foreach ($idxs as $idx => $idxdef) {
$sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
$sql = array_merge($sql, $sql_idxs);
}
}
return $sql;
}
}
|