Skip to content

Commit

Permalink
Generalized search() method to search all columns, not just id column.
Browse files Browse the repository at this point in the history
  • Loading branch information
hover2pi committed Feb 24, 2016
1 parent 9dc1010 commit d976abf
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions astrodbkit/astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def query(self, SQL, params='', fmt='array', fetch='all', unpack=False, export='
except:
print 'Could not execute: '+SQL

def search(self, criterion, table, columns=[], fetch=False):
def search(self, criterion, table, columns='', fetch=False):
"""
General search method for tables. For (ra,dec) input in decimal degrees,
i.e. '(12.3456,-65.4321)', returns all sources within 1 arcminute.
Expand Down Expand Up @@ -648,7 +648,7 @@ def search(self, criterion, table, columns=[], fetch=False):
except:
print "Could not search SOURCES table by coordinates {}. Try again.".format(criterion)

# Text string search of all columns with 'TEXT' data type
# Text string search of columns with 'TEXT' data type
elif isinstance(criterion, (str,unicode)) and any(columns) and 'TEXT' in types:
try:
q = "SELECT * FROM {} WHERE {}".format(table,' OR '.join([r"REPLACE("+c+r",' ','') like '%"\
Expand All @@ -657,10 +657,11 @@ def search(self, criterion, table, columns=[], fetch=False):
except:
print "Could not search {} table by string {}. Try again.".format(table.upper(),criterion)

# Integer id search
# Integer search of columns with 'INTEGER' data type
elif isinstance(criterion, int):
try:
q = "SELECT * FROM {} WHERE id={}".format(table,str(criterion))
q = "SELECT * FROM {} WHERE {}".format(table,' OR '.join(['{}={}'.format(c,criterion) \
for c,t in zip(columns,types[np.in1d(columns,all_columns)]) if t=='INTEGER']))
results = self.query(q, fmt='table')
except:
print "Could not search {} table by id {}. Try again.".format(table.upper(),criterion)
Expand Down

0 comments on commit d976abf

Please sign in to comment.