def breakquery(q): if len(q) > 1: raise functions.OperatorError( __name__.rsplit('.')[-1], "Ambiguous query column, result has more than one columns") st = '' for row in q[0].splitlines(): strow = filterlinecomment(row) if strow == '': continue if st != '': st += '\n' + strow else: st += strow if apsw.complete(st): yield st st = '' if len(st) > 0 and not re.match(r'\s+$', st, re.DOTALL | re.UNICODE): if len(st) > 35: raise functions.OperatorError( __name__.rsplit('.')[-1], "Incomplete statement found : %s ... %s" % (st[:15], st[-15:])) else: raise functions.OperatorError( __name__.rsplit('.')[-1], "Incomplete statement found : %s" % (st, ))
def VTiter(self, *parsedArgs,**envars): largs, dictargs = self.full_parse(parsedArgs) if 'query' not in dictargs: raise functions.OperatorError(__name__.rsplit('.')[-1],"No query argument ") query=dictargs['query'] connection=envars['db'] yield (('query', 'text'),) cur=connection.cursor() execit=cur.execute(query, parse = False) st='' for row in execit: strow=filterlinecomment(' '.join(row)) if strow=='': continue if st!='': st+='\n'+strow else: st+=strow if apsw.complete(st): yield [st] st='' if len(st)>0 and not re.match(r'\s+$', st, re.DOTALL| re.UNICODE): if len(st)>35: raise functions.OperatorError(__name__.rsplit('.')[-1],"Incomplete statement found : %s ... %s" %(st[:15],st[-15:])) else: raise functions.OperatorError(__name__.rsplit('.')[-1],"Incomplete statement found : %s" %(st))
def sqlstatement(iter): st='' for row in iter: strow=filterlinecomment(' '.join(row)) if strow=='': continue if st!='': st+='\n'+strow else: st+=strow if apsw.complete(st): yield [st] st='' if len(st)>0 and not re.match(r'\s+$', st, re.DOTALL| re.UNICODE): if len(st)>35: raise functions.OperatorError(__name__.rsplit('.')[-1],"Incomplete statement found : %s ... %s" %(st[:15],st[-15:])) else: raise functions.OperatorError(__name__.rsplit('.')[-1],"Incomplete statement found : %s" %(st)) return
def breakquery(q): if len(q) > 1: raise functions.OperatorError(__name__.rsplit('.')[-1], "Ambiguous query column, result has more than one columns") st = '' for row in q[0].splitlines(): strow = filterlinecomment(row) if strow == '': continue if st != '': st += '\n'+strow else: st += strow if apsw.complete(st): yield st st = '' if len(st) > 0 and not re.match(r'\s+$', st, re.DOTALL| re.UNICODE): if len(st) > 35: raise functions.OperatorError(__name__.rsplit('.')[-1], "Incomplete statement found : %s ... %s" % (st[:15], st[-15:])) else: raise functions.OperatorError(__name__.rsplit('.')[-1], "Incomplete statement found : %s" % (st,))
def VTiter(self, *parsedArgs, **envars): largs, dictargs = self.full_parse(parsedArgs) if 'query' not in dictargs: raise functions.OperatorError( __name__.rsplit('.')[-1], "No query argument ") query = dictargs['query'] connection = envars['db'] yield (('query', 'text'), ) cur = connection.cursor() execit = cur.execute(query, parse=False) st = '' for row in execit: strow = filterlinecomment(' '.join(row)) if strow == '': continue if st != '': st += '\n' + strow else: st += strow if apsw.complete(st): yield [st] st = '' if len(st) > 0 and not re.match(r'\s+$', st, re.DOTALL | re.UNICODE): if len(st) > 35: raise functions.OperatorError( __name__.rsplit('.')[-1], "Incomplete statement found : %s ... %s" % (st[:15], st[-15:])) else: raise functions.OperatorError( __name__.rsplit('.')[-1], "Incomplete statement found : %s" % (st))
if flowname == None: parser.print_help() sys.exit(1) try: f = open(flowname, 'r') except Exception, e: exitwitherror("Error in opening SQL flow: " + str(e)) statement = f.readline() if not statement: sys.exit() while True: while not apsw.complete(statement): line = f.readline() statement += line if not line: if statement.rstrip('\r\n') != '': sys.stderr.write("Incomplete query:" + statement) f.close() sys.exit() try: for row in Connection.cursor().execute(statement): if len(row) > 1: print(json.dumps(row, separators=(',', ':'), ensure_ascii=False).encode('utf_8', 'replace')) else: print(unicode(row[0]).encode('utf_8', 'replace')) statement = '' except Exception, e:
if flowname == None: parser.print_help() sys.exit(1) try: f = io.open(flowname, mode='r', encoding='utf-8') except Exception, e: exitwitherror("Error in opening SQL flow: " + str(e)) statement = f.readline() if not statement: sys.exit() while True: while not apsw.complete(statement): line = f.readline() statement += line if not line: if statement.rstrip('\r\n')!='': sys.stderr.write("Incomplete query:"+statement) f.close() sys.exit() try : for row in Connection.cursor().execute(statement): if len(row) > 1: print(json.dumps(row, separators=(',',':'), ensure_ascii=False).encode('utf_8', 'replace')) else: print(unicode(row[0]).encode('utf_8', 'replace')) statement = '' except Exception, e:
def main(): desc = """Use this program to run madSQL queries on data coming from standard input. You may provide a database to run your queries. Results are streamed to standard output. """ parser = OptionParser(description=desc, usage="usage: %prog [options] [dbname] flowname", version="%prog 1.0") parser.add_option("-f", "--flow", help="flow file to execute") parser.add_option("-d", "--db", help="db to connect") parser.add_option("-w", "--dbw", help="db to connect (open in create mode)") (options, args) = parser.parse_args() dbname = '' flags = apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE try: dbname = args[0] flags = apsw.SQLITE_OPEN_READWRITE except: pass if options.db != None: dbname = options.db flags = apsw.SQLITE_OPEN_READWRITE if options.dbw != None: dbname = options.dbw try: Connection = madis.functions.Connection(dbname, flags) except Exception as e: exitwitherror("Error in opening DB: " + str(dbname) + "\nThe error was: " + str(e)) flowname = None try: flowname = args[1] except: pass if options.flow is not None: flowname = options.flow if flowname is None: parser.print_help() sys.exit(1) try: f = open(flowname, 'r') except Exception as e: exitwitherror("Error in opening SQL flow: " + str(e)) statement = f.readline() if not statement: sys.exit() while True: while not apsw.complete(statement): line = f.readline() statement += line if not line: if statement.rstrip('\r\n') != '': sys.stderr.write("Incomplete query:" + statement) f.close() sys.exit() try: for row in Connection.cursor().execute(statement): if len(row) > 1: print( json.dumps(row, separators=(',', ':'), ensure_ascii=False).encode( 'utf_8', 'replace')) else: print(str(row[0]).encode('utf_8', 'replace')) statement = '' except Exception as e: exitwitherror("Error when executing query: \n" + statement + "\nThe error was: " + str(e))