def VTiter(self, *parsedArgs, **envars):
        largs, dictargs = self.full_parse(parsedArgs)
        fromv = 0
        tov = None
        stepv = 1
        checkfirst = True
        query = 'select 1'
        con = None

        if 'from' in dictargs:
            fromv = int(dictargs['from'])
        if 'to' in dictargs:
            tov = int(dictargs['to'])
        if 'step' in dictargs:
            stepv = int(dictargs['step'])
        if 'checkfirst' in dictargs and dictargs['checkfirst'] in ('f', 'F',
                                                                   '0'):
            checkfirst = False
        if len(largs) >= 1:
            fromv = int(largs[0])
        if len(largs) >= 2:
            tov = int(largs[1])
        if len(largs) >= 3:
            stepv = int(largs[2])
        if len(largs) == 1:
            fromv = 1
            tov = int(largs[0]) + 1

        if functions.variables.execdb is None:
            con = functions.Connection('')
        else:
            con = functions.Connection(functions.variables.execdb)
        functions.register(con)

        if 'query' not in dictargs:
            raise functions.OperatorError(
                __name__.rsplit('.')[-1], "Needs a query")
        else:
            query = dictargs['query']
        yield [('C1', 'int')]

        if checkfirst:
            cur = con.cursor()
            res = list(cur.execute(query))
            cur.close()
            if len(res) == 0 or len(res[0]) == 0 or res[0][0] != 1:
                return

        yield (fromv, )

        while True:
            cur = con.cursor()
            res = list(cur.execute(query))
            cur.close()
            if len(res) == 0 or len(res[0]) == 0 or res[0][0] != 1:
                return
            fromv += 1
            if tov is not None and fromv >= tov:
                return
            yield (fromv, )
Beispiel #2
0
def createConnection(db):
    try:
        if 'SQLITE_OPEN_URI' in apsw.__dict__:
            connection = functions.Connection(db, flags=apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_URI)
        else:
            connection = functions.Connection(db)
        functions.register(connection)
        connection.enableloadextension(True)
    except Exception, e:
        exitwitherror(e)
Beispiel #3
0
    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']

        if len(largs) > 0:
            sqdb = largs[0]

        if 'db' in dictargs:
            sqdb = dictargs['db']

        sqdb = str(
            os.path.abspath(
                os.path.expandvars(os.path.expanduser(
                    os.path.normcase(sqdb)))))
        conn = functions.Connection(sqdb)

        cur = conn.cursor()
        cur.execute(query)

        yield cur.getdescriptionsafe()

        while True:
            yield cur.next()

        cur.close()
Beispiel #4
0
#-*- coding: utf-8 -*-

import functions

_collections = functions.Connection()

_contests = _collections.find()
for x in _collections
Beispiel #5
0
def execflow(diter, schema, connection, *args, **kargs):
    ignoreflag = 'ignorefail'

    if functions.variables.execdb is None:
        functions.variables.execdb = connection.filename
    con = functions.Connection(functions.variables.execdb)

    functions.register(con)
    oldvars = functions.variables
    newvars = lambda x: x
    newpath = None
    path = os.getcwd()

    if 'path' in kargs:
        newpath = os.path.abspath(kargs['path'])
        del kargs['path']
        os.chdir(newpath)

    newvars.execdb = functions.variables.execdb
    newvars.flowname = 'notset'
    for v in args:
        if hasattr(functions.variables, v):
            newvars.__dict__[v] = functions.variables.__dict__[v]
        else:
            raise functions.OperatorError(__name__.rsplit('.')[-1], "Variable %s doesn't exist" % (v,))
    for newv, oldv in kargs.items():
        if hasattr(functions.variables, oldv):
            newvars.__dict__[newv] = functions.variables.__dict__[oldv]
        else:
            raise functions.OperatorError(__name__.rsplit('.')[-1], "Variable %s doen't exist" % (oldv,))
    functions.variables = newvars

    if functions.settings['logging']:
        lg = logging.LoggerAdapter(logging.getLogger(__name__), {"flowname": functions.variables.flowname})
        lg.info("############FLOW START###################")
    before = datetime.datetime.now()

    query = ''
    try:
        line = 0
        for t in diter:
            for query in breakquery(t):
                line += 1
                if type(query) not in types.StringTypes:
                    raise functions.OperatorError(__name__.rsplit('.')[-1], "Content is not sql query")
                # Skip empty queries or comment lines
                query = query.strip()
                if query.startswith("--"):
                    continue
                cmatch = comment_line.match(query)
                if query == '' or (cmatch is not None and cmatch.groups()[0] == ''):
                    continue

                if functions.settings['logging']:
                    lg = logging.LoggerAdapter(logging.getLogger(__name__), {"flowname": functions.variables.flowname})
                    lg.info("STARTING: %s" % (query))
                before = datetime.datetime.now()
                c = con.cursor()
                # check ignore flag
                catchexception = False
                if query.startswith(ignoreflag):
                    catchexception = True
                    query = query[len(ignoreflag):]
                try:
                    for i in c.execute(query):
                        pass
                except Exception, e:  # Cathing IGNORE FAIL EXCEPTION
                    if catchexception:
                        if functions.settings['logging']:
                            lg = logging.LoggerAdapter(logging.getLogger(__name__),
                                                       {"flowname": functions.variables.flowname})
                            lg.exception("Ignoring Exception: " + str(e))
                        continue
                    else:
                        try:
                            c.close()
                            c = con.cursor()
                            c.execute('rollback')
                        except:
                            pass
                        raise e

                if functions.settings['logging']:
                    lg = logging.LoggerAdapter(logging.getLogger(__name__), {"flowname": functions.variables.flowname})
                    after = datetime.datetime.now()
                    tmdiff = after - before
                    duration = "%s min. %s sec %s msec" % ((
                    int(tmdiff.days) * 24 * 60 + (int(tmdiff.seconds) / 60), (int(tmdiff.seconds) % 60),
                    (int(tmdiff.microseconds) / 1000)))
                    lg.info("FINISHED in %s: %s" % (duration, query))
                c.close()
    except Exception, e:
        if functions.settings['logging']:
            lg = logging.LoggerAdapter(logging.getLogger(__name__), {"flowname": functions.variables.flowname})
            lg.exception(e)
        raise functions.OperatorError(__name__.rsplit('.')[-1],
                                      "Error in statement no. %s query '%s':\n%s" % (line, query, str(e)))
