Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. //propriedades
  2. public $servidor = "localhost";
  3. public $usuario = "usuario";
  4. public $senha = "senha";
  5. public $nomebanco = "crud";
  6. public $conexao = NULL;
  7. public $dataset = NULL;
  8. public $linhasafetadas = -1;
  9.  
  10. //métodos
  11. public function __construct() {
  12. $this->conecta();
  13. }
  14.  
  15. public function __destruct() {
  16. if ($this->conexao != NULL):
  17. mysql_close($this->conexao);
  18. endif;
  19. }
  20.  
  21. public function conecta() {
  22. $this->conexao = mysql_connect($this->servidor, $this->usuario, $this->senha, TRUE) or die($this->trataerro(__FILE__, __FUNCTION__, mysql_errno(), mysql_error(), TRUE));
  23. mysql_select_db($this->nomebanco) or die($this->trataerro(__FILE__, __FUNCTION__, mysql_errno(), mysql_error(), TRUE));
  24. mysql_query("SET NAMES 'utf8'");
  25. mysql_query("SET character_set_connection=utf8");
  26. mysql_query("SET character_set_client=utf8");
  27. mysql_query("SET character_set_results=utf8");
  28. ##echo "O metodo conecta foi chamado";
  29. }
  30.  
  31. public function inserir($objeto) {
  32. $sql = "INSERT INTO " . $objeto->tabela . " (";
  33. for ($i = 0; $i < count($objeto->campos_valores); $i++):
  34. $sql.= key($objeto->campos_valores);
  35. if ($i < (count($objeto->campos_valores) - 1)):
  36. $sql .= ", ";
  37. else:
  38. $sql .= ") ";
  39. endif;
  40. next($objeto->campos_valores);
  41. endfor;
  42. //Resetando os valores...
  43. reset($objeto->campos_valores);
  44. $sql .= "VALUES (";
  45. for ($i = 0; $i < count($objeto->campos_valores); $i++):
  46. $sql.= is_numeric($objeto->campos_valores[key($objeto->campos_valores)]) ?
  47. $objeto->campos_valores[key($objeto->campos_valores)] :
  48. "'" . $objeto->campos_valores[key($objeto->campos_valores)] . "'";
  49. if ($i < (count($objeto->campos_valores) - 1)):
  50. $sql .= ", ";
  51. else:
  52. $sql .= ") ";
  53. endif;
  54. next($objeto->campos_valores);
  55. endfor;
  56. return $this->executaSQL($sql);
  57. }
  58.  
  59. public function atualizar($objeto) {
  60. $sql = "UPDATE " . $objeto->tabela . " SET ";
  61. for ($i = 0; $i < count($objeto->campos_valores); $i++):
  62. $sql.= key($objeto->campos_valores) . "=";
  63. $sql.= is_numeric($objeto->campos_valores[key($objeto->campos_valores)]) ?
  64. $objeto->campos_valores[key($objeto->campos_valores)] :
  65. "'" . $objeto->campos_valores[key($objeto->campos_valores)] . "'";
  66. if ($i < (count($objeto->campos_valores) - 1)):
  67. $sql .= ", ";
  68. else:
  69. $sql .= " ";
  70. endif;
  71. next($objeto->campos_valores);
  72. endfor;
  73. $sql .= "WHERE " . $objeto->campopk . "=";
  74. $sql .= is_numeric($objeto->campopk) ? $objeto->valorpk :
  75. "'" . $objeto->valorpk . "'";
  76. return $this->executaSQL($sql);
  77. }
  78.  
  79. public function deletar($objeto) {
  80. $sql = "DELETE FROM " . $objeto->tabela;
  81. $sql .= " WHERE " . $objeto->campopk . "=";
  82. $sql .= is_numeric($objeto->campopk) ? $objeto->valorpk :
  83. "'" . $objeto->valorpk . "'";
  84.  
  85. return $this->executaSQL($sql);
  86. }
  87.  
  88. public function selecionaTudo($objeto) {
  89. $sql = "SELECT * FROM " . $objeto->tabela;
  90. if ($objeto->extras_select != NULL):
  91. $sql .= " " . $objeto->extras_select;
  92. endif;
  93. return $this->executaSQL($sql);
  94. }
  95.  
  96. public function selecionaCampos($objeto) {
  97. $sql = "SELECT ";
  98. for ($i = 0; $i < count($objeto->campos_valores); $i++):
  99. $sql.= key($objeto->campos_valores);
  100. if ($i < (count($objeto->campos_valores) - 1)):
  101. $sql .= ", ";
  102. else:
  103. $sql .= " ";
  104. endif;
  105. next($objeto->campos_valores);
  106. endfor;
  107.  
  108. $sql .= " FROM " . $objeto->tabela;
  109. if ($objeto->extras_select != NULL):
  110. $sql .= " " . $objeto->extras_select;
  111. endif;
  112. return $this->executaSQL($sql);
  113. }
  114.  
  115. public function executaSQL($sql = NULL) {
  116. if ($sql != NULL):
  117. $query = mysql_query($sql) or $this->trataerro(__FILE__, __FUNCTION__);
  118. $this->linhasafetadas = mysql_affected_rows($this->conexao);
  119. if (substr(trim(strtolower($sql)), 0, 6) == 'select'):
  120. $this->dataset = $query;
  121. return $query;
  122. else:
  123. return $this->linhasafetadas;
  124. endif;
  125. else:
  126. $this->trataerro(__FILE__, __FUNCTION__, 'Comando SQL nao informado na rotina', FALSE);
  127. endif;
  128. }
  129.  
  130. public function retornaDados($tipo = NULL) {
  131. switch (strtolower($tipo)):
  132. case "array":
  133. return mysql_fetch_array($this->dataset);
  134. break;
  135. case "assoc":
  136. return mysql_fetch_assoc($this->dataset);
  137. break;
  138. case "object":
  139. return mysql_fetch_object($this->dataset);
  140. break;
  141. default:
  142. return mysql_fetch_object($this->dataset);
  143. break;
  144. endswitch;
  145. }
  146.  
  147. public function trataerro($arquivo = NULL, $rotina = NULL, $numerro = NULL, $msgerro = NULL, $geraexcept = FALSE) {
  148. if ($arquivo == NULL)
  149. $arquivo = "nao informado";
  150. if ($rotina == NULL)
  151. $rotina = "nao informada";
  152. if ($numerro == NULL)
  153. $numerro = mysql_errno($this->conexao);
  154. if ($msgerro == NULL)
  155. $msgerro = mysql_error($this->conexao);
  156. $resultado = 'Ocorreu um erro com os seguintes detalhes:<br />
  157. <strong>Arquivo:</strong> ' . $arquivo . '<br />
  158. <strong>Rotina:</strong> ' . $rotina . '<br />
  159. <strong>Codigo:</strong> ' . $numerro . '<br />
  160. <strong>Mensagem:</strong> ' . $msgerro;
  161. if ($geraexcept == FALSE):
  162. echo($resultado);
  163. else:
  164. die($resultado);
  165. endif;
  166. }
  167.  
  168. <?php
  169.  
  170. //Propriedades
  171. public $tabela = "";
  172. public $campos_valores = array();
  173. public $campopk = NULL; //pk primary key ou chave primaria
  174. public $valorpk = NULL;
  175. public $extras_select = "";
  176.  
  177. //metodos
  178. //Adicionar campo na tabela
  179. public function addCampo($campo = NULL, $valor = NULL) {
  180. if ($campo != NULL):
  181. $this->campos_valores[$campo] = $valor; //$valor que for passado
  182. endif;
  183. }
  184.  
  185. public function delCampo($campo = NULL) {
  186. if (array_key_exists($campo, $this->campos_valores)):
  187. unset($this->campos_valores[$campo]);
  188. endif;
  189. }
  190.  
  191. public function setValor($campo = NULL, $valor = NULL) {
  192. if ($campo != NULL && $valor != NULL):
  193. $this->campos_valores[$campo] = $valor;
  194. endif;
  195. }
  196.  
  197. public function getValor($campo = NULL) {
  198. if ($campo != NULL && array_key_exists($campo, $this->campos_valores)):
  199. return $this->campos_valores[$campo];
  200. else:
  201. return FALSE;
  202. endif;
  203. }
  204.  
  205. <?php
  206.  
  207. //Contruct
  208. public function __construct($campos = array()) {
  209. parent::__construct();
  210. $this->tabela = "clientes";
  211. if (sizeof($campos) <= 0):
  212. $this->campos_valores = array(
  213. "nome" => NULL,
  214. "sobrenome" => NULL,
  215. );
  216. else:
  217. $this->campos_valores = $campos;
  218. endif;
  219. $this->campopk = "id";
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement