Browse Source

Small bugfixes in sqlobject and sqlwrapper

Yann Weber 9 years ago
parent
commit
9896c16e8e
3 changed files with 19 additions and 16 deletions
  1. 12
    6
      Database/sqlobject.py
  2. 6
    5
      Database/sqlwrapper.py
  3. 1
    5
      EditorialModel/components.py

+ 12
- 6
Database/sqlobject.py View File

27
     """
27
     """
28
 
28
 
29
     def __init__(self, tname):
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
         self.tname = tname
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
     @property
42
     @property
37
     def col(self):
43
     def col(self):
39
     
45
     
40
     @property
46
     @property
41
     def sel(self):
47
     def sel(self):
42
-        return sql.select(self.table)
48
+        return sql.select([self.table])
43
 
49
 
44
     @property
50
     @property
45
     def where(self):
51
     def where(self):
51
 
57
 
52
     @property
58
     @property
53
     def rconn(self):
59
     def rconn(self):
54
-        return SqlWrapper.rconn()
60
+        return SqlWrapper.rc()
55
 
61
 
56
     @property
62
     @property
57
     def wconn(self):
63
     def wconn(self):
58
-        return SqlWrapper.wconn()
64
+        return SqlWrapper.wc()
59
 
65
 
60
     def sFetchAll(self, sel):
66
     def sFetchAll(self, sel):
61
         return self.rexec(sel).fetchall()
67
         return self.rexec(sel).fetchall()

+ 6
- 5
Database/sqlwrapper.py View File

46
         pass
46
         pass
47
 
47
 
48
     @classmethod
48
     @classmethod
49
-    def table(c, o):
49
+    def table(c, tname):
50
         """ Return a SqlAlchemy Table object
50
         """ Return a SqlAlchemy Table object
51
             @param o str: Table name
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
     @classmethod
58
     @classmethod
58
     def connect(c,read = None):
59
     def connect(c,read = None):
75
         if res == None:
76
         if res == None:
76
             if not c.connect(read):
77
             if not c.connect(read):
77
                 raise RuntimeError('Unable to connect to Db')
78
                 raise RuntimeError('Unable to connect to Db')
78
-            return self.conn(read)
79
+            return c.conn(read)
79
 
80
 
80
         return c.rconn
81
         return c.rconn
81
 
82
 

+ 1
- 5
EditorialModel/components.py View File

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

Loading…
Cancel
Save