cron_log2sqlite, sqlite connection(proto, working for purpose)

This commit is contained in:
Ulf Gebhardt 2014-11-28 00:46:37 +01:00
parent c83b5a03f9
commit f2f3c6b839
9 changed files with 258 additions and 2 deletions

70
cron/cron_log2sqlite.php Normal file
View File

@ -0,0 +1,70 @@
<?php
namespace SYSTEM\CRON;
class cron_log2sqlite extends \SYSTEM\CRON\cronjob{
public static function run(){
//find oldest value
$oldest = \SYSTEM\DBD\SYS_LOG_OLDEST::Q1();
list( $now_month, $now_year ) = preg_split( "/ /", date("m Y"));
//All fine -> abort
if( $oldest['year'] >= $now_year &&
$oldest['month'] >= $now_month){
return cronstatus::CRON_STATUS_SUCCESFULLY;}
$filename = '/home/web/webdir/mojotrollz/mojotrollz/files/log/'.$oldest['year'].'.'.$oldest['month'].'.db';
//extract whole month to file
$con = new \SYSTEM\DB\Connection(new \SYSTEM\DB\DBInfoSQLite($filename,0666));
//create table
$con->query('CREATE TABLE IF NOT EXISTS `system_log` ('.
' `ID` INT(10) NOT NULL,'.
' `class` TEXT NOT NULL,'.
' `message` TEXT NOT NULL,'.
' `code` INT(11) NOT NULL,'.
' `file` TEXT NOT NULL,'.
' `line` INT(11) NOT NULL,'.
' `trace` TEXT NOT NULL,'.
' `ip` TEXT NOT NULL,'.
' `querytime` DOUBLE NOT NULL,'.
' `time` DATETIME NOT NULL,'.
' `server_name` CHAR(255) NOT NULL,'.
' `server_port` INT(10) NOT NULL,'.
' `request_uri` CHAR(255) NOT NULL,'.
' `post` TEXT NOT NULL,'.
' `http_referer` CHAR(255) NULL DEFAULT NULL,'.
' `http_user_agent` TEXT NOT NULL,'.
' `user` INT(11) NULL DEFAULT NULL,'.
' `thrown` BIT(1) NOT NULL,'.
' PRIMARY KEY (`ID`)'.');');
//write data
$con->exec('begin transaction');
$res = \SYSTEM\DBD\SYS_LOG_MONTH::QQ(array($oldest['month'],$oldest['year']));
$i = 0;
while($row = $res->next()){
set_time_limit(30);
$i++;
if(!$con->exec('INSERT OR IGNORE INTO '.\SYSTEM\DBD\system_log::NAME_MYS.
'(`ID`, `class`, `message`, `code`, `file`, `line`, `trace`, `ip`, `querytime`, `time`,'.
' `server_name`, `server_port`, `request_uri`, `post`,'.
' `http_referer`, `http_user_agent`, `user`, `thrown`)'.
'VALUES ('.$row['ID'].', \''.\SQLite3::escapeString($row['class']).'\', \''.\SQLite3::escapeString($row['message']).'\', '.
$row['code'].', \''.\SQLite3::escapeString($row['file']).'\', '.$row['line'].', \''.\SQLite3::escapeString($row['trace']).'\', \''.
$row['ip'].'\', '.$row['querytime'].', \''.$row['time'].'\', \''.
\SQLite3::escapeString($row['server_name']).'\', '.$row['server_port'].', \''.\SQLite3::escapeString($row['request_uri']).'\', \''.\SQLite3::escapeString($row['post']).'\', \''.
\SQLite3::escapeString($row['http_referer']).'\', \''.\SQLite3::escapeString($row['http_user_agent']).'\', '.$row['user'].','.true.');')){
new \SYSTEM\LOG\ERROR('failed to insert into log archiev');
return cronstatus::CRON_STATUS_FAIL;
}
}
if(!$con->exec('end transaction')){
new \SYSTEM\LOG\ERROR('failed to insert into log archiev');
return cronstatus::CRON_STATUS_FAIL;};
//delete from database
if(!\SYSTEM\DBD\SYS_LOG_MONTH_DEL::QI(array($oldest['month'],$oldest['year']))){
new \SYSTEM\LOG\ERROR('failed to delete log entries');
return cronstatus::CRON_STATUS_FAIL;}
return cronstatus::CRON_STATUS_SUCCESFULLY;
}
}

View File

@ -1,7 +1,6 @@
<?php
namespace SYSTEM\CRON;
class cronjob {
class cronjob{
public static function run(){
new \RuntimeException("Unimplemented!");}
}

View File

@ -20,6 +20,8 @@ class Connection extends ConnectionAbstr{
$this->connection = new \SYSTEM\DB\ConnectionMYS($dbinfo);
} else if ($dbinfo instanceof \SYSTEM\DB\DBInfoAMQP){
$this->connection = new \SYSTEM\DB\ConnectionAMQP($dbinfo);
} else if ($dbinfo instanceof \SYSTEM\DB\DBInfoSQLite){
$this->connection = new \SYSTEM\DB\ConnectionSQLite($dbinfo);
} else {
throw new \Exception('Could not understand Database Settings. Check ur Database Settings');}
}
@ -39,4 +41,7 @@ class Connection extends ConnectionAbstr{
//Query connected Database
public function query($query){
return $this->connection->query($query);}
public function exec($query){
return $this->connection->exec($query);}
}

View File

