Browse Source

Updated sqlwrapper to enable sqlite support and logging enable/disable

Yann Weber 9 years ago
parent
commit
aad5586cb1
3 changed files with 41 additions and 22 deletions
  1. 5
    1
      Database/sqlsettings.py
  2. 36
    15
      Database/sqlwrapper.py
  3. 0
    6
      test.py

+ 5
- 1
Database/sqlsettings.py View File

@@ -13,7 +13,11 @@ class SQLSettings(object):
13 13
             # TODO à définir
14 14
             'driver': 'pymysql',
15 15
             'encoding': 'utf8',
16
-        }
16
+        },
17
+        'sqlite': {
18
+            'driver': 'pysqlite',
19
+            'encoding': 'utf8',
20
+        },
17 21
     }
18 22
 
19 23
     DB_READ_CONNECTION_NAME = 'default'  # TODO A configurer

+ 36
- 15
Database/sqlwrapper.py View File

@@ -10,36 +10,57 @@ logger.getLogger().setLevel('DEBUG')
10 10
 
11 11
 class SqlWrapper(object):
12 12
 
13
-    def __init__(self):
13
+    def __init__(self, log=False):
14
+        """ Instanciate a new SqlWrapper
15
+            @param log bool: If true activate sqlalchemy logger
16
+        """
14 17
         # TODO Passer ces deux éléments dans les settings django (au niveau de l'init de l'appli)
15
-        self.read_engine = self.get_engine(sqlsettings.DB_READ_CONNECTION_NAME)
16
-        self.write_engine = self.get_engine(sqlsettings.DB_WRITE_CONNECTION_NAME)
18
+        self.read_engine = self.get_engine(sqlsettings.DB_READ_CONNECTION_NAME, log)
19
+        self.write_engine = self.get_engine(sqlsettings.DB_WRITE_CONNECTION_NAME, log)
17 20
 
18
-    def get_engine(self, connection_name):
21
+    def get_engine(self, connection_name, log=False):
19 22
         """ Crée un moteur logique sur une base de données
20 23
 
21 24
             @param connection_name str: Connection name as defined is django settings
25
+            @param log bool: If true this engine will have logging enabled
22 26
             @return Engine
23 27
         """
24 28
 
25 29
         connection_params = settings.DATABASES[connection_name]
26 30
 
27
-        dialect = None
28
-        for dbms in sqlsettings.dbms_list:
29
-            if dbms in connection_params['ENGINE']:
30
-                dialect=dbms
31
-                break
31
+        if 'ENGINE' not in connection_params:
32
+            raise NameError('Missing ENGINE in configuration file')
33
+
34
+        if connection_params['ENGINE'] not in sqlsettings.dbms_list:
35
+            raise NameError('Wrong ENGINE "'+connection_params['ENGINE']+'" in configuration files')
36
+        dialect = connection_params['ENGINE']
32 37
 
38
+        if 'driver' not in sqlsettings.dbms_list[dialect]:
39
+            raise NameError('Wrong ENGINE "'+dialect+'" in configuration files')
33 40
         driver = sqlsettings.dbms_list[dialect]['driver']
34
-        username = connection_params['USER']
35
-        password = connection_params['PASSWORD']
41
+
42
+        if 'NAME' not in connection_params:
43
+            raise NameError('Missing database name in configuration files')
44
+        database = connection_params['NAME']
45
+
46
+        if 'USER' in connection_params:
47
+            user = connection_params['USER']
48
+            if 'PASSWORD' in connection_params:
49
+                user += ':'+connection_params['PASSWORD']
50
+        else:
51
+            user = ''
52
+
36 53
         hostname = connection_params['HOST'] if 'HOST' in connection_params else sqlsettings.DEFAULT_HOSTNAME
37 54
         port = connection_params['PORT'] if 'PORT' in connection_params else ''
38 55
         host = hostname if port=='' else '%s:%s' % (hostname, port)
39
-        database = connection_params['NAME']
40 56
 
41
-        connection_string = '%s+%s://%s:%s@%s/%s' % (dialect, driver, username, password, host, database)
42
-        engine = create_engine(connection_string, encoding=sqlsettings.dbms_list[dialect]['encoding'], echo=True)
57
+
58
+        if dialect == 'sqlite':
59
+            connection_string = '%s+%s:///%s' % (dialect, driver, database)
60
+        else:
61
+            connection_string = '%s+%s://%s@%s/%s' % (dialect, driver, user, host, database)
62
+
63
+        engine = create_engine(connection_string, encoding=sqlsettings.dbms_list[dialect]['encoding'], echo=log)
43 64
         return engine
44 65
 
45 66
     def get_read_engine(self):
@@ -126,7 +147,7 @@ class SqlWrapper(object):
126 147
                 column.primary_key = column_extra['primarykey']
127 148
             if 'default' in column_extra:
128 149
                 column.default = ColumnDefault(column_extra['default'])
129
-            if 'foreingkey' in column_extra:
150
+            if 'foreignkey' in column_extra:
130 151
                 column.append_foreign_key(ForeignKey(column_extra['foreignkey']))
131 152
 
132 153
 

+ 0
- 6
test.py View File

@@ -1,6 +0,0 @@
1
-from django.conf import settings
2
-import logging
3
-
4
-logger = logging.getLogger('Lodel2.Database')
5
-
6
-logger.debug('Foobar')

Loading…
Cancel
Save