-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo2sql.py
2251 lines (1801 loc) · 78.1 KB
/
mongo2sql.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
##TODO:db.coll.mapReduce(), db.coll.findAndXXX(),db.coll.group()完善
import os
import threading
import MySQLdb
L_SWITCH = 1
S_SWITCH = 1
configure = {"hostname": "localhost", "username": "root", "password": "lai", "database": "test" }
def connect(hostname, username, password, database, port=3306):
conn = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database, port=port)
cur = conn.cursor()
return cur
cursor = connect(**configure)
###Base class################
class ExtractSql:
def __init__(self, obj):
self.obj = obj
def __iter__(self): # for Find
sql = self.to_sql()
print sql
count = cursor.execute(sql)
print "there are %d rows record" % count
result = cursor.fetchall()
return iter(result)
def execute(self): # insert, update, remove etc
sql = self.to_sql()
print sql
count = cursor.execute(sql)
return count
DATABASE_METHODS = ["createDatabase", "dropDatabase", 'cloneCollection', 'cloneDatabase', 'commandHelp', 'copyDatabase', 'createCollection', 'currentOp', 'eval', 'fsyncLock', 'fsyncUnlock', 'getCollection', 'getCollectionInfos', 'getCollectionNames', 'getLastError', 'getLastErrorObj', 'getLogComponents', 'getMongo', 'getName', 'getPrevError', 'getProfilingLevel', 'getProfilingStatus', 'getReplicationInfo', 'getSiblingDB', 'help', 'hostInfo', 'isMaster', 'killOp', 'listCommands', 'loadServerScripts', 'logout', 'printCollectionStatus', 'printReplicationInfo', 'printShardingStatus', 'printSlaveReplicationInfo', 'repairDatabase', 'resetError', 'runCommand', 'serverBuildInfo', 'serverCmdLineOpts', 'serverStatus', 'setLogLevel', 'setProfilingLevel', 'shutdownServer', 'stats', 'version', 'upgradeCheck', 'upgradeCheckAllDBs']
###classes for Database and database's methods####
class Db(dict, ExtractSql):
"""Docstring for Db. """
def __init__(self, name):
self.name = name
def createDatabase(self):
return CreateDatabase(self)
def dropDatabase(self):
return DropDatabase(self)
def __getattr__(self, attr):
if attr not in DATABASE_METHODS:
return Table(self, attr)
def cloneDatabase(self, host_name):
return CloneDatabase(self, host_name)
def cloneCollection(self,from_host, from_coll, query=None):
"""
e.g:
db.cloneCollection('mongodb.example.net:27017', 'users.profiles', {'active': 'true'}) -->从服务器mongodb.example.net:27017的users数据库的profiles集合中复制条件满足{' active': 'true'}的文档到当前的数据库集合中。
"""
return CloneCollection(self, from_host, from_coll, query)
def copyDatabase(self, from_db, to_db, from_host=None, username=None, password=None, mechanisum=None):
return CopyDatabase(self, from_db, to_db, from_host, username, password, mechanisum)
def createCollection(self, name, options=None):
raise ValueError('createCollection unsupported')
#return CreateCollection(self, name, options)
def currentOp(self, operatoins=None):
return CurrentOp(self, operations)
def eval(self, func, arguments):
raise ValueError('eval unsupported')
#return Eval(self, func, arguments)
def fsyncLock(self):
raise ValueError('fsyncLock unsupported')
def fsyncUnlock(self):
raise ValueError('fsyncUnlock unsupported')
def getCollection(self, name):
return GetCollection(self, name)
def getCollectionInfos(self):
return GetCollectionInfos(self)
def getCollectionNames(self):
return GetCollectionNames(self)
def getLastError(self, w_c=None, w_timeout=None):
raise ValueError('getLastError unsupported')
#return GetLastError(self, w_c, w_timeout)
def getLastErrorObj(self, key=None, w_timeout=None):
raise ValueError('getLastErrorObj unsupported')
#return GetLastErrorObj(self, key, w_timeout)
def getLogComponents(self):
return GetLogComponents(self)
def getMongo(self):
return GetMongo(self)
def getName(self):
return GetName(self)
def getPrevError(self):
return GetPrevError(self)
def getProfilingLevel(self):
return GetProfilingLevel(self)
def getProfilingStatus(self):
return GetProfilingStatus(self)
def getReplicationInfo(self):
raise ValueError('getReplicationInfo unsupported')
#return GetReplicationInfo(self)
def getSiblingDB(self, database):
return GetSiblingDB(self, database)
def help(self):
return Help(self)
def hostInfo(self):
return HostInfo(self)
def isMaster(self):
raise ValueError('isMaster unsupported')
#return IsMaster(self)
def killOp(self, opid):
return KillOp(self, opid)
def listCommands(self):
return ListCommands(self)
def loadServerScripts(self):
raise ValueError('loadServerScripts unsupported')
#return LoadServerScripts(self)
def logout(self):
return Logout(self)
def printCollectionStatus(self):
return PrintCollectionStatus(self)
def printReplicationInfo(self):
raise ValueError('printReplicationInfo unsupported')
#return PrintReplicationInfo(self)
def printShardingStatus(self, verbose=False):
raise ValueError('printShardingStatus unsupported')
#return PrintShardingStatus(self, verbose)
def printSlaveReplicationInfo(self):
raise ValueError('printSlaveReplicationInfo unsupported')
#return PrintSlaveReplicationStatus(self)
def repairDatabase(self):
return RepairDatabase(self)
def resetError(self):
return ResetError(self)
def runCommand(self, command):
return RunCommand(self, command)
def serverBuildInfo(self):
return ServerBuildInfo(self)
def serverCmdLineOpts(self):
raise ValueError('serverCmdBuildInfo unsupported')
# return GetCmdLineOpts(self)
def serverStatus(self):
return ServerStatus(self)
def setProfilingLevel(self, level=None, slowms=None):
return SetProfilingLevel(self, level, slowms)
def shutdownServer(self):
return ShutdownServer(self)
def setLogLevel(self, level=None, component=None):
raise ValueError('setLogLevel unsupported')
#return SetLogLevel(self, level, component)
def stats(self, scale=None):
return Stats(self, scale)
def version(self):
return Version(self)
def upgradeCheck(self, scope=None):
raise ValueError('upgradeCheck unsupported')
#return UpgradeCheck(self, scope)
def upgradeCheckAllDBs(self):
raise ValueError('upgradeCheckAllDBs unsupported')
# return UpgradeCheckAllDBs(self)
class CloneDatabase(object, ExtractSql):
def __init__(self, db, host_name):
self.db = db
self.host_name = host_name
def to_sql(self):
r_user_name = raw_input('remote_mysql_username: \n')
r_password = raw_input('remote_mysql_password: \n')
l_user_name = raw_input('localhost_user_name: \n')
l_password = raw_input('localhost_password: \n')
if r_user_name and r_password and l_user_name and l_password:
r_mysql_dump = 'mysqldump -h %s -u %s -p%s %s > %s.sql' % (self.host_name, r_user_name, r_password, self.db.name, self.db.name)
l_mysql_import = 'mysql -h localhost -u %s -p%s < %s.sql' % (l_user_name, l_password, self.db.name)
os.system('&&'.join([r_mysql_dump, l_mysql_import]))
return 'OK'
else:
ValueError('To CloneDatabase, input infos')
class UpgradeCheck(object, ExtractSql):
pass
class UpgradeCheckAllDBs(object, ExtractSql):
pass
class Version(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SELECT VERSION()'
class Stats(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
table_name = 'information_schema.PROCESSLIST'
where_fmt = 'WHERE' + 'DB=%s' % self.db.name
return 'SELECT * FROM %s %s' % (table_name, where_fmt)
class ShutdownServer(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return os.system('service mysql stop')
class SetProfilingLevel(object, ExtractSql):
def __init__(self, db, level, slowms=None):
self.db = db
self.level = level
self.slowms = None
def to_sql(self):
return 'SET PROFILING = 1'
class ServerStatus(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SHOW STATUS'
class ServerBuildInfo(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return "SELECT VERSION()"
class ServerCmdLineOpts(object, ExtractSql):
pass
class SetLogLevel(object, ExtractSql):
pass
class CreateDatabase(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'CREATE DATABASE %s' % self.db.name
class DropDatabase(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'DROP DATABASE %s' % self.db.name
class CloneCollection(object, ExtractSql):
def __init__(self, db, from_host, from_coll, query=None):
self.db = db
self.from_host = from_host
self.from_coll = from_coll
self.query = query
def to_sql(self):
if '.' in self.from_coll:
old_table_name = self.from_coll.split('.')[-1]
else:
old_table_name = self.from_coll
new_table_name = self.db.old_table_name.name
if self.query:
where_fmt = 'WHERE ' + handle_condition(self.query)
else:
where_fmt = ''
return 'SELECT * INTO %s FROM %s %s' % (new_table_name, old_table_name, where_fmt)
class CopyDatabase(object, ExtractSql):
def __init__(self, db, from_db, to_db, from_host=None, username=None, password=None, mechanisum=None):
self.db = db
self.from_db = from_db
self.to_db = to_db
self.from_host = from_host
self.username= username
self.password = password
self.mechanisum = mechanisum
def to_sql(self):
if self.from_host is not None and self.username is not None and self.password is not None:
dump_sql = 'mysqldump -h %s -u %s -p%s %s > %s' % (self.from_host, self.username, self.password, self.from_db, '%s.sql'.format(self.from_db))
l_host = raw_input('to_mysql_hostname:')
l_user = raw_input('to_mysql_user:')
l_password = raw_input('to_mysql_password:')
import_sql = 'mysql -h %s -u %s -p%s < %s.sql' % (l_host, l_user, l_password, self.from_db)
os.system('&&'.join([dump_sql, import_sql]))
return 'OK'
else:
raise ValueError('To CopyDatabase, input host, username, password')
class CreateCollection(object, ExtractSql):
def __init__(self, db, table_name, options=None):
self.db = db
self.table_name = table_name
self.options = options
def handle_fields(self):
fields_fmt_list = []
if isinstance(self.options, dict) and self.options != {}:
for key, val in self.options.items():
pass
else:
raise ValueError('The second parameter must be a dict and not {}')
if len(fields_fmt_list) == 1:
fields_fmt = fields_fmt_list[0]
elif len(fields_fmt_list) == 0:
return ''
else:
fields_fmt = ','.join(fields_fmt_list)
return "(%s)" % fields_fmt
def to_sql(self):
sql_list = []
sql_list.append('USE %s' % self.db.name)
fileds_fmt = self.handle_fields(self.options)
sql_list.append('CREATE TABLE %s %s' % (self.table_name, fileds_fmt)) #字段和字段类型的来源
sql = ';'.join(sql_list)
return sql
class CurrentOp(object, ExtractSql):
def __init__(self, db, operations=None):
self.db = db
self.operations = operations
def to_sql(self):
talbe_name = 'information_schema.PROCESSLIST'
if operations:
if operations == True:
where_fmt = 'WHERE ' + 'DB=%s' % self.db.name
return 'SELECT * FROM %s %s ' % (table_name, where_fmt)
#elif isinstance(operations, dict):
# where_fmt = 'WHERE'
# return 'SELECT * FROM %s %s' % (table_name, where_fmt)
else:
where_fmt = 'WHERE ' + 'DB=%s' % self.db.name
return 'SELECT * FROM %s %s' % (table_name, where_fmt)
class Eval(object, ExtractSql):
def __init__(self, db, func, arguments):
self.db = db
self.func = func
self.arguments = arguments
def to_sql(self):
raise ValueError('Eval unsupported')
class fsynLock(object, ExtractSql):
pass
class fsynUnlock(object, ExtractSql):
pass
class GetCollection(object, ExtractSql):
def __init__(self, db, name):
self.db = db
self.name = name
def to_sql(self):
table_name = 'information_schema.TABLES'
where_fmt = 'WHERE ' + 'TABLE_SCHEMA=%s AND TABLE_NAME=%s' % (self.db.name, self.name)
return 'SELECT TABLE_NAME FROM %s %s' % (table_name, where_fmt)
class GetCollectionInfos(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
table_name = 'information_schema.TABLES'
where_fmt = 'WHERE ' + 'TABLE_SCHEMA=%s' % self.db.name
return 'SELECT * FROM %s %s' % (table_name, where_fmt)
class GetCollectionNames(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
table_name = 'information_schema.TABLES'
where_fmt = 'WHERE ' + 'TABLE_SCHEMA=%s' % self.db.name
return 'SELECT TABLE_NAME FROM %s %s' % (table_name, where_fmt)
class GetLastError(object, ExtractSql):
def __init__(self, db, w, wtimeout):
self.db = db
self.w = w
self.wtimeout = wtimeout
def to_sql(self):
raise ValueError('GetLastError can\'t To_sql()')
class GetLastErrorObj(object, ExtractSql):
pass
class GetLogComponents(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return '%s; %s' % ('USE %s' % self.db.name, 'SHOW status')
class GetMongo(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SHOW PROCESSLIST'
class GetName(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SELECT database()'
class GetPrevError(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SHOW ERRORS LIMIT 1'
class GetProfilingLevel(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SHOW PROFILES'
class GetProfilingStatus(object, ExtractSql):
def __init__(self, db):
self.db = db
self.get_profiling_level = GetProfilingLevel(self.db)
def to_sql(self):
return self.get_profiling_level.to_sql()
class GetReplicationInfo(object, ExtractSql):
pass
class GetSiblingDB(object, ExtractSql):
def __init__(self, db, database):
self.db = db
self.database = database
def to_sql(self):
return 'USE %s' % self.database
class Help(object, ExtractSql):
def __init__(self, obj):
self.obj = obj
def to_sql(self):
return 'help'
class HostInfo(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
table_name = 'information_schema.STATISTICS, information_schema.PROCESSLIST'
where_fmt = 'information_schema.STATISTICS.TABLE_SCHEMA=%s AND information.PROCESSLIST.DB=%s' % (self.db.name, self.db.name)
return 'SELECT * FORM %s %s' % (table_name, where_fmt)
class IsMaster(object, ExtractSql):
pass
class KillOp(object, ExtractSql):
def __init__(self, db, opid):
self.db = db
self.opid = opid
def to_sql(self):
return 'SHOW PROCESSLIST; KILL %s' % self.opid
class ListCommands(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return Help(self.db).to_sql()
class ListDatabases(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return "SHOW DATABASES"
class LoadServerScripts(object, ExtractSql):
pass
class Logout(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'DROP DATABASE %s' % (self.db.name)
class PrintCollectionStatus(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
table_name = 'information_schema.STATISTICS'
where_fmt = 'TABLE_SCHEMA=%s' % self.db.name
return 'SELECT * FORM %s %s' % (table_name, where_fmt)
class PrintReplicationInfo(object, ExtractSql):
pass
class PrintShardingStatus(object, ExtractSql):
pass
class PrintSlaveReplicationStatus(object, ExtractSql):
pass
class RepairDatabase(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'USE %s' % self.db.name + ';' + 'REPAIR TABLE *'
class ResetError(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return GetPrevError(self.db).to_sql()
class Ping(object, ExtractSql):
def __init__(self, db):
self.db = db
def to_sql(self):
return 'SHOW PROCESSLIST'
class RunCommand(object, ExtractSql):
def __init__(self, db, command):
self.db = db
self.command = command
def to_sql(self):
cmd_doc = self.command
if isinstance(cmd_doc, str):
pass
elif isinstance(cmd_doc, dict):
if 'drop' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('drop'))
return Drop(table).to_sql()
elif 'buildInfo' in cmd_doc.keys():
if cmd_doc.get('buildInfo') == 1:
return ServerBuildInfo(self.db).to_sql()
elif 'collStats' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('collStats'))
return Stats(table).to_sql()
elif 'distinct' if cmd_doc.keys():
table = Table(self.db, cmd_doc.get('distinct'))
key_val = cmd_doc.get('key')
query_val = cmd_doc.get('query')
return Distinct(table, key_val, query_val).to_sql()
elif 'dropDatabase' in cmd_doc.keys():
if cmd_doc.get('dropDatabase') == 1:
return DropDatabase(self.db).to_sql()
elif 'dropIndexes' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('dropIndexes'))
index_name = cmd_doc.get('index')
if index_name == '*':
return DropIndexes(table).to_sql()
else:
return DropIndex(table, index_name).to_sql()
elif 'findAndModify' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('findAndModify'))
doc = cmd_doc.copy()
del doc['findAndModify']
return FindAndModify(table, doc).to_sql()
elif 'getLastError' in cmd_doc.keys():
if cmd_doc.get('getLastError') == 1:
doc = cmd_doc.copy()
del doc['getLastError']
options = doc
return GetLastError(self.db, options).to_sql()
elif 'isMaster' in cmd_doc.keys():
if cmd_doc.get('isMaster') == 1:
return IsMaster(self.db).to_sql()
elif 'listCommands' in cmd_doc.keys():
if cmd_doc.get('listCommand') == 1:
return ListCommands(self.db).to_sql()
elif 'listDatabases' in cmd_doc.keys():
if cmd_doc.get('listDatabases') == 1:
return ListDatabases(self.db).to_sql()
elif 'ping' in cmd_doc.keys():
if cmd_doc.get('ping') == 1:
return Ping(self.db).to_sql()
elif 'renameCollection' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('renameCollection'))
new_name = cmd_doc.get('target')
return RenameCollection(table, new_name)
elif 'repairDatabase' in cmd_doc.keys():
if cmd_doc.get('repairDatabase') == 1:
return RepairDatabase(self.db).to_sql()
elif 'serverStatus' in cmd_doc.keys():
if cmd_doc.get('serverStatus') == 1:
return ServerStatus(self.db).to_sql()
elif 'cloneCollection' in cmd_doc.keys():
if cmd_doc.get('cloneCollection') == 1:
frm = cmd_doc.get('from')
coll_name = cmd_doc.get('collection')
query = cmd_doc.get('query', None)
return CloneCollection(self.db, frm, coll_name, query).to_sql()
elif 'cloneDatabase' in cmd_doc.keys():
if cmd_doc.get('cloneDatabase') == 1:
host_name = cmd_doc.get('hostname')
return CloneDatabase(self.db, host_name).to_sql()
elif 'commandHelp' in cmd_doc.keys():
if cmd_doc.get('commandHelp') == 1:
cmd = cmd_doc.get('command')
return CommandHelp(self.db, cmd).to_sql()
elif 'copyDatabase' in cmd_doc.keys():
if cmd_doc.get('copyDatabase') == 1:
frm_db = cmd_doc.get('fromdb')
to_db = cmd_doc.get('todb')
frm_host = cmd_doc.get('fromhost', None)
user_name = cmd_doc.get("username", None)
password = cmd_doc.get('password', None)
mechanism = cmd_doc.get('mechanism', None)
return CopyDatabase(self.db, frm_db, to_db, user_name, password, mechanism).to_sql()
elif 'createCollection' in cmd_doc.keys():
if cmd_doc.get('createCollection') == 1:
coll_name = cmd_doc.get('name')
del cmd_doc['createCollection']
options = cmd_doc
return CreateCollection(self.db, name, options).to_sql()
elif 'currentOp' in cmd_doc.keys():
if cmd_doc.get('currentOp') == 1:
operations = cmd_doc.get('operations')
return CurrentOp(self.db, operations).to_sql()
elif 'dropDatabase' in cmd_doc.keys():
if cmd_doc.get('dropDatabase') == 1:
return DropDatabase(self.db).to_sql()
elif 'eval' in cmd_doc.keys():
if cmd_doc.get('eval') == 1:
func = cmd_doc.get('function')
args = cmd_doc.get('arguments', None)
return Eval(self.db, func, args).to_sql()
elif 'fsyncLock' in cmd_doc.keys():
if cmd_doc.get('fsyncLock') == 1:
return FsyncLock(self.db).to_sql()
elif 'fsyncUnlock' in cmd_doc.keys():
if cmd_doc.get('fsyncUnlock') == 1:
return FsyncUnlock(self.db).to_sql()
elif 'getCollection' in cmd_doc.keys():
if cmd_doc.get('getCollection') == 1:
coll_name = cmd_doc.get('name')
return GetCollection(self.db, coll_name).to_sql()
elif 'getCollectionInfos' in cmd_doc.keys():
if cmd_doc.get('getCollectionInfos') == 1:
return GetCollectionInfos(self.db).to_sql()
elif 'getCollectionNames' in cmd_doc.keys():
if cmd_doc.get('getCollectionNames') == 1:
return GetCollectionNames(self.db).to_sql()
elif 'getLastErrorObj' in cmd_doc.keys():
if cmd_doc.get('getLastErrorObj') == 1:
return GetLastErrorObj(self.db).to_sql()
elif 'getLogComponents' in cmd_doc.keys():
if cmd_doc.get('getLogComponents') == 1:
return GetLogComponents(self.db).to_sql()
elif 'getMongo' in cmd_doc.keys():
if cmd_doc.get('getMongo') == 1:
return GetMongo(self.db).to_sql()
elif 'getName' in cmd_doc.keys():
if cmd_doc.get('getName') == 1:
return GetName(self.db).to_sql()
elif 'getPrevError' in cmd_doc.keys():
if cmd_doc.get('getPrevError') == 1:
return GetPrevError(self.db).to_sql()
elif 'getProfilingLevel' in cmd_doc.keys():
if cmd_doc.get('getProfilingLevel') == 1:
return GetProfilingLevel(self.db).to_sql()
elif 'getProfilingStatus' in cmd_doc.keys():
if cmd_doc.get('getProfilingStatus') == 1:
return GetProfilingStatus(self.db).to_sql()
elif 'getReplicationInfo' in cmd_doc.keys():
if cmd_doc.get('getReplicationInfo') == 1:
return GetReplicationInfo(self.db).to_sql()
elif 'getSiblingDB' in cmd_doc.keys():
if cmd_doc.get('getSiblingDB') == 1:
db_name = cmd_doc.get('database')
return GetSiblingDB(self.db, db_name).to_sql()
elif 'help' in cmd_doc.keys():
if cmd_doc.get('help') == 1:
return Help(self.db).to_sql()
elif 'hostInfo' in cmd_doc.keys():
if cmd_doc.get('hostInfo') == 1:
return HostInfo(self.db).to_sql()
elif 'killOp' in cmd_doc.keys():
if cmd_doc.get('killOp') == 1:
op_id = cmd_doc.get('opid')
return KillOp(self.db, op_id).to_sql()
elif 'loadServerScripts' in cmd_doc.keys():
if cmd_doc.get('loadServerScripts') == 1:
return LoadServerScripts(self.db).to_sql()
elif 'logout' in cmd_doc.keys():
if cmd_doc.get('logout') == 1:
return Logout(self.db).to_sql()
elif 'printCollectionStats' in cmd_doc.keys():
if cmd_doc.get('printCollectionStats') == 1:
return PringCollectionStats(self.db).to_sql()
elif 'printReplicationInfo' in cmd_doc.keys():
if cmd_doc.get('printReplicationInfo') == 1:
return PrintReplicationInfo(self.db).to_sql()
elif 'printShardingStatus' in cmd_doc.keys():
if cmd_doc.get('printShardingStatus') == 1:
verbose = cmd_doc.get('verbose', False)
return PrintShardingStatus(self.db, verbose).to_sql()
elif 'printSlaveReplicationInfo' in cmd_doc.keys():
if cmd_doc.get('printSlaveReplicationInfo') == 1:
return PrintSlaveReplicationInfo(self.db).to_sql()
elif 'resetError' in cmd_doc.keys():
if cmd_doc.get('resetError') == 1:
return ResetError(self.db).to_sql()
elif 'serverBuildInfo' in cmd_doc.keys():
if cmd_doc.get('serverBuildInfo') == 1:
return ServerBuildInfo(self.db).to_sql()
elif 'serverCmdLineOpts' in cmd_doc.keys():
if cmd_doc.get('serverCmdLineOpts') == 1:
return ServerCmdLineOpts(self.db).to_sql()
elif 'setLogLevel' in cmd_doc.keys():
if cmd_doc.get('setLogLevel') == 1:
level = cmd_doc.get('level')
component = cmd_doc.get('component', None)
return SetLogLevel(self.db, level, component).to_sql()
elif 'setProfilingLevel' in cmd_doc.keys():
if cmd_doc.get('setProfilingLevel') == 1:
lev = cmd_doc.get('level')
slowms = cmd_doc.get('slowms', None)
return SetProfilingLevel(self.db, lev, slowms).to_sql()
elif 'shutdownServer' in cmd_doc.keys():
if cmd_doc.get('shutdownServer') == 1:
return ShutdwonServer(self.db).to_sql()
elif 'stats' in cmd_doc.keys():
if cmd_doc.get('stats') == 1:
scale = cmd_doc.get('scale')
return Stats(self.db, scale).to_sql()
elif 'version' in cmd_doc.keys():
if cmd_doc.get('version') == 1:
return Version(self.db).to_sql()
elif 'upgradeCheck' in cmd_doc.keys():
if cmd_doc.get('upgradeCheck') == 1:
scope = cmd_doc.get('scope', None)
return UpgradeCheck(self.db, scope).to_sql()
elif 'upgradeCheckAllDB' in cmd_doc.keys():
if cmd_doc.get('upgradeCheckAllDB') == 1:
return UpgradeCheckAllDB(self.db).to_sql()
## collection methods ##
elif 'aggregate' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('aggregate'))
pipeline = cmd_doc.get('pipeline')
ag_opts = cmd_doc.get('options')
return Aggregate(table, pipeline, ag_opts).to_sql()
elif 'bulkWrite' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('bulkWrite'))
bw_ops = cmd_doc.get('operations')
bw_wc = cmd_doc.get('writeConcern', None)
return BulkWrite(table, bw_ops, bw_wc).to_sql()
elif 'count' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('count'))
cot_query = cmd_doc.get('query')
return Count(table, cot_query).to_sql()
elif 'copyTo' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('copyTo'))
new_coll = cmd_doc.get('newCollection')
return CopyTo(table, new_coll).to_sql()
elif 'createIndex' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('createIndex'))
ci_keys = cmd_doc.get('keys')
ci_opts = cmd_doc.get('options', None)
return CreateIndex(table, ci_keys, ci_opts).to_sql()
elif 'dataSize' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('dataSize'))
return DataSize(table).to_sql()
elif 'deleteOne' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('deleteOne'))
query = cmd_doc.get('filter')
w_c = cmd_doc.get('writeConcern', None)
return DeleteOne(table, query, w_c).to_sql()
elif 'deleteMany' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('deleteMany'))
w_c = cmd_doc.get('writeConcern', None)
return DeleteMany(table, w_c).to_sql()
elif 'dropIndex' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('dropIndex'))
index_name = cmd_doc.get('index')
return DropIndex(table, index_name).to_sql()
elif 'dropIndexes' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('dropIndexes'))
return DropIndexes(table).to_sql()
elif 'ensureIndex' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('ensureIndex'))
keys = cmd_doc.get('keys')
options = cmd_doc.get('options', None)
return EnsureIndex(table, keys, options).to_sql()
elif 'explain' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('explain'))
verbosity = cmd_doc.get('verbosity', None)
return Explain(table, verbosity).to_sql()
elif 'find' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('find'))
query = cmd_doc.get('query',None)
projection = cmd_doc.get('projection', None)
return Find(table, query, projection).to_sql()
elif 'findOne' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('findOne'))
query = cmd_doc.get('query', None)
projection = cmd_doc.get('projection', None)
return FindOne(table, query, projection).to_sql()
elif 'findOneAndDelete' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('findOneAndDelete'))
query = cmd_doc.get('filter', None)
options = cmd_doc.get('options', None)
return FindOneAndDelete(table, query, options).to_sql()
elif 'findOneAndReplace' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('findOneAndReplace'))
query = cmd_doc.get('filter', None)
replacement = cmd_doc.get('replacement')
options = cmd_doc.get('options', None)
return FindOneAndReplace(table, query, replacement, options).to_sql()
elif 'findOneAndUpdate' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('findOneAndUpdate'))
query = cmd_doc.get('filter', None)
update = cmd_doc.get('update')
options = cmd_doc.get('options', None)
return FindOneAndUpdate(table, query, update, options).to_sql()
elif 'getIndexes' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('getIndexes'))
return GetIndexes(table).to_sql()
elif 'getShardDistribution' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('getShardDistribution'))
return GetShardDistribution(table).to_sql()
elif 'getShardVersion' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('getShardVersion'))
return GetShardVersion(table).to_sql()
elif 'group' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('group'))
del cmd_doc['group']
doc = cmd_doc
return Group(table, doc).to_sql()
elif 'insert' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('insert'))
doc = cmd_doc.get('document')
w_c = cmd_doc.get('writeConcern', None)
ordered = cmd_doc.get('ordered', False)
return Insert(table, doc, w_c, ordered).to_sql()
elif 'insertOne' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('insertOne'))
doc = cmd_doc.get('document')
w_c = cmd_doc.get('writeConcern', None)
return InsertOne(table, doc, w_c).to_sql()
elif 'insertMany' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('insertMany'))
docs = cmd_doc.get('document')
del cmd_doc['insertMany']
del cmd_doc['document']
options = cmd_doc
#w_c = cmd_doc.get('writeConcern', None)
#ordered = cmd_doc.get('ordered', None)
return InsertMany(table, docs, options).to_sql()
elif 'isCapped' in cmd_doc.keys():
table = Table(self.db, cmd_doc.get('isCapped'))
return IsCapped(table).to_sql()
elif 'mapReduce' in cmd_doc.get('mapReduce'):
table = Table(self.db, cmd_doc.get('mapReduce'))
map_func = cmd_doc.get('map')
reduce_func = cmd_doc.get('reduce')
del cmd_doc['mapReduce']
del cmd_doc['map']
del cmd_doc['reduce']
options = cmd_doc