1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 To write data into SQL table.
24
25 Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
26 GNU General Public License.
27
28 """
29
30 from etl.component import component
31 import datetime
32
34 """
35 This is an ETL Component that writes data in SQL table.
36
37 Type : Data Component.
38 Computing Performance : Streamline.
39 Input Flows : 0-x.
40 * .* : The main data flow with input data.
41 Output Flows : 0-1.
42 * main : Return all data.
43 """
44
45 - def __init__(self, sqlconnector, sqltable, name='component.output.sql_out', transformer=None, row_limit=0):
46
47 """
48 Required Parameters
49 sql_connector : SQL connector.
50 sqltable : The name of the SQL table.
51
52 Extra Parameters
53 name : Name of Component.
54 transformer : Transformer object to transform string data into particular object.
55 row_limit : Limited records are sent to destination if row limit is specified. If row limit is 0, all records are sent.
56 """
57 super(sql_out, self).__init__(name=name, connector=sqlconnector, transformer=transformer, row_limit=row_limit)
58 self._type = 'component.output.sql_out'
59 self.sqltable = sqltable
60
64
69
71 datas = []
72 self.sqlconnector = False
73 for channel, trans in self.input_get().items():
74 for iterator in trans:
75 for d in iterator:
76 if not self.sqlconnector:
77 self.sqlconnector = self.connector.open()
78 insert_query = " INSERT into %s (%s) VALUES (%s)" % (self.sqltable, ','.join(d.keys()), ','.join(map(lambda x: (type(x) in (int, long, float, complex)) and x or repr(str(x)), d.values())))
79 cr = self.sqlconnector.cursor()
80 cr.execute(insert_query)
81 self.sqlconnector.commit()
82 yield d, 'main'
83
85 from etl_test import etl_test
86 import etl
87 sql_conn = etl.connector.sql_connector('localhost', 5432, 'trunk', 'postgres', 'postgres')
88 test = etl_test.etl_component_test(sql_out(sql_conn, 'res_partner'))
89 test.check_input({'main': [{'name': 'tinyerp belgium'}]})
90 test.check_output([{'name': 'tinyerp belgium'}], 'main')
91 res = test.output()
92 print res
93
94 if __name__ == '__main__':
95 test()
96