Package etl :: Package connector :: Module sql_connector
[hide private]
[frames] | no frames]

Source Code for Module etl.connector.sql_connector

  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  """ 
 24   To provide connectivity with SQL database server. 
 25   supported connection with : 
 26       * postgres server 
 27       * mysql server 
 28       * oracle server 
 29   
 30   Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). 
 31   GNU General Public License. 
 32  """ 
 33  from etl.connector import connector 
34 -class sql_connector(connector):
35 - def __init__(self, host, port, db, uid, passwd, sslmode='allow', con_type='postgres', name='sql_connector'):
36 """ 37 Required Parameters 38 host : Database server host name. 39 port : Database server port. 40 db : Database name. 41 uid : User name to login into Database. 42 passwd : Password of the user. 43 44 Extra Parameters 45 sslmode : For SSL connection. 46 con_type : Type of connection (postgres, mysql, oracle). 47 name : Name of the connector. 48 """ 49 super(sql_connector, self).__init__(name) 50 self._type = 'connector.sql_connector' 51 self.uri = host + ':' + str(port) 52 self.host = host 53 self.port = port 54 self.sslmode = sslmode 55 self.db = db 56 self.uid = uid 57 self.con_type = con_type 58 self.passwd = passwd
59 60
61 - def open(self):
62 """ 63 Opens a connection to Database server. 64 """ 65 super(sql_connector, self).open() 66 connector = False 67 if self.con_type == 'postgres': 68 import psycopg2 69 connector = psycopg2.connect("dbname=%s user=%s host=%s port=%s password=%s sslmode=%s" \ 70 % (self.db, self.uid, self.host, self.port, self.passwd, self.sslmode)) 71 elif self.con_type == 'mysql': 72 import MySQLdb 73 connector = MySQLdb.Connection(db=self.db, host=self.host, port=self.port, user=self.uid, passwd=self.pwd) 74 elif self.con_type == 'oracle': 75 import cx_Oracle 76 dsn_tns = cx_Oracle.makedsn(self.host, self.port, self.db) 77 connector = cx_Oracle.connect(self.uid, self.passwd, dsn_tns) 78 else: 79 raise Exception('Not Supported') 80 return connector
81
82 - def close(self, connector):
83 """ 84 Closes the connection made to Database Server. 85 """ 86 super(sql_connector, self).close() 87 return connector.close()
88
89 - def __copy__(self):
90 """ 91 Overrides copy method. 92 """ 93 res = sql_connector(self.host, self.port, self.db, self.uid, self.passwd, self.sslmode, self.con_type, self.name) 94 return res
95
96 -def test():
97 98 from etl_test import etl_test 99 import etl 100 sqlconnector_partner=sql_connector('localhost',5432, 'test', 'postgres', 'postgres') 101 test = etl_test.etl_component_test(etl.component.input.sql_in(sqlconnector_partner,'select * from res_partner where id<=10 order by id')) 102 res=test.output() 103 print res 104 105 pass
106 107 if __name__ == '__main__': 108 test() 109