From b20b81b8f5112b6ba0bc4f0378708dc97d09ffc7 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 8 Sep 2014 15:59:16 +0200 Subject: [PATCH] Updated DatabaseStoredFile to latest web2py master --- gluon/dal/helpers/classes.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/gluon/dal/helpers/classes.py b/gluon/dal/helpers/classes.py index 6fb93d28..51afaa40 100644 --- a/gluon/dal/helpers/classes.py +++ b/gluon/dal/helpers/classes.py @@ -191,24 +191,28 @@ class MethodAdder(object): class DatabaseStoredFile: - web2py_filesystem = False + web2py_filesystems = set() def escape(self,obj): return self.db._adapter.escape(obj) + @staticmethod + def try_create_web2py_filesystem(db): + if not db._uri in DatabaseStoredFile.web2py_filesystems: + if db._adapter.dbengine == 'mysql': + sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content LONGTEXT, PRIMARY KEY(path) ) ENGINE=InnoDB;" + elif db._adapter.dbengine in ('postgres', 'sqlite'): + sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content TEXT, PRIMARY KEY(path));" + db.executesql(sql) + DatabaseStoredFile.web2py_filesystems.add(db._uri) + def __init__(self,db,filename,mode): if not db._adapter.dbengine in ('mysql', 'postgres', 'sqlite'): raise RuntimeError("only MySQL/Postgres/SQLite can store metadata .table files in database for now") self.db = db self.filename = filename self.mode = mode - if not self.web2py_filesystem: - if db._adapter.dbengine == 'mysql': - sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content LONGTEXT, PRIMARY KEY(path) ) ENGINE=InnoDB;" - elif db._adapter.dbengine in ('postgres', 'sqlite'): - sql = "CREATE TABLE IF NOT EXISTS web2py_filesystem (path VARCHAR(255), content TEXT, PRIMARY KEY(path));" - self.db.executesql(sql) - DatabaseStoredFile.web2py_filesystem = True + DatabaseStoredFile.try_create_web2py_filesystem(db) self.p=0 self.data = '' if mode in ('r','rw','a'): @@ -259,6 +263,9 @@ class DatabaseStoredFile: def exists(db, filename): if exists(filename): return True + + DatabaseStoredFile.try_create_web2py_filesystem(db) + query = "SELECT path FROM web2py_filesystem WHERE path='%s'" % filename try: if db.executesql(query):