~s7v7nislands/myconnpy/speed

« back to all changes in this revision

Viewing changes to mysql/connector/mysql.py

  • Committer: Geert Vanderkelen
  • Date: 2010-01-22 15:50:00 UTC
  • Revision ID: geert.vanderkelen@sun.com-20100122155000-tg6ppo1b4rvmz4pn
Implementing Raise-On-Warnings

* It is now possible to raise warnings reported by MySQL as excpetions in
Python. This is done by setting raise_on_warnings to True when connecting.
  connect(raise_on_warnings=True,..)
  or it can be toggled by the application:
   db.set_warnings(raise_on_warnings)
* Cursor has some changes and _get_warnings was removed. It now uses self.db
to get the information. A non-bufering cursor (default) will raise the
exceptions on warnigns after result is read; buffered cursors will raise when
the SELECT statement is executed.
* Test cases have been updated.
* Some test cases have been fixed in relation to BINARY fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
        self.info_msg = ''
63
63
        self.use_unicode = True
64
64
        self.get_warnings = False
 
65
        self.raise_on_warnings = False
65
66
        self.autocommit = False
66
67
        self.connection_timeout = None
67
68
        self.buffered = False
234
235
            self.set_charset_info(info=info)
235
236
            self.converter.set_charset(self.charset_name)
236
237
 
237
 
    def set_getwarnings(self, bool):
238
 
        """
 
238
    def set_warnings(self, fetch=False, raise_on_warnings=False):
 
239
        """Set how to handle warnings coming from MySQL
 
240
        
239
241
        Set wheter we should get warnings whenever an operation produced some.
 
242
        If you set raise_on_warnings to True, any warning will be raised
 
243
        as a DataError exception.
240
244
        """
241
 
        self.get_warnings = bool
 
245
        if raise_on_warnings is True:
 
246
            self.get_warnings = True
 
247
            self.raise_on_warnings = True
 
248
        else:
 
249
            self.get_warnings = fetch
 
250
            self.raise_on_warnings = False
242
251
    
243
252
    def set_autocommit(self, switch):
244
253
        """
307
316
        self.connect(*args, **kwargs)
308
317
            
309
318
    def connect(self, dsn='', user='', password='', host='127.0.0.1',
310
 
            port=3306, db=None, database=None, use_unicode=True, charset='utf8', get_warnings=False,
 
319
            port=3306, db=None, database=None, use_unicode=True, charset='utf8',
 
320
            get_warnings=False, raise_on_warnings=False,
311
321
            autocommit=False, unix_socket=None,
312
322
            connection_timeout=None, client_flags=0, buffered=False):
313
323
        """
356
366
            enable it though, or use strict mode in MySQL to make most of these
357
367
            warnings errors.
358
368
            Default: False
 
369
        
 
370
        raise_on_warnings
 
371
            If set to True, warnings will be raised as exceptions. raise_on_warings
 
372
            overrides get_warnings.
 
373
            Default: False
359
374
 
360
375
        autocommit
361
376
            Auto commit is OFF by default, which is required by the Python Db API
385
400
        self.set_host(host)
386
401
        self.set_port(port)
387
402
        self.set_database(database)
388
 
        self.set_getwarnings(get_warnings)
 
403
        self.set_warnings(get_warnings,raise_on_warnings)
389
404
        self.set_unixsocket(unix_socket)
390
405
        self.set_connection_timeout(connection_timeout)
391
406
        self.set_buffered(buffered)