Пример #1
0
  */
 function __construct($fileName)
 {
     $this->fileName = $fileName;
 }
 /**
  * Write line to log
  * @param $line
  * @return mixed
  * @throws Exception
  */
 function line($line)
 {
     if ($this->stderr) {
         error_log("{$line}");
     }
     if ($this->stdout) {
         echo "{$line}\n";
     }
     $fp = @fopen($this->fileName, 'a');
     if (!$fp) {
Пример #2
0
 public function query($sql, $bind = array())
 {
     if (false == empty($bind)) {
         $this->adjust($sql, $bind);
     }
     try {
         $timeStart = dbtime();
         $conn = $this->dataSource->connect4Read();
         $timeStart2 = dbtime();
         BeanFinder::get('debug')->begin('sql', 'sql query');
         if (empty($bind)) {
             BeanFinder::get('debug')->appendDescription($sql);
             $stmt = $conn->query($sql);
             $timeEnd3 = dbtime();
             $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
             BeanFinder::get('debug')->end();
             if ($this->isDebug && false == $this->isMssqlDataSource()) {
                 $stmt = $conn->query('explain ' . $sql);
                 $explain = $stmt->fetchAll(PDO::FETCH_ASSOC);
             }
             $timeEnd4 = dbtime();
             $time = ($timeEnd4 - $timeStart) * 1000;
             $time2 = ($timeStart2 - $timeStart) * 1000;
             $time3 = 0;
             $time4 = ($timeEnd3 - $timeStart2) * 1000;
             $time5 = ($timeEnd4 - $timeEnd3) * 1000;
             $this->writeDevLog($time, $sql);
             if ($time > 1000) {
                 //总时间 连接 0 query fetch
                 error_log(date('m-d H:i:s') . " - [{$time}] - [{$time2}] - [{$time3}] - [{$time4}] - [{$time5}] - [{$this->dataSource->getConnectionStatus4Read()}] - " . $sql . "\n", 3, '/tmp/slowsqls.log');
             }
         } else {
             $echoSql = $this->buildSql($sql, $bind);
             BeanFinder::get('debug')->appendDescription(' with bind => ' . $echoSql);
             BeanFinder::get('debug')->addParams(array(array('sql' => $sql), array('bind' => $bind)), true);
             $stmt = $conn->prepare($sql);
             foreach ($bind as $key => $value) {
                 if (false !== strpos($key, '?')) {
                     if (is_int($value)) {
                         $stmt->bindValue($key, $value, PDO::PARAM_INT);
                     } else {
                         $stmt->bindValue($key, $value, PDO::PARAM_STR);
                     }
                 }
             }
             $timeEnd2 = dbtime();
             $stmt->execute($bind);
             $timeEnd3 = dbtime();
             $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
             $timeEnd4 = dbtime();
             BeanFinder::get('debug')->end();
             if ($this->isDebug && false == $this->isMssqlDataSource()) {
                 $stmt = $conn->prepare('explain ' . $sql);
                 $stmt->execute($bind);
                 $explain = $stmt->fetchAll(PDO::FETCH_ASSOC);
             }
             $time = ($timeEnd4 - $timeStart) * 1000;
             $time2 = ($timeStart2 - $timeStart) * 1000;
             $time3 = ($timeEnd2 - $timeStart2) * 1000;
             $time4 = ($timeEnd3 - $timeEnd2) * 1000;
             $time5 = ($timeEnd4 - $timeEnd3) * 1000;
             $this->writeDevLog($time, $echoSql);
             if ($time > 1000) {
                 //总时间 连接 prepare execute fetch
                 error_log(date('m-d H:i:s') . " - [{$time}] - [{$time2}] - [{$time3}] - [{$time4}] - [{$time5}] - [{$this->dataSource->getConnectionStatus4Read()}] - " . $echoSql . "\n", 3, '/tmp/slowsqls.log');
             }
         }
         return $result;
     } catch (Exception $e) {
         error_log(date('Y-m-d H:i:s') . " " . print_r(unserialize(serialize($e)), true), 3, '/tmp/exception_db.log');
         throw new SystemException("SqlError : {$this->buildSql($sql, $bind)}, Message : " . $e->getMessage() . ", Trace : " . $e->getTraceAsString());
     }
 }
Пример #3
0
<?php

$response = new JsonResponse();
try {
    if (!filter_var(r('email'), FILTER_VALIDATE_EMAIL)) {
        throw new Exception("Please provide valid email address!");
    }
    if (!($subscriber = LingopandaSubscribers::findRow('email = ?', r('email')))) {
        $subscriber = new LingopandaSubscribers();
        $subscriber->email = r('email');
        $subscriber->firstname = r('firstname');
        $subscriber->lastname = r('lastname');
        $subscriber->created = dbtime();
        $subscriber->id = $subscriber->insert();
    }
    $response->data = $subscriber;
    $response->send();
} catch (Exception $e) {
    $response->error($e->getMessage());
}