示例#1
0
	def on_get(self,req,resp,dbname,table,action):
		if dbname not in self.databases:
			raise falcon.HTTPError(falcon.HTTP_404,'invalid_grant',"invalid databases name %s" % dbname)
		if '.' in table:
			raise falcon.HTTPError(falcon.HTTP_404,'invalid_grant',"invalid table name %s" % table)			
		kw 			= req.params
		kw['table']	= table
		kw['dbname']= dbname
		if action == 'getbyid':
			if 'ids' not in kw:
				raise falcon.HTTPBadRequest('illegal_argument','ids must provided')
			result = dbhandle.getbyid(kw)
		
		elif action == 'getcount':
			result = dbhandle.getcount(kw)
			
		elif action == 'getlist':
			result = dbhandle.getlist(kw)
		
		else:
			raise falcon.HTTPError(falcon.HTTP_404,'invalid_grant','invalid action %s' % action)
			
		if result[0] == -1:
			raise falcon.HTTPBadRequest('invalid_sql_syntax',str(result[1]))

		result={'result':result[1]}
		resp.body = JSONEncoder().encode(result)
示例#2
0
def search(kw):
	"""提交搜索任务获取搜索结果及数据结果
	"""
	index = kw.get('index')
	host_info = sconf.get_search_info(index)
	kw['index'] = host_info['index']
	sp = sphinx(host_info['info']['host'],host_info['info']['port'])

	sp.initQuery(kw)
	rs = sp.RunQueries()
	result = {}
	if rs: 
		if  rs[0]['status']==0:
			result['total_found']	= rs[0]['total_found']
			result['total']			= rs[0]['total']
			result['time']			= rs[0]['time']
			
			ids			= []
			for row in rs[0]['matches']:
				if 'id' in row:
					ids.append(str(row['id']))
			if ids :
				dbinf			= {}
				dbinf['dbname'] = host_info['dbname']
				dbinf['table']	= host_info['table']
				dbinf['ids']	= ",".join(ids)
				dbinf['fields']	= kw.get('fields','*')
				if type(dbinf['fields']) in (tuple,list):
					if 'id' not in dbinf['fields']:
						dbinf['fields'].append('id')
					dbinf['fields'] = ','.join(dbinf['fields'])

				res,desc = dbhandle.getbyid(dbinf)
				if res == 0 and desc:
					rows = {str(row['id']):row for row in desc}
					result['data'] = [ rows[k] for k in ids if k in rows]
		else:
			result['warning'] = rs[0]['warning']
			result['error']	= rs[0]['error']
	else:
		return -1,sp._error
	return 0,result