PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/IBM_DB/ibm_db/ibm_db_dbi.py

http://ibm-db.googlecode.com/
Python | 1505 lines | 1320 code | 62 blank | 123 comment | 11 complexity | 7bfa25d02b524dbe4ca46aecc24c021a MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. # +--------------------------------------------------------------------------+
  2. # | Licensed Materials - Property of IBM |
  3. # | |
  4. # | (C) Copyright IBM Corporation 2007-2009 |
  5. # +--------------------------------------------------------------------------+
  6. # | This module complies with SQLAlchemy 0.4 and is |
  7. # | Licensed under the Apache License, Version 2.0 (the "License"); |
  8. # | you may not use this file except in compliance with the License. |
  9. # | You may obtain a copy of the License at |
  10. # | http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable |
  11. # | law or agreed to in writing, software distributed under the License is |
  12. # | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
  13. # | KIND, either express or implied. See the License for the specific |
  14. # | language governing permissions and limitations under the License. |
  15. # +--------------------------------------------------------------------------+
  16. # | Authors: Swetha Patel, Abhigyan Agrawal, Tarun Pasrija, Rahul Priyadarshi|
  17. # +--------------------------------------------------------------------------+
  18. """
  19. This module implements the Python DB API Specification v2.0 for DB2 database.
  20. """
  21. import types, string, time, datetime, decimal, sys
  22. if sys.version_info >= (3, ):
  23. buffer = memoryview
  24. if sys.version_info < (3, ):
  25. import exceptions
  26. exception = exceptions.StandardError
  27. else:
  28. exception = Exception
  29. import ibm_db
  30. __version__ = ibm_db.__version__
  31. # Constants for specifying database connection options.
  32. SQL_ATTR_AUTOCOMMIT = ibm_db.SQL_ATTR_AUTOCOMMIT
  33. SQL_ATTR_CURRENT_SCHEMA = ibm_db.SQL_ATTR_CURRENT_SCHEMA
  34. SQL_AUTOCOMMIT_OFF = ibm_db.SQL_AUTOCOMMIT_OFF
  35. SQL_AUTOCOMMIT_ON = ibm_db.SQL_AUTOCOMMIT_ON
  36. ATTR_CASE = ibm_db.ATTR_CASE
  37. CASE_NATURAL = ibm_db.CASE_NATURAL
  38. CASE_LOWER = ibm_db.CASE_LOWER
  39. CASE_UPPER = ibm_db.CASE_UPPER
  40. SQL_FALSE = ibm_db.SQL_FALSE
  41. SQL_TRUE = ibm_db.SQL_TRUE
  42. SQL_TABLE_STAT = ibm_db.SQL_TABLE_STAT
  43. SQL_INDEX_CLUSTERED = ibm_db.SQL_INDEX_CLUSTERED
  44. SQL_INDEX_OTHER = ibm_db.SQL_INDEX_OTHER
  45. SQL_DBMS_VER = ibm_db.SQL_DBMS_VER
  46. SQL_DBMS_NAME = ibm_db.SQL_DBMS_NAME
  47. # Module globals
  48. apilevel = '2.0'
  49. threadsafety = 0
  50. paramstyle = 'qmark'
  51. class Error(exception):
  52. """This is the base class of all other exception thrown by this
  53. module. It can be use to catch all exceptions with a single except
  54. statement.
  55. """
  56. def __init__(self, message):
  57. """This is the constructor which take one string argument."""
  58. self._message = message
  59. def __str__(self):
  60. """Converts the message to a string."""
  61. return 'ibm_db_dbi::'+str(self.__class__.__name__)+': '+str(self._message)
  62. class Warning(exception):
  63. """This exception is used to inform the user about important
  64. warnings such as data truncations.
  65. """
  66. def __init__(self, message):
  67. """This is the constructor which take one string argument."""
  68. self._message = message
  69. def __str__(self):
  70. """Converts the message to a string."""
  71. return 'ibm_db_dbi::'+str(self.__class__.__name__)+': '+str(self._message)
  72. class InterfaceError(Error):
  73. """This exception is raised when the module interface is being
  74. used incorrectly.
  75. """
  76. pass
  77. class DatabaseError(Error):
  78. """This exception is raised for errors related to database."""
  79. pass
  80. class InternalError(DatabaseError):
  81. """This exception is raised when internal database error occurs,
  82. such as cursor is not valid anymore.
  83. """
  84. pass
  85. class OperationalError(DatabaseError):
  86. """This exception is raised when database operation errors that are
  87. not under the programmer control occur, such as unexpected
  88. disconnect.
  89. """
  90. pass
  91. class ProgrammingError(DatabaseError):
  92. """This exception is raised for programming errors, such as table
  93. not found.
  94. """
  95. pass
  96. class IntegrityError(DatabaseError):
  97. """This exception is thrown when errors occur when the relational
  98. integrity of database fails, such as foreign key check fails.
  99. """
  100. pass
  101. class DataError(DatabaseError):
  102. """This exception is raised when errors due to data processing,
  103. occur, such as divide by zero.
  104. """
  105. pass
  106. class NotSupportedError(DatabaseError):
  107. """This exception is thrown when a method in this module or an
  108. database API is not supported.
  109. """
  110. pass
  111. def Date(year, month, day):
  112. """This method can be used to get date object from integers, for
  113. inserting it into a DATE column in the database.
  114. """
  115. return datetime.date(year, month, day)
  116. def Time(hour, minute, second):
  117. """This method can be used to get time object from integers, for
  118. inserting it into a TIME column in the database.
  119. """
  120. return datetime.time(hour, minute, second)
  121. def Timestamp(year, month, day, hour, minute, second):
  122. """This method can be used to get timestamp object from integers,
  123. for inserting it into a TIMESTAMP column in the database.
  124. """
  125. return datetime.datetime(year, month, day, hour, minute, second)
  126. def DateFromTicks(ticks):
  127. """This method can be used to get date object from ticks seconds,
  128. for inserting it into a DATE column in the database.
  129. """
  130. time_tuple = time.localtime(ticks)
  131. return datetime.date(time_tuple[0], time_tuple[1], time_tuple[2])
  132. def TimeFromTicks(ticks):
  133. """This method can be used to get time object from ticks seconds,
  134. for inserting it into a TIME column in the database.
  135. """
  136. time_tuple = time.localtime(ticks)
  137. return datetime.time(time_tuple[3], time_tuple[4], time_tuple[5])
  138. def TimestampFromTicks(ticks):
  139. """This method can be used to get timestamp object from ticks
  140. seconds, for inserting it into a TIMESTAMP column in the database.
  141. """
  142. time_tuple = time.localtime(ticks)
  143. return datetime.datetime(time_tuple[0], time_tuple[1], time_tuple[2],
  144. time_tuple[3], time_tuple[4], time_tuple[5])
  145. def Binary(string):
  146. """This method can be used to store binary information, for
  147. inserting it into a binary type column in the database.
  148. """
  149. if not isinstance( string, types.StringType):
  150. raise InterfaceError("Binary function expects type string argument.")
  151. return buffer(string)
  152. class DBAPITypeObject(frozenset):
  153. """Class used for creating objects that can be used to compare
  154. in order to determine the python type to provide in parameter
  155. sequence argument of the execute method.
  156. """
  157. def __new__(cls, col_types):
  158. return frozenset.__new__(cls, col_types)
  159. def __init__(self, col_types):
  160. """Constructor for DBAPITypeObject. It takes a tuple of
  161. database column type as an argument.
  162. """
  163. self.col_types = col_types
  164. def __cmp__(self, cmp):
  165. """This method checks if the string compared with is in the
  166. tuple provided to the constructor of this object. It takes
  167. string as an argument.
  168. """
  169. if cmp in self.col_types:
  170. return 0
  171. if cmp < self.col_types:
  172. return 1
  173. else:
  174. return -1
  175. # The user can use these objects to compare the database column types
  176. # with in order to determine the python type to provide in the
  177. # parameter sequence argument of the execute method.
  178. STRING = DBAPITypeObject(("CHARACTER", "CHAR", "VARCHAR",
  179. "CHARACTER VARYING", "CHAR VARYING", "STRING",))
  180. TEXT = DBAPITypeObject(("CLOB", "CHARACTER LARGE OBJECT", "CHAR LARGE OBJECT",))
  181. XML = DBAPITypeObject(("XML",))
  182. BINARY = DBAPITypeObject(("BLOB", "BINARY LARGE OBJECT",))
  183. NUMBER = DBAPITypeObject(("INTEGER", "INT", "SMALLINT",))
  184. BIGINT = DBAPITypeObject(("BIGINT",))
  185. FLOAT = DBAPITypeObject(("FLOAT", "REAL", "DOUBLE", "DECFLOAT"))
  186. DECIMAL = DBAPITypeObject(("DECIMAL", "DEC", "NUMERIC", "NUM",))
  187. DATE = DBAPITypeObject(("DATE",))
  188. TIME = DBAPITypeObject(("TIME",))
  189. DATETIME = DBAPITypeObject(("TIMESTAMP",))
  190. ROWID = DBAPITypeObject(())
  191. # This method is used to determine the type of error that was
  192. # generated. It takes an exception instance as an argument, and
  193. # returns exception object of the appropriate type.
  194. def _get_exception(inst):
  195. # These tuple are used to determine the type of exceptions that are
  196. # thrown by the database. They store the SQLSTATE code and the
  197. # SQLSTATE class code(the 2 digit prefix of the SQLSTATE code)
  198. warning_error_tuple = ('01', )
  199. data_error_tuple = ('02', '22', '10601', '10603', '10605', '10901', '10902',
  200. '38552', '54')
  201. operational_error_tuple = ( '08', '09', '10502', '10000', '10611', '38501',
  202. '38503', '38553', '38H01', '38H02', '38H03', '38H04',
  203. '38H05', '38H06', '38H07', '38H09', '38H0A')
  204. integrity_error_tuple = ('23', )
  205. internal_error_tuple = ('24', '25', '26', '2D', '51', '57')
  206. programming_error_tuple = ('08002', '07', 'OD', 'OF','OK','ON','10', '27',
  207. '28', '2E', '34', '36', '38', '39', '56', '42',
  208. '3B', '40', '44', '53', '55', '58', '5U', '21')
  209. not_supported_error_tuple = ('0A', '10509')
  210. # These tuple are used to determine the type of exceptions that are
  211. # thrown from the driver module.
  212. interface_exceptions = ( "Supplied parameter is invalid",
  213. "ATTR_CASE attribute must be one of "
  214. "CASE_LOWER, CASE_UPPER, or CASE_NATURAL",
  215. "Connection or statement handle must be passed in.",
  216. "Param is not a tuple")
  217. programming_exceptions = ( "Connection is not active",
  218. "qualifier must be a string",
  219. "unique must be a boolean",
  220. "Parameters not bound",
  221. "owner must be a string",
  222. "table_name must be a string",
  223. "table type must be a string",
  224. "column_name must be a string",
  225. "Column ordinal out of range",
  226. "procedure name must be a string",
  227. "Requested row number must be a positive value",
  228. "Options Array must have string indexes")
  229. database_exceptions = ( "Binding Error",
  230. "Column information cannot be retrieved: ",
  231. "Column binding cannot be done: ",
  232. "Failed to Determine XML Size: ")
  233. statement_exceptions = ( "Statement Execute Failed: ",
  234. "Describe Param Failed: ",
  235. "Sending data failed: ",
  236. "Fetch Failure: ",
  237. "SQLNumResultCols failed: ",
  238. "SQLRowCount failed: ",
  239. "SQLGetDiagField failed: ",
  240. "Statement prepare Failed: ")
  241. operational_exceptions = ( "Connection Resource cannot be found",
  242. "Failed to Allocate Memory",
  243. "Describe Param Failed: ",
  244. "Statement Execute Failed: ",
  245. "Sending data failed: ",
  246. "Failed to Allocate Memory for XML Data",
  247. "Failed to Allocate Memory for LOB Data")
  248. # First check if the exception is from the database. If it is
  249. # determine the SQLSTATE code which is used further to determine
  250. # the exception type. If not check if the exception is thrown by
  251. # by the driver and return the appropriate exception type. If it
  252. # is not possible to determine the type of exception generated
  253. # return the generic Error exception.
  254. if inst is not None:
  255. message = repr(inst)
  256. if message.startswith("Exception('") and message.endswith("',)"):
  257. message = message[11:]
  258. message = message[:len(message)-3]
  259. index = message.find('SQLSTATE=')
  260. if( message != '') & (index != -1):
  261. error_code = message[(index+9):(index+14)]
  262. prefix_code = error_code[:2]
  263. else:
  264. for key in interface_exceptions:
  265. if message.find(key) != -1:
  266. return InterfaceError(message)
  267. for key in programming_exceptions:
  268. if message.find(key) != -1:
  269. return ProgrammingError(message)
  270. for key in operational_exceptions:
  271. if message.find(key) != -1:
  272. return OperationalError(message)
  273. for key in database_exceptions:
  274. if message.find(key) != -1:
  275. return DatabaseError(message)
  276. for key in statement_exceptions:
  277. if message.find(key) != -1:
  278. return DatabaseError(message)
  279. return Error(message)
  280. else:
  281. return Error('An error has occured')
  282. # First check if the SQLSTATE is in the tuples, if not check
  283. # if the SQLSTATE class code is in the tuples to determine the
  284. # exception type.
  285. if ( error_code in warning_error_tuple or
  286. prefix_code in warning_error_tuple ):
  287. return Warning(message)
  288. if ( error_code in data_error_tuple or
  289. prefix_code in data_error_tuple ):
  290. return DataError(message)
  291. if ( error_code in operational_error_tuple or
  292. prefix_code in operational_error_tuple ):
  293. return OperationalError(message)
  294. if ( error_code in integrity_error_tuple or
  295. prefix_code in integrity_error_tuple ):
  296. return IntegrityError(message)
  297. if ( error_code in internal_error_tuple or
  298. prefix_code in internal_error_tuple ):
  299. return InternalError(message)
  300. if ( error_code in programming_error_tuple or
  301. prefix_code in programming_error_tuple ):
  302. return ProgrammingError(message)
  303. if ( error_code in not_supported_error_tuple or
  304. prefix_code in not_supported_error_tuple ):
  305. return NotSupportedError(message)
  306. return DatabaseError(message)
  307. def _server_connect(dsn, user='', password='', host=''):
  308. """This method create connection with server
  309. """
  310. if dsn is None:
  311. raise InterfaceError("dsn value should not be None")
  312. if (not isinstance(dsn, basestring)) | \
  313. (not isinstance(user, basestring)) | \
  314. (not isinstance(password, basestring)) | \
  315. (not isinstance(host, basestring)):
  316. raise InterfaceError("Arguments should be of type string or unicode")
  317. # If the dsn does not contain port and protocal adding database
  318. # and hostname is no good. Add these when required, that is,
  319. # if there is a '=' in the dsn. Else the dsn string is taken to be
  320. # a DSN entry.
  321. if dsn.find('=') != -1:
  322. if dsn[len(dsn) - 1] != ';':
  323. dsn = dsn + ";"
  324. if host != '' and dsn.find('HOSTNAME=') == -1:
  325. dsn = dsn + "HOSTNAME=" + host + ";"
  326. else:
  327. dsn = "DSN=" + dsn + ";"
  328. if dsn.find('attach=') == -1:
  329. dsn = dsn + "attach=true;"
  330. if user != '' and dsn.find('UID=') == -1:
  331. dsn = dsn + "UID=" + user + ";"
  332. if password != '' and dsn.find('PWD=') == -1:
  333. dsn = dsn + "PWD=" + password + ";"
  334. try:
  335. conn = ibm_db.connect(dsn, '', '')
  336. except Exception, inst:
  337. raise _get_exception(inst)
  338. return conn
  339. def createdb(database, dsn, user='', password='', host='', codeset='', mode=''):
  340. """This method creates a database by using the specified database name, code set, and mode
  341. """
  342. if database is None:
  343. raise InterfaceError("createdb expects a not None database name value")
  344. if (not isinstance(database, basestring)) | \
  345. (not isinstance(codeset, basestring)) | \
  346. (not isinstance(mode, basestring)):
  347. raise InterfaceError("Arguments sould be string or unicode")
  348. conn = _server_connect(dsn, user=user, password=password, host=host)
  349. try:
  350. return_value = ibm_db.createdb(conn, database, codeset, mode)
  351. except Exception, inst:
  352. raise _get_exception(inst)
  353. finally:
  354. try:
  355. ibm_db.close(conn)
  356. except Exception, inst:
  357. raise _get_exception(inst)
  358. return return_value
  359. def dropdb(database, dsn, user='', password='', host=''):
  360. """This method drops the specified database
  361. """
  362. if database is None:
  363. raise InterfaceError("dropdb expects a not None database name value")
  364. if (not isinstance(database, basestring)):
  365. raise InterfaceError("Arguments sould be string or unicode")
  366. conn = _server_connect(dsn, user=user, password=password, host=host)
  367. try:
  368. return_value = ibm_db.dropdb(conn, database)
  369. except Exception, inst:
  370. raise _get_exception(inst)
  371. finally:
  372. try:
  373. ibm_db.close(conn)
  374. except Exception, inst:
  375. raise _get_exception(inst)
  376. return return_value
  377. def recreatedb(database, dsn, user='', password='', host='', codeset='', mode=''):
  378. """This method drops and then recreate the database by using the specified database name, code set, and mode
  379. """
  380. if database is None:
  381. raise InterfaceError("recreatedb expects a not None database name value")
  382. if (not isinstance(database, basestring)) | \
  383. (not isinstance(codeset, basestring)) | \
  384. (not isinstance(mode, basestring)):
  385. raise InterfaceError("Arguments sould be string or unicode")
  386. conn = _server_connect(dsn, user=user, password=password, host=host)
  387. try:
  388. return_value = ibm_db.recreatedb(conn, database, codeset, mode)
  389. except Exception, inst:
  390. raise _get_exception(inst)
  391. finally:
  392. try:
  393. ibm_db.close(conn)
  394. except Exception, inst:
  395. raise _get_exception(inst)
  396. return return_value
  397. def createdbNX(database, dsn, user='', password='', host='', codeset='', mode=''):
  398. """This method creates a database if it not exist by using the specified database name, code set, and mode
  399. """
  400. if database is None:
  401. raise InterfaceError("createdbNX expects a not None database name value")
  402. if (not isinstance(database, basestring)) | \
  403. (not isinstance(codeset, basestring)) | \
  404. (not isinstance(mode, basestring)):
  405. raise InterfaceError("Arguments sould be string or unicode")
  406. conn = _server_connect(dsn, user=user, password=password, host=host)
  407. try:
  408. return_value = ibm_db.createdbNX(conn, database, codeset, mode)
  409. except Exception, inst:
  410. raise _get_exception(inst)
  411. finally:
  412. try:
  413. ibm_db.close(conn)
  414. except Exception, inst:
  415. raise _get_exception(inst)
  416. return return_value
  417. def connect(dsn, user='', password='', host='', database='', conn_options=None):
  418. """This method creates a non persistent connection to the database. It returns
  419. a ibm_db_dbi.Connection object.
  420. """
  421. if dsn is None:
  422. raise InterfaceError("connect expects a not None dsn value")
  423. if (not isinstance(dsn, basestring)) | \
  424. (not isinstance(user, basestring)) | \
  425. (not isinstance(password, basestring)) | \
  426. (not isinstance(host, basestring)) | \
  427. (not isinstance(database, basestring)):
  428. raise InterfaceError("connect expects the first five arguments to"
  429. " be of type string or unicode")
  430. if conn_options is not None:
  431. if not isinstance(conn_options, dict):
  432. raise InterfaceError("connect expects the sixth argument"
  433. " (conn_options) to be of type dict")
  434. if not SQL_ATTR_AUTOCOMMIT in conn_options:
  435. conn_options[SQL_ATTR_AUTOCOMMIT] = SQL_AUTOCOMMIT_OFF
  436. else:
  437. conn_options = {SQL_ATTR_AUTOCOMMIT : SQL_AUTOCOMMIT_OFF}
  438. # If the dsn does not contain port and protocal adding database
  439. # and hostname is no good. Add these when required, that is,
  440. # if there is a '=' in the dsn. Else the dsn string is taken to be
  441. # a DSN entry.
  442. if dsn.find('=') != -1:
  443. if dsn[len(dsn) - 1] != ';':
  444. dsn = dsn + ";"
  445. if database != '' and dsn.find('DATABASE=') == -1:
  446. dsn = dsn + "DATABASE=" + database + ";"
  447. if host != '' and dsn.find('HOSTNAME=') == -1:
  448. dsn = dsn + "HOSTNAME=" + host + ";"
  449. else:
  450. dsn = "DSN=" + dsn + ";"
  451. if user != '' and dsn.find('UID=') == -1:
  452. dsn = dsn + "UID=" + user + ";"
  453. if password != '' and dsn.find('PWD=') == -1:
  454. dsn = dsn + "PWD=" + password + ";"
  455. try:
  456. conn = ibm_db.connect(dsn, '', '', conn_options)
  457. ibm_db.set_option(conn, {SQL_ATTR_CURRENT_SCHEMA : user}, 1)
  458. except Exception, inst:
  459. raise _get_exception(inst)
  460. return Connection(conn)
  461. def pconnect(dsn, user='', password='', host='', database='', conn_options=None):
  462. """This method creates persistent connection to the database. It returns
  463. a ibm_db_dbi.Connection object.
  464. """
  465. if dsn is None:
  466. raise InterfaceError("connect expects a not None dsn value")
  467. if (not isinstance(dsn, basestring)) | \
  468. (not isinstance(user, basestring)) | \
  469. (not isinstance(password, basestring)) | \
  470. (not isinstance(host, basestring)) | \
  471. (not isinstance(database, basestring)):
  472. raise InterfaceError("connect expects the first five arguments to"
  473. " be of type string or unicode")
  474. if conn_options is not None:
  475. if not isinstance(conn_options, dict):
  476. raise InterfaceError("connect expects the sixth argument"
  477. " (conn_options) to be of type dict")
  478. if not SQL_ATTR_AUTOCOMMIT in conn_options:
  479. conn_options[SQL_ATTR_AUTOCOMMIT] = SQL_AUTOCOMMIT_OFF
  480. else:
  481. conn_options = {SQL_ATTR_AUTOCOMMIT : SQL_AUTOCOMMIT_OFF}
  482. # If the dsn does not contain port and protocal adding database
  483. # and hostname is no good. Add these when required, that is,
  484. # if there is a '=' in the dsn. Else the dsn string is taken to be
  485. # a DSN entry.
  486. if dsn.find('=') != -1:
  487. if dsn[len(dsn) - 1] != ';':
  488. dsn = dsn + ";"
  489. if database != '' and dsn.find('DATABASE=') == -1:
  490. dsn = dsn + "DATABASE=" + database + ";"
  491. if host != '' and dsn.find('HOSTNAME=') == -1:
  492. dsn = dsn + "HOSTNAME=" + host + ";"
  493. else:
  494. dsn = "DSN=" + dsn + ";"
  495. if user != '' and dsn.find('UID=') == -1:
  496. dsn = dsn + "UID=" + user + ";"
  497. if password != '' and dsn.find('PWD=') == -1:
  498. dsn = dsn + "PWD=" + password + ";"
  499. try:
  500. conn = ibm_db.pconnect(dsn, '', '', conn_options)
  501. ibm_db.set_option(conn, {SQL_ATTR_CURRENT_SCHEMA : user}, 1)
  502. except Exception, inst:
  503. raise _get_exception(inst)
  504. return Connection(conn)
  505. class Connection(object):
  506. """This class object represents a connection between the database
  507. and the application.
  508. """
  509. def __init__(self, conn_handler):
  510. """Constructor for Connection object. It takes ibm_db
  511. connection handler as an argument.
  512. """
  513. self.conn_handler = conn_handler
  514. # Used to identify close cursors for generating exceptions
  515. # after the connection is closed.
  516. self._cursor_list = []
  517. self.__dbms_name = ibm_db.get_db_info(conn_handler, SQL_DBMS_NAME)
  518. self.__dbms_ver = ibm_db.get_db_info(conn_handler, SQL_DBMS_VER)
  519. # This method is used to get the DBMS_NAME
  520. def __get_dbms_name( self ):
  521. return self.__dbms_name
  522. # This attribute specifies the DBMS_NAME
  523. # It is a read only attribute.
  524. dbms_name = property(__get_dbms_name, None, None, "")
  525. # This method is used to get the DBMS_ver
  526. def __get_dbms_ver( self ):
  527. return self.__dbms_ver
  528. # This attribute specifies the DBMS_ver
  529. # It is a read only attribute.
  530. dbms_ver = property(__get_dbms_ver, None, None, "")
  531. def close(self):
  532. """This method closes the Database connection associated with
  533. the Connection object. It takes no arguments.
  534. """
  535. self.rollback()
  536. try:
  537. if self.conn_handler is None:
  538. raise ProgrammingError("Connection cannot be closed; "
  539. "connection is no longer active.")
  540. else:
  541. return_value = ibm_db.close(self.conn_handler)
  542. except Exception, inst:
  543. raise _get_exception(inst)
  544. self.conn_handler = None
  545. for index in range(len(self._cursor_list)):
  546. self._cursor_list[index].conn_handler = None
  547. self._cursor_list[index].stmt_handler = None
  548. self._cursor_list[index]._all_stmt_handlers = None
  549. return return_value
  550. def commit(self):
  551. """This method commits the transaction associated with the
  552. Connection object. It takes no arguments.
  553. """
  554. try:
  555. return_value = ibm_db.commit(self.conn_handler)
  556. except Exception, inst:
  557. raise _get_exception(inst)
  558. return return_value
  559. def rollback(self):
  560. """This method rollbacks the transaction associated with the
  561. Connection object. It takes no arguments.
  562. """
  563. try:
  564. return_value = ibm_db.rollback(self.conn_handler)
  565. except Exception, inst:
  566. raise _get_exception(inst)
  567. return return_value
  568. def cursor(self):
  569. """This method returns a Cursor object associated with the
  570. Connection. It takes no arguments.
  571. """
  572. if self.conn_handler is None:
  573. raise ProgrammingError("Cursor cannot be returned; "
  574. "connection is no longer active.")
  575. cursor = Cursor(self.conn_handler, self)
  576. self._cursor_list.append(cursor)
  577. return cursor
  578. # Sets connection attribute values
  579. def set_option(self, attr_dict):
  580. """Input: connection attribute dictionary
  581. Return: True on success or False on failure
  582. """
  583. return ibm_db.set_option(self.conn_handler, attr_dict, 1)
  584. # Retrieves connection attributes values
  585. def get_option(self, attr_key):
  586. """Input: connection attribute key
  587. Return: current setting of the resource attribute requested
  588. """
  589. return ibm_db.get_option(self.conn_handler, attr_key, 1)
  590. # Sets connection AUTOCOMMIT attribute
  591. def set_autocommit(self, is_on):
  592. """Input: connection attribute: true if AUTOCOMMIT ON, false otherwise (i.e. OFF)
  593. Return: True on success or False on failure
  594. """
  595. try:
  596. if is_on:
  597. is_set = ibm_db.set_option(self.conn_handler, {SQL_ATTR_AUTOCOMMIT : SQL_AUTOCOMMIT_ON}, 1)
  598. else:
  599. is_set = ibm_db.set_option(self.conn_handler, {SQL_ATTR_AUTOCOMMIT : SQL_AUTOCOMMIT_OFF}, 1)
  600. except Exception, inst:
  601. raise _get_exception(inst)
  602. return is_set
  603. # Sets connection attribute values
  604. def set_current_schema(self, schema_name):
  605. """Input: connection attribute dictionary
  606. Return: True on success or False on failure
  607. """
  608. self.current_schema = schema_name
  609. try:
  610. is_set = ibm_db.set_option(self.conn_handler, {SQL_ATTR_CURRENT_SCHEMA : schema_name}, 1)
  611. except Exception, inst:
  612. raise _get_exception(inst)
  613. return is_set
  614. # Retrieves connection attributes values
  615. def get_current_schema(self):
  616. """Return: current setting of the schema attribute
  617. """
  618. try:
  619. conn_schema = ibm_db.get_option(self.conn_handler, SQL_ATTR_CURRENT_SCHEMA, 1)
  620. if conn_schema is not None and conn_schema != '':
  621. self.current_schema = conn_schema
  622. except Exception, inst:
  623. raise _get_exception(inst)
  624. return self.current_schema
  625. # Retrieves the IBM Data Server version for a given Connection object
  626. def server_info(self):
  627. """Return: tuple (DBMS_NAME, DBMS_VER)
  628. """
  629. try:
  630. server_info = []
  631. server_info.append(self.dbms_name)
  632. server_info.append(self.dbms_ver)
  633. except Exception, inst:
  634. raise _get_exception(inst)
  635. return tuple(server_info)
  636. def set_case(self, server_type, str_value):
  637. return str_value.upper()
  638. # Retrieves the tables for a specified schema (and/or given table name)
  639. def tables(self, schema_name=None, table_name=None):
  640. """Input: connection - ibm_db.IBM_DBConnection object
  641. Return: sequence of table metadata dicts for the specified schema
  642. """
  643. result = []
  644. if schema_name is not None:
  645. schema_name = self.set_case("DB2_LUW", schema_name)
  646. if table_name is not None:
  647. table_name = self.set_case("DB2_LUW", table_name)
  648. try:
  649. stmt = ibm_db.tables(self.conn_handler, None, schema_name, table_name)
  650. row = ibm_db.fetch_assoc(stmt)
  651. i = 0
  652. while (row):
  653. result.append( row )
  654. i += 1
  655. row = ibm_db.fetch_assoc(stmt)
  656. ibm_db.free_result(stmt)
  657. except Exception, inst:
  658. raise _get_exception(inst)
  659. return result
  660. # Retrieves metadata pertaining to index for specified schema (and/or table name)
  661. def indexes(self, unique=True, schema_name=None, table_name=None):
  662. """Input: connection - ibm_db.IBM_DBConnection object
  663. Return: sequence of index metadata dicts for the specified table
  664. Example:
  665. Index metadata retrieved from schema 'PYTHONIC.TEST_TABLE' table
  666. {
  667. 'TABLE_SCHEM': 'PYTHONIC', 'TABLE_CAT': None,
  668. 'TABLE_NAME': 'ENGINE_USERS', 'PAGES': None,
  669. 'COLUMN_NAME': 'USER_ID' 'FILTER_CONDITION': None,
  670. 'INDEX_NAME': 'SQL071201150750170', 'CARDINALITY': None,
  671. 'ORDINAL_POSITION': 1, 'INDEX_QUALIFIER': 'SYSIBM',
  672. 'TYPE': 3,
  673. 'NON_UNIQUE': 0,
  674. 'ASC_OR_DESC': 'A'
  675. }
  676. """
  677. result = []
  678. if schema_name is not None:
  679. schema_name = self.set_case("DB2_LUW", schema_name)
  680. if table_name is not None:
  681. table_name = self.set_case("DB2_LUW", table_name)
  682. try:
  683. stmt = ibm_db.statistics(self.conn_handler, None, schema_name, table_name, unique)
  684. row = ibm_db.fetch_assoc(stmt)
  685. i = 0
  686. while (row):
  687. if row['TYPE'] == SQL_INDEX_OTHER:
  688. result.append( row )
  689. i += 1
  690. row = ibm_db.fetch_assoc(stmt)
  691. ibm_db.free_result(stmt)
  692. except Exception, inst:
  693. raise _get_exception(inst)
  694. return result
  695. # Retrieves metadata pertaining to primary keys for specified schema (and/or table name)
  696. def primary_keys(self, unique=True, schema_name=None, table_name=None):
  697. """Input: connection - ibm_db.IBM_DBConnection object
  698. Return: sequence of PK metadata dicts for the specified table
  699. Example:
  700. PK metadata retrieved from 'PYTHONIC.ORDERS' table
  701. {
  702. 'TABLE_SCHEM': 'PYTHONIC', 'TABLE_CAT': None,
  703. 'TABLE_NAME': 'ORDERS',
  704. 'COLUMN_NAME': 'ORDER_ID'
  705. 'PK_NAME': 'SQL071128122038680',
  706. 'KEY_SEQ': 1
  707. }
  708. """
  709. result = []
  710. if schema_name is not None:
  711. schema_name = self.set_case("DB2_LUW", schema_name)
  712. if table_name is not None:
  713. table_name = self.set_case("DB2_LUW", table_name)
  714. try:
  715. stmt = ibm_db.primary_keys(self.conn_handler, None, schema_name, table_name)
  716. row = ibm_db.fetch_assoc(stmt)
  717. i = 0
  718. while (row):
  719. result.append( row )
  720. i += 1
  721. row = ibm_db.fetch_assoc(stmt)
  722. ibm_db.free_result(stmt)
  723. except Exception, inst:
  724. raise _get_exception(inst)
  725. return result
  726. # Retrieves metadata pertaining to foreign keys for specified schema (and/or table name)
  727. def foreign_keys(self, unique=True, schema_name=None, table_name=None):
  728. """Input: connection - ibm_db.IBM_DBConnection object
  729. Return: sequence of FK metadata dicts for the specified table
  730. Example:
  731. FK metadata retrieved from 'PYTHONIC.ENGINE_EMAIL_ADDRESSES' table
  732. {
  733. 'PKTABLE_SCHEM': 'PYTHONIC', 'PKTABLE_CAT': None,
  734. 'PKTABLE_NAME': 'ENGINE_USERS', 'FKTABLE_CAT': None,
  735. 'PKCOLUMN_NAME': 'USER_ID', 'UPDATE_RULE': 3,
  736. 'PK_NAME': 'SQL071205090958680', 'DELETE_RULE': 3
  737. 'KEY_SEQ': 1, 'DEFERRABILITY': 7,
  738. 'FK_NAME': 'SQL071205091000160',
  739. 'FKCOLUMN_NAME': 'REMOTE_USER_ID',
  740. 'FKTABLE_NAME': 'ENGINE_EMAIL_ADDRESSES',
  741. 'FKTABLE_SCHEM': 'PYTHONIC'
  742. }
  743. """
  744. result = []
  745. if schema_name is not None:
  746. schema_name = self.set_case("DB2_LUW", schema_name)
  747. if table_name is not None:
  748. table_name = self.set_case("DB2_LUW", table_name)
  749. try:
  750. stmt = ibm_db.foreign_keys(self.conn_handler, None, None, None, None, schema_name, table_name)
  751. row = ibm_db.fetch_assoc(stmt)
  752. i = 0
  753. while (row):
  754. result.append( row )
  755. i += 1
  756. row = ibm_db.fetch_assoc(stmt)
  757. ibm_db.free_result(stmt)
  758. except Exception, inst:
  759. raise _get_exception(inst)
  760. return result
  761. # Retrieves the columns for a specified schema (and/or table name and column name)
  762. def columns(self, schema_name=None, table_name=None, column_names=None):
  763. """Input: connection - ibm_db.IBM_DBConnection object
  764. Return: sequence of column metadata dicts for the specified schema
  765. Example:
  766. Column metadata retrieved from schema 'PYTHONIC.FOO' table, column 'A'
  767. {
  768. 'TABLE_NAME': 'FOO', 'NULLABLE': 1,
  769. 'ORDINAL_POSITION': 2L, 'REMARKS': None,
  770. 'COLUMN_NAME': 'A', 'BUFFER_LENGTH': 30L,
  771. 'TYPE_NAME': 'VARCHAR', 'SQL_DATETIME_SUB': None,
  772. 'COLUMN_DEF': None, 'DATA_TYPE': 12,
  773. 'IS_NULLABLE': 'YES', 'SQL_DATA_TYPE': 12,
  774. 'COLUMN_SIZE': 30L, 'TABLE_CAT': None,
  775. 'CHAR_OCTET_LENGTH': 30L, 'TABLE_SCHEM': 'PYTHONIC',
  776. 'NUM_PREC_RADIX': None,
  777. 'DECIMAL_DIGITS': None
  778. }
  779. """
  780. result = []
  781. if schema_name is not None:
  782. schema_name = self.set_case("DB2_LUW", schema_name)
  783. if table_name is not None:
  784. table_name = self.set_case("DB2_LUW", table_name)
  785. try:
  786. stmt = ibm_db.columns(self.conn_handler, None, schema_name, table_name)
  787. row = ibm_db.fetch_assoc(stmt)
  788. i = 0
  789. while (row):
  790. result.append( row )
  791. i += 1
  792. row = ibm_db.fetch_assoc(stmt)
  793. ibm_db.free_result(stmt)
  794. col_names_lower = []
  795. if column_names is not None:
  796. for name in column_names:
  797. col_names_lower.append(name.lower())
  798. include_columns = []
  799. if column_names and column_names != '':
  800. for column in result:
  801. if column['COLUMN_NAME'].lower() in col_names_lower:
  802. column['COLUMN_NAME'] = column['COLUMN_NAME'].lower()
  803. include_columns.append(column)
  804. result = include_columns
  805. except Exception, inst:
  806. raise _get_exception(inst)
  807. return result
  808. # Defines a cursor for the driver connection
  809. class Cursor(object):
  810. """This class represents a cursor of the connection. It can be
  811. used to process an SQL statement.
  812. """
  813. # This method is used to get the description attribute.
  814. def __get_description(self):
  815. """ If this method has already been called, after executing a select statement,
  816. return the stored information in the self.__description.
  817. """
  818. if self.__description is not None:
  819. return self.__description
  820. if self.stmt_handler is None:
  821. return None
  822. self.__description = []
  823. try:
  824. num_columns = ibm_db.num_fields(self.stmt_handler)
  825. """ If the execute statement did not produce a result set return None.
  826. """
  827. if num_columns == False:
  828. self.__description = None
  829. return None
  830. for column_index in range(num_columns):
  831. column_desc = []
  832. column_desc.append(ibm_db.field_name(self.stmt_handler,
  833. column_index))
  834. type = ibm_db.field_type(self.stmt_handler, column_index)
  835. type = type.upper()
  836. if STRING.__cmp__(type) == 0:
  837. column_desc.append(STRING)
  838. elif TEXT.__cmp__(type) == 0:
  839. column_desc.append(TEXT)
  840. elif XML.__cmp__(type) == 0:
  841. column_desc.append(XML)
  842. elif BINARY.__cmp__(type) == 0:
  843. column_desc.append(BINARY)
  844. elif NUMBER.__cmp__(type) == 0:
  845. column_desc.append(NUMBER)
  846. elif BIGINT.__cmp__(type) == 0:
  847. column_desc.append(BIGINT)
  848. elif FLOAT.__cmp__(type) == 0:
  849. column_desc.append(FLOAT)
  850. elif DECIMAL.__cmp__(type) == 0:
  851. column_desc.append(DECIMAL)
  852. elif DATE.__cmp__(type) == 0:
  853. column_desc.append(DATE)
  854. elif TIME.__cmp__(type) == 0:
  855. column_desc.append(TIME)
  856. elif DATETIME.__cmp__(type) == 0:
  857. column_desc.append(DATETIME)
  858. elif ROWID.__cmp__(type) == 0:
  859. column_desc.append(ROWID)
  860. column_desc.append(ibm_db.field_display_size(
  861. self.stmt_handler, column_index))
  862. column_desc.append(ibm_db.field_display_size(
  863. self.stmt_handler, column_index))
  864. column_desc.append(ibm_db.field_precision(
  865. self.stmt_handler, column_index))
  866. column_desc.append(ibm_db.field_scale(self.stmt_handler,
  867. column_index))
  868. column_desc.append(None)
  869. self.__description.append(column_desc)
  870. except Exception, inst:
  871. self.messages.append(_get_exception(inst))
  872. raise self.messages[len(self.messages) - 1]
  873. return self.__description
  874. # This attribute provides the metadata information of the columns
  875. # in the result set produced by the last execute function. It is
  876. # a read only attribute.
  877. description = property(fget = __get_description)
  878. # This method is used to get the rowcount attribute.
  879. def __get_rowcount( self ):
  880. return self.__rowcount
  881. # This attribute specifies the number of rows the last executeXXX()
  882. # produced or affected. It is a read only attribute.
  883. rowcount = property(__get_rowcount, None, None, "")
  884. # This method is used to get the Connection object
  885. def __get_connection( self ):
  886. return self.__connection
  887. # This attribute specifies the connection object.
  888. # It is a read only attribute.
  889. connection = property(__get_connection, None, None, "")
  890. def __init__(self, conn_handler, conn_object=None):
  891. """Constructor for Cursor object. It takes ibm_db connection
  892. handler as an argument.
  893. """
  894. # This attribute is used to determine the fetch size for fetchmany
  895. # operation. It is a read/write attribute
  896. self.arraysize = 1
  897. self.__rowcount = -1
  898. self._result_set_produced = False
  899. self.__description = None
  900. self.conn_handler = conn_handler
  901. self.stmt_handler = None
  902. self._is_scrollable_cursor = False
  903. self.__connection = conn_object
  904. self.messages = []
  905. # This method closes the statemente associated with the cursor object.
  906. # It takes no argument.
  907. def close(self):
  908. """This method closes the cursor object. After this method is
  909. called the cursor object is no longer usable. It takes no
  910. arguments.
  911. """
  912. messages = []
  913. if self.conn_handler is None:
  914. self.messages.append(ProgrammingError("Cursor cannot be closed; connection is no longer active."))
  915. raise self.messages[len(self.messages) - 1]
  916. try:
  917. return_value = ibm_db.free_stmt(self.stmt_handler)
  918. except Exception, inst:
  919. self.messages.append(_get_exception(inst))
  920. raise self.messages[len(self.messages) - 1]
  921. self.stmt_handler = None
  922. self.conn_handler = None
  923. self._all_stmt_handlers = None
  924. if self.__connection is not None:
  925. try:
  926. self.__connection._cursor_list.remove(self)
  927. except:
  928. pass
  929. return return_value
  930. # helper for calling procedure
  931. def _callproc_helper(self, procname, parameters=None):
  932. if parameters is not None:
  933. buff = []
  934. CONVERT_STR = (datetime.datetime, datetime.date, datetime.time, buffer)
  935. # Convert date/time and binary objects to string for
  936. # inserting into the database.
  937. for param in parameters:
  938. if isinstance(param, CONVERT_STR):
  939. param = str(param)
  940. buff.append(param)
  941. parameters = tuple(buff)
  942. try:
  943. result = ibm_db.callproc(self.conn_handler, procname,parameters)
  944. except Exception, inst:
  945. self.messages.append(_get_exception(inst))
  946. raise self.messages[len(self.messages) - 1]
  947. else:
  948. try:
  949. result = ibm_db.callproc(self.conn_handler, procname)
  950. except Exception, inst:
  951. self.messages.append(_get_exception(inst))
  952. raise self.messages[len(self.messages) - 1]
  953. return result
  954. def callproc(self, procname, parameters=None):
  955. """This method can be used to execute a stored procedure.
  956. It takes the name of the stored procedure and the parameters to
  957. the stored procedure as arguments.
  958. """
  959. self.messages = []
  960. if not isinstance(procname, basestring):
  961. self.messages.append(InterfaceError("callproc expects the first argument to be of type String or Unicode."))
  962. raise self.messages[len(self.messages) - 1]
  963. if parameters is not None:
  964. if not isinstance(parameters, (types.ListType, types.TupleType)):
  965. self.messages.append(InterfaceError("callproc expects the second argument to be of type list or tuple."))
  966. raise self.messages[len(self.messages) - 1]
  967. result = self._callproc_helper(procname, parameters)
  968. return_value = None
  969. self.__description = None
  970. self._all_stmt_handlers = []
  971. if isinstance(result, types.TupleType):
  972. self.stmt_handler = result[0]
  973. return_value = result[1:]
  974. else:
  975. self.stmt_handler = result
  976. self._result_set_produced = True
  977. return return_value
  978. # Helper for preparing an SQL statement.
  979. def _prepare_helper(self, operation, parameters=None):
  980. try:
  981. ibm_db.free_stmt(self.stmt_handler)
  982. except:
  983. pass
  984. try:
  985. self.stmt_handler = ibm_db.prepare(self.conn_handler, operation)
  986. except Exception, inst:
  987. self.messages.append(_get_exception(inst))
  988. raise self.messages[len(self.messages) - 1]
  989. # Helper for preparing an SQL statement.
  990. def _set_cursor_helper(self):
  991. if (ibm_db.get_option(self.stmt_handler, ibm_db.SQL_ATTR_CURSOR_TYPE, 0) != ibm_db.SQL_CURSOR_FORWARD_ONLY):
  992. self._is_scrollable_cursor = True
  993. else:
  994. self._is_scrollable_cursor = False
  995. self._result_set_produced = False
  996. try:
  997. num_columns = ibm_db.num_fields(self.stmt_handler)
  998. except Exception, inst:
  999. self.messages.append(_get_exception(inst))
  1000. raise self.messages[len(self.messages) - 1]
  1001. if not num_columns:
  1002. return True
  1003. self._result_set_produced = True
  1004. return True
  1005. # Helper for executing an SQL statement.
  1006. def _execute_helper(self, parameters=None):
  1007. if parameters is not None:
  1008. buff = []
  1009. CONVERT_STR = (datetime.datetime, datetime.date, datetime.time, buffer)
  1010. # Convert date/time and binary objects to string for
  1011. # inserting into the database.
  1012. for param in parameters:
  1013. if isinstance(param, CONVERT_STR):
  1014. param = str(param)
  1015. buff.append(param)
  1016. parameters = tuple(buff)
  1017. try:
  1018. return_value = ibm_db.execute(self.stmt_handler, parameters)
  1019. if not return_value:
  1020. if ibm_db.conn_errormsg() is not None:
  1021. self.messages.append(Error(str(ibm_db.conn_errormsg())))
  1022. raise self.messages[len(self.messages) - 1]
  1023. if ibm_db.stmt_errormsg() is not None:
  1024. self.messages.append(Error(str(ibm_db.stmt_errormsg())))
  1025. raise self.messages[len(self.messages) - 1]
  1026. except Exception, inst:
  1027. self.messages.append(_get_exception(inst))
  1028. raise self.messages[len(self.messages) - 1]
  1029. else:
  1030. try:
  1031. return_value = ibm_db.execute(self.stmt_handler)
  1032. if not return_value:
  1033. if ibm_db.conn_errormsg() is not None:
  1034. self.messages.append(Error(str(ibm_db.conn_errormsg())))
  1035. raise self.messages[len(self.messages) - 1]
  1036. if ibm_db.stmt_errormsg() is not None:
  1037. self.messages.append(Error(str(ibm_db.stmt_errormsg())))
  1038. raise self.messages[len(self.messages) - 1]
  1039. except Exception, inst:
  1040. self.messages.append(_get_exception(inst))
  1041. raise self.messages[len(self.messages) - 1]
  1042. return return_value
  1043. # This method is used to set the rowcount after executing an SQL
  1044. # statement.
  1045. def _set_rowcount(self):
  1046. self.__rowcount = -1
  1047. if not self._result_set_produced:
  1048. try:
  1049. counter = ibm_db.num_rows(self.stmt_handler)
  1050. except Exception, inst:
  1051. self.messages.append(_get_exception(inst))
  1052. raise self.messages[len(self.messages) - 1]
  1053. self.__rowcount = counter
  1054. elif self._is_scrollable_cursor:
  1055. try:
  1056. counter = ibm_db.get_num_result(self.stmt_handler)
  1057. except Exception, inst:
  1058. self.messages.append(_get_exception(inst))
  1059. raise self.messages[len(self.messages) - 1]
  1060. if counter >= 0:
  1061. self.__rowcount = counter
  1062. return True
  1063. # Retrieves the last generated identity value from the DB2 catalog
  1064. def _get_last_identity_val(self):
  1065. """
  1066. The result of the IDENTITY_VAL_LOCAL function is not affected by the following:
  1067. - A single row INSERT statement with a VALUES clause for a table without an
  1068. identity column
  1069. - A multiple row INSERT statement with a VALUES clause
  1070. - An INSERT statement with a fullselect
  1071. """
  1072. operat

Large files files are truncated, but you can click here to view the full file