/** * Constructor for simpledb Proxy * Use the values from the configuration file provided to create a PDO for the database * Query the database to obtain column metadata and primary key * * @param string $target Target * @param string $immediate_caller_directory Directory * @param string $binding_config Config */ public function __construct($target, $immediate_caller_directory, $binding_config) { SCA::$logger->log('Entering constructor'); try { $this->table = $target; $this->config = SCA_Helper::mergeBindingIniAndConfig($binding_config, $immediate_caller_directory); if (array_key_exists('username', $this->config)) { $username = $this->config['username']; } else { $username = null; } if (array_key_exists('password', $this->config)) { $password = $this->config['password']; } else { $password = null; } if (array_key_exists('namespace', $this->config)) { $this->namespace = $this->config['namespace']; } if (array_key_exists('case', $this->config)) { $this->case = $this->config['case']; } else { $this->case = 'lower'; } if (!array_key_exists('dsn', $this->config)) { throw new SCA_RuntimeException("Data source name should be specified"); } $tableName = $this->table; // Special processing for IBM databases: // IBM table names can contain schema name as prefix // Column metadata returned by pdo_ibm does not specify the primary key // Hence primary key for IBM databases has to be obtained using // db2_primary_key. if (strpos($this->config["dsn"], "ibm:") === 0 || strpos($this->config["dsn"], "IBM:") === 0) { $this->isIBM = true; // Table could be of format schemaName.tableName $schemaName = null; if (($pos = strrpos($tableName, '.')) !== false) { $schemaName = substr($tableName, 0, $pos); $tableName = substr($tableName, $pos + 1); } // DSN for IBM databases can be a database name or a connection string // Both can be passed onto db2_connect. Remove the dsn prefix if specified $database = substr($this->config["dsn"], 4); if (strpos($database, "dsn=") === 0 || strpos($database, "DSN=") === 0) { $database = substr($database, 4); } // Need to make sure the name is in DB2 uppercase style $db2TableName = strtoupper($tableName); $conn = db2_connect($database, $username, $password); $stmt = db2_primary_keys($conn, null, $schemaName, $db2TableName); $keys = db2_fetch_array($stmt); if (count($keys) > 3) { $this->primary_key = $keys[3]; } else { throw new SCA_RuntimeException("Table '{$tableName}' does not appear to have a primary key."); } } $this->table_name = $this->_getName($tableName); if ($username != null) { $this->pdo = new PDO($this->config["dsn"], $username, $password, $this->config); } else { $this->pdo = new PDO($this->config["dsn"]); } $this->pdo_driver = $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); $stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table); if (!$stmt->execute()) { throw new SCA_RuntimeException(self::_getPDOError($stmt, "select")); } $columns = array(); for ($i = 0; $i < $stmt->columnCount(); $i++) { $meta = $stmt->getColumnMeta($i); $name = $this->_getName($meta["name"]); if (in_array("primary_key", $meta["flags"], true)) { $this->primary_key = $name; } $columns[] = $name; } //$pk = $this->_getName($this->primary_key); SCA::$logger->log("Table {$tableName} PrimaryKey {$this->primary_key}"); /* $metadata = array( 'name' => $this->table_name, 'columns' => $columns, 'PK' => $pk ); */ $this->datafactory = SDO_DAS_DataFactory::getDataFactory(); // Define the model on the data factory (from the database) $this->datafactory->addType(SCA_Bindings_simpledb_Proxy::ROOT_NS, SCA_Bindings_simpledb_Proxy::ROOT_TYPE); $this->datafactory->addType($this->namespace, $this->table_name); foreach ($columns as $name) { $this->datafactory->addPropertyToType($this->namespace, $this->table_name, $name, 'commonj.sdo', 'String'); } $this->datafactory->addPropertyToType(SCA_Bindings_simpledb_Proxy::ROOT_NS, SCA_Bindings_simpledb_Proxy::ROOT_TYPE, $this->table_name, $this->namespace, $this->table_name, array('many' => true)); } catch (Exception $e) { throw new SCA_RuntimeException($e->getMessage()); } SCA::$logger->log("Exiting constructor"); }
function &MetaColumns($table) { global $ADODB_FETCH_MODE; $false = false; if ($this->uCaseTables) { $table = strtoupper($table); } $schema = ''; $this->_findschema($table, $schema); $savem = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $colname = "%"; $qid = db2_columns($this->_connectionID, "", $schema, $table, $colname); if (empty($qid)) { return $false; } $rs =& new ADORecordSet_db2($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) { return $false; } $rs->_fetch(); $retarr = array(); /* $rs->fields indices 0 TABLE_QUALIFIER 1 TABLE_SCHEM 2 TABLE_NAME 3 COLUMN_NAME 4 DATA_TYPE 5 TYPE_NAME 6 PRECISION 7 LENGTH 8 SCALE 9 RADIX 10 NULLABLE 11 REMARKS */ while (!$rs->EOF) { if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { $fld = new ADOFieldObject(); $fld->name = $rs->fields[3]; $fld->type = $this->DB2Types($rs->fields[4]); // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp // access uses precision to store length for char/varchar if ($fld->type == 'C' or $fld->type == 'X') { if ($rs->fields[4] <= -95) { // UNICODE $fld->max_length = $rs->fields[7] / 2; } else { $fld->max_length = $rs->fields[7]; } } else { $fld->max_length = $rs->fields[7]; } $fld->not_null = !empty($rs->fields[10]); $fld->scale = $rs->fields[8]; $fld->primary_key = false; $retarr[strtoupper($fld->name)] = $fld; } else { if (sizeof($retarr) > 0) { break; } } $rs->MoveNext(); } $rs->Close(); if (empty($retarr)) { $retarr = false; } $qid = db2_primary_keys($this->_connectionID, "", $schema, $table); if (empty($qid)) { return $false; } $rs =& new ADORecordSet_db2($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) { return $retarr; } $rs->_fetch(); /* $rs->fields indices 0 TABLE_CAT 1 TABLE_SCHEM 2 TABLE_NAME 3 COLUMN_NAME 4 KEY_SEQ 5 PK_NAME */ while (!$rs->EOF) { if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { $retarr[strtoupper($rs->fields[3])]->primary_key = true; } else { if (sizeof($retarr) > 0) { break; } } $rs->MoveNext(); } $rs->Close(); if (empty($retarr)) { $retarr = false; } return $retarr; }
/** * Given a table name and a hash of columns with values * Removes primary key columns from the hash where the value is NULL * * @param $table String: name of the table * @param $args Array of hashes of column names with values * @return Array: tuple containing filtered array of columns, array of primary keys */ private function removeNullPrimaryKeys($table, $args) { $schema = $this->mSchema; // find out the primary keys $keyres = db2_primary_keys($this->mConn, null, strtoupper($schema), strtoupper($table)); $keys = array(); for ($row = $this->fetchObject($keyres); $row != null; $row = $this->fetchRow($keyres)) { $keys[] = strtolower($row->column_name); } // remove primary keys foreach ($args as $ai => $row) { foreach ($keys as $ki => $key) { if ($row[$key] == null) { unset($row[$key]); } } $args[$ai] = $row; } // return modified hash return array($args, $keys); }