This repository was archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql2couchdb.php
More file actions
executable file
·216 lines (166 loc) · 5.26 KB
/
sql2couchdb.php
File metadata and controls
executable file
·216 lines (166 loc) · 5.26 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/php
<?php
/*
Copyright 2011 Sam Bisbee
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* sql2couchdb - Proof of Concept
* by Sam Bisbee <sam@sbisbee.com>
* on 2010-02-17
*
* This file is a hacked up proof of concept that will be turned into an actual
* program. It is only meant for development, has plenty of horrible code, and
* you should not try to use it. That being said, its basic functionality does
* work.
*/
require('sag-0.3.0/Sag.php');
function getColType($colName, $mysqlQuery)
{
for($i = 0; $i < mysql_num_fields($mysqlQuery); $i++)
{
$colMeta = mysql_fetch_field($mysqlQuery, $i);
if($colMeta->name == $colName)
return $colMeta->type;
}
return null;
}
function castSQLToPHP($mysqlType, $value)
{
//Leave textual and unknown types alone (defaults to string).
switch($mysqlType)
{
case 'int': $value = (int) $value; break;
case 'smallint': $value = (int) $value; break;
case 'mediumint': $value = (int) $value; break;
case 'bigint': $value = (int) $value; break;
case 'integer': $value = (int) $value; break;
case 'float': $value = (float) $value; break;
case 'double': $value = (double) $value; break;
case 'bool': $value = (bool) $value; break;
case 'boolean': $value = (bool) $value; break;
case 'tinyint': $value = (bool) $value; break;
}
return $value;
}
function map($row, $json, $mysqlQuery)
{
if($json)
{
//turn objects into associative arrays
if(is_object($json))
{
$wasObject = true;
$json = (array) $json;
}
if(is_array($json))
{
foreach($json as $k => $v)
{
$json[$k] = map($row, $v, $mysqlQuery);
if($k == '_id')
$json[$k] = (string) $json[$k];
}
}
elseif(is_string($json))
if($row[$json])
return castSQLToPHP(getColType($json, $mysqlQuery), $row[$json]);
if($wasObject)
$json = (object) $json;
}
return $json;
}
// Command line args
$options = getopt(null,
array(
"mysql-user:",
"mysql-pass:",
"mysql-db:",
"mysql-host:",
"mysql-port:",
"couchdb-user:",
"couchdb-pass:",
"couchdb-db:",
"couchdb-host:",
"couchdb-port:"
)
);
// Default config values
$mysqlConfig = new StdClass();
$mysqlConfig->host = '127.0.0.1';
$mysqlConfig->port = '3306';
$couchdbConfig = new StdClass();
$couchdbConfig->host = '127.0.0.1';
$couchdbConfig->port = '5984';
foreach($options as $opt => $value)
{
switch($opt)
{
case 'mysql-user':
$mysqlConfig->user = $value;
break;
case 'mysql-pass':
$mysqlConfig->pass = $value;
break;
case 'mysql-db':
$mysqlConfig->db = $value;
break;
case 'mysql-host':
$mysqlConfig->host = $value;
break;
case 'mysql-port':
$mysqlConfig->port = $value;
break;
case 'couchdb-user':
$couchdbConfig->user = $value;
break;
case 'couchdb-pass':
$couchdbConfig->pass = $value;
break;
case 'couchdb-db':
$couchdbConfig->db = $value;
break;
case 'couchdb-host':
$couchdbConfig->host = $value;
break;
case 'couchdb-port':
$couchdbConfig->port = $value;
break;
}
}
// Parse and check the JSON file
$jsonFile = json_decode(file_get_contents(array_pop($argv)));
if(!$jsonFile || !is_object($jsonFile))
throw new Exception('Unexpected JSON format.');
if(!$jsonFile->query || !is_string($jsonFile->query))
throw new Exception('Missing the SQL statement.');
if(!$jsonFile->doc || !is_object($jsonFile->doc))
throw new Exception('Missing the doc.');
// Set up Sag now so that we don't run the whole MySQL loop and then end up
// with an error.
$sag = new Sag($couchdbConfig->host, $couchdbConfig->port);
$sag->setDatabase($couchdbConfig->db);
if($couchdbConfig->user || $couchdbConfig->pass)
$sag->login($couchdbConfig->user, $couchdbConfig->pass);
// Get data from MySQL and send it to CouchDB.
if(!($mysqlCon = mysql_connect("{$mysqlConfig->host}:{$mysqlConfig->port}", $mysqlConfig->user, $mysqlConfig->pass)))
throw new Exception('Unable to connect to MySQL: '.mysql_error());
if(!mysql_select_db($mysqlConfig->db, $mysqlCon))
throw new Exception('Unable to select db: '.mysql_error());
if(!($mysqlQuery = mysql_query($jsonFile->query, $mysqlCon)))
throw new Exception('Invalid query: '.mysql_error());
$docsToSend = array();
while($row = mysql_fetch_array($mysqlQuery, MYSQL_ASSOC))
$docsToSend[] = map($row, clone $jsonFile->doc, $mysqlQuery);
$sag->bulk($docsToSend);
mysql_free_result($mysqlQuery);
mysql_close($mysqlCon);
?>