2021-10-30 19:02:07 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class Sql_model extends MO_Model
|
|
|
|
|
{
|
2021-11-14 11:02:22 +01:00
|
|
|
|
public function __construct()
|
2021-10-30 19:02:07 +02:00
|
|
|
|
{
|
2021-11-14 11:02:22 +01:00
|
|
|
|
parent::__construct();
|
2021-10-30 19:02:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
//updates the db version, to current time, only used when saving a change as sql which already in db
|
|
|
|
|
//like changes from phpmyadmin
|
|
|
|
|
//dummy function to call the inherited _update_db_version($time);
|
|
|
|
|
public function update_db_version($time)
|
|
|
|
|
{
|
|
|
|
|
$this->_update_db_version($time);
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
//dummy function which uses inherited _create_sql($sql);
|
|
|
|
|
public function create_sql($sql)
|
|
|
|
|
{
|
|
|
|
|
$this->_create_sql($sql);
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
public function get_db_version()
|
2021-10-30 19:02:07 +02:00
|
|
|
|
{
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$sql = "SELECT version FROM db_version WHERE id='1'";
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$q = $this->db->query($sql);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$res = $q->row_array();
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
return $res['version'];
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
public function get_appliable_files()
|
2021-10-30 19:02:07 +02:00
|
|
|
|
{
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$this->load->helper('file');
|
|
|
|
|
//$full_list = get_filenames('./sql');
|
|
|
|
|
|
|
|
|
|
$this->load->helper('directory');
|
|
|
|
|
$full_list = directory_map('./sql', 1);
|
|
|
|
|
|
|
|
|
|
//get out the full db dumps
|
|
|
|
|
foreach ($full_list as $row) {
|
|
|
|
|
if (substr_compare($row, 'db', 0, 2, true) && substr_compare($row, 'map', 0, 3, true) &&
|
|
|
|
|
substr_compare($row, 'old', 0, 3, true) && substr_compare($row, 'index', 0, 5, true)) {
|
|
|
|
|
$list[] = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
if (!isset($list)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$ver = $this->get_db_version();
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
foreach ($list as $row) {
|
|
|
|
|
$l = explode('.', $row);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
if ($l[0] > $ver) {
|
|
|
|
|
$data[] = $row;
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
if (!isset($data)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
//getting the max
|
|
|
|
|
$max = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($data as $row) {
|
|
|
|
|
$l = explode('.', $row);
|
|
|
|
|
|
|
|
|
|
if ($l[0] > $max) {
|
|
|
|
|
$max = $l[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$smallest = $max;
|
|
|
|
|
$last_found = 0;
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$ordered[] = $max . ".sql";
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
for ($i = 0; $i < (sizeof($data) - 1); $i++) {
|
|
|
|
|
foreach ($data as $row) {
|
|
|
|
|
$l = explode('.', $row);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
if (($l[0] < $smallest) && ($l[0] > $last_found)) {
|
|
|
|
|
$smallest = $l[0];
|
|
|
|
|
$ord = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$last_found = $smallest;
|
|
|
|
|
$smallest = $max;
|
|
|
|
|
$ordered[$i] = $ord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($ordered)) {
|
|
|
|
|
return $ordered;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function apply_all_sql()
|
2021-10-30 19:02:07 +02:00
|
|
|
|
{
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$this->load->helper('file');
|
|
|
|
|
|
|
|
|
|
$list = $this->get_appliable_files();
|
|
|
|
|
|
|
|
|
|
if (!$list) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < sizeof($list); $i++) {
|
|
|
|
|
//reading file, then applying it to the db
|
|
|
|
|
$loc = './sql/' . $list[$i];
|
|
|
|
|
$sql = read_file($loc);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
var_dump($loc);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
$this->db->query($sql);
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
//TODO check if error happened, then stop everything
|
|
|
|
|
//check is not needed, it just throws the error
|
2021-10-30 19:02:07 +02:00
|
|
|
|
|
2021-11-14 11:02:22 +01:00
|
|
|
|
//update db_version
|
|
|
|
|
$ver = explode('.', $list[$i]);
|
|
|
|
|
$sqlv = "UPDATE db_version SET version='" . $ver[0] . "' WHERE id='1'";
|
|
|
|
|
$this->db->query($sqlv);
|
|
|
|
|
}
|
2021-10-30 19:02:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//nowhitesp
|