function _GenFields($flds, $widespacing = false) { if (is_string($flds)) { $padding = ' '; $txt = $flds . $padding; $flds = array(); $flds0 = Lens_ParseArgs($txt, ','); $hasparam = false; foreach ($flds0 as $f0) { $f1 = array(); foreach ($f0 as $token) { switch (strtoupper($token)) { case 'INDEX': $f1['INDEX'] = ''; // fall through intentionally // fall through intentionally case 'CONSTRAINT': case 'DEFAULT': $hasparam = $token; break; default: if ($hasparam) { $f1[$hasparam] = $token; } else { $f1[] = $token; } $hasparam = false; break; } } // 'index' token without a name means single column index: name it after column if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') { $f1['INDEX'] = isset($f0['NAME']) ? $f0['NAME'] : $f0[0]; // check if column name used to create an index name was quoted if (($f1['INDEX'][0] == '"' || $f1['INDEX'][0] == "'" || $f1['INDEX'][0] == "`") && $f1['INDEX'][0] == substr($f1['INDEX'], -1)) { $f1['INDEX'] = $f1['INDEX'][0] . 'idx_' . substr($f1['INDEX'], 1, -1) . $f1['INDEX'][0]; } else { $f1['INDEX'] = 'idx_' . $f1['INDEX']; } } // reset it, so we don't get next field 1st token as INDEX... $hasparam = false; $flds[] = $f1; } } $this->autoIncrement = false; $lines = array(); $pkey = array(); $idxs = array(); foreach ($flds as $fld) { $fld = _array_change_key_case($fld); $fname = false; $fdefault = false; $fautoinc = false; $ftype = false; $fsize = false; $fprec = false; $fprimary = false; $fnoquote = false; $fdefts = false; $fdefdate = false; $fconstraint = false; $fnotnull = false; $funsigned = false; $findex = ''; $funiqueindex = false; //----------------- // Parse attributes foreach ($fld as $attr => $v) { if ($attr == 2 && is_numeric($v)) { $attr = 'SIZE'; } else { if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) { $attr = strtoupper($v); } } switch ($attr) { case '0': case 'NAME': $fname = $v; break; case '1': case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break; case 'SIZE': $dotat = strpos($v, '.'); if ($dotat === false) { $dotat = strpos($v, ','); } if ($dotat === false) { $fsize = $v; } else { $fsize = substr($v, 0, $dotat); $fprec = substr($v, $dotat + 1); } break; case 'UNSIGNED': $funsigned = true; break; case 'AUTOINCREMENT': case 'AUTO': $fautoinc = true; $fnotnull = true; break; case 'KEY': // a primary key col can be non unique in itself (if key spans many cols...) // a primary key col can be non unique in itself (if key spans many cols...) case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break; case 'DEF': case 'DEFAULT': $fdefault = $v; break; case 'NOTNULL': $fnotnull = $v; break; case 'NOQUOTE': $fnoquote = $v; break; case 'DEFDATE': $fdefdate = $v; break; case 'DEFTIMESTAMP': $fdefts = $v; break; case 'CONSTRAINT': $fconstraint = $v; break; // let INDEX keyword create a 'very standard' index on column // let INDEX keyword create a 'very standard' index on column case 'INDEX': $findex = $v; break; case 'UNIQUE': $funiqueindex = true; break; } //switch } // foreach $fld //-------------------- // VALIDATE FIELD INFO if (!strlen($fname)) { if ($this->debug) { ADOConnection::outp("Undefined NAME"); } return false; } $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname)); $fname = $this->NameQuote($fname); if (!strlen($ftype)) { if ($this->debug) { ADOConnection::outp("Undefined TYPE for field '{$fname}'"); } return false; } else { $ftype = strtoupper($ftype); } $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec); 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; } // build list of indexes if ($findex != '') { if (array_key_exists($findex, $idxs)) { $idxs[$findex]['cols'][] = $fname; if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) { if ($this->debug) { ADOConnection::outp("Index {$findex} defined once UNIQUE and once not"); } } if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts'])) { $idxs[$findex]['opts'][] = 'UNIQUE'; } } else { $idxs[$findex] = array(); $idxs[$findex]['cols'] = array($fname); if ($funiqueindex) { $idxs[$findex]['opts'] = array('UNIQUE'); } else { $idxs[$findex]['opts'] = array(); } } } //-------------------- // CONSTRUCT FIELD SQL if ($fdefts) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysTimeStamp; } } else { if ($fdefdate) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysDate; } } else { if ($fdefault !== false && !$fnoquote) { if ($ty == 'C' or $ty == 'X' or substr($fdefault, 0, 1) != "'" && !is_numeric($fdefault)) { if (($ty == 'D' || $ty == 'T') && strtolower($fdefault) != 'null') { // convert default date into database-aware code if ($ty == 'T') { $fdefault = $this->connection->DBTimeStamp($fdefault); } else { $fdefault = $this->connection->DBDate($fdefault); } } else { if (strlen($fdefault) != 1 && substr($fdefault, 0, 1) == ' ' && substr($fdefault, strlen($fdefault) - 1) == ' ') { $fdefault = trim($fdefault); } else { if (strtolower($fdefault) != 'null') { $fdefault = $this->connection->qstr($fdefault); } } } } } } } $suffix = $this->_CreateSuffix($fname, $ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint, $funsigned); // add index creation if ($widespacing) { $fname = str_pad($fname, 24); } // check for field names appearing twice if (array_key_exists($fid, $lines)) { ADOConnection::outp("Field '{$fname}' defined twice"); } $lines[$fid] = $fname . ' ' . $ftype . $suffix; if ($fautoinc) { $this->autoIncrement = true; } } // foreach $flds return array($lines, $pkey, $idxs); }
/** * 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 . ' )'; }
function _GenFields($flds, $widespacing = false) { if (is_string($flds)) { $padding = ' '; $txt = $flds . $padding; $flds = array(); $flds0 = Lens_ParseArgs($txt, ','); $hasparam = false; foreach ($flds0 as $f0) { $f1 = array(); foreach ($f0 as $token) { switch (strtoupper($token)) { case 'CONSTRAINT': case 'DEFAULT': $hasparam = $token; break; default: if ($hasparam) { $f1[$hasparam] = $token; } else { $f1[] = $token; } $hasparam = false; break; } } $flds[] = $f1; } } $this->autoIncrement = false; $lines = array(); $pkey = array(); foreach ($flds as $fld) { $fld = _array_change_key_case($fld); $fname = false; $fdefault = false; $fautoinc = false; $ftype = false; $fsize = false; $fprec = false; $fprimary = false; $fnoquote = false; $fdefts = false; $fdefdate = false; $fconstraint = false; $fnotnull = false; $funsigned = false; //----------------- // Parse attributes foreach ($fld as $attr => $v) { if ($attr == 2 && is_numeric($v)) { $attr = 'SIZE'; } else { if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) { $attr = strtoupper($v); } } switch ($attr) { case '0': case 'NAME': $fname = $v; break; case '1': case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break; case 'SIZE': $dotat = strpos($v, '.'); if ($dotat === false) { $dotat = strpos($v, ','); } if ($dotat === false) { $fsize = $v; } else { $fsize = substr($v, 0, $dotat); $fprec = substr($v, $dotat + 1); } break; case 'UNSIGNED': $funsigned = true; break; case 'AUTOINCREMENT': case 'AUTO': $fautoinc = true; $fnotnull = true; break; case 'KEY': case 'PRIMARY': $fprimary = $v; $fnotnull = true; break; case 'DEF': case 'DEFAULT': $fdefault = $v; break; case 'NOTNULL': $fnotnull = $v; break; case 'NOQUOTE': $fnoquote = $v; break; case 'DEFDATE': $fdefdate = $v; break; case 'DEFTIMESTAMP': $fdefts = $v; break; case 'CONSTRAINT': $fconstraint = $v; break; } //switch } // foreach $fld //-------------------- // VALIDATE FIELD INFO if (!strlen($fname)) { if ($this->debug) { ADOConnection::outp("Undefined NAME"); } return false; } $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname)); $fname = $this->NameQuote($fname); if (!strlen($ftype)) { if ($this->debug) { ADOConnection::outp("Undefined TYPE for field '{$fname}'"); } return false; } else { $ftype = strtoupper($ftype); } $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec); 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; } //-------------------- // CONSTRUCT FIELD SQL if ($fdefts) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysTimeStamp; } } else { if ($fdefdate) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysDate; } } else { if ($fdefault !== false && !$fnoquote) { if ($ty == 'C' or $ty == 'X' or substr($fdefault, 0, 1) != "'" && !is_numeric($fdefault)) { if (strlen($fdefault) != 1 && substr($fdefault, 0, 1) == ' ' && substr($fdefault, strlen($fdefault) - 1) == ' ') { $fdefault = trim($fdefault); } else { if (strtolower($fdefault) != 'null') { $fdefault = $this->connection->qstr($fdefault); } } } } } } $suffix = $this->_CreateSuffix($fname, $ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint, $funsigned); if ($widespacing) { $fname = str_pad($fname, 24); } $lines[$fid] = $fname . ' ' . $ftype . $suffix; if ($fautoinc) { $this->autoIncrement = true; } } // foreach $flds return array($lines, $pkey); }
function _GenFields($flds) { $lines = array(); $pkey = array(); foreach ($flds as $fld) { $fld = _array_change_key_case($fld); $fname = false; $fdefault = false; $fautoinc = false; $ftype = false; $fsize = false; $fprec = false; $fprimary = false; $fnoquote = false; $fdefts = false; $fdefdate = false; $fconstraint = false; $fnotnull = false; //----------------- // Parse attributes foreach ($fld as $attr => $v) { if ($attr == 2 && is_numeric($v)) { $attr = 'SIZE'; } else { if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) { $attr = strtoupper($v); } } switch ($attr) { case '0': case 'NAME': $fname = $v; break; case '1': case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break; case 'SIZE': $dotat = strpos($v, '.'); if ($dotat === false) { $fsize = $v; } else { $fsize = substr($v, 0, $dotat); $fprec = substr($v, $dotat + 1); } break; case 'AUTOINCREMENT': case 'AUTO': $fautoinc = true; $fnotnull = true; break; case 'KEY': case 'PRIMARY': $fprimary = $v; $fnotnull = true; break; case 'DEFAULT': $fdefault = $v; break; case 'NOTNULL': $fnotnull = $v; break; case 'NOQUOTE': $fnoquote = $v; break; case 'DEFDATE': $fdefdate = $v; break; case 'DEFTIMESTAMP': $fdefts = $v; break; case 'CONSTRAINT': $fconstraint = $v; break; } //switch } // foreach $fld //-------------------- // VALIDATE FIELD INFO if (!strlen($fname)) { if ($this->debug) { ADOConnection::outp("Undefined NAME"); } return false; } if (!strlen($ftype)) { if ($this->debug) { ADOConnection::outp("Undefined TYPE for field '{$fname}'"); } return false; } else { $ftype = strtoupper($ftype); } $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec); if ($fprimary) { $pkey[] = $fname; } // some databases do not allow blobs to have defaults if ($ty == 'X') { $fdefault = false; } //-------------------- // CONSTRUCT FIELD SQL if ($fdefts) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysTimeStamp; } } else { if ($fdefdate) { if (substr($this->connection->databaseType, 0, 5) == 'mysql') { $ftype = 'TIMESTAMP'; } else { $fdefault = $this->connection->sysDate; } } else { if (strlen($fdefault) && !$fnoquote) { if ($ty == 'C' or $ty == 'X' or substr($fdefault, 0, 1) != "'" && !is_numeric($fdefault)) { if (substr($fdefault, 0, 1) == ' ' && substr($fdefault, strlen($fdefault) - 1) == ' ') { $fdefault = trim($fdefault); } else { $fdefault = $this->connection->qstr($fdefault); } } } } } $suffix = $this->_CreateSuffix($fname, $ftype, $fnotnull, $fdefault, $fautoinc, $fconstraint); $fname = str_pad($fname, 16); $lines[] = "{$fname} {$ftype}{$suffix}"; } // foreach $flds return array($lines, $pkey); }
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; // Get the table name from the existing query. preg_match("/FROM\\s+" . ADODB_TABLE_REGEX . "/i", $rs->sql, $tableName); // 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 (isset($arrFields[$upperfname])) { // 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 ($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: $values .= (double) $arrFields[$upperfname] . ", "; break; } } } } // If there were any inserted fields then build the rest of the insert query. if ($fieldInsertedCount > 0) { // 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; } else { return false; } }
function CreateTableSQL($tabname, $flds, $tableoptions = false) { $sql = ADODB_DataDict::CreateTableSQL($tabname, $flds, $tableoptions); if (7.3 > (double) @$this->serverInfo['version']) { foreach ($flds as $fld) { $fld = _array_change_key_case($fld); $isAutoInc = false; foreach ($fld as $attr => $v) { switch ($attr) { case 'AUTOINCREMENT': case 'AUTO': $isAutoInc = true; break; case 'NAME': $fname = $v; break; } } if (isset($fname) && $isAutoInc) { // This field is an AUTOINCREMENT. Create a sequence // for it. $sequenceName = $this->makeObjectName($tabname, $fname, 'seq'); array_unshift($sql, "CREATE SEQUENCE {$sequenceName}"); array_push($sql, "ALTER TABLE {$tabname} ALTER COLUMN {$fname} SET DEFAULT nextval('{$sequenceName}')"); } } } return $sql; }
/** * 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 . ' )'; }