/***************************************************************
* Copyright notice
*
-* (c) 2004-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
+* (c) 2004-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
}
/**
+ * Creates and executes an INSERT SQL-statement for $table with multiple rows.
+ *
+ * @param string Table name
+ * @param array Field names
+ * @param array Table rows. Each row should be an array with field values mapping to $fields
+ * @param string/array See fullQuoteArray()
+ * @return pointer MySQL result pointer / DBAL object
+ */
+ public function exec_INSERTmultipleRows($table, array $fields, array $rows, $no_quote_fields = FALSE) {
+ $res = mysql_query($this->INSERTmultipleRows($table, $fields, $rows, $no_quote_fields), $this->link);
+ if ($this->debugOutput) {
+ $this->debug('exec_INSERTmultipleRows');
+ }
+ return $res;
+ }
+
+ /**
* Creates and executes an UPDATE SQL-statement for $table where $where-clause (typ. 'uid=...') from the array with field/value pairs $fields_values.
* Using this function specifically allow us to handle BLOB and CLOB fields depending on DB
* Usage count/core: 50
return $count;
}
+ /**
+ * Truncates a table.
+ *
+ * @param string Database tablename
+ * @return mixed Result from handler
+ */
+ public function exec_TRUNCATEquery($table) {
+ $res = mysql_query($this->TRUNCATEquery($table), $this->link);
+ if ($this->debugOutput) {
+ $this->debug('exec_TRUNCATEquery');
+ }
+ return $res;
+ }
+
}
/**
+ * Creates an INSERT SQL-statement for $table with multiple rows.
+ *
+ * @param string Table name
+ * @param array Field names
+ * @param array Table rows. Each row should be an array with field values mapping to $fields
+ * @param string/array See fullQuoteArray()
+ * @return string Full SQL query for INSERT (unless $rows does not contain any elements in which case it will be false)
+ */
+ public function INSERTmultipleRows($table, array $fields, array $rows, $no_quote_fields = FALSE) {
+ // Table and fieldnames should be "SQL-injection-safe" when supplied to this
+ // function (contrary to values in the arrays which may be insecure).
+ if (count($rows)) {
+ // Build query:
+ $query = 'INSERT INTO ' . $table .
+ ' (' . implode(', ', $fields) . ') VALUES ';
+
+ $rowSQL = array();
+ foreach ($rows as $row) {
+ // quote and escape values
+ $row = $this->fullQuoteArray($row, $table, $no_quote_fields);
+ $rowSQL[] = '(' . implode(', ', $row) . ')';
+ }
+
+ $query .= implode(', ', $rowSQL);
+
+ // Return query:
+ if ($this->debugOutput || $this->store_lastBuiltQuery) {
+ $this->debug_lastBuiltQuery = $query;
+ }
+
+ return $query;
+ }
+ }
+
+ /**
* Creates an UPDATE SQL-statement for $table where $where-clause (typ. 'uid=...') from the array with field/value pairs $fields_values.
* Usage count/core: 6
*
* @param string See exec_UPDATEquery()
* @param array See exec_UPDATEquery()
* @param array See fullQuoteArray()
- * @return string Full SQL query for UPDATE (unless $fields_values does not contain any elements in which case it will be false)
+ * @return string Full SQL query for UPDATE
*/
function UPDATEquery($table, $where, $fields_values, $no_quote_fields = FALSE) {
-
// Table and fieldnames should be "SQL-injection-safe" when supplied to this
// function (contrary to values in the arrays which may be insecure).
if (is_string($where)) {
+ $fields = array();
if (is_array($fields_values) && count($fields_values)) {
// quote and escape values
$nArr = $this->fullQuoteArray($fields_values, $table, $no_quote_fields);
- $fields = array();
foreach ($nArr as $k => $v) {
$fields[] = $k.'='.$v;
}
+ }
- // Build query:
- $query = 'UPDATE ' . $table . ' SET ' . implode(',', $fields) .
- (strlen($where) > 0 ? ' WHERE ' . $where : '');
+ // Build query:
+ $query = 'UPDATE ' . $table . ' SET ' . implode(',', $fields) .
+ (strlen($where) > 0 ? ' WHERE ' . $where : '');
- // Return query:
- if ($this->debugOutput || $this->store_lastBuiltQuery) {
- $this->debug_lastBuiltQuery = $query;
- }
- return $query;
+ if ($this->debugOutput || $this->store_lastBuiltQuery) {
+ $this->debug_lastBuiltQuery = $query;
}
+ return $query;
} else {
- die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for UPDATE ' .
- 'query was not a string in $this->UPDATEquery() !');
+ throw new InvalidArgumentException(
+ 'TYPO3 Fatal Error: "Where" clause argument for UPDATE query was not a string in $this->UPDATEquery() !',
+ 1270853880
+ );
}
}
}
return $query;
} else {
- die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for DELETE ' .
- 'query was not a string in $this->DELETEquery() !');
+ throw new InvalidArgumentException(
+ 'TYPO3 Fatal Error: "Where" clause argument for DELETE query was not a string in $this->DELETEquery() !',
+ 1270853881
+ );
}
}
}
/**
+ * Creates a SELECT SQL-statement to be used as subquery within another query.
+ * BEWARE: This method should not be overriden within DBAL to prevent quoting from happening.
+ *
+ * @param string $select_fields: List of fields to select from the table.
+ * @param string $from_table: Table from which to select.
+ * @param string $where_clause: Conditional WHERE statement
+ * @return string Full SQL query for SELECT
+ */
+ public function SELECTsubquery($select_fields, $from_table, $where_clause) {
+ // Table and fieldnames should be "SQL-injection-safe" when supplied to this function
+ // Build basic query:
+ $query = 'SELECT ' . $select_fields . ' FROM ' . $from_table .
+ (strlen($where_clause) > 0 ? ' WHERE ' . $where_clause : '');
+
+ // Return query:
+ if ($this->debugOutput || $this->store_lastBuiltQuery) {
+ $this->debug_lastBuiltQuery = $query;
+ }
+
+ return $query;
+ }
+
+ /**
+ * Creates a TRUNCATE TABLE SQL-statement
+ *
+ * @param string See exec_TRUNCATEquery()
+ * @return string Full SQL query for TRUNCATE TABLE
+ */
+ public function TRUNCATEquery($table) {
+ // Table should be "SQL-injection-safe" when supplied to this function
+ // Build basic query:
+ $query = 'TRUNCATE TABLE ' . $table;
+
+ // Return query:
+ if ($this->debugOutput || $this->store_lastBuiltQuery) {
+ $this->debug_lastBuiltQuery = $query;
+ }
+
+ return $query;
+ }
+
+ /**
* Returns a WHERE clause that can find a value ($value) in a list field ($field)
* For instance a record in the database might contain a list of numbers,
* "34,234,5" (with no spaces between). This query would be able to select that
- * record based on the value "34", "234" or "5" regardless of their positioni in
+ * record based on the value "34", "234" or "5" regardless of their position in
* the list (left, middle or right).
+ * The value must not contain a comma (,)
* Is nice to look up list-relations to records or files in TYPO3 database tables.
*
* @param string Field name
* @param string Table in which we are searching (for DBAL detection of quoteStr() method)
* @return string WHERE clause for a query
*/
- function listQuery($field, $value, $table) {
+ public function listQuery($field, $value, $table) {
+ $value = (string)$value;
+ if (strpos(',', $value) !== FALSE) {
+ throw new InvalidArgumentException('$value must not contain a comma (,) in $this->listQuery() !');
+ }
$pattern = $this->quoteStr($value, $table);
- $patternForLike = $this->escapeStrForLike($pattern, $table);
- $where = '(' . $field . ' LIKE \'%,' . $patternForLike . ',%\' OR ' .
- $field . ' LIKE \'' . $patternForLike . ',%\' OR ' .
- $field . ' LIKE \'%,' . $patternForLike . '\' OR ' .
- $field . '=\'' . $pattern . '\')';
+ $where = 'FIND_IN_SET(\'' . $pattern . '\',' . $field . ')';
return $where;
}
* @param string Database name
* @param string Query to execute
* @return pointer Result pointer / DBAL object
- * @deprecated since TYPO3 3.6
+ * @deprecated since TYPO3 3.6, will be removed in TYPO3 4.5
* @see sql_query()
*/
function sql($db, $query) {
+ t3lib_div::logDeprecatedFunction();
+
$res = mysql_query($query, $this->link);
if ($this->debugOutput) {
$this->debug('sql', $query);
// check if MySQL extension is loaded
if (!extension_loaded('mysql')) {
- $header = 'Database Error';
- $message = 'It seems that MySQL support for PHP is not installed!';
- t3lib_timeTrack::debug_typo3PrintError($header, $message, false, t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
- exit;
+ $message = 'Database Error: It seems that MySQL support for PHP is not installed!';
+ throw new RuntimeException($message, 1271492606);
}
// Check for client compression
4
);
} else {
- $setDBinit = t3lib_div::trimExplode(chr(10), $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit'], TRUE);
+ $setDBinit = t3lib_div::trimExplode(LF, str_replace("' . LF . '", LF, $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit']), TRUE);
foreach ($setDBinit as $v) {
if (mysql_query($v, $this->link) === FALSE) {
t3lib_div::sysLog('Could not initialize DB connection with query "' . $v .
/**
* Connects to database for TYPO3 sites:
*
+ * @param string $host
+ * @param string $user
+ * @param string $password
+ * @param string $db
* @return void
*/
- function connectDB() {
- if ($this->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password)) {
- if (!TYPO3_db) {
- die('No database selected');
- exit;
- } elseif (!$this->sql_select_db(TYPO3_db)) {
- die('Cannot connect to the current database, "' . TYPO3_db . '"');
- exit;
+ function connectDB($host = TYPO3_db_host, $user = TYPO3_db_username, $password = TYPO3_db_password, $db = TYPO3_db) {
+ if ($this->sql_pconnect($host, $user, $password)) {
+ if (!$db) {
+ throw new RuntimeException(
+ 'TYPO3 Fatal Error: No database selected!',
+ 1270853882
+ );
+ } elseif (!$this->sql_select_db($db)) {
+ throw new RuntimeException(
+ 'TYPO3 Fatal Error: Cannot connect to the current database, "' . $db . '"!',
+ 1270853883
+ );
}
} else {
- die('The current username, password or host was not accepted when the ' .
- 'connection to the database was attempted to be established!');
- exit;
+ throw new RuntimeException(
+ 'TYPO3 Fatal Error: The current username, password or host was not accepted when the connection to the database was attempted to be established!',
+ 1270853884
+ );
}
}
-
-
-
-
-
-
-
-
+ /**
+ * Checks if database is connected
+ *
+ * @return boolean
+ */
+ public function isConnected() {
+ return is_resource($this->link);
+ }
'lastBuiltQuery' => ($query ? $query : $this->debug_lastBuiltQuery),
'debug_backtrace' => t3lib_div::debug_trail(),
),
- 'SQL debug'
+ $func,
+ is_object($GLOBALS['error']) && @is_callable(array($GLOBALS['error'], 'debug')) ? '' : 'DB Error'
);
}
}
$debug = true;
foreach ($explain_tables as $table) {
- $res = $this->sql_query('SHOW INDEX FROM ' . $table, $this->link);
- if (is_resource($res)) {
- while ($tempRow = $this->sql_fetch_assoc($res)) {
- $indices_output[] = $tempRow;
+ $tableRes = $this->sql_query('SHOW TABLE STATUS LIKE \'' . $table . '\'');
+ $isTable = $this->sql_num_rows($tableRes);
+ if ($isTable) {
+ $res = $this->sql_query('SHOW INDEX FROM ' . $table, $this->link);
+ if (is_resource($res)) {
+ while ($tempRow = $this->sql_fetch_assoc($res)) {
+ $indices_output[] = $tempRow;
+ }
+ $this->sql_free_result($res);
}
- $this->sql_free_result($res);
}
+ $this->sql_free_result($tableRes);
}
} else {
$debug = false;
}
if ($debug) {
- if ($explainMode == 1) {
- t3lib_div::debug('QUERY: ' . $query);
- t3lib_div::debug(array('Debug trail:' => $trail), 'Row count: ' . $row_count);
-
- if ($error) {
- t3lib_div::debug($error);
- }
- if (count($explain_output)) {
- t3lib_div::debug($explain_output);
- }
- if (count($indices_output)) {
- t3lib_div::debugRows($indices_output);
- }
-
- } elseif ($explainMode == 2) {
+ if ($explainMode) {
$data = array();
$data['query'] = $query;
$data['trail'] = $trail;
if (count($indices_output)) {
$data['indices'] = $indices_output;
}
- $GLOBALS['TT']->setTSselectQuery($data);
+
+ if ($explainMode == 1) {
+ t3lib_div::debug($data, 'Tables: ' . $from_table, 'DB SQL EXPLAIN');
+ } elseif ($explainMode == 2) {
+ $GLOBALS['TT']->setTSselectQuery($data);
+ }
}
return true;
}