sqlitemulti/sqlitemulti/sqlitemulti.py
def sqlite_worker(
    queue, database, timeout=5, isolation_level="", uri=None, verbose: bool = False
):
    """Worker, running in thread or Process"""
    try:
        # print("isolation", isolation_level)
        db = sqlite3.connect(
            database,
            timeout=timeout,
            isolation_level=isolation_level,
            uri=uri,
            check_same_thread=False,  # We will not need this, but I suppose this could save some tests/time.
        )
    except Exception as e:
        if verbose:
            print(f"DB Process error at connect {e}")
        raise
    if verbose:
        print(f"DB Queue {database} started")
    while True:
        try:
            result_queue, command, sql, params, commit = queue.get(
                block=True, timeout=30
            )
            if verbose:
                print(f"DB Queue got {command}:{sql} {params}")
            res = None
            if command in (SqlCommand.EXECUTE, SqlCommand.INSERT, SqlCommand.DELETE):
                if type(sql) is list:
                    # We have a transaction - sql as well as params are lists
                    if type(params) is not list:
                        raise ValueError("Params has to be a list, too")
                    try:
                        for i, sql_line in enumerate(sql):
                            db.execute(sql_line, params[i])
                        commit = True  # Force commit since all went fine.
                        #  TODO: returns proper info depending on request.
                        res = len(sql)  # returns len of sql
                    except Exception as e:
                        print(f"Exception {e}")
                        db.rollback()
                        res = False
                else:
                    db.execute(sql, params)
                    res = True
                    if command == SqlCommand.INSERT:
                        # TODO: send inserted count back
                        pass
                    elif command == SqlCommand.DELETE:
                        # TODO: sends deleted back
                        pass
            elif command == SqlCommand.EXECUTEMANY:
                # Execute and sends the value back
                db.executemany(sql, params)
                commit = True
                res = True

            elif command == SqlCommand.FETCHONE:
                # Execute and sends the value back
                result = db.execute(sql, params)
                res = result.fetchone()
            elif command == SqlCommand.FETCHALL:
                # Execute and sends the value back
                result = db.execute(sql, params)
                res = result.fetchall()
            elif command == SqlCommand.COMMIT:
                db.commit()
            elif command == SqlCommand.STOP:
                if verbose:
                    print("DB Process stopping")
                return
            else:
                raise ValueError("Unknown SqlCommand")
            if commit:
                db.commit()
            # Send the data back to the provided queue
            if result_queue:
                result_queue.put(res)
            queue.task_done()
        except Exception as e:
            if verbose:
                print(f"DB Process running {e}")
Worker, running in thread or Process
Worker, running in thread or Process
Worker running in thread or Process
def sqlite_worker(
    queue, database, timeout=5, isolation_level="", uri=None, verbose: bool = False
):
    try:
        db = sqlite3.connect(
            database,
            timeout=timeout,
            isolation_level=isolation_level,
            uri=uri,
            check_same_thread=False,  
        )
    except Exception as e:
        if verbose:
            print(f"DB Process error at connect {e}")
        raise
    if verbose:
        print(f"DB Queue {database} started")
    while True:
        try:
            result_queue, command, sql, params, commit = queue.get(
                block=True, timeout=30
            )
            if verbose:
                print(f"DB Queue got {command}:{sql} {params}")
            res = None
            if command in (SqlCommand.EXECUTE, SqlCommand.INSERT, SqlCommand.DELETE):
                if type(sql) is list:
                    if type(params) is not list:
                        raise ValueError("Params has to be a list, too")
                    try:
                        for i, sql_line in enumerate(sql):
                            db.execute(sql_line, params[i])
                        commit = True  
                        res = len(sql)  
                    except Exception as e:
                        print(f"Exception {e}")
                        db.rollback()
                        res = False
                else:
                    db.execute(sql, params)
                    res = True
                    if command == SqlCommand.INSERT:
                        pass
                    elif command == SqlCommand.DELETE:
                        pass
            elif command == SqlCommand.EXECUTEMANY:
                db.executemany(sql, params)
                commit = True
                res = True
            elif command == SqlCommand.FETCHONE:
                result = db.execute(sql, params)
                res = result.fetchone()
            elif command == SqlCommand.FETCHALL:
                result = db.execute(sql, params)
                res = result.fetchall()
            elif command == SqlCommand.COMMIT:
                db.commit()
            elif command == SqlCommand.STOP:
                if verbose:
                    print("DB Process stopping")
                return
            else:
                raise ValueError("Unknown SqlCommand")
            if commit:
                db.commit()
            if result_queue:
                result_queue.put(res)
            queue.task_done()
        except Exception as e:
            if verbose:
                print(f"DB Process running {e}")
def sqlite_worker ( queue , database , timeout = 5 , isolation_level = "" , uri = None , verbose : bool = False ) : try : db = sqlite3 . connect ( database , timeout = timeout , isolation_level = isolation_level , uri = uri , check_same_thread = False , ) except Exception as e : if verbose : print ( f"DB Process error at connect {e}" ) raise if verbose : print ( f"DB Queue {database} started" ) while True : try : result_queue , command , sql , params , commit = queue . get ( block = True , timeout = 30 ) if verbose : print ( f"DB Queue got {command}:{sql} {params}" ) res = None if command in ( SqlCommand . EXECUTE , SqlCommand . INSERT , SqlCommand . DELETE ) : if type ( sql ) is list : if type ( params ) is not list : raise ValueError ( "Params has to be a list, too" ) try : for i , sql_line in enumerate ( sql ) : db . execute ( sql_line , params [ i ] ) commit = True res = len ( sql ) except Exception as e : print ( f"Exception {e}" ) db . rollback ( ) res = False else : db . execute ( sql , params ) res = True if command == SqlCommand . INSERT : pass elif command == SqlCommand . DELETE : pass elif command == SqlCommand . EXECUTEMANY : db . executemany ( sql , params ) commit = True res = True elif command == SqlCommand . FETCHONE : result = db . execute ( sql , params ) res = result . fetchone ( ) elif command == SqlCommand . FETCHALL : result = db . execute ( sql , params ) res = result . fetchall ( ) elif command == SqlCommand . COMMIT : db . commit ( ) elif command == SqlCommand . STOP : if verbose : print ( "DB Process stopping" ) return else : raise ValueError ( "Unknown SqlCommand" ) if commit : db . commit ( ) if result_queue : result_queue . put ( res ) queue . task_done ( ) except Exception as e : if verbose : print ( f"DB Process running {e}" )
Worker, running in thread or Process
Worker running in thread or Process
"""Worker, running in thread or Process""" # print("isolation", isolation_level) # We will not need this, but I suppose this could save some tests/time. # We have a transaction - sql as well as params are lists # Force commit since all went fine. #  TODO: returns proper info depending on request. # returns len of sql # TODO: send inserted count back # TODO: sends deleted back # Execute and sends the value back # Execute and sends the value back # Execute and sends the value back # Send the data back to the provided queue
queue
database
timeout
isolation_level
uri
verbose
bool