/** * Permite consultar toda la informacion de una tabla * Si no existen datos, inicializa los arreglos para que no mande error en caso de los arreglos * Ultima revision: 2012-02-21 * Reviso: Martin Tlapa * @param type $tabla * @return boolean */ public static function consulta_estructura($tabla) { if (!isset(DB_Structure::$_table_structure[$tabla])) { $es_vista_filtrado = false; if (stripos($tabla, "v_") == 0 && "v_" . substr($tabla, 2) == $tabla) { $tabla = substr($tabla, 2); //Es el nombre de la tabla $es_vista_filtrado = true; } if (DB::get_cnx_name() == "mysql") { $sql = "DESCRIBE " . $tabla; $registros = DB::get_registros($sql); if (empty($registros) && $es_vista_filtrado) { $sql = "DESCRIBE v_" . $tabla; $registros = DB::get_registros($sql); } if ($registros) { $requerido = false; $id_asumido = null; $campos_requeridos = array(); // Inicializa los arreglo si no hay campos requeridos o algun otro tipo de dato DB_Structure::$_table_structure = array(); DB_Structure::$_required_fields = array(); DB_Structure::$_fields_null_difinition = array(); DB_Structure::$_ids = array(); DB_Structure::$_identifier = array(); foreach ($registros as $registro) { if (!isset(DB_Structure::$_identifier[$tabla])) { DB_Structure::$_identifier[$tabla] = strtolower(substr($registro["Field"], 0, 3)); } if (strtoupper($registro["Key"]) == "PRI") { $id_asumido = $registro["Field"]; //Para que no entre al else de abajo DB_Structure::$_ids[$tabla] = $registro["Field"]; } elseif (null == $id_asumido) { //Sino encuentra ID primario definido en la estructura, se asaume que es el primero de la vista $id_asumido = $registro["Field"]; } DB_Structure::$_table_structure[$tabla][] = $registro["Field"]; if (strtoupper($registro["Null"]) == "NO") { $requerido = true; if ($registro["Extra"] != "auto_increment" && $registro["Field"] != DB_Structure::$_identifier[$tabla] . "_creado" && $registro["Field"] != DB_Structure::$_identifier[$tabla] . "_fecha_creado") { $campos_requeridos[] = $registro["Field"]; } } else { $requerido = false; } DB_Structure::$_fields_null_difinition[$registro["Field"]] = $requerido; } if ($id_asumido && !isset(DB_Structure::$_ids[$tabla])) { DB_Structure::$_ids[$tabla] = $id_asumido; } DB_Structure::$_required_fields[$tabla] = $campos_requeridos; } else { die("La tabla <b>" . $tabla . "</b> no existe. Favor de verificarlo"); } } if ($es_vista_filtrado) { DB_Structure::$_identifier["v_" . $tabla] = DB_Structure::$_identifier[$tabla]; DB_Structure::$_ids["v_" . $tabla] = DB_Structure::$_ids[$tabla]; DB_Structure::$_table_structure["v_" . $tabla] = DB_Structure::$_table_structure[$tabla]; DB_Structure::$_required_fields["v_" . $tabla] = DB_Structure::$_required_fields[$tabla]; DB_Structure::$_table_structure["v_" . $tabla] = DB_Structure::$_table_structure[$tabla]; } } return true; }
public function consulta_registro($where = null) { if (!$this->get_tabla()) { die("DB_info.consulta_registro dice:<br />No se ha seteado el nombre de la tabla"); } if ($where == null) { $where = $this->get_condicion(); } $tbl_arreglo = DB_Structure::get_campos_tabla($this->get_tabla()); if (stristr($where, "WHERE")) { $where = str_ireplace("WHERE", "", $where); } $sql = "SELECT " . DB_Structure::get_id($this->get_tabla()) . " FROM " . $this->get_tabla() . " WHERE " . $where; // debug( $sql ); if ($this->regCount($sql)) { $sql = "SELECT * FROM " . $this->get_tabla() . " WHERE " . $where; // debug( $sql, "Query de consulta" ); $registro = $this->get_registro($sql); foreach ($tbl_arreglo as $nombre) { $this->_campos_consultados[$this->get_tabla()][$this->get_id()][$nombre] = isset($registro->{$nombre}) ? $this->get_salto_de_linea() ? $this->get_utf8_string(nl2br($registro->{$nombre})) : $this->get_utf8_string($registro->{$nombre}) : false; } } else { foreach ($tbl_arreglo as $nombre) { $this->_campos_consultados[$this->get_tabla()][$this->get_id()][$nombre] = ""; } } }
/** * Permite establecer los datos que se van a guardar en un INSERT * Ultima revision: 2012-03-13 * Reviso: Martin Tlapa * @param type $nombre_campos * @param type $datos * @return type */ public function prepara_datos_insert($nombre_campos, $datos) { foreach ($nombre_campos as $nombre_campo) { if ($datos[$nombre_campo] || $datos[$nombre_campo] == 0) { $campos[] = $nombre_campo; // if( $datos[$nombre_campo]==null && $datos[$nombre_campo]!=0 ) if (!$datos[$nombre_campo]) { $request[] = "NULL"; } elseif ($datos[$nombre_campo] === 0) { echo $nombre_campo . "<br />"; $request[] = "0"; } elseif (stristr($datos[$nombre_campo], "NOW()") || stristr($datos[$nombre_campo], "NULL") && strtoupper($datos[$nombre_campo]) == "NULL" || stristr($datos[$nombre_campo], "MD5(RAND())") || stristr($datos[$nombre_campo], "DATE_ADD(")) { $request[] = $datos[$nombre_campo]; } else { $request[] = "\"" . stripslashes(addslashes($this->get_capital_letters() ? strtoupper($this->get_utf8_string($datos[$nombre_campo])) : $this->get_utf8_string($datos[$nombre_campo]))) . "\""; } // $request[] = "'" . mysql_escape_string( ($this->get_capital_letters())?strtoupper($this->get_utf8_string($datos[$nombre_campo])):$this->get_utf8_string($datos[$nombre_campo]) ) . "'"; } } if (!$this->get_ignorar_campos_control()) { $identifier = DB_Structure::get_identifier($this->get_tabla()); $campos[] = $identifier . "_creado"; $request[] = "'" . $_SESSION[$this->__auth_nombre_id_usuario] . "'"; $campos[] = $identifier . "_fecha_creado"; $request[] = "NOW()"; } return array($campos, $request); }
/** * Permite eliminar el registro de una tabla * Ultima revision: 2012-03-28 * Reviso: Martin Tlapa * @param type $id * @return boolean */ function elimina() { if (!$this->get_tabla()) { die('DB_Crud.elimina() dice:<br />Falta definir el nombre de la tabla'); } if ($this->get_id()) { $this->set_cmd("deleted"); if ($this->get_custom_id()) { $id = $this->get_custom_id(); } else { $id = DB_Structure::get_id($this->get_tabla()); } if (!$this->get_ignorar_campos_control()) { $identifier = strtolower(substr($id, 0, 3)); $deletes[] = $identifier . "_eliminado = '" . $_SESSION[$this->__auth_nombre_id_usuario] . "'"; $deletes[] = $identifier . "_fecha_eliminado = NOW()"; $sql = "UPDATE " . $this->get_tabla() . " SET " . implode(", ", $deletes) . " WHERE " . $id . " = " . $this->get_id() . ";"; } else { $sql = "DELETE FROM " . $this->get_tabla() . " WHERE " . $id . " = " . $this->get_id() . ";"; } $this->execute($sql); return true; } else { $this->set_error("id_no_especificado"); $this->set_more_info("No se ha especificado el ID a eliminar"); } if ($this->get_error() && DB::$_file_log_name && DB::$_path_log) { $this->save_log("ERROR - DB_CRUD.elimina() dice: " . $this->get_error() . "\n" . $this->get_more_info()); } elseif ($this->get_error() && $this->get_debug_mode()) { die("ERROR - DB_CRUD.elimina() dice: " . $this->get_error() . "\n" . $this->get_more_info()); } return false; }