ソースを参照

Small bugfixes in sqlobject and sqlwrapper

Yann Weber 9年前
コミット
9896c16e8e
3個のファイルの変更19行の追加16行の削除
  1. 12
    6
      Database/sqlobject.py
  2. 6
    5
      Database/sqlwrapper.py
  3. 1
    5
      EditorialModel/components.py

+ 12
- 6
Database/sqlobject.py ファイルの表示

@@ -27,11 +27,17 @@ class SqlObject(object):
27 27
     """
28 28
 
29 29
     def __init__(self, tname):
30
+        if not type(tname) is str:
31
+            logger.error("Unable to instanciate, bad argument...")
32
+            raise TypeError('Excepted a string not a '+str(type(tname)))
33
+
30 34
         self.tname = tname
35
+        self.table = self.Table()
36
+        pass
31 37
 
32
-    @property
33
-    def table(self):
34
-        return sql.Table(self.tname, sql.MetaData())
38
+    def Table(self):
39
+        self.table = sql.Table(self.tname, sql.MetaData(), autoload_with=SqlWrapper.rengine, autoload=True)
40
+        return self.table
35 41
 
36 42
     @property
37 43
     def col(self):
@@ -39,7 +45,7 @@ class SqlObject(object):
39 45
     
40 46
     @property
41 47
     def sel(self):
42
-        return sql.select(self.table)
48
+        return sql.select([self.table])
43 49
 
44 50
     @property
45 51
     def where(self):
@@ -51,11 +57,11 @@ class SqlObject(object):
51 57
 
52 58
     @property
53 59
     def rconn(self):
54
-        return SqlWrapper.rconn()
60
+        return SqlWrapper.rc()
55 61
 
56 62
     @property
57 63
     def wconn(self):
58
-        return SqlWrapper.wconn()
64
+        return SqlWrapper.wc()
59 65
 
60 66
     def sFetchAll(self, sel):
61 67
         return self.rexec(sel).fetchall()

+ 6
- 5
Database/sqlwrapper.py ファイルの表示

@@ -46,13 +46,14 @@ class SqlWrapper(object):
46 46
         pass
47 47
 
48 48
     @classmethod
49
-    def table(c, o):
49
+    def table(c, tname):
50 50
         """ Return a SqlAlchemy Table object
51 51
             @param o str: Table name
52
+            @return a SqlAlchemy Table instance
52 53
         """
53
-        if isinstance(o, str):
54
-            return sqla.Table(o, sqla.MetaData())
55
-        return None
54
+        if not isinstance(tname, str):
55
+            raise TypeError("Excepting a str but got a "+str(type(name)))
56
+        return sqla.Table(o, sqla.MetaData())
56 57
 
57 58
     @classmethod
58 59
     def connect(c,read = None):
@@ -75,7 +76,7 @@ class SqlWrapper(object):
75 76
         if res == None:
76 77
             if not c.connect(read):
77 78
                 raise RuntimeError('Unable to connect to Db')
78
-            return self.conn(read)
79
+            return c.conn(read)
79 80
 
80 81
         return c.rconn
81 82
 

+ 1
- 5
EditorialModel/components.py ファイルの表示

@@ -37,18 +37,14 @@ class EmComponent(object):
37 37
     def populate(self):
38 38
         dbo = SqlObject(self.table)
39 39
         
40
-        t = dbo.table
41
-
42 40
         req = dbo.sel
43
-        print(t.c.__dict__)
44 41
         
45 42
         if self.id is None:
46
-            req.where(t.c.name == self.name)
43
+            req.where(dbo.col.name == self.name)
47 44
         else:
48 45
             req.where(dbo.col.id == self.id)
49 46
 
50 47
         sqlresult = dbo.rexec(req)
51
-        print (sqlresult)
52 48
 
53 49
         # Transformation du résultat en une liste de dictionnaires
54 50
         records = sqlresult.fetchall()

読み込み中…
キャンセル
保存