chang "Text" to "String" in create_table() function, fix issue #51

This commit is contained in:
Chen 2013-12-06 23:58:54 -05:00
parent d393523764
commit 4a86981026

View File

@ -1,5 +1,6 @@
import logging import logging
import threading import threading
import re
from urlparse import parse_qs from urlparse import parse_qs
from urllib import urlencode from urllib import urlencode
@ -8,7 +9,7 @@ from migrate.versioning.util import construct_engine
from sqlalchemy.pool import NullPool from sqlalchemy.pool import NullPool
from sqlalchemy.schema import MetaData, Column, Index from sqlalchemy.schema import MetaData, Column, Index
from sqlalchemy.schema import Table as SQLATable from sqlalchemy.schema import Table as SQLATable
from sqlalchemy import Integer, Text from sqlalchemy import Integer, Text, String
from dataset.persistence.table import Table from dataset.persistence.table import Table
from dataset.persistence.util import ResultIter from dataset.persistence.util import ResultIter
@ -106,7 +107,8 @@ class Database(object):
unless specified via optional parameter primary_id, which will be used unless specified via optional parameter primary_id, which will be used
as the primary key of the table. Automatic id is set to be an as the primary key of the table. Automatic id is set to be an
auto-incrementing integer, while the type of custom primary_id can be a auto-incrementing integer, while the type of custom primary_id can be a
Text or an Integer as specified with primary_type flag. String or an Integer as specified with primary_type flag. The default
length of String is 256. The caller can specify the length.
The caller will be responsible for the uniqueness of manual primary_id. The caller will be responsible for the uniqueness of manual primary_id.
This custom id feature is only available via direct create_table call. This custom id feature is only available via direct create_table call.
@ -118,23 +120,31 @@ class Database(object):
# custom id and type # custom id and type
table2 = db.create_table('population2', 'age') table2 = db.create_table('population2', 'age')
table3 = db.create_table('population3', primary_id='race', primary_type='Text') table3 = db.create_table('population3', primary_id='race', primary_type='String')
# custom length of String
table4 = db.create_table('population4', primary_id='race', primary_type='String(50)')
""" """
self._acquire() self._acquire()
try: try:
log.debug("Creating table: %s on %r" % (table_name, self.engine)) log.debug("Creating table: %s on %r" % (table_name, self.engine))
table = SQLATable(table_name, self.metadata) match = re.match(r'^(Integer)$|^(String)(\(\d+\))?$', primary_type)
if primary_type == 'Integer': if match:
if match.group(1) == 'Integer':
auto_flag = False auto_flag = False
if primary_id == 'id': if primary_id == 'id':
auto_flag = True auto_flag = True
col = Column(primary_id, Integer, primary_key=True, autoincrement=auto_flag) col = Column(primary_id, Integer, primary_key=True, autoincrement=auto_flag)
elif primary_type == 'Text': elif not match.group(3):
col = Column(primary_id, Text, primary_key=True) col = Column(primary_id, String(256), primary_key=True)
else:
len_string = int(match.group(3)[1:-1])
len_string = min(len_string, 256)
col = Column(primary_id, String(len_string), primary_key=True)
else: else:
raise DatasetException( raise DatasetException(
"The primary_type has to be either 'Integer' or 'Text'.") "The primary_type has to be either 'Integer' or 'String'.")
table = SQLATable(table_name, self.metadata)
table.append_column(col) table.append_column(col)
table.create(self.engine) table.create(self.engine)
self._tables[table_name] = table self._tables[table_name] = table