[ Index ] |
|
Code source de CMS made simple 1.0.5 |
1 <?php 2 /** 3 * ADOdb Lite Meta Module for Mysql 4 * 5 * Portions of the Meta Coding came from ADOdb 6 */ 7 8 /* 9 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved. 10 Released under both BSD license and Lesser GPL library license. 11 Whenever there is any discrepancy between the two licenses, 12 the BSD license will take precedence. See License.txt. 13 */ 14 15 eval('class mysql_meta_EXTENDER extends mysql_'. $last_module . '_ADOConnection { }'); 16 17 class mysql_meta_ADOConnection extends mysql_meta_EXTENDER 18 { 19 var $metaDatabasesSQL = ''; 20 var $metaTablesSQL = ''; 21 22 function MetaError($err=false) 23 { 24 include_once(ADODB_DIR."/adodb-error.inc.php"); 25 if ($err === false) 26 $err = $this->ErrorNo(); 27 28 return adodb_error($this->dataProvider,$this->databaseType,$err); 29 } 30 31 function MetaErrorMsg($errno) 32 { 33 include_once(ADODB_DIR."/adodb-error.inc.php"); 34 return adodb_errormsg($errno); 35 } 36 37 /** 38 * @returns an array with the primary key columns in it. 39 */ 40 function MetaPrimaryKeys($table, $owner=false) 41 { 42 // owner not used in base class - see oci8 43 $p = array(); 44 $objs =& $this->MetaColumns($table); 45 if ($objs) { 46 foreach($objs as $v) { 47 if (!empty($v->primary_key)) 48 $p[] = $v->name; 49 } 50 } 51 if (sizeof($p)) 52 return $p; 53 if (function_exists('ADODB_VIEW_PRIMARYKEYS')) 54 return ADODB_VIEW_PRIMARYKEYS($this->databaseType, $this->database, $table, $owner); 55 return false; 56 } 57 58 /** 59 * @returns assoc array where keys are tables, and values are foreign keys 60 */ 61 function MetaForeignKeys($table, $owner=false, $upper=false) 62 { 63 return false; 64 } 65 66 // not the fastest implementation - quick and dirty - jlim 67 // for best performance, use the actual $rs->MetaType(). 68 function MetaType($t,$len=-1,$fieldobj=false) 69 { 70 if (empty($this->_metars)) { 71 $rsclass = $this->rsPrefix.$this->databaseType; 72 $this->_metars =& new $rsclass(false,$this->fetchMode); 73 } 74 75 return $this->_metars->MetaType($t,$len,$fieldobj); 76 } 77 78 /** 79 * return the databases that the driver can connect to. 80 * Some databases will return an empty array. 81 * 82 * @return an array of database names. 83 */ 84 function MetaDatabases() 85 { 86 global $ADODB_FETCH_MODE; 87 88 if ($this->metaDatabasesSQL) { 89 $save = $ADODB_FETCH_MODE; 90 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 91 92 if ($this->fetchMode !== false) 93 $savem = $this->SetFetchMode(false); 94 95 $arr = $this->GetCol($this->metaDatabasesSQL); 96 if (isset($savem)) 97 $this->SetFetchMode($savem); 98 $ADODB_FETCH_MODE = $save; 99 100 return $arr; 101 } 102 return false; 103 } 104 105 /** 106 * @param ttype can either be 'VIEW' or 'TABLE' or false. 107 * If false, both views and tables are returned. 108 * "VIEW" returns only views 109 * "TABLE" returns only tables 110 * @param showSchema returns the schema/user with the table name, eg. USER.TABLE 111 * @param mask is the input mask - only supported by oci8 and postgresql 112 * 113 * @return array of tables for current database. 114 */ 115 function &MetaTables($ttype=false,$showSchema=false,$mask=false) 116 { 117 global $ADODB_FETCH_MODE; 118 119 $false = false; 120 if ($mask) { 121 return $false; 122 } 123 124 if ($this->metaTablesSQL) { 125 $save = $ADODB_FETCH_MODE; 126 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 127 128 if ($this->fetchMode !== false) 129 $savem = $this->SetFetchMode(false); 130 131 $rs = $this->Execute($this->metaTablesSQL); 132 if (isset($savem)) 133 $this->SetFetchMode($savem); 134 $ADODB_FETCH_MODE = $save; 135 136 if ($rs === false) 137 return $false; 138 139 $arr =& $rs->GetArray(); 140 $arr2 = array(); 141 142 if ($hast = ($ttype && isset($arr[0][1]))) { 143 $showt = strncmp($ttype,'T',1); 144 } 145 146 for ($i=0; $i < sizeof($arr); $i++) { 147 if ($hast) { 148 if ($showt == 0) { 149 if (strncmp($arr[$i][1],'T',1) == 0) 150 $arr2[] = trim($arr[$i][0]); 151 } else { 152 if (strncmp($arr[$i][1],'V',1) == 0) 153 $arr2[] = trim($arr[$i][0]); 154 } 155 } else 156 $arr2[] = trim($arr[$i][0]); 157 } 158 $rs->Close(); 159 return $arr2; 160 } 161 return $false; 162 } 163 164 function _findschema(&$table,&$schema) 165 { 166 if (!$schema && ($at = strpos($table,'.')) !== false) { 167 $schema = substr($table,0,$at); 168 $table = substr($table,$at+1); 169 } 170 } 171 172 /** 173 * List columns in a database as an array of ADOFieldObjects. 174 * See top of file for definition of object. 175 * 176 * @param $table table name to query 177 * @param $normalize makes table name case-insensitive (required by some databases) 178 * @schema is optional database schema to use - not supported by all databases. 179 * 180 * @return array of ADOFieldObjects for current table. 181 */ 182 function &MetaColumns($table,$normalize=true) 183 { 184 global $ADODB_FETCH_MODE; 185 186 $false = false; 187 if (!empty($this->metaColumnsSQL)) { 188 $schema = false; 189 $this->_findschema($table,$schema); 190 $save = $ADODB_FETCH_MODE; 191 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 192 193 if ($this->fetchMode !== false) 194 $savem = $this->SetFetchMode(false); 195 196 $rs = $this->Execute(sprintf($this->metaColumnsSQL,($normalize)?strtoupper($table):$table)); 197 198 if (isset($savem)) 199 $this->SetFetchMode($savem); 200 $ADODB_FETCH_MODE = $save; 201 if ($rs === false || $rs->EOF) 202 return $false; 203 204 $retarr = array(); 205 while (!$rs->EOF) { //print_r($rs->fields); 206 $fld = new ADOFieldObject(); 207 $fld->name = $rs->fields[0]; 208 $fld->type = $rs->fields[1]; 209 if (isset($rs->fields[3]) && $rs->fields[3]) { 210 if ($rs->fields[3]>0) 211 $fld->max_length = $rs->fields[3]; 212 $fld->scale = $rs->fields[4]; 213 if ($fld->scale>0) 214 $fld->max_length += 1; 215 } else 216 $fld->max_length = $rs->fields[2]; 217 218 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) 219 $retarr[] = $fld; 220 else $retarr[strtoupper($fld->name)] = $fld; 221 $rs->MoveNext(); 222 } 223 $rs->Close(); 224 return $retarr; 225 } 226 return $false; 227 } 228 229 /** 230 * List indexes on a table as an array. 231 * @param table table name to query 232 * @param primary true to only show primary keys. Not actually used for most databases 233 * 234 * @return array of indexes on current table. Each element represents an index, and is itself an associative array. 235 236 Array ( 237 [name_of_index] => Array 238 ( 239 [unique] => true or false 240 [columns] => Array 241 ( 242 [0] => firstname 243 [1] => lastname 244 ) 245 ) 246 */ 247 function &MetaIndexes($table, $primary = false, $owner = false) 248 { 249 $false = false; 250 return $false; 251 } 252 253 /** 254 * List columns names in a table as an array. 255 * @param table table name to query 256 * 257 * @return array of column names for current table. 258 */ 259 function &MetaColumnNames($table, $numIndexes=false) 260 { 261 $objarr =& $this->MetaColumns($table); 262 if (!is_array($objarr)) { 263 $false = false; 264 return $false; 265 } 266 $arr = array(); 267 if ($numIndexes) { 268 $i = 0; 269 foreach($objarr as $v) $arr[$i++] = $v->name; 270 } else 271 foreach($objarr as $v) $arr[strtoupper($v->name)] = $v->name; 272 273 return $arr; 274 } 275 276 } 277 278 eval('class mysql_meta_resultset_EXTENDER extends mysql_'. $last_module . '_ResultSet { }'); 279 280 class mysql_meta_ResultSet extends mysql_meta_resultset_EXTENDER 281 { 282 /** 283 * Get the metatype of the column. This is used for formatting. This is because 284 * many databases use different names for the same type, so we transform the original 285 * type to our standardised version which uses 1 character codes: 286 * 287 * @param t is the type passed in. Normally is ADOFieldObject->type. 288 * @param len is the maximum length of that field. This is because we treat character 289 * fields bigger than a certain size as a 'B' (blob). 290 * @param fieldobj is the field object returned by the database driver. Can hold 291 * additional info (eg. primary_key for mysql). 292 * 293 * @return the general type of the data: 294 * C for character < 250 chars 295 * X for teXt (>= 250 chars) 296 * B for Binary 297 * N for numeric or floating point 298 * D for date 299 * T for timestamp 300 * L for logical/Boolean 301 * I for integer 302 * R for autoincrement counter/integer 303 * 304 * 305 */ 306 function MetaType($t,$len=-1,$fieldobj=false) 307 { 308 if (is_object($t)) { 309 $fieldobj = $t; 310 $t = $fieldobj->type; 311 $len = $fieldobj->max_length; 312 } 313 // changed in 2.32 to hashing instead of switch stmt for speed... 314 static $typeMap = array( 315 'VARCHAR' => 'C', 316 'VARCHAR2' => 'C', 317 'CHAR' => 'C', 318 'C' => 'C', 319 'STRING' => 'C', 320 'NCHAR' => 'C', 321 'NVARCHAR' => 'C', 322 'VARYING' => 'C', 323 'BPCHAR' => 'C', 324 'CHARACTER' => 'C', 325 'INTERVAL' => 'C', # Postgres 326 ## 327 'LONGCHAR' => 'X', 328 'TEXT' => 'X', 329 'NTEXT' => 'X', 330 'M' => 'X', 331 'X' => 'X', 332 'CLOB' => 'X', 333 'NCLOB' => 'X', 334 'LVARCHAR' => 'X', 335 ## 336 'BLOB' => 'B', 337 'IMAGE' => 'B', 338 'BINARY' => 'B', 339 'VARBINARY' => 'B', 340 'LONGBINARY' => 'B', 341 'B' => 'B', 342 ## 343 'YEAR' => 'D', // mysql 344 'DATE' => 'D', 345 'D' => 'D', 346 ## 347 'TIME' => 'T', 348 'TIMESTAMP' => 'T', 349 'DATETIME' => 'T', 350 'TIMESTAMPTZ' => 'T', 351 'T' => 'T', 352 ## 353 'BOOL' => 'L', 354 'BOOLEAN' => 'L', 355 'BIT' => 'L', 356 'L' => 'L', 357 ## 358 'COUNTER' => 'R', 359 'R' => 'R', 360 'SERIAL' => 'R', // ifx 361 'INT IDENTITY' => 'R', 362 ## 363 'INT' => 'I', 364 'INT2' => 'I', 365 'INT4' => 'I', 366 'INT8' => 'I', 367 'INTEGER' => 'I', 368 'INTEGER UNSIGNED' => 'I', 369 'SHORT' => 'I', 370 'TINYINT' => 'I', 371 'SMALLINT' => 'I', 372 'I' => 'I', 373 ## 374 'LONG' => 'N', // interbase is numeric, oci8 is blob 375 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers 376 'DECIMAL' => 'N', 377 'DEC' => 'N', 378 'REAL' => 'N', 379 'DOUBLE' => 'N', 380 'DOUBLE PRECISION' => 'N', 381 'SMALLFLOAT' => 'N', 382 'FLOAT' => 'N', 383 'NUMBER' => 'N', 384 'NUM' => 'N', 385 'NUMERIC' => 'N', 386 'MONEY' => 'N', 387 388 ## informix 9.2 389 'SQLINT' => 'I', 390 'SQLSERIAL' => 'I', 391 'SQLSMINT' => 'I', 392 'SQLSMFLOAT' => 'N', 393 'SQLFLOAT' => 'N', 394 'SQLMONEY' => 'N', 395 'SQLDECIMAL' => 'N', 396 'SQLDATE' => 'D', 397 'SQLVCHAR' => 'C', 398 'SQLCHAR' => 'C', 399 'SQLDTIME' => 'T', 400 'SQLINTERVAL' => 'N', 401 'SQLBYTES' => 'B', 402 'SQLTEXT' => 'X' 403 ); 404 405 $tmap = false; 406 $t = strtoupper($t); 407 $tmap = (isset($typeMap[$t])) ? $typeMap[$t] : 'N'; 408 switch ($tmap) { 409 case 'C': 410 // is the char field is too long, return as text field... 411 if ($this->blobSize >= 0) { 412 if ($len > $this->blobSize) 413 return 'X'; 414 } else if ($len > 250) { 415 return 'X'; 416 } 417 return 'C'; 418 419 case 'I': 420 if (!empty($fieldobj->primary_key)) 421 return 'R'; 422 return 'I'; 423 424 case false: 425 return 'N'; 426 427 case 'B': 428 if (isset($fieldobj->binary)) 429 return ($fieldobj->binary) ? 'B' : 'X'; 430 return 'B'; 431 432 case 'D': 433 if (!empty($this->datetime)) 434 return 'T'; 435 return 'D'; 436 437 default: 438 if ($t == 'LONG' && $this->dataProvider == 'oci8') 439 return 'B'; 440 return $tmap; 441 } 442 } 443 444 } 445 446 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 18:50:37 2007 | par Balluche grâce à PHPXref 0.7 |