@ -0,0 +1,57 @@
<?php
namespace SYSTEM\DB;
class ConnectionSQLite extends ConnectionAbstr {
private $connection = NULL;
public function __construct(DBInfo $dbinfo, $new_link = false, $client_flag = 0){
$error = null;
$this->connection = new \SQLite3($dbinfo->m_database);
if(!$this->connection){
throw new \Exception('Could not connect to Database. Check ur Database Settings: '.$error);}
}
public function __destruct(){
$this->close();}
public function prepare($stmtName, $stmt, $values){
$prepStmt = \sqlite_prepare($this->connection, $stmt,$error);
if(!$prepStmt){
throw new \SYSTEM\LOG\ERROR('Prepared Statement prepare fail: '. $error);}
$types = '';
$binds = array($prepStmt,null);
for($i =0; $i < \count($values);$i++){
$types .= self::getPrepareValueType($values[$i]);
$binds[] = &$values[$i];}
$binds[1] = $types;
\call_user_func_array('sqlite_stmt_bind_param', $binds); //you need 2 append the parameters - thats the right way to do that.
if(!sqlite_stmt_execute($prepStmt,$error)){
throw new \SYSTEM\LOG\ERROR("Could not execute prepare statement: ". $error);}
return new ResultSQLite($prepStmt,$this);
}
public function close(){
return $this->connection->close();}
public function query($query){
$result = $this->connection->query($query);
if(!$result){
throw new \SYSTEM\LOG\ERROR('Could not query Database. Check ur Query Syntax or required Rights: '.$this->connection->lastErrorMsg());}
if($result === TRUE){
return TRUE;}
return new ResultSQLite($result);
}
public function exec($query){
$result = false;
$result = $this->connection->exec($query);
return $result;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace SYSTEM\DB;
class DBInfoSQLite extends \SYSTEM\DB\DBInfo {
public function __construct($database , $permissions , $password = null, $host = null, $port = null){
$this->m_database = $database;
$this->m_user = $permissions;
$this->m_password = $password;
$this->m_host = $host;
$this->m_port = $port;
if( $this->m_database == null ||
$this->m_user == null){
throw new \Exception("DBInfo not correct, database, permissions are null");}
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace SYSTEM\DB;
class ResultSQLite extends \SYSTEM\DB\Result{
private $res = NULL;
private $current = NULL;
//Result from mysql_query
public function __construct($res){
$this->res = $res;}
public function __destruct(){
$this->close();}
public function close(){
$this->res->finalize();}
public function count(){
throw new Exception("Problem SQLite");
return mysqli_num_rows($this->res);}
/*
* if ($res->numColumns() && $res->columnType(0) != SQLITE3_NULL) {
// have rows
} else {
// zero rows
}
*/
public function affectedRows(){
return mysqli_affected_rows($this->res);}
public function next($object = false, $result_type = SQLITE3_BOTH){
if($object){
throw new Exception("Problem SQLite");
} else {
$this->current = $this->res->fetchArray($result_type);
}
return $this->current;
}
/*
* function fetchObject($sqlite3result, $objectType = NULL) {
$array = $sqlite3result->fetchArray();
if(is_null($objectType)) {
$object = new stdClass();
} else {
// does not call this class' constructor
$object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($objectType), $objectType));
}
$reflector = new ReflectionObject($object);
for($i = 0; $i < $sqlite3result->numColumns(); $i++) {
$name = $sqlite3result->columnName($i);
$value = $array[$name];
try {
$attribute = $reflector->getProperty($name);
$attribute->setAccessible(TRUE);
$attribute->setValue($object, $value);
} catch (ReflectionException $e) {
$object->$name = $value;
}
}
return $object;
}
*/
public function seek($row_number){
throw new Exception("Problem SQLite");
return mysqli_data_seek($this->res,$row_number);}
}

11
dbd/qq/SYS_LOG_MONTH.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace SYSTEM\DBD;
class SYS_LOG_MONTH extends \SYSTEM\DB\QP {
protected static function query(){
return new \SYSTEM\DB\QQuery(get_class(),
//pg
'SELECT * FROM '.\SYSTEM\DBD\system_log::NAME_PG.' WHERE MONTH(time) = $1 AND YEAR(time) = $2 ORDER BY time ASC',
//mys
'SELECT * FROM '.\SYSTEM\DBD\system_log::NAME_MYS.' WHERE MONTH(time) = ? AND YEAR(time) = ? ORDER BY time ASC'
);}}

View File

@ -0,0 +1,11 @@
<?php
namespace SYSTEM\DBD;
class SYS_LOG_MONTH_DEL extends \SYSTEM\DB\QP {
protected static function query(){
return new \SYSTEM\DB\QQuery(get_class(),
//pg
'DELETE FROM '.\SYSTEM\DBD\system_log::NAME_PG.' WHERE MONTH(time) = $1 AND YEAR(time) = $2;',
//mys
'DELETE FROM '.\SYSTEM\DBD\system_log::NAME_MYS.' WHERE MONTH(time) = ? AND YEAR(time) = ?;'
);}}

11
dbd/qq/SYS_LOG_OLDEST.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace SYSTEM\DBD;
class SYS_LOG_OLDEST extends \SYSTEM\DB\QQ {
protected static function query(){
return new \SYSTEM\DB\QQuery(get_class(),
//pg
'SELECT MONTH(time) as month, YEAR(time) as year FROM '.\SYSTEM\DBD\system_log::NAME_PG.' ORDER BY time ASC LIMIT 1',
//mys
'SELECT MONTH(time) as month, YEAR(time) as year FROM '.\SYSTEM\DBD\system_log::NAME_MYS.' ORDER BY time ASC LIMIT 1'
);}}