Author: phd
Date: 2005-10-25 16:45:52 +0000 (Tue, 25 Oct 2005)
New Revision: 1136
Added:
SQLObject/trunk/sqlobject/tests/test_string_id.py
Modified:
SQLObject/trunk/docs/FAQ.txt
SQLObject/trunk/docs/SQLObject.txt
SQLObject/trunk/sqlobject/col.py
SQLObject/trunk/sqlobject/firebird/firebirdconnection.py
SQLObject/trunk/sqlobject/maxdb/maxdbconnection.py
SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
SQLObject/trunk/sqlobject/mysql/mysqlconnection.py
SQLObject/trunk/sqlobject/postgres/pgconnection.py
SQLObject/trunk/sqlobject/sqlite/sqliteconnection.py
SQLObject/trunk/sqlobject/sybase/sybaseconnection.py
Log:
Applied the patch 1333498 by Jeff Bauer: String IDs. I refactored the patch
and updated documentation.
Modified: SQLObject/trunk/docs/FAQ.txt
===================================================================
--- SQLObject/trunk/docs/FAQ.txt 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/docs/FAQ.txt 2005-10-25 16:45:52 UTC (rev 1136)
@@ -279,14 +279,13 @@
Non-Integer IDs
---------------
-Yes, you can use non-integer IDs, but only in CVS_ now.
+Yes, you can use non-integer IDs.
-.. _CVS: http://sqlobject.org/#anonymous-cvs
+If you use non-integer IDs, you will not be able to use automatic ``CREATE
+TABLE`` generation (i.e., ``createTable``); SQLObject can create tables
+with int or str IDs. You also will have to give your own ID values when
+creating an object, like::
-If you use non-integer IDs, you will not be able to use automatic
-``CREATE TABLE`` generation (i.e., ``createTable``). You also will
-have to give your own ID values when creating an object, like::
-
color = Something(id="blue", r=0, b=100, g=0)
IDs are, and always will in future versions, be considered immutable.
@@ -308,7 +307,7 @@
This will be resolved in a future version when ID column types can be
declared like other columns.
-Additionally you can set _idType = str in you SQLObject class.
+Additionally you can set idType=str in you SQLObject class.
Binary Values
Modified: SQLObject/trunk/docs/SQLObject.txt
===================================================================
--- SQLObject/trunk/docs/SQLObject.txt 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/docs/SQLObject.txt 2005-10-25 16:45:52 UTC (rev 1136)
@@ -1396,11 +1396,12 @@
Non-Integer Keys
----------------
-While not strictly a legacy database issue, this fits into the
-category of "irregularities". If you use non-integer keys, all
-primary key management is up to you. You must create the table
-yourself, and when you create instances you must pass a ``id`` keyword
-argument into constructor (like ``Person(id='555-55-5555', ...)``).
+While not strictly a legacy database issue, this fits into the category of
+"irregularities". If you use non-integer keys, all primary key management
+is up to you. You must create the table yourself (SQLObject can create
+tables with int or str IDs), and when you create instances you must pass a
+``id`` keyword argument into constructor
+(like ``Person(id='555-55-5555', ...)``).
DBConnection: Database Connections
==================================
Modified: SQLObject/trunk/sqlobject/col.py
===================================================================
--- SQLObject/trunk/sqlobject/col.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/col.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -687,31 +687,22 @@
class SOKeyCol(SOCol):
+ key_type = {int: "INT", str: "TEXT"}
# 3-03 @@: this should have a simplified constructor
# Should provide foreign key information for other DBs.
- def _mysqlType(self):
- return 'INT'
+ def _sqlType(self):
+ return self.key_type[self.soClass.sqlmeta.idType]
- def _sqliteType(self):
- return 'INT'
-
- def _postgresType(self):
- return 'INT'
-
def _sybaseType(self):
- return 'NUMERIC(18,0) NULL'
+ key_type = {int: "NUMERIC(18,0) NULL", str: "TEXT"}
+ return key_type[self.soClass.sqlmeta.idType]
def _mssqlType(self):
- return 'INT NULL'
+ key_type = {int: "INT NULL", str: "TEXT"}
+ return key_type[self.soClass.sqlmeta.idType]
- def _firebirdType(self):
- return 'INT'
-
- def _maxdbType(self):
- return 'INT'
-
class KeyCol(Col):
baseClass = SOKeyCol
Modified: SQLObject/trunk/sqlobject/firebird/firebirdconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/firebird/firebirdconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/firebird/firebirdconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -131,7 +131,8 @@
return col.firebirdCreateSQL()
def createIDColumn(self, soClass):
- return '%s INT NOT NULL PRIMARY KEY' % soClass.sqlmeta.idName
+ key_type = {int: "INT", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s NOT NULL PRIMARY KEY' % (soClass.sqlmeta.idName, key_type)
def createIndexSQL(self, soClass, index):
return index.firebirdCreateIndexSQL(soClass)
Modified: SQLObject/trunk/sqlobject/maxdb/maxdbconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/maxdb/maxdbconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/maxdb/maxdbconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -172,7 +172,8 @@
return col.maxdbCreateSQL()
def createIDColumn(self, soClass):
- return '%s INT PRIMARY KEY' % soClass.sqlmeta.idName
+ key_type = {int: "INT", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s PRIMARY KEY' % (soClass.sqlmeta.idName, key_type)
def createIndexSQL(self, soClass, index):
return index.maxdbCreateIndexSQL(soClass)
Modified: SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -129,7 +129,8 @@
return col.mssqlCreateSQL()
def createIDColumn(self, soClass):
- return '%s INT IDENTITY UNIQUE' % soClass.sqlmeta.idName
+ key_type = {int: "INT", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s IDENTITY UNIQUE' % (soClass.sqlmeta.idName, key_type)
def createIndexSQL(self, soClass, index):
return index.mssqlCreateIndexSQL(soClass)
Modified: SQLObject/trunk/sqlobject/mysql/mysqlconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/mysql/mysqlconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/mysql/mysqlconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -99,6 +99,8 @@
return index.mysqlCreateIndexSQL(soClass)
def createIDColumn(self, soClass):
+ if soClass.sqlmeta.idType == str:
+ return '%s TEXT PRIMARY KEY' % soClass.sqlmeta.idName
return '%s INT PRIMARY KEY AUTO_INCREMENT' % soClass.sqlmeta.idName
def joinSQLType(self, join):
Modified: SQLObject/trunk/sqlobject/postgres/pgconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/postgres/pgconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/postgres/pgconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -145,7 +145,8 @@
return index.postgresCreateIndexSQL(soClass)
def createIDColumn(self, soClass):
- return '%s SERIAL PRIMARY KEY' % soClass.sqlmeta.idName
+ key_type = {int: "SERIAL", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s PRIMARY KEY' % (soClass.sqlmeta.idName, key_type)
def dropTable(self, tableName, cascade=False):
if self.server_version[:3] <= "7.2":
Modified: SQLObject/trunk/sqlobject/sqlite/sqliteconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/sqlite/sqliteconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/sqlite/sqliteconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -120,7 +120,8 @@
return None
def createIDColumn(self, soClass):
- return '%s INTEGER PRIMARY KEY' % soClass.sqlmeta.idName
+ key_type = {int: "INTEGER", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s PRIMARY KEY' % (soClass.sqlmeta.idName, key_type)
def joinSQLType(self, join):
return 'INT NOT NULL'
Modified: SQLObject/trunk/sqlobject/sybase/sybaseconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/sybase/sybaseconnection.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/sybase/sybaseconnection.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -102,7 +102,8 @@
return col.sybaseCreateSQL()
def createIDColumn(self, soClass):
- return '%s NUMERIC(18,0) IDENTITY UNIQUE' % soClass.sqlmeta.idName
+ key_type = {int: "NUMERIC(18,0)", str: "TEXT"}[soClass.sqlmeta.idType]
+ return '%s %s IDENTITY UNIQUE' % (soClass.sqlmeta.idName, key_type)
def createIndexSQL(self, soClass, index):
return index.sybaseCreateIndexSQL(soClass)
Added: SQLObject/trunk/sqlobject/tests/test_string_id.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_string_id.py 2005-10-23 13:28:07 UTC (rev 1135)
+++ SQLObject/trunk/sqlobject/tests/test_string_id.py 2005-10-25 16:45:52 UTC (rev 1136)
@@ -0,0 +1,17 @@
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+
+class TestStringID(SQLObject):
+ class sqlmeta(sqlmeta):
+ idType = str
+ idName = 'test_id_here'
+ name = StringCol()
+
+def test_string_id():
+ conn = getConnection()
+ TestStringID.setConnection(conn)
+ TestStringID.dropTable(ifExists=True)
+ assert not conn.tableExists(TestStringID.sqlmeta.table)
+ TestStringID.createTable()
+ TestStringID(id="TestStringID", name="TestStringID name")
+ assert len(list(TestStringID.selectBy(id='TestStringID'))) == 1
|