forked from flightaware/mongotcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.tcl
161 lines (129 loc) · 2.97 KB
/
mongo.tcl
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
#
# mongo support functions
#
#
#
package require mongo
namespace eval ::mongo {
#
# _search - search routine called by search method of tcl mongodb objects
#
# m search -namespace tutorial.persons -array row -code {parray row; puts ""}
#
proc _search {args} {
set obj [lindex $args 0]
set args [lrange $args 1 end]
if {[llength $args] & 1} {
error "list of key-value pairs must have an even number of arguments"
}
foreach "key value" $args {
if {[string index $key 0] != "-"} {
error "key '$key' of key-value pairs doesn't start with a -"
}
switch -exact -- $key {
"-fields" {
set fields $value
}
"-code" {
set code $value
}
"-array" {
set arrayName $value
upvar $arrayName array
}
"-typearray" {
set typeArrayName $value
upvar $typeArrayName typeArray
}
"-list" {
set listName $value
upvar $listName listVar
}
"-offset" {
set offset $value
}
"-limit" {
set limit $value
}
"-comparebson" {
set compareBson $value
}
"-sort" {
set sort $value
}
"-namespace" {
set namespace $value
}
default {
error "unknown key $key: must be one of -namespace, -fields, -code, -array, -typearray, -list, -offset, -limit"
}
}
}
if {![info exists namespace]} {
error "required field '-namespace' is missing"
}
set cursor [$obj cursor #auto $namespace]
if {[info exists fields]} {
set fieldList [list]
foreach field $fields {
if {[string index $field 0] == "-"} {
lappend fieldList [string range $field 1 end] 0
} else {
lappend fieldList $field 1
}
}
$cursor set_fields $fieldList
}
if {[info exists limit]} {
$cursor set_limit $limit
}
if {[info exists offset]} {
$cursor set_skip $offset
}
# generate the query
set queryBson [::mongo::bson create #auto]
if {[info exists compareBson]} {
$queryBson start_object {$query} bson $compareBson finish_object
}
# if there's a sort option, append it to the query
if {[info exists sort]} {
$queryBson start_object {$orderby}
foreach field $sort {
if {[string index $field 0] == "-"} {
$queryBson int [string range $field 1 end] 0
} else {
$queryBson int $field 1
}
}
$queryBson finish_object
}
$cursor set_query $queryBson
#
# iterate over the matching rows.
# you have to invoke "next" to get the first row, by the way
#
while {[$cursor next]} {
# pull the data out of the row, get types too if a type array
# is specified
if {[info exists arrayName]} {
if {![info exists typeArrayName]} {
unset -nocomplain array
$cursor to_array array
} else {
unset -nocomplain array typeArray
$cursor to_array array typeArray
}
}
# if they specified -list, give them their list of type triplets
if {[info exists listName]} {
set listVar [$cursor to_list]
}
# if they specified a code block, execute it
if {[info exists code]} {
uplevel $code
}
}
$cursor destroy
}
} ;# namespace ::mongo
# vim: set ts=4 sw=4 sts=4 noet :