Merge pull request #55 from cli248/upstream

Fix #51: change 'Text' to 'String' in create_table function
This commit is contained in:
Friedrich Lindenberg 2013-12-07 02:25:18 -08:00
commit 2d526a2b73
2 changed files with 48 additions and 16 deletions

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:
auto_flag = False if match.group(1) == 'Integer':
if primary_id == 'id': auto_flag = False
auto_flag = True if primary_id == 'id':
col = Column(primary_id, Integer, primary_key=True, autoincrement=auto_flag) auto_flag = True
elif primary_type == 'Text': col = Column(primary_id, Integer, primary_key=True, autoincrement=auto_flag)
col = Column(primary_id, Text, primary_key=True) elif not match.group(3):
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

View File

@ -39,7 +39,7 @@ class DatabaseTestCase(unittest.TestCase):
def test_create_table_custom_id1(self): def test_create_table_custom_id1(self):
pid = "string_id" pid = "string_id"
table = self.db.create_table("foo2", pid, 'Text') table = self.db.create_table("foo2", pid, 'String')
assert table.table.exists() assert table.table.exists()
assert len(table.table.columns) == 1, table.table.columns assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c assert pid in table.table.c, table.table.c
@ -49,8 +49,19 @@ class DatabaseTestCase(unittest.TestCase):
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar' assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
def test_create_table_custom_id2(self): def test_create_table_custom_id2(self):
pid = "string_id"
table = self.db.create_table("foo3", pid, 'String(50)')
assert table.table.exists()
assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c
table.insert({
'string_id': 'foobar'})
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
def test_create_table_custom_id3(self):
pid = "int_id" pid = "int_id"
table = self.db.create_table("foo3", primary_id = pid) table = self.db.create_table("foo4", primary_id = pid)
assert table.table.exists() assert table.table.exists()
assert len(table.table.columns) == 1, table.table.columns assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c assert pid in table.table.c, table.table.c
@ -64,7 +75,7 @@ class DatabaseTestCase(unittest.TestCase):
def test_create_table_shorthand1(self): def test_create_table_shorthand1(self):
pid = "int_id" pid = "int_id"
table = self.db['foo4', pid] table = self.db['foo5', pid]
assert table.table.exists assert table.table.exists
assert len(table.table.columns) == 1, table.table.columns assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c assert pid in table.table.c, table.table.c
@ -78,7 +89,18 @@ class DatabaseTestCase(unittest.TestCase):
def test_create_table_shorthand2(self): def test_create_table_shorthand2(self):
pid = "string_id" pid = "string_id"
table = self.db['foo5', pid, 'Text'] table = self.db['foo6', pid, 'String']
assert table.table.exists
assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c
table.insert({
'string_id': 'foobar'})
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
def test_create_table_shorthand3(self):
pid = "string_id"
table = self.db['foo7', pid, 'String(20)']
assert table.table.exists assert table.table.exists
assert len(table.table.columns) == 1, table.table.columns assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c assert pid in table.table.c, table.table.c