2021-03-24 10:26:51 +00:00
|
|
|
<?php
|
2021-03-24 15:47:27 +00:00
|
|
|
|
2021-03-24 10:26:51 +00:00
|
|
|
/*
|
|
|
|
GPLv2 (C) <2021> <HashCasper>
|
|
|
|
|
|
|
|
This file is part of D2IM.
|
|
|
|
|
|
|
|
D2IM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
D2IM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with D2IM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class D2Database {
|
|
|
|
|
|
|
|
public function __construct() {
|
2021-03-26 18:31:13 +00:00
|
|
|
PDO_Connect("sqlite:" . DB_FILE);
|
2021-03-24 10:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function createTables($file, $data) {
|
|
|
|
$tableName = basename($file);
|
|
|
|
$tableName = strtolower(substr($tableName, 0, -4));
|
|
|
|
$sql = "CREATE TABLE IF NOT EXISTS `$tableName` (";
|
2021-03-26 21:44:33 +00:00
|
|
|
if (!empty($data)) {
|
|
|
|
foreach ($data[0] as $k => $v) {
|
|
|
|
if (is_numeric($v)) {
|
|
|
|
$dataType = "INT";
|
|
|
|
} else {
|
|
|
|
$dataType = "VARCHAR(255)";
|
|
|
|
}
|
|
|
|
$sql .= "`$k` $dataType NOT NULL,";
|
2021-03-24 10:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$sql = rtrim($sql, ",");
|
|
|
|
$sql .= ")";
|
|
|
|
$res = PDO_Execute($sql);
|
|
|
|
}
|
2021-03-26 21:44:33 +00:00
|
|
|
|
2021-03-24 10:26:51 +00:00
|
|
|
public function fillsTables($file, $data) {
|
|
|
|
$tableName = basename($file);
|
|
|
|
$tableName = strtolower(substr($tableName, 0, -4));
|
|
|
|
|
2021-03-26 19:09:05 +00:00
|
|
|
$sql = '';
|
2021-03-26 21:44:33 +00:00
|
|
|
if (!empty($data)) {
|
|
|
|
foreach ($data as $d) {
|
2021-03-26 21:52:19 +00:00
|
|
|
|
2021-03-26 21:44:33 +00:00
|
|
|
if (!empty($d)) {
|
2021-03-26 21:52:19 +00:00
|
|
|
$sql = "INSERT INTO `$tableName` (";
|
2021-03-26 21:44:33 +00:00
|
|
|
foreach ($d as $k => $v) {
|
|
|
|
$sql .= "`$k`" . ",";
|
|
|
|
}
|
2021-03-26 21:52:19 +00:00
|
|
|
$sql = rtrim($sql, ",");
|
|
|
|
$sql .= ") ";
|
2021-03-24 10:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 14:37:56 +00:00
|
|
|
}
|
|
|
|
$sql .= "VALUES ";
|
2021-03-26 21:44:33 +00:00
|
|
|
if (!empty($data)) {
|
|
|
|
foreach ($data as $d) {
|
|
|
|
if (!empty($d)) {
|
2021-03-26 21:52:19 +00:00
|
|
|
$sql .= "(";
|
2021-03-26 21:44:33 +00:00
|
|
|
foreach ($d as $k => $v) {
|
|
|
|
$sql .= '"' . $v . '"' . ",";
|
|
|
|
}
|
2021-03-26 21:52:19 +00:00
|
|
|
$sql = rtrim($sql, ",");
|
|
|
|
$sql .= "), ";
|
2021-03-24 10:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 21:44:33 +00:00
|
|
|
$sql = rtrim($sql, ", ");
|
|
|
|
PDO_Execute($sql);
|
2021-03-24 10:26:51 +00:00
|
|
|
}
|
2021-03-26 21:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|