[ Index ] |
|
Code source de Serendipity 1.2 |
1 <?php # $Id: db.inc.php 1770 2007-07-12 11:23:20Z garvinhicking $ 2 # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team) 3 # All rights reserved. See LICENSE file for licensing details 4 5 if (defined('S9Y_FRAMEWORK_DB')) { 6 return; 7 } 8 @define('S9Y_FRAMEWORK_DB', true); 9 10 if (@include(S9Y_INCLUDE_PATH . "include/db/{$serendipity['dbType']}.inc.php")) { 11 @define('S9Y_DB_INCLUDED', TRUE); 12 } 13 14 /** 15 * Perform a query to update the data of a certain table row 16 * 17 * You can pass the tablename and an array of keys to select the row, 18 * and an array of values to UPDATE in the DB table. 19 * 20 * @access public 21 * @param string Name of the DB table 22 * @param array Input array that controls the "WHERE" condition part. Pass it an associative array like array('key1' => 'value1', 'key2' => 'value2') to get a statement like "WHERE key1 = value1 AND key2 = value2". Escaping is done automatically in this function. 23 * @param array Input array that controls the "SET" condition part. Pass it an associative array like array('key1' => 'value1', 'key2' => 'value2') to get a statement like "SET key1 = value1, key2 = value2". Escaping is done automatically in this function. 24 * @param string What do do with the SQL query (execute, display) 25 * @return array Returns the result of the SQL query 26 */ 27 function serendipity_db_update($table, $keys, $values, $action = 'execute') 28 { 29 global $serendipity; 30 31 $set = ''; 32 33 foreach ($values as $k => $v) { 34 if (strlen($set)) 35 $set .= ', '; 36 $set .= $k . '=\'' . serendipity_db_escape_string($v) . '\''; 37 } 38 39 $where = ''; 40 foreach ($keys as $k => $v) { 41 if (strlen($where)) 42 $where .= ' AND '; 43 $where .= $k . '=\'' . serendipity_db_escape_string($v) . '\''; 44 } 45 46 if (strlen($where)) { 47 $where = " WHERE $where"; 48 } 49 50 $q = "UPDATE {$serendipity['dbPrefix']}$table SET $set $where"; 51 if ($action == 'execute') { 52 return serendipity_db_query($q); 53 } else { 54 return $q; 55 } 56 } 57 58 /** 59 * Perform a query to insert an associative array into a specific SQL table 60 * 61 * You can pass a tablename and an array of input data to insert into an array. 62 * 63 * @access public 64 * @param string Name of the SQL table 65 * @param array Associative array of keys/values to insert into the table. Escaping is done automatically. 66 * @param string What do do with the SQL query (execute, display) 67 * @return array Returns the result of the SQL query 68 */ 69 function serendipity_db_insert($table, $values, $action = 'execute') 70 { 71 global $serendipity; 72 73 $names = implode(',', array_keys($values)); 74 75 $vals = ''; 76 foreach ($values as $k => $v) { 77 if (strlen($vals)) 78 $vals .= ', '; 79 $vals .= '\'' . serendipity_db_escape_string($v) . '\''; 80 } 81 82 $q = "INSERT INTO {$serendipity['dbPrefix']}$table ($names) values ($vals)"; 83 84 if ($action == 'execute') { 85 return serendipity_db_query($q); 86 } else { 87 return $q; 88 } 89 } 90 91 /** 92 * Check whether an input value corresponds to a TRUE/FALSE option in the SQL database. 93 * 94 * Because older DBs could not store TRUE/FALSE values to be restored into a PHP variable, 95 * this function tries to detect what the return code of a SQL column is, and convert it 96 * to a PHP native boolean. 97 * 98 * Values that will be recognized as TRUE are 'true', 't' and '1'. 99 * 100 * @access public 101 * @param string input value to compare 102 * @return boolean boolean conversion of the input value 103 */ 104 function serendipity_db_bool($val) 105 { 106 if(($val === true) || ($val == 'true') || ($val == 't') || ($val == '1')) 107 return true; 108 #elseif (($val === false || $val == 'false' || $val == 'f')) 109 else 110 return false; 111 } 112 113 /** 114 * Return a SQL statement for a time interval or timestamp, specific to certain SQL backends 115 * 116 * @access public 117 * @param string Indicate whether to return a timestamp, or an Interval 118 * @param int The interval one might want to use, if Interval return was selected 119 * @return string SQL statement 120 */ 121 function serendipity_db_get_interval($val, $ival = 900) { 122 global $serendipity; 123 124 switch($serendipity['dbType']) { 125 case 'sqlite': 126 case 'sqlite3': 127 $interval = $ival; 128 $ts = time(); 129 break; 130 131 case 'pdo-postgres': 132 case 'postgres': 133 $interval = "interval '$ival'"; 134 $ts = 'NOW()'; 135 break; 136 137 case 'mysql': 138 case 'mysqli': 139 default: 140 $interval = $ival; 141 $ts = 'NOW()'; 142 break; 143 } 144 145 switch($val) { 146 case 'interval': 147 return $interval; 148 149 default: 150 case 'ts': 151 return $ts; 152 } 153 } 154 155 /** 156 * Operates on an array to prepare it for SQL usage. 157 * 158 * @access public 159 * @param string Concatenation character 160 * @param array Input array 161 * @param string How to convert (int: Only numbers, string: serendipity_db_escape_String) 162 * @return string Imploded string 163 */ 164 function serendipity_db_implode($string, &$array, $type = 'int') { 165 $new_array = array(); 166 if (!is_array($array)) { 167 return ''; 168 } 169 170 foreach($array AS $idx => $key) { 171 if ($type == 'int') { 172 $new_array[$idx] = (int)$key; 173 } else { 174 $new_array[$idx] = serendipity_db_escape_string($key); 175 } 176 } 177 178 $string = implode($string, $new_array); 179 return $string; 180 } 181 182 /* vim: set sts=4 ts=4 expandtab : */
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Nov 24 09:00:37 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |