-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmongosql.php
executable file
·160 lines (154 loc) · 4.34 KB
/
mongosql.php
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
<?php
/**
* @version $Id: mongosql.php 20806 2011-02-21 19:44:59Z drahmel $
* @package mongosql
* @copyright Copyright (C) 2011 Dan Rahmel All rights reserved.
* @license Apache license
*/
defined('SYSPATH') or die('No direct script access.');
class Controller_Mongosql extends Controller {
public function action_index()
{
define('NL','<br/>');
$sql = "SELECT * from mytable where id = 10 ORDER BY id DESC";
$sql = "SELECT a,b from mytable where id = 10 ORDER BY id DESC";
$mongoSelect = self::convertSelect($sql);
$sqlInsert = "INSERT INTO mytable (id,a,b) VALUES (10,".rand(1,5).",".rand(10,100).")";
$mongoInsert = self::convertInsert($sqlInsert);
$this->response->body($mongoSelect.'<hr/>'.$mongoInsert);
}
public static function processKeywords($sql,&$keywords)
{
$lastpos = 0;
$regex = '';
foreach($keywords as $keyword => &$info) {
$c = strpos($sql,$keyword,$lastpos);
if($c!==false) {
//echo $keyword.'='.$c.NL;
$endpos = $c + strlen($keyword);
$lastpos = $endpos;
$info['pos'] = $endpos;
$kw = trim($keyword);
$kw = str_replace('(','\(',$kw);
$regex .= "{$kw}(.*)";
} else {
unset($keywords[$keyword]);
}
}
$regex = '/'.$regex.'/';
$matches = array();
preg_match_all($regex,$sql,$matches);
$pointer = 1;
foreach($keywords as $keyword => &$info) {
if(isset($info['pos'])) {
$info['val'] = $matches[$pointer][0];
$pointer++;
}
}
}
public static function convertInsert($sql)
{
$sql = strtolower($sql);
$sql = str_replace('insert into','insert',$sql);
$keywords = array('insert'=>array(),
'('=>array(),
'values'=>array()
);
self::processKeywords($sql,$keywords);
echo '<pre>'.print_r($keywords,true).'</pre>';
$collection = trim($keywords['insert']['val']);
$colStr = trim(str_replace(array('(',')'),'',$keywords['(']['val']));
$cols = explode(',',$colStr);
$valueStr = trim(str_replace(array('(',')'),'',$keywords['values']['val']));
$values = explode(',',$valueStr);
$assoc = array();
foreach($cols as $key => $col) {
$assoc[] = "$col:{$values[$key]}";
}
$assocStr = implode(',',$assoc);
$mongo = "db.$collection.insert({ $assocStr })";
return $mongo;
}
public static function convertSelect($sql)
{
$sql = strtolower($sql);
$outStr = '';
$keywords = array('select'=>array(),
'from'=>array(),
'where'=>array(),
'order by'=>array()
);
self::processKeywords($sql,$keywords);
$regex = '';
foreach($keywords as $keyword => &$info) {
if(isset($info['pos'])) {
$kw = trim($keyword);
$regex .= "{$kw}(.*)";
}
}
$regex = '/'.$regex.'/';
$matches = array();
preg_match_all($regex,$sql,$matches);
$pointer = 1;
foreach($keywords as $keyword => &$info) {
if(isset($info['pos'])) {
$info['val'] = $matches[$pointer][0];
$pointer++;
}
}
$outStr .= $sql.NL;
//echo '<pre>'.print_r($keywords,true).'</pre>';
$findStr = '';
$colStr = '';
$collection = trim($keywords['from']['val']);
$cols = trim($keywords['select']['val']);
if($cols=='*') {
$colStr = '';
} else {
$colList = explode(',',$cols);
foreach($colList as $key => &$col) {
$col = trim($col);
if(!empty($col)) {
$col = "{$col}:1";
} else {
unset($colList[$key]);
}
}
$colStr = '{' . implode(',',$colList) . '}';
}
if(isset($keywords['where']['val']) && !empty($keywords['where']['val'])) {
$where = trim($keywords['where']['val']);
if(strpos($where,'=')!==false) {
$equalParts = explode('=',$where);
if(is_numeric($equalParts[0])) {
$var = trim($equalParts[1]);
$val = trim($equalParts[0]);
} else {
$var = trim($equalParts[0]);
$val = trim($equalParts[1]);
}
$findStr = "$var:$val";
}
}
if(!empty($findStr) && empty($colStr)) {
$mongo = "db.{$collection}.find( { $findStr } )";
} elseif(!empty($colStr)) {
$mongo = "db.{$collection}.find({ $findStr },$colStr)";
} else {
$mongo = "db.{$collection}.find()";
}
if(isset($keywords['order by']['val']) && !empty($keywords['order by']['val'])) {
$orderParts = explode(' ',trim($keywords['order by']['val']));
$orderCol = $orderParts[0];
//print_r($orderParts);
if(isset($orderParts[1]) && strpos($orderParts[1],'desc')!==false) {
$orderType = -1;
} else {
$orderType = 1;
}
$mongo .= ".sort({ $orderCol : $rderType })";
}
return $mongo;
}
}
?>