Package etl :: Package component :: Package output :: Module sql_out'
[hide private]
[frames] | no frames]

Source Code for Module etl.component.output.sql_out'

 1  # -*- encoding: utf-8 -*- 
 2  ############################################################################## 
 3  # 
 4  #    ETL system- Extract Transfer Load system 
 5  #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved 
 6  #    $Id$ 
 7  # 
 8  #    This program is free software: you can redistribute it and/or modify 
 9  #    it under the terms of the GNU General Public License as published by 
10  #    the Free Software Foundation, either version 3 of the License, or 
11  #    (at your option) any later version. 
12  # 
13  #    This program is distributed in the hope that it will be useful, 
14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  #    GNU General Public License for more details. 
17  # 
18  #    You should have received a copy of the GNU General Public License 
19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
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   
33 -class sql_out(component):
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
61 - def __copy__(self):
62 res = sql_out(self.connector, self.sqlquery, self.name, self.transformer, self.row_limit) 63 return res
64
65 - def end(self):
66 super(sql_out, self).end() 67 if self.sqlconnector: 68 self.connector.close(self.sqlconnector)
69
70 - def process(self):
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
84 -def test():
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