12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796 |
- <?php
- class BlankObject {
- }
- class IBM_DB2Field {
- private $name, $tablename, $type, $nullable, $max_length;
-
- static function fromText($db, $table, $field) {
- global $wgDBmwschema;
- $q = <<<END
- SELECT
- lcase(coltype) AS typname,
- nulls AS attnotnull, length AS attlen
- FROM sysibm.syscolumns
- WHERE tbcreator=%s AND tbname=%s AND name=%s;
- END;
- $res = $db->query(sprintf($q,
- $db->addQuotes($wgDBmwschema),
- $db->addQuotes($table),
- $db->addQuotes($field)));
- $row = $db->fetchObject($res);
- if (!$row)
- return null;
- $n = new IBM_DB2Field;
- $n->type = $row->typname;
- $n->nullable = ($row->attnotnull == 'N');
- $n->name = $field;
- $n->tablename = $table;
- $n->max_length = $row->attlen;
- return $n;
- }
-
- function name() { return $this->name; }
-
- function tableName() { return $this->tablename; }
-
- function type() { return $this->type; }
-
- function nullable() { return $this->nullable; }
-
- function maxLength() { return $this->max_length; }
- }
- class IBM_DB2Blob {
- private $mData;
- function __construct($data) {
- $this->mData = $data;
- }
- function getData() {
- return $this->mData;
- }
- }
- class DatabaseIbm_db2 extends Database {
-
-
-
- protected $mPort = NULL;
-
- protected $mCataloged = NULL;
-
- protected $mSchema = NULL;
-
- protected $mSchemaSet = false;
-
- protected $mLastResult = NULL;
-
- protected $mAffectedRows = NULL;
-
- protected $mNumRows = NULL;
-
-
- const CATALOGED = "cataloged";
- const UNCATALOGED = "uncataloged";
- const USE_GLOBAL = "get from global";
-
-
- protected $mInsertId = NULL;
-
-
-
-
-
-
-
-
-
-
- function cascadingDeletes() {
- return true;
- }
-
- function cleanupTriggers() {
- return true;
- }
-
- function strictIPs() {
- return true;
- }
-
-
- function realTimestamps() {
- return true;
- }
-
- function implicitGroupby() {
- return false;
- }
-
- function implicitOrderby() {
- return false;
- }
-
- function searchableIPs() {
- return true;
- }
-
- function functionalIndexes() {
- return true;
- }
-
-
- function getWikiID() {
- if( $this->mSchema ) {
- return "{$this->mDBname}-{$this->mSchema}";
- } else {
- return $this->mDBname;
- }
- }
-
-
-
-
-
-
-
-
- public function DatabaseIbm_db2($server = false, $user = false, $password = false,
- $dbName = false, $failFunction = false, $flags = 0,
- $schema = self::USE_GLOBAL )
- {
- global $wgOut, $wgDBmwschema;
-
- if ( !isset( $wgOut ) ) {
- $wgOut = NULL;
- }
- $this->mOut =& $wgOut;
- $this->mFailFunction = $failFunction;
- $this->mFlags = DBO_TRX | $flags;
-
- if ( $schema == self::USE_GLOBAL ) {
- $this->mSchema = $wgDBmwschema;
- }
- else {
- $this->mSchema = $schema;
- }
-
- $this->open( $server, $user, $password, $dbName);
- }
-
-
- public function open( $server, $user, $password, $dbName )
- {
-
- global $wgDBport_db2, $wgDBcataloged;
- wfProfileIn( __METHOD__ );
-
-
- if (!@extension_loaded('ibm_db2')) {
- @dl('ibm_db2.so');
- }
-
- if ( !function_exists( 'db2_connect' ) ) {
- $error = "DB2 functions missing, have you enabled the ibm_db2 extension for PHP?\n";
- wfDebug($error);
- $this->reportConnectionError($error);
- }
- if (!strlen($user)) {
- return null;
- }
-
-
- $this->close();
-
- $this->mServer = $server;
- $this->mPort = $port = $wgDBport_db2;
- $this->mUser = $user;
- $this->mPassword = $password;
- $this->mDBname = $dbName;
- $this->mCataloged = $cataloged = $wgDBcataloged;
-
- if ( $cataloged == self::CATALOGED ) {
- $this->openCataloged($dbName, $user, $password);
- }
- elseif ( $cataloged == self::UNCATALOGED ) {
- $this->openUncataloged($dbName, $user, $password, $server, $port);
- }
-
-
-
-
- db2_autocommit($this->mConn, DB2_AUTOCOMMIT_ON);
- if ( $this->mConn == false ) {
- wfDebug( "DB connection error\n" );
- wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
- wfDebug( $this->lastError()."\n" );
- return null;
- }
- $this->mOpened = true;
- $this->applySchema();
-
- wfProfileOut( __METHOD__ );
- return $this->mConn;
- }
-
-
- protected function openCataloged( $dbName, $user, $password )
- {
- @$this->mConn = db2_connect($dbName, $user, $password);
- }
-
-
- protected function openUncataloged( $dbName, $user, $password, $server, $port )
- {
- $str = "DRIVER={IBM DB2 ODBC DRIVER};";
- $str .= "DATABASE=$dbName;";
- $str .= "HOSTNAME=$server;";
- if ($port) $str .= "PORT=$port;";
- $str .= "PROTOCOL=TCPIP;";
- $str .= "UID=$user;";
- $str .= "PWD=$password;";
-
- @$this->mConn = db2_connect($str, $user, $password);
- }
-
-
- public function close() {
- $this->mOpened = false;
- if ( $this->mConn ) {
- if ($this->trxLevel() > 0) {
- $this->commit();
- }
- return db2_close( $this->mConn );
- }
- else {
- return true;
- }
- }
-
-
- static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0)
- {
- return new DatabaseIbm_db2( $server, $user, $password, $dbName, $failFunction, $flags );
- }
-
-
- public function lastError() {
- if ($this->lastError2()) {
- $this->rollback();
- return true;
- }
- return false;
- }
-
- private function lastError2() {
- $connerr = db2_conn_errormsg();
- if ($connerr) return $connerr;
- $stmterr = db2_stmt_errormsg();
- if ($stmterr) return $stmterr;
- if ($this->mConn) return "No open connection.";
- if ($this->mOpened) return "No open connection allegedly.";
-
- return false;
- }
-
-
- public function lastErrno() {
- $connerr = db2_conn_error();
- if ($connerr) return $connerr;
- $stmterr = db2_stmt_error();
- if ($stmterr) return $stmterr;
- return 0;
- }
-
-
- public function isOpen() { return $this->mOpened; }
-
-
-
- public function doQuery( $sql ) {
-
-
- $this->applySchema();
-
- $ret = db2_exec( $this->mConn, $sql );
- if( !$ret ) {
- print "<br><pre>";
- print $sql;
- print "</pre><br>";
- $error = db2_stmt_errormsg();
- throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( $error ) );
- }
- $this->mLastResult = $ret;
- $this->mAffectedRows = NULL;
- return $ret;
- }
-
-
- public function getServerVersion() {
- $info = db2_server_info( $this->mConn );
- return $info->DBMS_VER;
- }
-
-
- public function tableExists( $table ) {
- $schema = $this->mSchema;
- $sql = <<< EOF
- SELECT COUNT(*) FROM SYSIBM.SYSTABLES ST
- WHERE ST.NAME = '$table' AND ST.CREATOR = '$schema'
- EOF;
- $res = $this->query( $sql );
- if (!$res) return false;
-
-
- @$row = $this->fetchRow($res);
- $count = $row[0];
- if ($count == '1' or $count == 1) {
- return true;
- }
-
- return false;
- }
-
-
- public function fetchObject( $res ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- @$row = db2_fetch_object( $res );
- if( $this->lastErrno() ) {
- throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
- }
-
- if ($row)
- {
- $row2 = new BlankObject();
- foreach ($row as $key => $value)
- {
- $keyu = strtolower($key);
- $row2->$keyu = $value;
- }
- $row = $row2;
- }
- return $row;
- }
-
- public function fetchRow( $res ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- @$row = db2_fetch_array( $res );
- if ( $this->lastErrno() ) {
- throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
- }
- return $row;
- }
-
-
- public function initial_setup() {
-
- }
-
-
- public function setup_database() {
-
-
-
- try {
-
-
-
- $this->applySchema();
- $this->begin();
-
- $res = dbsource( "../maintenance/ibm_db2/tables.sql", $this);
- $res = null;
-
-
-
-
-
- $this->commit();
- }
- catch (MWException $mwe)
- {
- print "<br><pre>$mwe</pre><br>";
- }
- }
-
- public function addQuotes( $s ) {
-
- if ( is_null( $s ) ) {
- return "NULL";
- } else if ($s instanceof Blob) {
- return "'".$s->fetch($s)."'";
- }
- $s = $this->strencode($s);
- if ( is_numeric($s) ) {
- return $s;
- }
- else {
- return "'$s'";
- }
- }
-
-
- public function addQuotesSmart( $table, $field, $s ) {
- if ( is_null( $s ) ) {
- return "NULL";
- } else if ($s instanceof Blob) {
- return "'".$s->fetch($s)."'";
- }
- $s = $this->strencode($s);
- if ( is_numeric($s) ) {
-
-
- $res = $this->doQuery("SELECT $field FROM $table FETCH FIRST 1 ROWS ONLY");
- $type = db2_field_type($res, strtoupper($field));
- if ( $this->is_numeric_type( $type ) ) {
-
- return $s;
- }
- else {
- wfDebug("DB2: Numeric in non-numeric: '$s' in $type $field in $table\n");
- return "'$s'";
- }
- }
- else {
- return "'$s'";
- }
- }
-
-
- public function is_numeric_type( $type ) {
- switch (strtoupper($type)) {
- case 'SMALLINT':
- case 'INTEGER':
- case 'INT':
- case 'BIGINT':
- case 'DECIMAL':
- case 'REAL':
- case 'DOUBLE':
- case 'DECFLOAT':
- return true;
- }
- return false;
- }
-
-
- public function strencode( $s ) {
-
-
-
- $s = db2_escape_string($s);
-
- $s = utf8_encode($s);
-
- $from = array("\\\\", "\\'", '\\n', '\\t', '\\"', '\\r');
- $to = array("\\", "''", "\n", "\t", '"', "\r");
- $s = str_replace($from, $to, $s);
- return $s;
- }
-
-
- protected function applySchema() {
- if ( !($this->mSchemaSet) ) {
- $this->mSchemaSet = true;
- $this->begin();
- $this->doQuery("SET SCHEMA = $this->mSchema");
- $this->commit();
- }
- }
-
-
- public function begin() {
-
- db2_autocommit($this->mConn, DB2_AUTOCOMMIT_OFF);
- $this->mTrxLevel = 1;
- }
-
-
- public function commit() {
- db2_commit($this->mConn);
-
- db2_autocommit($this->mConn, DB2_AUTOCOMMIT_ON);
- $this->mTrxLevel = 0;
- }
-
-
- public function rollback() {
- db2_rollback($this->mConn);
-
-
- db2_autocommit($this->mConn, DB2_AUTOCOMMIT_ON);
- $this->mTrxLevel = 0;
- }
-
-
- public function makeList( $a, $mode = LIST_COMMA ) {
- wfDebug("DB2::makeList()\n");
- if ( !is_array( $a ) ) {
- throw new DBUnexpectedError( $this, 'Database::makeList called with incorrect parameters' );
- }
- $first = true;
- $list = '';
- foreach ( $a as $field => $value ) {
- if ( !$first ) {
- if ( $mode == LIST_AND ) {
- $list .= ' AND ';
- } elseif($mode == LIST_OR) {
- $list .= ' OR ';
- } else {
- $list .= ',';
- }
- } else {
- $first = false;
- }
- if ( ($mode == LIST_AND || $mode == LIST_OR) && is_numeric( $field ) ) {
- $list .= "($value)";
- } elseif ( ($mode == LIST_SET) && is_numeric( $field ) ) {
- $list .= "$value";
- } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array($value) ) {
- if( count( $value ) == 0 ) {
- throw new MWException( __METHOD__.': empty input' );
- } elseif( count( $value ) == 1 ) {
-
-
-
- $value = array_values( $value );
- $list .= $field." = ".$this->addQuotes( $value[0] );
- } else {
- $list .= $field." IN (".$this->makeList($value).") ";
- }
- } elseif( is_null($value) ) {
- if ( $mode == LIST_AND || $mode == LIST_OR ) {
- $list .= "$field IS ";
- } elseif ( $mode == LIST_SET ) {
- $list .= "$field = ";
- }
- $list .= 'NULL';
- } else {
- if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
- $list .= "$field = ";
- }
- if ( $mode == LIST_NAMES ) {
- $list .= $value;
- }
-
-
- else if ( is_numeric($value) ) {
- $list .= $value;
- }
- else {
- $list .= $this->addQuotes( $value );
- }
- }
- }
- return $list;
- }
-
-
- public function makeListSmart( $table, $a, $mode = LIST_COMMA ) {
- if ( !is_array( $a ) ) {
- throw new DBUnexpectedError( $this, 'Database::makeList called with incorrect parameters' );
- }
- $first = true;
- $list = '';
- foreach ( $a as $field => $value ) {
- if ( !$first ) {
- if ( $mode == LIST_AND ) {
- $list .= ' AND ';
- } elseif($mode == LIST_OR) {
- $list .= ' OR ';
- } else {
- $list .= ',';
- }
- } else {
- $first = false;
- }
- if ( ($mode == LIST_AND || $mode == LIST_OR) && is_numeric( $field ) ) {
- $list .= "($value)";
- } elseif ( ($mode == LIST_SET) && is_numeric( $field ) ) {
- $list .= "$value";
- } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array($value) ) {
- if( count( $value ) == 0 ) {
- throw new MWException( __METHOD__.': empty input' );
- } elseif( count( $value ) == 1 ) {
-
-
-
- $value = array_values( $value );
- $list .= $field." = ".$this->addQuotes( $value[0] );
- } else {
- $list .= $field." IN (".$this->makeList($value).") ";
- }
- } elseif( is_null($value) ) {
- if ( $mode == LIST_AND || $mode == LIST_OR ) {
- $list .= "$field IS ";
- } elseif ( $mode == LIST_SET ) {
- $list .= "$field = ";
- }
- $list .= 'NULL';
- } else {
- if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
- $list .= "$field = ";
- }
- if ( $mode == LIST_NAMES ) {
- $list .= $value;
- }
- else {
- $list .= $this->addQuotesSmart( $table, $field, $value );
- }
- }
- }
- return $list;
- }
-
-
- public function limitResult($sql, $limit, $offset=false) {
- if( !is_numeric($limit) ) {
- throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
- }
- if( $offset ) {
- wfDebug("Offset parameter not supported in limitResult()\n");
- }
-
-
- return "$sql FETCH FIRST $limit ROWS ONLY ";
- }
-
-
- public function tableName( $name ) {
-
- switch( $name ) {
- case 'user':
- return 'mwuser';
- case 'text':
- return 'pagecontent';
- default:
- return $name;
- }
- }
-
-
- public function timestamp( $ts=0 ) {
-
- return wfTimestamp(TS_DB2,$ts);
- }
-
- public function nextSequenceValue( $seqName ) {
- $safeseq = preg_replace( "/'/", "''", $seqName );
- $res = $this->query( "VALUES NEXTVAL FOR $safeseq" );
- $row = $this->fetchRow( $res );
- $this->mInsertId = $row[0];
- $this->freeResult( $res );
- return $this->mInsertId;
- }
-
-
- public function insertId() {
- return $this->mInsertId;
- }
-
-
- public function insert( $table, $args, $fname = 'DatabaseIbm_db2::insert', $options = array() ) {
- wfDebug("DB2::insert($table)\n");
- if ( !count( $args ) ) {
- return true;
- }
- $table = $this->tableName( $table );
- if ( !is_array( $options ) )
- $options = array( $options );
- if ( isset( $args[0] ) && is_array( $args[0] ) ) {
- }
- else {
- $args = array($args);
- }
- $keys = array_keys( $args[0] );
-
- $ignore = in_array( 'IGNORE', $options ) ? 'mw' : '';
-
-
- $oldautocommit = db2_autocommit($this->mConn);
-
- $didbegin = 0;
- if (! $this->mTrxLevel) {
- $this->begin();
- $didbegin = 1;
- }
- if ( $ignore ) {
- $olde = error_reporting( 0 );
-
-
- $numrowsinserted = 0;
- }
- $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
- if ( !$ignore ) {
- $first = true;
- foreach ( $args as $row ) {
- if ( $first ) {
- $first = false;
- } else {
- $sql .= ',';
- }
- $sql .= '(' . $this->makeListSmart( $table, $row ) . ')';
- }
- $res = (bool)$this->query( $sql, $fname, $ignore );
- }
- else {
- $res = true;
- $origsql = $sql;
- foreach ( $args as $row ) {
- $tempsql = $origsql;
- $tempsql .= '(' . $this->makeListSmart( $table, $row ) . ')';
- if ( $ignore ) {
- db2_exec($this->mConn, "SAVEPOINT $ignore");
- }
- $tempres = (bool)$this->query( $tempsql, $fname, $ignore );
- if ( $ignore ) {
- $bar = db2_stmt_error();
- if ($bar != false) {
- db2_exec( $this->mConn, "ROLLBACK TO SAVEPOINT $ignore" );
- }
- else {
- db2_exec( $this->mConn, "RELEASE SAVEPOINT $ignore" );
- $numrowsinserted++;
- }
- }
-
-
- if (! $tempres)
- $res = false;
- }
- }
- if ($didbegin) {
- $this->commit();
- }
-
- else if ($oldautocommit)
- {
- $this->commit();
- }
-
- if ( $ignore ) {
- $olde = error_reporting( $olde );
-
- $this->mAffectedRows = $numrowsinserted;
-
- return true;
- }
-
- return $res;
- }
-
-
- function update( $table, $values, $conds, $fname = 'Database::update', $options = array() ) {
- $table = $this->tableName( $table );
- $opts = $this->makeUpdateOptions( $options );
- $sql = "UPDATE $opts $table SET " . $this->makeListSmart( $table, $values, LIST_SET );
- if ( $conds != '*' ) {
- $sql .= " WHERE " . $this->makeListSmart( $table, $conds, LIST_AND );
- }
- return $this->query( $sql, $fname );
- }
-
-
- function delete( $table, $conds, $fname = 'Database::delete' ) {
- if ( !$conds ) {
- throw new DBUnexpectedError( $this, 'Database::delete() called with no conditions' );
- }
- $table = $this->tableName( $table );
- $sql = "DELETE FROM $table";
- if ( $conds != '*' ) {
- $sql .= ' WHERE ' . $this->makeListSmart( $table, $conds, LIST_AND );
- }
- return $this->query( $sql, $fname );
- }
-
-
- public function affectedRows() {
- if ( !is_null( $this->mAffectedRows ) ) {
-
- return $this->mAffectedRows;
- }
- if( empty( $this->mLastResult ) )
- return 0;
- return db2_num_rows( $this->mLastResult );
- }
-
-
- public function useIndexClause( $index ) {
- return "";
- }
-
-
- function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseIbm_db2::replace' ) {
- $table = $this->tableName( $table );
- if (count($rows)==0) {
- return;
- }
-
- if ( !is_array( reset( $rows ) ) ) {
- $rows = array( $rows );
- }
- foreach( $rows as $row ) {
-
- if ( $uniqueIndexes ) {
- $sql = "DELETE FROM $table WHERE ";
- $first = true;
- foreach ( $uniqueIndexes as $index ) {
- if ( $first ) {
- $first = false;
- $sql .= "(";
- } else {
- $sql .= ') OR (';
- }
- if ( is_array( $index ) ) {
- $first2 = true;
- foreach ( $index as $col ) {
- if ( $first2 ) {
- $first2 = false;
- } else {
- $sql .= ' AND ';
- }
- $sql .= $col.'=' . $this->addQuotes( $row[$col] );
- }
- } else {
- $sql .= $index.'=' . $this->addQuotes( $row[$index] );
- }
- }
- $sql .= ')';
- $this->query( $sql, $fname );
- }
-
- $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
- $this->makeList( $row, LIST_COMMA ) . ')';
- $this->query( $sql, $fname );
- }
- }
-
-
- public function numRows( $res ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- if ( $this->mNumRows ) {
- return $this->mNumRows;
- }
- else {
- return 0;
- }
- }
-
-
- public function dataSeek( $res, $row ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- return db2_fetch_row( $res, $row );
- }
-
-
-
-
-
-
- public function freeResult( $res ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- if ( !@db2_free_result( $res ) ) {
- throw new DBUnexpectedError($this, "Unable to free DB2 result\n" );
- }
- }
-
-
- public function numFields( $res ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- return db2_num_fields( $res );
- }
-
-
- public function fieldName( $res, $n ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- return db2_field_name( $res, $n );
- }
-
-
- public function select( $table, $vars, $conds='', $fname = 'DatabaseIbm_db2::select', $options = array(), $join_conds = array() )
- {
- $res = parent::select( $table, $vars, $conds, $fname, $options, $join_conds );
-
-
- if ( isset( $options['LIMIT'] ) ) {
- if ( isset ($options['OFFSET'] ) ) {
- $limit = $options['LIMIT'];
- $offset = $options['OFFSET'];
- }
- }
-
-
-
-
-
-
-
- $vars2 = array('count(*) as num_rows');
-
- $options2 = array();
- if ( isset( $options['LIMIT'] ) ) $options2['LIMIT'] = $options['LIMIT'];
-
- if ( isset( $options['GROUP BY'] ) ) return $res;
-
- $res2 = parent::select( $table, $vars2, $conds, $fname, $options2, $join_conds );
- $obj = $this->fetchObject($res2);
- $this->mNumRows = $obj->num_rows;
-
- wfDebug("DatabaseIbm_db2::select: There are $this->mNumRows rows.\n");
-
- return $res;
- }
-
-
- function makeSelectOptions( $options ) {
- $preLimitTail = $postLimitTail = '';
- $startOpts = '';
- $noKeyOptions = array();
- foreach ( $options as $key => $option ) {
- if ( is_numeric( $key ) ) {
- $noKeyOptions[$option] = true;
- }
- }
- if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
- if ( isset( $options['HAVING'] ) ) $preLimitTail .= " HAVING {$options['HAVING']}";
- if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
-
- if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
-
- return array( $startOpts, '', $preLimitTail, $postLimitTail );
- }
-
-
- public function getSoftwareLink() {
- return "[http://www.ibm.com/software/data/db2/express/?s_cmp=ECDDWW01&s_tact=MediaWiki IBM DB2]";
- }
-
-
- public function selectDB( $db ) {
- return true;
- }
-
-
- public function conditional( $cond, $trueVal, $falseVal ) {
- return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
- }
-
-
-
-
-
- public function getSearchEngine() {
- return "SearchIBM_DB2";
- }
-
-
-
-
-
- public function wasDeadlock() {
-
- $err = $this->lastErrno();
- switch($err) {
- case '40001':
- case '57011':
- case '57033':
- wfDebug("In a deadlock because of SQLSTATE $err");
- return true;
- }
- return false;
- }
-
-
- public function ping() {
-
-
- $this->close();
- if ($this->mCataloged == NULL) {
- return false;
- }
- else if ($this->mCataloged) {
- $this->mConn = $this->openCataloged($this->mDBName, $this->mUser, $this->mPassword);
- }
- else if (!$this->mCataloged) {
- $this->mConn = $this->openUncataloged($this->mDBName, $this->mUser, $this->mPassword, $this->mServer, $this->mPort);
- }
- return false;
- }
-
-
-
-
- public function getStatus( $which ) { wfDebug('Not implemented for DB2: getStatus()'); return ''; }
-
- public function setTimeout( $timeout ) { wfDebug('Not implemented for DB2: setTimeout()'); }
-
- public function lock( $lockName, $method ) { wfDebug('Not implemented for DB2: lock()'); return true; }
-
- public function unlock( $lockName, $method ) { wfDebug('Not implemented for DB2: unlock()'); return true; }
-
- public function setFakeSlaveLag( $lag ) { wfDebug('Not implemented for DB2: setFakeSlaveLag()'); }
-
- public function setFakeMaster( $enabled ) { wfDebug('Not implemented for DB2: setFakeMaster()'); }
-
-
- public function limitResultForUpdate($sql, $num) { return $sql; }
-
- public function lowPriorityOption() { return ''; }
-
-
-
-
-
-
- public function fieldExists( $table, $field, $fname = 'DatabaseIbm_db2::fieldExists' ) {
- $table = $this->tableName( $table );
- $schema = $this->mSchema;
- $etable = preg_replace("/'/", "''", $table);
- $eschema = preg_replace("/'/", "''", $schema);
- $ecol = preg_replace("/'/", "''", $field);
- $sql = <<<SQL
- SELECT 1 as fieldexists
- FROM sysibm.syscolumns sc
- WHERE sc.name='$ecol' AND sc.tbname='$etable' AND sc.tbcreator='$eschema'
- SQL;
- $res = $this->query( $sql, $fname );
- $count = $res ? $this->numRows($res) : 0;
- if ($res)
- $this->freeResult( $res );
- return $count;
- }
-
-
- public function indexInfo( $table, $index, $fname = 'DatabaseIbm_db2::indexExists' ) {
- $table = $this->tableName( $table );
- $sql = <<<SQL
- SELECT name as indexname
- FROM sysibm.sysindexes si
- WHERE si.name='$index' AND si.tbname='$table' AND sc.tbcreator='$this->mSchema'
- SQL;
- $res = $this->query( $sql, $fname );
- if ( !$res ) {
- return NULL;
- }
- $row = $this->fetchObject( $res );
- if ($row != NULL) return $row;
- else return false;
- }
-
-
- public function fieldInfo( $table, $field ) {
- return IBM_DB2Field::fromText($this, $table, $field);
- }
-
-
- public function fieldType( $res, $index ) {
- if ( $res instanceof ResultWrapper ) {
- $res = $res->result;
- }
- return db2_field_type( $res, $index );
- }
-
-
- public function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
- $table = $this->tableName( $table );
- $sql = <<<SQL
- SELECT si.name as indexname
- FROM sysibm.sysindexes si
- WHERE si.name='$index' AND si.tbname='$table' AND sc.tbcreator='$this->mSchema'
- AND si.uniquerule IN ('U', 'P')
- SQL;
- $res = $this->query( $sql, $fname );
- if ( !$res ) {
- return null;
- }
- if ($this->fetchObject( $res )) {
- return true;
- }
- return false;
- }
-
-
- public function textFieldSize( $table, $field ) {
- $table = $this->tableName( $table );
- $sql = <<<SQL
- SELECT length as size
- FROM sysibm.syscolumns sc
- WHERE sc.name='$field' AND sc.tbname='$table' AND sc.tbcreator='$this->mSchema'
- SQL;
- $res = $this->query($sql);
- $row = $this->fetchObject($res);
- $size = $row->size;
- $this->freeResult( $res );
- return $size;
- }
-
-
- public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "DatabaseIbm_db2::deleteJoin" ) {
- if ( !$conds ) {
- throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
- }
- $delTable = $this->tableName( $delTable );
- $joinTable = $this->tableName( $joinTable );
- $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
- if ( $conds != '*' ) {
- $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
- }
- $sql .= ')';
- $this->query( $sql, $fname );
- }
-
-
- public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) {
- $rows = 0;
- $res = $this->select ($table, 'COUNT(*) as mwrowcount', $conds, $fname, $options );
- if ($res) {
- $row = $this->fetchRow($res);
- $rows = (isset($row['mwrowcount'])) ? $row['mwrowcount'] : 0;
- }
- $this->freeResult($res);
- return $rows;
- }
-
-
- public function encodeBlob($b) {
- return new IBM_DB2Blob($b);
- }
-
-
- public function decodeBlob($b) {
- return $b->getData();
- }
-
-
- public function buildConcat( $stringList ) {
-
-
- return implode( ' || ', $stringList );
- }
-
-
- public function extractUnixEpoch( $column ) {
-
-
- }
- }
- ?>
|