-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathsql_datasource.php
More file actions
119 lines (104 loc) · 3.16 KB
/
Copy pathsql_datasource.php
File metadata and controls
119 lines (104 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
require_once("dbhelper.class.php");
class ExtDBHelper extends DBHelper
{
public function initDBMysql($dbname, $host, $user, $pass)
{
$this->_db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);
}
public function initDBPDOStr($user, $pass, $pdostr)
{
// fix by Mr Lei for UTF8 special chars
$this->_db = new PDO("$pdostr", $user, $pass);
}
}
class SQL_Datasource extends Magmi_Datasource
{
public $dbh;
public $stmt;
public $extractsql;
public $sqlfile;
public function initialize($params)
{
$this->dbh = new ExtDBHelper();
$cdbtype = $this->getParam("SQL:dbtype");
$cdbusr = $this->getParam("SQL:dbuser");
$cdbpass = $this->getParam("SQL:dbpass");
if ($cdbtype == "other") {
$cdbpdostr = $this->getParam("SQL:pdostr", "");
$this->dbh->initDBPDOStr($cdbusr, $cdbpass, $cdbpdostr);
} else {
$cdbname = $this->getParam("SQL:dbname");
$cdbhost = $this->getParam("SQL:dbhost");
$extra = $this->getParam("SQL:dbextra");
$this->dbh->initDbMysql($cdbname, $cdbhost, $cdbusr, $cdbpass);
}
// handle extra initial commands
if (isset($extra) && $extra != "") {
foreach (explode(";\n", $extra) as $st) {
if ($st != "") {
$this->dbh->exec_stmt($st);
}
}
}
$this->stmt = null;
$this->sqlfile = $this->getParam("SQL:queryfile");
$this->extractsql = file_get_contents($this->sqlfile);
}
public function getPluginInfo()
{
return array("name"=>"Generic SQL Datasource","author"=>"Dweeves","version"=>"1.0.3");
}
public function getPluginParamNames()
{
return array("SQL:dbtype","SQL:dbname","SQL:dbhost","SQL:dbuser","SQL:dbpass","SQL:dbextra","SQL:queryfile",
"SQL:pdostr");
}
public function startImport()
{
}
public function getSQLFileList()
{
$files = glob(dirname(__file__) . "/requests/*.sql");
return $files;
}
public function getRecordsCount()
{
$sql = null;
// optimized count query
if (file_exists($this->sqlfile . ".count")) {
$sql = file_get_contents($this->sqlfile . ".count");
}
if (!isset($sql)) {
$sql = "SELECT COUNT(*) as cnt FROM (" . str_replace("\n", " ", $this->extractsql) . ") as t1";
}
$cnt = $this->dbh->selectone($sql, null, "cnt");
return $cnt;
}
public function getColumnNames($prescan = false)
{
$s = $this->dbh->select($this->extractsql);
$test = $s->fetch();
$s->closeCursor();
unset($s);
$cl = array_keys($test);
return $cl;
}
public function getNextRecord()
{
if (!isset($this->stmt)) {
$this->stmt = $this->dbh->select($this->extractsql);
}
$data = $this->stmt->fetch();
if (!$data) {
return false;
}
return $data;
}
public function endImport()
{
}
public function afterImport()
{
}
}