Beispiel #6
0
    def VTiter(self, *parsedArgs, **envars):

        # default  parsing
        largs, dictargs = self.full_parse(parsedArgs)
        # print largs
        # print dictargs

        # get default connection
        connection = envars['db']
        if functions.variables.execdb is None:
            functions.variables.execdb = connection.filename
        tconnection = functions.Connection(functions.variables.execdb)
        functions.register(tconnection)

        # get query
        if 'query' not in dictargs:
            raise functions.OperatorError(__name__.rsplit('.')[-1], "No query argument.")
            # print "error 1"
        else:
            query = dictargs['query']

        # set up variables
        oldvars = functions.variables
        newvars = lambda x: x
        newpath = None
        path = os.getcwd()
        if 'path' in dictargs:
            newpath = os.path.abspath(dictargs['path'])
            del dictargs['path']
            os.chdir(newpath)
        newvars.execdb = functions.variables.execdb
        newvars.flowname = 'notset'

        for key in largs:
            if hasattr(functions.variables, key):
                setattr(newvars, key, getattr(functions.variables, key))
            else:
                raise functions.OperatorError(__name__.rsplit('.')[-1], "Variable %s doesn't exist" % (key,))
                # print "error 2"
        for key, value in dictargs.items():
            setattr(newvars, key, value)
        functions.variables = newvars

        # execute target queries
        try:
            counter = -1
            cursor = connection.cursor()
            tcursor = tconnection.cursor()
            tqlast = ''
            databases = cursor.execute("PRAGMA database_list")
            for database in databases:
                dbname = database[1]
                dbfile = database[2]
                if dbname != "main" and dbname != "mem" and dbname != "temp" and dbfile != '':
                    list(tcursor.execute("attach database '{0}' as {1};".format(dbfile, dbname)))

            counter = 0
            results = cursor.execute(query, parse=False)
            for result in results:
                for tquery in breakquery(result):
                    # print "tquery", tquery
                    # print "tqlast", tqlast
                    if tqlast != '':
                        list(tcursor.execute(tqlast))
                        counter += 1
                    tqlast = tquery
            # print "tqlast", len(tqlast)
            if len(tqlast) > 0:
                tresults = tcursor.execute(tqlast)
                counter += 1

                # print 'lala3'
                try:
                    yield tcursor.getdescriptionsafe()
                except apsw.ExecutionCompleteError:
                    try:
                        raise
                    finally:
                        try:
                            tcursor.close()
                            tconnection.close()
                            cursor.close()
                        except:
                            pass
                else:
                    for tresult in tresults:
                        yield tresult
            else:
                yield ('c1',)
            tcursor.close()
            tconnection.close()
            cursor.close()
        except Exception as ex:
            import traceback
            traceback.print_exc()
            raise functions.OperatorError(__name__.rsplit('.')[-1], "Error in query no. %d - %s" % (counter, str(ex)))
            # print "error 3"
        # restore env
        functions.variables = oldvars
        if newpath:
            os.chdir(path)