def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) cursor.execute("select column_name, data_type, data_length, " "data_precision, data_scale, nullable, " "char_col_decl_length from all_tab_columns " "where lower(table_name) = '%s'" % table.lower()) results = [] rs = cursor.fetchone() while rs: column = rs[0] coltype = rs[1] data_length = rs[2] precision = rs[3] scale = rs[4] nullable = (rs[5] == 'Y') declared_char_length = rs[6] if declared_char_length: length = declared_char_length else: length = data_length results += [ TableMetadata(column, coltype, length, precision, scale, nullable) ] rs = cursor.fetchone() return results
def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) dbi = self.get_import() cursor.execute('DESC %s' % table) rs = cursor.fetchone() results = [] while rs is not None: column = rs[0] coltype = rs[1] null = False if rs[2] == 'NO' else True match = self.TYPE_RE.match(coltype) if match: coltype = match.group(1) size = match.group(2) if size: size = size[1:-1] if coltype in ['varchar', 'char']: max_char_size = size precision = None else: max_char_size = None precision = size data = TableMetadata(column, coltype, max_char_size, precision, 0, null) results += [data] rs = cursor.fetchone() return results
def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) cursor.execute("SELECT column_name FROM __columns__ " "WHERE table_name = '%s'" % table.upper()) result = [] column_names = [] for row in cursor.fetchall(): result += [TableMetadata(row[0], 'object', None, None, None, True)] return result
def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) sel = """\ SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS DEFAULT, a.attnotnull, a.attnum, a.attrelid as table_oid FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE (pg_table_is_visible(c.oid)) AND c.relname = '%s' AND c.relkind in ('r','v')) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum""" cursor.execute(sel % table) rs = cursor.fetchone() results = [] while rs is not None: column = rs[0] coltype = rs[1] null = not rs[3] match = self.TYPE_RE.match(coltype) if match: coltype = match.group(1) size = match.group(2) if size: size = size[1:-1] if 'char' in coltype: max_char_size = size precision = None else: max_char_size = None precision = size data = TableMetadata(column, coltype, max_char_size, precision, 0, null) results += [data] rs = cursor.fetchone() return results
def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) dbi = self.get_import() cursor.execute("SELECT column_name, data_type, " \ "character_maximum_length, numeric_precision, " \ "numeric_scale, is_nullable "\ "FROM information_schema.columns WHERE "\ "LOWER(table_name) = '%s'" % table) rs = cursor.fetchone() results = [] while rs is not None: is_nullable = False if rs[5] == 'YES': is_nullable = True data = TableMetadata(rs[0], rs[1], rs[2], rs[3], rs[4], is_nullable) results += [data] rs = cursor.fetchone() return results
def get_table_metadata(self, table, cursor): self._ensure_valid_table(cursor, table) # The table_info pragma returns results looking something like this: # # cid name type notnull dflt_value pk # --- --------------- ----------------- ------- ---------- -- # 0 id integer 99 NULL 1 # 1 action_time datetime 99 NULL 0 # 2 user_id integer 99 NULL 0 # 3 content_type_id integer 0 NULL 0 # 4 object_id text 0 NULL 0 # 5 object_repr varchar(200) 99 NULL 0 # 6 action_flag smallint unsigned 99 NULL 0 # 7 change_message text 99 NULL 0 cursor.execute('PRAGMA table_info(%s)' % table) rs = cursor.fetchone() result = [] char_field_re = re.compile(r'(varchar|char)\((\d+)\)') while rs is not None: (id, name, type, not_null, default_value, is_primary) = rs m = char_field_re.match(type) if m: type = m.group(1) try: max_char_size = int(m.group(2)) except ValueError: log.error('Bad number in "%s" type for column "%s"' % (type, name)) else: max_char_size = 0 data = TableMetadata(name, type, max_char_size, 0, 0, not not_null) result += [data] rs = cursor.fetchone() return result