awips2/edexOsgi/com.raytheon.edex.uengine/utility/edex_static/base/python/GempakSqlQuery.py
2018-06-20 17:39:08 -06:00

130 lines
No EOL
4 KiB
Python

##
# This software was developed and / or modified by Raytheon Company,
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
#
# U.S. EXPORT CONTROLLED TECHNICAL DATA
# This software product contains export-restricted data whose
# export/transfer/disclosure is restricted by U.S. law. Dissemination
# to non-U.S. persons whether in the United States or abroad requires
# an export license or other authorization.
#
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
#
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
##
##
# This is a base file that is not intended to be overridden.
##
##
# uengine is deprecated and will be removed from the system soon. Migrate your
# apps to using the Data Access Framework (DAF).
##
#
# GempakSqlQuery
#
# This code has been developed by the SIB for use in the AWIPS2 system.
# Performs a BaseRequest for a grid data from GEMPAK, and stores
# the float data in the uEngineProducts directory for later transfer.
#
# Usage:
# import GempakSqlQuery
# gsq = GempakSqlQuery.GempakSqlQuery()
# gsq.setQuery("...")
# [gsq.setReturnTest()]
# return gsq.execute()
#
# SOFTWARE HISTORY
#
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 06/02/10 173_partC mgamazaychikov Initial Creation.
# 09/09/10 mgamazaychikov Added setSeparator function
# 07/13/15 4500 rjpeter Remove SqlQueryTask
#
from com.raytheon.uf.common.message.response import ResponseMessageGeneric
from com.raytheon.uf.common.dataquery.db import QueryResult
from com.raytheon.uf.edex.database.dao import CoreDao
from com.raytheon.uf.edex.database.dao import DaoConfig
from java.util import ArrayList
class GempakSqlQuery():
#
# Initializes the query
#
def __init__ (self, dbName="metadata"):
self.dbname = dbName
self.queryResults = None
self.returnText = False
self.isTextSeparated = False
def setQuery (self, aQuery):
self.query = aQuery
def setReturnText (self):
self.returnText = True
def setSeparator(self, aSeparator):
self.separator = aSeparator
self.isTextSeparated = True
#
# Returns a string with null response
#
def makeNullResponse(self):
nullStr = "Database Query returned no results"
if self.returnText:
return nullStr
else:
response = ArrayList()
response.add(ResponseMessageGeneric(nullStr))
return response
#
# Returns a string with response
#
def makeResponse(self):
response = ArrayList()
returnString=""
queryRows = ArrayList()
queryRows = self.queryResults.getRows()
for qrow in queryRows:
rowStr = "%s" % qrow
if self.returnText:
returnString = returnString + rowStr[1:-1]
if self.isTextSeparated:
returnString = returnString + self.separator
else:
response.add(ResponseMessageGeneric(rowStr[1:-1]))
if self.returnText:
if self.isTextSeparated:
return returnString[0:-1]
else:
return returnString
else:
return response
#
# Executes the query and calls appropriate response functions
#
def execute(self):
#self.queryResults = ArrayList()
dao = CoreDao(DaoConfig.forDatabase(self.dbname))
self.queryResults = dao.executeMappedSQLQuery(self.query)
#
# Make response based on the query results
#
if self.queryResults is None:
return self.makeNullResponse()
else:
return self.makeResponse()