-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
199 lines (83 loc) · 2.72 KB
/
command.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
ss="""
$dns=fltorder.db.ctripcorp.com,55944
$db='fltorderdb'
$sql=
select name
from $db.sys.tables(nolock)
$return=tablesobject
$a=
select * from sys.databases
$b=
select * from cf_services
"""
print "-"*30
import re
import os
def parse_param_config(strconfig):
sep=os.linesep
vardict={}
l=strconfig.splitlines()
multi_lines_cmd=[]
variable_name=''
length=len(l)
for i,line in enumerate(l):
line=line.lstrip()
if line.startswith("$"):
if variable_name:
vardict[variable_name]=(sep.join(multi_lines_cmd)).lstrip()
del multi_lines_cmd[:]
variable_name=''
try:
variable_name,value=line.split("=")
multi_lines_cmd.append(str(value))
except ValueError, e:
raise ValueError("line %d,%s" %(i,line,))
else:
multi_lines_cmd.append(line)
if variable_name:
vardict[variable_name]=(sep.join(multi_lines_cmd)).lstrip()
del multi_lines_cmd[:]
variable_name=''
return vardict
print parse_param_config(ss)
class ParamError(Exception):
def __init__(self,msg):
super(ParamError,self).__init__(msg)
class SqlCommand(Command):
options_fix=["$sql","$dns","$port","$return"]
def __init__(self,str_config):
self.config_option=parse_param_config(str_config)
self._check_config(self.config_option)
def _check_config(self):
cf=self.config
for option in options_fix:
try:
_c=cf[option]
except KeyError:
raise ParamError("need this parameter %s" % (option,))
def substitute_sql(self,frame):
t=SqlTemplate(self._sql)
return t.substutite(frame.localVariables)
def run(self,frame):
sql=self.substitute_sql(frame)
if '$' not in sql:
conn=None
try:
conn=mssql.get_connect(self._host,self._db)
if self._hasresult:
retset=conn.execute_query_dict(substitute_sql(self._sql))
frame.set_local_variable(_result_name,transfer_to_briefobject(retset))
else:
conn.execute_update(sql)
except Exception, e:
pass
finally:
del conn
else:
raise ValueError("sql中有部分变量未替换")
def __str__(self):
return self.substitute_sql(frame)
def __repr__(self):
self.__str__
def serialize_db(self):
pass