Last active
April 23, 2018 07:45
-
-
Save JannieT/3a6e278f7c58281d0e742b2b0dfdd8a7 to your computer and use it in GitHub Desktop.
Sample php script to populate a mysql databse from a sqlite database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'Source.php'; | |
include 'Target.php'; | |
$SELECTED = isset($argv[1]) ? $argv[1] : null; | |
if ($SELECTED == "1") { | |
$target = new Target(); | |
try { | |
echo $target->clear("logs") . "\n"; | |
$result = Source::loop("tbl_log", [$target,'addLog']); | |
echo count($result) . " log records added.\n"; | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
$target->close(); | |
} | |
if ($SELECTED == "2") { | |
$target = new Target(); | |
try { | |
echo $target->clear("donors") . "\n"; | |
$result = Source::loop("tbl_donors", [$target,'addDonor']); | |
echo count($result) . " donor records added.\n"; | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
$target->close(); | |
} | |
?> | |
------------------------------ | |
Menu | |
------------------------------ | |
1. Logs | |
2. Donors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Source | |
{ | |
const CONNECTION = 'sqlite:pim6.sqlite'; | |
private $db; | |
public function __construct() | |
{ | |
$this->db = new PDO(self::CONNECTION); | |
// $this->db->exec("SET NAMES 'UTF8'"); | |
} | |
public function prepare($sql) | |
{ | |
return $this->db->prepare($sql); | |
} | |
public function close() | |
{ | |
$this->db = null; | |
} | |
public static function loop($table, $callback) | |
{ | |
$out = []; | |
$source = new Source(); | |
$sql = " | |
SELECT * | |
FROM $table | |
ORDER BY id | |
"; | |
$query = $source->prepare($sql); | |
if ($query->execute([])) | |
{ | |
while ($node = $query->fetchObject()) | |
{ | |
$out[] = call_user_func_array($callback, [$node]); | |
} | |
} | |
$source->close(); | |
return $out; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Target | |
{ | |
const CONNECTION = 'mysql:host=localhost;dbname=pim7'; | |
const USER = 'root'; | |
const PASSWORD = '<redacted>'; | |
private static $blacklist = ["0", "596"]; | |
private $db; | |
public function __construct() | |
{ | |
$this->db = new PDO(self::CONNECTION, self::USER, self::PASSWORD); | |
$this->db->exec("SET NAMES 'utf8'"); | |
} | |
public function addDonor($row) | |
{ | |
if (in_array($row->name_id, self::$blacklist)) return ""; | |
$now = (new \DateTime())->format('Y-m-d H:i:s'); | |
$data = [ | |
"created_at" => $now, | |
"updated_at" => $now, | |
"name_id" => $row->name_id, | |
"pattern" => $row->pattern, | |
"last_amount" => $row->last_amount, | |
"last_date" => $row->last_date, | |
"is_fixed" => empty($row->fixed_amount) ? 0 : 1 | |
]; | |
return $this->add("donors", $data); | |
} | |
public function addLog($row) | |
{ | |
if (in_array($row->name_id, self::$blacklist)) return ""; | |
$now = (new \DateTime())->format('Y-m-d H:i:s'); | |
$data = [ | |
"created_at" => $now, | |
"updated_at" => $now, | |
"name_id" => $row->name_id, | |
"notes" => $row->notes, | |
"logged_at" => $row->log_date, | |
"is_incoming" => $row->incoming, | |
"is_auto" => $row->auto_entry | |
]; | |
// var_dump($data); die(); | |
return $this->add("logs", $data); | |
} | |
/** | |
* ------------------------------------------- | |
* utilities | |
* ------------------------------------------- | |
*/ | |
protected function add($table, $data) | |
{ | |
$sql = $this->prepInsert($table, $data); | |
if ($this->prepare($sql)->execute($data)) | |
{ | |
return "."; | |
} | |
die("not added " . print_r($data)); | |
} | |
public function clear($table) | |
{ | |
$sql = "DELETE FROM $table"; | |
if ($this->prepare($sql)->execute([])) { | |
return "$table table cleared."; | |
} | |
throw new Exception("Couldn't clear table $table."); | |
} | |
public function prepare($sql) | |
{ | |
return $this->db->prepare($sql); | |
} | |
public function close() | |
{ | |
$this->db = null; | |
} | |
private function prepInsert($tableName, $data) | |
{ | |
$keys = array_keys($data); | |
$fields = '`' . implode('`, `', $keys) . '`'; | |
// $placeholder = substr(str_repeat('?,',count($keys)),0,-1); | |
$placeholder = implode(', ', array_map(function($key){ | |
return ":$key"; | |
}, $keys)); | |
return "INSERT INTO $tableName ($fields) VALUES ($placeholder)"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment