Skip to content

Commit

Permalink
SQLITE3_Connection to be used instead of DBConnection when threading …
Browse files Browse the repository at this point in the history
…is needed--e.g. for web apps
  • Loading branch information
bart mosley committed Oct 15, 2011
1 parent 648feb1 commit 2fc3a2b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
18 changes: 7 additions & 11 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

import sys

if sys.platform == 'cli':
from autumn.Ipy import Model, DBConnection, Query, Filter, OneToMany, ForeignKey, escape

else:
from model import Model
from db.connection import autumn_db
from db.dbconnection import DBConnection
from db.query import Query
from db.filter import Filter
from db.relations import OneToMany, ForeignKey
from db import escape
from model import Model
from db.connection import autumn_db
from db.dbconnection import DBConnection, SQLITE3_Connection
from db.query import Query
from db.filter import Filter
from db.relations import OneToMany, ForeignKey
from db import escape
2 changes: 1 addition & 1 deletion db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Database(object):

def connect(self, dbtype, *args, **kwargs):
if dbtype == 'sqlite3':
import sqlite3
import sqlite3.dbapi2 as sqlite3
self.connection = sqlite3.connect(*args, **kwargs)
self.lastrowid_ = "LAST_INSERT_ROWID()"

Expand Down
30 changes: 29 additions & 1 deletion db/dbconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ class Table(marketsdb, Model):
Query.raw_sql("select * from mytable", db=marketsdb.db)
'''
import os
from threading import local as threading_local

from connection import Database, DBConn, autumn_db
from autumn.util import AutoConn

class DBConnector(Database):

def getconnection(self, dbtype=None, db=None, dbfile=None, **kwcfg):
'''return a Database object connected to the database'''
self.dbtype = dbtype
if dbtype == 'sqlite3':
import os
if db is None:
db = os.path.split(dbfile)[1]
elif os.path.isdir(dbfile):
dbfile = os.path.join(dbfile,db)
else:
pass # because dbfile better be a good file name

else:
self.dbname = db
if 'user' in kwcfg:
Expand All @@ -70,3 +73,28 @@ def __init__(self, dbtype=None, db=None, dbfile=None, **kwcfg):
autumn_db = self



class SQLITE3_Connection(object):
"""
A container that will automatically create a database connection object
for each thread that accesses it. Useful with SQLite, because the Python
modules for SQLite require a different connection object for each thread.
"""
def __init__(self, db_filename, db_path=None):
self.b_debug = False
self.b_commit = True
self.db_name = os.path.join(db_path, db_filename)
self.container = threading_local()

def __getattr__(self, name):
try:
if "conn" == name:
return self.container.conn

except BaseException:
self.container.conn = DBConnector()
self.container.conn.getconnection('sqlite3',
dbfile=self.db_name)
return self.container.conn

raise AttributeError

0 comments on commit 2fc3a2b

Please sign in to comment.