Home | Trees | Indices | Help |
---|
|
1 # $Id: a384faeed9e9e609ca4b056c682c8ecda51f9e64 $ 2 3 """ 4 MySQL extended database driver. 5 """ 6 7 __docformat__ = "restructuredtext en" 8 9 # --------------------------------------------------------------------------- 10 # Imports 11 # --------------------------------------------------------------------------- 12 13 import os 14 import sys 15 import re 16 17 from grizzled.db.base import (DBDriver, Error, Warning, TableMetadata, 18 IndexMetadata, RDBMSMetadata) 19 20 # --------------------------------------------------------------------------- 21 # Constants 22 # --------------------------------------------------------------------------- 23 24 VENDOR = 'MySQL AB' 25 PRODUCT = 'MySQL' 26 27 # --------------------------------------------------------------------------- 28 # Classes 29 # --------------------------------------------------------------------------- 3032 """DB Driver for MySQL, using the MySQLdb DB API module.""" 33 34 TYPE_RE = re.compile('([a-z]+)(\([0-9]+\))?') 35 39 4214143 - def do_connect(self, 44 host="localhost", 45 port=None, 46 user="sa", 47 password="", 48 database="default"):49 dbi = self.get_import() 50 return dbi.connect(host=host, user=user, passwd=password, db=database)5153 cursor.execute('SELECT version()') 54 rs = cursor.fetchone() 55 if rs is None: 56 result = RDBMSMetadata(VENDOR, PRODUCT, 'unknown') 57 else: 58 result = RDBMSMetadata(VENDOR, PRODUCT, rs[0]) 59 60 return result6163 self._ensure_valid_table(cursor, table) 64 dbi = self.get_import() 65 cursor.execute('DESC %s' % table) 66 rs = cursor.fetchone() 67 results = [] 68 while rs is not None: 69 column = rs[0] 70 coltype = rs[1] 71 null = False if rs[2] == 'NO' else True 72 73 match = self.TYPE_RE.match(coltype) 74 if match: 75 coltype = match.group(1) 76 size = match.group(2) 77 if size: 78 size = size[1:-1] 79 if coltype in ['varchar', 'char']: 80 max_char_size = size 81 precision = None 82 else: 83 max_char_size = None 84 precision = size 85 86 data = TableMetadata(column, 87 coltype, 88 max_char_size, 89 precision, 90 0, 91 null) 92 results += [data] 93 rs = cursor.fetchone() 94 95 return results9698 self._ensure_valid_table(cursor, table) 99 dbi = self.get_import() 100 cursor.execute('SHOW INDEX FROM %s' % table) 101 rs = cursor.fetchone() 102 result = [] 103 columns = {} 104 descr = {} 105 while rs is not None: 106 name = rs[2] 107 try: 108 columns[name] 109 except KeyError: 110 columns[name] = [] 111 112 columns[name] += [rs[4]] 113 114 # Column 1 is a "non-unique" flag. 115 116 if (not rs[1]) or (name.lower() == 'primary'): 117 description = 'Unique' 118 else: 119 description = 'Non-unique' 120 if rs[10] is not None: 121 description += ', %s index' % rs[10] 122 descr[name] = description 123 rs = cursor.fetchone() 124 125 names = columns.keys() 126 names.sort() 127 for name in names: 128 result += [IndexMetadata(name, columns[name], descr[name])] 129 130 return result131
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Mon Mar 14 15:21:07 2016 | http://epydoc.sourceforge.net |