Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# coding: latin-1
2"""
3@file
5@brief @see cl Database
6"""
7from pyquickhelper.loghelper.flog import fLOG
9module_odbc = None
12class DatabaseObject:
14 """
15 Methods for database related to object, see @see cl Database.
16 """
18 def fill_table_with_objects(
19 self, tablename, iterator_on, check_existence=False, skip_exception=False):
20 """
21 Fill a table of a database with object (from ``iterator_on``) following the interface:
23 - a property ``schema_database`` which gives the schema
24 - a property ``asrow`` which puts all values in a row
25 - a property ``index`` which precises the index to unless it returns None
26 - a property ``indexes`` which precises other indexes to create (optional)
28 The property ``asrow`` must not include other objects, only their ids.
29 If the table does not exists, it creates it.
31 @param tablename name of a table (created if it does not exists)
32 @param iterator_on iterator_on on StreamRSS object
33 @param check_existence avoid adding an element if it already exists (based on the index columns)
34 @param skip_exception skip exception while inserting an element
36 The function do not check duplicate among elements sent in ``iterator_on``,
37 it only checks duplicate between the new and the old ones (meaning in the database).
38 """
39 schema = None
40 index = None
41 indexes = None
42 for _ in iterator_on:
43 schema = _.schema_database
44 index = _.index
45 try:
46 indexes = _.indexes
47 except Exception:
48 pass
49 break
51 if schema is None:
52 # nothing to do, it is empty
53 return
55 if tablename not in self.get_table_list():
56 fLOG("create table ", tablename)
57 cursor = self.create_table(tablename, schema)
58 if index is not None:
59 self.create_index(
60 index + "_" + tablename + "_index",
61 tablename,
62 [index],
63 unique=True)
64 if indexes is not None:
65 for ind in indexes:
66 if isinstance(ind, str):
67 self.create_index(
68 ind + "_" + tablename + "_index",
69 tablename,
70 [ind],
71 unique=False)
72 else:
73 self.create_index(
74 "_".join(ind) + "_" + tablename + "_index",
75 tablename,
76 ind,
77 unique=False)
78 ite = map(lambda m: m.asrow, iterator_on)
79 self.append_values(
80 ite,
81 tablename,
82 schema,
83 cursor=cursor,
84 skip_exception=skip_exception)
86 else:
87 if check_existence:
88 if index is None:
89 raise ValueError(
90 "unable to check existence because index property is not set up")
92 def enumerate_nodup(iterator):
93 for it in iterator:
94 val = it.__dict__[index]
95 view = self.execute_view(
96 "SELECT * FROM %s WHERE %s=\"%s\";" %
97 (tablename, index, val))
98 if len(view) == 0:
99 yield it
101 ite = map(lambda m: m.asrow, enumerate_nodup(iterator_on))
102 self.append_values(
103 ite,
104 tablename,
105 schema,
106 cursor=None,
107 skip_exception=skip_exception)
108 else:
109 ite = map(lambda m: m.asrow, iterator_on)
110 self.append_values(
111 ite,
112 tablename,
113 schema,
114 cursor=None,
115 skip_exception=skip_exception)
117 self.commit()
119 def enumerate_objects(self, table, classObject):
120 """
121 Iterator on objects assuming each row of a table is a object (classObject type).
122 The classObject must have the following properties:
123 - a staticmethod ``schema_database_read`` which gives the schema
124 - the constructor must accept a constructor where parameter have the same name as the column names
126 @param table table name
127 @param classObject class object
128 @return iterator on object
130 Example:
132 ::
134 for blog in db.enumerate_objects ("blogs", StreamRSS):
135 #...
136 """
137 schema = classObject.schema_database_read()
138 for row in self.execute("SELECT * FROM %s" % table):
139 di = {schema[i][0]: v for i, v in enumerate(row)}
140 yield classObject(**di)