Skip to content

Commit

Permalink
Fix ADODB odbc_mssql issues, fix mssql datadict
Browse files Browse the repository at this point in the history
+ //if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
+//if ($ty == 'X') $fdefault = false;

These have been commented out as we've made possible values for custom fields as type X: ADODB doesn't allow 'type X' to be not null or have default values as some db's may not support it for some blob types - so far i'm not aware of any issues, and the behaviour of silently dropping DEFAULT/NOTNULL for blobs breaks updates for mssql.

git-svn-id: http://mantisbt.svn.sourceforge.net/svnroot/mantisbt/trunk@5599 f5dc347c-c33d-0410-90a0-b07cc1902cb9
  • Loading branch information
mantis committed Sep 27, 2008
1 parent a482474 commit 19dbfb0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/adodb/adodb-datadict.inc.php
Expand Up @@ -713,12 +713,12 @@ function _GenFields($flds,$widespacing=false)

$ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);

if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
//if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls

if ($fprimary) $pkey[] = $fname;

// some databases do not allow blobs to have defaults
if ($ty == 'X') $fdefault = false;
//if ($ty == 'X') $fdefault = false;

// build list of indexes
if ($findex != '') {
Expand Down
62 changes: 56 additions & 6 deletions core/adodb/datadict/datadict-mssql.inc.php
Expand Up @@ -121,19 +121,62 @@ function AddColumnSQL($tabname, $flds)
return $sql;
}

/*
function AlterColumnSQL($tabname, $flds)
function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
{
$tabname = $this->TableName ($tabname);
$sql = array();
list($lines,$pkey) = $this->_GenFields($flds);

list($lines,$pkey,$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) {
$sql[] = "ALTER TABLE $tabname $this->alterCol $v";

$not_null = false;
if ($not_null = preg_match('/NOT NULL/i',$v)) {
$v = preg_replace('/NOT NULL/i','',$v);
}

if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) {
list(,$colname,$default) = $matches;
$existing = $this->MetaColumns($tabname);
$constraintname = false;
$rs = $this->connection->Execute( "select name from sys.default_constraints WHERE object_name(parent_object_id) = '" . $tabname ."' AND col_name(parent_object_id, parent_column_id) = '" . $colname . "'");
if ( is_object($rs) ) {
$row = $rs->FetchRow();
$constraintname = $row[0];
}
$v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v);
$t = trim(str_replace('DEFAULT '.$default,'',$v));
if ( $constraintname != false ) {
$sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname;
}
$sql[] = $alter . $colname . ' ' . $t ;
if ( $constraintname != false ) {
$sql[] = 'ALTER TABLE '.$tabname.' ADD CONSTRAINT '.$constraintname.' DEFAULT ' . $default . ' FOR ' . $colname;
} else {
$sql[] = 'ALTER TABLE '.$tabname.' ADD CONSTRAINT DF__'. $tabname . '__'. $colname. '__' . dechex(rand()) .' DEFAULT ' . $default . ' FOR ' . $colname;
}
if ($not_null) {
$sql[] = $alter . $colname . ' ' . $t . ' NOT NULL';
}
} else {
if ($not_null) {
$sql[] = $alter . $v . ' NOT NULL';
} else {
$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;
}
*/
}

function DropColumnSQL($tabname, $flds)
{
Expand All @@ -143,6 +186,13 @@ function DropColumnSQL($tabname, $flds)
$f = array();
$s = 'ALTER TABLE ' . $tabname;
foreach($flds as $v) {
$rs = $this->connection->Execute( "select name from sys.default_constraints WHERE object_name(parent_object_id) = '" . $tabname ."' AND col_name(parent_object_id, parent_column_id) = '" . $v . "'");
if ( is_object($rs) ) {
$row = $rs->FetchRow();
$constraintname = $row[0];
$sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname;
}

$f[] = "\n$this->dropCol ".$this->NameQuote($v);
}
$s .= implode(', ',$f);
Expand Down
8 changes: 5 additions & 3 deletions core/adodb/drivers/adodb-odbc.inc.php
Expand Up @@ -500,13 +500,13 @@ function _query($sql,$inputarr=false)
$stmtid = $sql[1];
} else {
$stmtid = odbc_prepare($this->_connectionID,$sql);

if ($stmtid == false) {
$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
return false;
}
}

if (! odbc_execute($stmtid,$inputarr)) {
//@odbc_free_result($stmtid);
if ($this->_haserrorfunctions) {
Expand All @@ -515,7 +515,6 @@ function _query($sql,$inputarr=false)
}
return false;
}

} else if (is_array($sql)) {
$stmtid = $sql[1];
if (!odbc_execute($stmtid)) {
Expand All @@ -531,6 +530,9 @@ function _query($sql,$inputarr=false)

$this->_lastAffectedRows = 0;
if ($stmtid) {
if (!is_array($sql) && preg_match('/^\\s*insert/i', $sql)) {
return $stmtid;
}
if (@odbc_num_fields($stmtid) == 0) {
$this->_lastAffectedRows = odbc_num_rows($stmtid);
$stmtid = true;
Expand Down
19 changes: 18 additions & 1 deletion core/adodb/drivers/adodb-odbc_mssql.inc.php
Expand Up @@ -72,7 +72,11 @@ function _insertid()
// the same scope. A scope is a module -- a stored procedure, trigger,
// function, or batch. Thus, two statements are in the same scope if
// they are in the same stored procedure, function, or batch.
if ($this->lastInsID !== false) {
return $this->lastInsID; // InsID from sp_executesql call
} else {
return $this->GetOne($this->identitySQL);
}
}


Expand Down Expand Up @@ -180,7 +184,20 @@ function MetaIndexes($table,$primary=false)
function _query($sql,$inputarr)
{
if (is_string($sql)) $sql = str_replace('||','+',$sql);
return ADODB_odbc::_query($sql,$inputarr);
$getIdentity = false;
if (!is_array($sql) && preg_match('/^\\s*insert/i', $sql)) {
$getIdentity = true;
$sql .= (preg_match('/;\\s*$/i', $sql) ? ' ' : '; ') . $this->identitySQL . ' as insertid';
}

if ($getIdentity) {
$res = ADODB_odbc::_query($sql,$inputarr);
$row = odbc_fetch_array($res);
$this->lastInsID = isset($row['insertid']) ? (int)$row['insertid'] : false;
} else {
$this->lastInsID = false;
return ADODB_odbc::_query($sql,$inputarr);
}
}

function SetTransactionMode( $transaction_mode )
Expand Down

0 comments on commit 19dbfb0

Please sign in to comment.