function _adodb_getinsertsql(&$zthis, &$rs, $arrFields, $magicq = false) { $values = ''; $fields = ''; $arrFields = _array_change_key_case($arrFields); if (!$rs) { printf(ADODB_BAD_RS, 'GetInsertSQL'); return false; } $fieldInsertedCount = 0; // Loop through all of the fields in the recordset for ($i = 0, $max = $rs->FieldCount(); $i < $max; $i++) { // Get the field from the recordset $field = $rs->FetchField($i); // If the recordset field is one // of the fields passed in then process. $upperfname = strtoupper($field->name); if (adodb_key_exists($upperfname, $arrFields)) { // Set the counter for the number of fields that will be inserted. $fieldInsertedCount++; // Get the name of the fields to insert $fields .= $field->name . ", "; $mt = $rs->MetaType($field->type); // "mike" <*****@*****.**> patch and "Ryan Bailey" <*****@*****.**> //PostgreSQL uses a 't' or 'f' and therefore needs to be processed as a string ('C') type field. if (strncmp($zthis->databaseType, "postgres", 8) === 0 && $mt == "L") { $mt = "C"; } // Based on the datatype of the field // Format the value properly for the database if (defined('ADODB_FORCE_NULLS') && is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === 'null') { $values .= "null, "; } else { switch ($mt) { case "C": case "X": case 'B': $values .= $zthis->qstr($arrFields[$upperfname], $magicq) . ", "; break; case "D": $values .= $zthis->DBDate($arrFields[$upperfname]) . ", "; break; case "T": $values .= $zthis->DBTimeStamp($arrFields[$upperfname]) . ", "; break; default: $val = $arrFields[$upperfname]; /*if (!is_numeric($val)) { if (strncmp($val,'=',1) == 0) $val = substr($val,1); else $val = (float) $val; }*/ if (empty($val)) { $val = '0'; } $values .= $val . ", "; break; } } } } // If there were any inserted fields then build the rest of the insert query. if ($fieldInsertedCount <= 0) { return false; } // Get the table name from the existing query. preg_match("/FROM\\s+" . ADODB_TABLE_REGEX . "/is", $rs->sql, $tableName); // Strip off the comma and space on the end of both the fields // and their values. $fields = substr($fields, 0, -2); $values = substr($values, 0, -2); // Append the fields and their values to the insert query. $insertSQL = "INSERT INTO " . $tableName[1] . " ( {$fields} ) VALUES ( {$values} )"; return $insertSQL; }
/** * There is a special case of this function for the oci8 driver. * The proper way to handle an insert w/ a blob in oracle requires * a returning clause with bind variables and a descriptor blob. * * */ function _adodb_getinsertsql(&$zthis, &$rs, $arrFields, $magicq = false, $force = 2) { static $cacheRS = false; static $cacheSig = 0; static $cacheCols; global $ADODB_QUOTE_FIELDNAMES; $tableName = ''; $values = ''; $fields = ''; $recordSet = null; $arrFields = _array_change_key_case($arrFields); $fieldInsertedCount = 0; if (is_string($rs)) { //ok we have a table name //try and get the column info ourself. $tableName = $rs; //we need an object for the recordSet //because we have to call MetaType. //php can't do a $rsclass::MetaType() $rsclass = $zthis->rsPrefix . $zthis->databaseType; $recordSet = new $rsclass(-1, $zthis->fetchMode); $recordSet->connection = $zthis; if (is_string($cacheRS) && $cacheRS == $rs) { $columns = $cacheCols; } else { $columns = $zthis->MetaColumns($tableName); $cacheRS = $tableName; $cacheCols = $columns; } } else { if (is_subclass_of($rs, 'adorecordset')) { if (isset($rs->insertSig) && is_integer($cacheRS) && $cacheRS == $rs->insertSig) { $columns = $cacheCols; } else { for ($i = 0, $max = $rs->FieldCount(); $i < $max; $i++) { $columns[] = $rs->FetchField($i); } $cacheRS = $cacheSig; $cacheCols = $columns; $rs->insertSig = $cacheSig++; } $recordSet = $rs; } else { printf(ADODB_BAD_RS, 'GetInsertSQL'); return false; } } // Loop through all of the fields in the recordset foreach ($columns as $field) { $upperfname = strtoupper($field->name); if (adodb_key_exists($upperfname, $arrFields, $force)) { $bad = false; if (strpos($upperfname, ' ') !== false || $ADODB_QUOTE_FIELDNAMES) { switch ($ADODB_QUOTE_FIELDNAMES) { case 'LOWER': $fnameq = $zthis->nameQuote . strtolower($field->name) . $zthis->nameQuote; break; case 'NATIVE': $fnameq = $zthis->nameQuote . $field->name . $zthis->nameQuote; break; case 'UPPER': default: $fnameq = $zthis->nameQuote . $upperfname . $zthis->nameQuote; break; } } else { $fnameq = $upperfname; } $type = $recordSet->MetaType($field->type); /********************************************************/ if (is_null($arrFields[$upperfname]) || empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0 || $arrFields[$upperfname] === $zthis->null2null) { switch ($force) { case 0: // we must always set null if missing $bad = true; break; case 1: $values .= "null, "; break; case 2: //Set empty $arrFields[$upperfname] = ""; $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq); break; default: case 3: //Set the value that was given in array, so you can give both null and empty values if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) { $values .= "null, "; } else { $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq); } break; } // switch /*********************************************************/ } else { //we do this so each driver can customize the sql for //DB specific column types. //Oracle needs BLOB types to be handled with a returning clause //postgres has special needs as well $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq); } if ($bad) { continue; } // Set the counter for the number of fields that will be inserted. $fieldInsertedCount++; // Get the name of the fields to insert $fields .= $fnameq . ", "; } } // If there were any inserted fields then build the rest of the insert query. if ($fieldInsertedCount <= 0) { return false; } // Get the table name from the existing query. if (!$tableName) { if (!empty($rs->tableName)) { $tableName = $rs->tableName; } else { if (preg_match("/FROM\\s+" . ADODB_TABLE_REGEX . "/is", $rs->sql, $tableName)) { $tableName = $tableName[1]; } else { return false; } } } // Strip off the comma and space on the end of both the fields // and their values. $fields = substr($fields, 0, -2); $values = substr($values, 0, -2); // Append the fields and their values to the insert query. return 'INSERT INTO ' . $tableName . ' ( ' . $fields . ' ) VALUES ( ' . $values . ' )'; }
/** * There is a special case of this function for the oci8 driver. * The proper way to handle an insert w/ a blob in oracle requires * a returning clause with bind variables and a descriptor blob. * * */ function _adodb_getinsertsql(&$zthis, &$rs, $arrFields, $magicq = false, $forcenulls = false) { $tableName = ''; $values = ''; $fields = ''; $recordSet = null; $arrFields = _array_change_key_case($arrFields); $fieldInsertedCount = 0; if (is_string($rs)) { //ok we have a table name //try and get the column info ourself. $tableName = $rs; //we need an object for the recordSet //because we have to call MetaType. //php can't do a $rsclass::MetaType() $rsclass = $zthis->rsPrefix . $zthis->databaseType; $recordSet =& new $rsclass(-1, $zthis->fetchMode); $recordSet->connection =& $zthis; $columns = $zthis->MetaColumns($tableName); } else { if (is_subclass_of($rs, 'adorecordset')) { for ($i = 0, $max = $rs->FieldCount(); $i < $max; $i++) { $columns[] = $rs->FetchField($i); } $recordSet =& $rs; } else { printf(ADODB_BAD_RS, 'GetInsertSQL'); return false; } } // Loop through all of the fields in the recordset foreach ($columns as $field) { $upperfname = strtoupper($field->name); if (adodb_key_exists($upperfname, $arrFields)) { // Set the counter for the number of fields that will be inserted. $fieldInsertedCount++; if (strpos($upperfname, ' ') !== false) { $fnameq = $zthis->nameQuote . $upperfname . $zthis->nameQuote; } else { $fnameq = $upperfname; } // Get the name of the fields to insert $fields .= $fnameq . ", "; $type = $recordSet->MetaType($field->type); if ($forcenulls && is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === 'null') { $values .= "null, "; } else { //we do this so each driver can customize the sql for //DB specific column types. //Oracle needs BLOB types to be handled with a returning clause //postgres has special needs as well $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq); } } } // If there were any inserted fields then build the rest of the insert query. if ($fieldInsertedCount <= 0) { return false; } // Get the table name from the existing query. if (!$tableName) { preg_match("/FROM\\s+" . ADODB_TABLE_REGEX . "/is", $rs->sql, $tableName); $tableName = $tableName[1]; } // Strip off the comma and space on the end of both the fields // and their values. $fields = substr($fields, 0, -2); $values = substr($values, 0, -2); // Append the fields and their values to the insert query. return 'INSERT INTO ' . $tableName . ' ( ' . $fields . ' ) VALUES ( ' . $values . ' )'; }