Exemplo n.º 1
0
 /**
  * Reset the internal static instance.
  */
 public static function resetInstance()
 {
     if (self::$instance) {
         self::$instance->reset();
         self::$instance = null;
     }
 }
Exemplo n.º 2
0
 /**
  * Log method, must be in all classes for Logger class functionality.
  *
  * @param string $type Log message type from Logger class.
  * @param string $message Message text.
  * @param int $time Timestamp.
  *
  * @return int Number of affected rows.
  * @throws \Asymptix\db\DBCoreException If some database error occurred.
  */
 public static function log($type, $message, $time = null)
 {
     if (is_null($time)) {
         $time = time();
     }
     $query = "INSERT INTO " . self::TABLE_NAME . " (type, message, count, last_seen) VALUES (?, ?, 1, ?)\n                  ON DUPLICATE KEY UPDATE count = count + 1, last_seen = ?";
     try {
         return DBCore::doUpdateQuery($query, "ssss", [$type, $message, date("Y-m-d H:i:s", $time), date("Y-m-d H:i:s", $time)]);
     } catch (\Asymptix\db\DBCoreException $e) {
         print $e->getMessage();
     }
 }
Exemplo n.º 3
0
 public static function log($type, $called, $script, $line, $message)
 {
     if (is_null($called)) {
         $called = $_SERVER['SCRIPT_NAME'];
     }
     $errorTypes = array();
     if ($type === 0) {
         $errorTypes[] = "E_LOG_INFO";
     } else {
         for ($i = 0; $i < 15; $i++) {
             $errorType = self::friendlyErrorType($type & pow(2, $i));
             if (!empty($errorType)) {
                 $errorTypes[] = $errorType;
             }
         }
     }
     $query = "INSERT INTO " . self::TABLE_NAME . " (type, error_type, called, script, line, message, count) VALUES (?, ?, ?, ?, ?, ?, 1)\n                  ON DUPLICATE KEY UPDATE count = count + 1, last_seen = ?";
     return DBCore::doUpdateQuery($query, "isssiss", array($type, implode(", ", $errorTypes), $called, $script, $line, $message, date("Y-m-d H:i:s")));
 }
Exemplo n.º 4
0
$query = new DBPreparedQuery("SHOW TABLES");
$stmt = $query->go();
if ($stmt !== false) {
    $tpl = file_get_contents(CLASS_TPL);
    while ($resultSet = DBCore::bindResults($stmt)) {
        $tableName = $resultSet['TABLE_NAMES']['Tables_in_' . conf\Config::getDBConfigParam('DBNAME')];
        OutputStream::msg(OutputStream::MSG_INFO, "Reading structure for table '" . $tableName . "'...");
        $idFieldName = 'id';
        $fieldsListStr = "";
        $fieldsList = DBCore::getTableFieldsList($tableName);
        if (!empty($fieldsList)) {
            foreach ($fieldsList as $field => $attributes) {
                if ($attributes['key'] === 'PRI') {
                    $idFieldName = $field;
                }
                $fieldsListStr .= "        " . DBCore::getPrintableFieldString($field, $attributes);
            }
            $fieldsListStr = substr($fieldsListStr, 0, strlen($fieldsListStr) - 1);
            $className = getClassName($tableName);
            $content = str_replace(array('{{CLASS_NAME}}', '{{TABLE_NAME}}', '{{PRIMARY_KEY}}', '{{FIELDS_LIST}}', '{{YEAR}}', '{{AUTHOR}}', '{{EMAIL}}'), array($className, $tableName, $idFieldName, $fieldsListStr, date("Y"), AUTHOR, EMAIL), $tpl);
            file_put_contents(RESULTS_PATH . $className . ".php", $content);
            OutputStream::msg(OutputStream::MSG_SUCCESS, "Class '" . RESULTS_PATH . $className . ".php' generated.");
        } else {
            OutputStream::msg(OutputStream::MSG_ERROR, "Can't read structure for table '" . $tableName . "'.");
        }
    }
    $stmt->close();
} else {
    OutputStream::msg(OutputStream::MSG_ERROR, "Can't read tables list.");
}
OutputStream::close();
Exemplo n.º 5
0
 /**
  * Deletes DB record for current DBObject.
  *
  * @return mixed Number of affected rows (1 if some record was deleted,
  *            0 - if no) or FALSE if some error occurred.
  */
 public function delete()
 {
     return DBCore::deleteDBObject($this);
 }
Exemplo n.º 6
0
 /**
  * Executes SQL query.
  *
  * @param boolean $debug Debug mode flag.
  *
  * @return mixed Statement object or FALSE if an error occurred if SELECT
  *           query executed or number of affected rows on success if other
  *           type of query executed.
  */
 public function go($debug = false)
 {
     if ($debug) {
         DBQuery::showQueryDebugInfo($this->query, $this->types, $this->params);
     } else {
         if ($this->isSelect()) {
             return DBCore::doSelectQuery($this);
         } else {
             return DBCore::doUpdateQuery($this);
         }
     }
 }
Exemplo n.º 7
0
 /**
  * Executes SQL query.
  *
  * @param bool $debug Debug mode flag.
  *
  * @return mixed Statement object or FALSE if an error occurred if SELECT
  *           query executed or number of affected rows on success if other
  *           type of query executed.
  */
 public function go($debug = false)
 {
     if ($debug) {
         $this->debug();
     } else {
         if ($this->isSelector()) {
             return DBCore::doSelectQuery($this);
         }
         return DBCore::doUpdateQuery($this);
     }
 }
Exemplo n.º 8
0
/**
 * Database connections module.
 *
 * @category Asymptix PHP Framework
 * @author Dmytro Zarezenko <*****@*****.**>
 * @copyright (c) 2009 - 2015, Dmytro Zarezenko
 *
 * @git https://github.com/Asymptix/Framework
 * @license http://opensource.org/licenses/MIT
 */
use Asymptix\db\DBCore;
use conf\Config;
$mysqli = new mysqli(Config::getDBConfigParam('HOST'), Config::getDBConfigParam('USER'), Config::getDBConfigParam('PASSWORD'), Config::getDBConfigParam('DBNAME'));
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
} else {
    if (!$mysqli->set_charset(Config::getDBConfigParam('DB_CHARSET'))) {
        printf("Error loading character set " . Config::getDBConfigParam('DB_CHARSET') . ": %s\n", $mysqli->error);
    }
    $manager = DBCore::getInstance();
    DBCore::connection($mysqli);
    //$manager->setCurrentConnection('first');
}
// Register a shutdown function which will close DB connection
register_shutdown_function(function () {
    global $manager;
    $conns = $manager->getConnections();
    foreach ($conns as $connResource) {
        $manager->closeConnection($connResource);
    }
});
Exemplo n.º 9
0
 /**
  * Executes SQL query with single record and return this record.
  *
  * @param string $query SQL query to execute.
  * @param string $types Types string (ex: "isdb").
  * @param array $params Parameters in the same order like types string.
  *
  * @return array Selected record with table names as keys or NULL if no
  *           data selected.
  * @throws DBCoreException If no one or more than one records selected.
  */
 public static function selectSingleRecord($query, $types = "", $params = array())
 {
     $stmt = self::doSelectQuery($query, $types, $params);
     if ($stmt !== false) {
         $record = null;
         if ($stmt->num_rows === 1) {
             $record = DBCore::bindResults($stmt);
         }
         $stmt->close();
         if (is_null($record)) {
             throw new DBCoreException("No one or more than one records selected.");
         }
         return $record;
     }
     return null;
 }