Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. /**
  2. * Em desenvolvimento.
  3. * Objeto para gerenciar uma
  4. *conexão ao banco de dados Mysql
  5. * @author Igor
  6. */
  7. class db {
  8. private $dsn;
  9. private $username;
  10. private $password;
  11. private $db;
  12. private $conn;
  13.  
  14. private $arrConnAttr;//Array of connection attributes. Ex: ["PDO::ATTR_ERRMODE", "PDO::ERRMODE_EXCEPTION"]
  15. private $pathErroLog;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
  16. private $arrCatchConnResult;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
  17. private $die;//If we set $setValues["die"]=NULL, during the object initialization, errors will not stop the process.
  18.  
  19. public $sql;
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. public function __construct(array $setValues=NULL) {
  27.  
  28. xdebug_break();
  29. $arrInitDefault["dsn"]="localhost";
  30. $arrInitDefault["username"]= "root";
  31. $arrInitDefault["password"]="";
  32. $arrInitDefault["db"]="drset2014";
  33.  
  34.  
  35.  
  36. $arrInitDefault["arrConnAttr"]= ["PDO::ATTR_ERRMODE"," PDO::ERRMODE_EXCEPTION"];
  37.  
  38. $arrInitDefault["pathErroLog"]= dirname(__FILE__)."/Log/Error";//Default path to dir. Note: error pathLog can be "/Log/Error/UserName" or "/Log/Error/UserId", etc
  39. $arrInitDefault["die"]= TRUE;
  40.  
  41. $arrInitDefault["sql"]= FALSE;
  42.  
  43.  
  44. if(!is_null($setValues)){
  45. $arrInitDefault= array_merge($arrInitDefault,$setValues);
  46. }
  47.  
  48. //After merge, initialaze private variables with result merged $arrValues
  49. $this->dsn = $arrInitDefault["dsn"];
  50. $this->username = $arrInitDefault["username"];
  51. $this->password = $arrInitDefault["password"];
  52. $this->db = $arrInitDefault["db"];
  53.  
  54.  
  55. $this->arrConnAttr = implode(",", $arrInitDefault["arrConnAttr"]);
  56. $this->pathErroLog=$arrInitDefault["pathErroLog"];
  57. $this->die=$arrInitDefault["die"];
  58. xdebug_break();
  59. //This try and catch is only to estabilish the connection during the initialization
  60. try {
  61.  
  62. $this->conn = new PDO("mysql:host=$this->dsn; dbname=$this->db", "$this->username", "$this->password");//Now private $conn is a PDO object
  63.  
  64. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Setting Attributes to the PDO object ($this->conn)
  65. }// End of try
  66. catch(PDOException $e){//If had some error. The PDO object ($this->conn) could not be created. Throw an error.
  67. $this->arrCatchConnResult = $this->saveLogError($e->getMessage());//Save a message in the log file and throw a message
  68. $msg= $this->arrCatchConnResult["strToHTML"];
  69. $this->conn = null;
  70. if($this->die){
  71. die($msg);
  72. }
  73. }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. public function saveLogError($msg=NULL,$sql=NULL) {
  80. $year = date("Y");
  81. $month = date("M");
  82. $day = date("d");
  83. $dt = date("d.M.Y_H.i.s");
  84.  
  85. $pathErroLog= $this->pathErroLog."/".$year."/".$month."/".$day."/";
  86. if (!is_dir($pathErroLog)) {
  87. mkdir($pathErroLog,0777,true);
  88. }//If is not a dir, create it.
  89.  
  90. $sqlErrorStringTXT="";
  91. if(!is_null($sql)){
  92. $sqlErrorStringTXT = "Sql: $sql";
  93. $sqlErrorStringHTML = "<kbd>Sql</kbd>: $sql";
  94. }
  95.  
  96. /////////////// Maybe should be good save errors in database too.
  97.  
  98. $strToTxt=<<<EOF
  99. >>>>> Mensagem de erro: <<<<< \n
  100. Date: $dt \n
  101. Msg: $msg \n
  102. $sqlErrorStringTXT \n
  103. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  104. EOF;
  105.  
  106. //Save error message to the file
  107. $f=$pathErroLog."$dt.txt";
  108. $myfile = fopen($f, "w") or die("Unable to open file!");
  109. fwrite($myfile, $strToTxt);
  110. fclose($myfile);
  111.  
  112. //Create a string to be displayed in the broser
  113. $strToHTML=<<<EOF
  114. <div class="alert alert-danger" role="alert">
  115. Date: $dt <br/>
  116. Msg: <span class="label label-danger">$msg</span><br/>
  117. $sqlErrorStringHTML<br/>
  118. </div>
  119. EOF;
  120.  
  121. $arrCatchConnResult["status"]=0;
  122. $arrCatchConnResult["strToTxt"]=$strToTxt;
  123. $arrCatchConnResult["strToHTML"]=$strToHTML;
  124.  
  125. return $arrCatchConnResult;
  126.  
  127. }//End of saveLogError()
  128. public function db_Map() {
  129.  
  130. }
  131. public function get_values() {
  132. try {
  133. $stmt = $this->conn->prepare($this->sql);
  134. $stmt->execute();
  135. while ($field = $stmt->fetch(PDO::FETCH_ASSOC)) {
  136. $arrResult[]=$field;
  137. }
  138. return $arrResult;
  139. }
  140. catch (PDOException $e) {
  141. $this->arrCatchConnResult = $this->saveLogError($e->getMessage(), $this->sql);//Save a message in the log file and set a message
  142. $msg= $this->arrCatchConnResult["strToHTML"];
  143. $this->conn = null;
  144. if($this->die){
  145. die($msg);
  146. }
  147. }
  148. }
  149.  
  150. }//End of class db
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement