Esempio n. 1
0
    def test_classCreate(self):
        class AutoTest(SQLObject):
            _connection = getConnection()

            class sqlmeta(sqlmeta):
                idName = 'auto_id'
                fromDatabase = True

        if AutoTest._connection.dbName == 'mssql':
            use_microseconds(False)
        john = AutoTest(firstName='john',
                        lastName='doe',
                        age=10,
                        created=now(),
                        wannahavefun=False,
                        longField='x' * 1000)
        jane = AutoTest(firstName='jane',
                        lastName='doe',
                        happy='N',
                        created=now(),
                        wannahavefun=True,
                        longField='x' * 1000)
        assert not john.wannahavefun
        assert jane.wannahavefun
        assert john.longField == 'x' * 1000
        assert jane.longField == 'x' * 1000
        del classregistry.registry(
            AutoTest.sqlmeta.registry).classes['AutoTest']

        columns = AutoTest.sqlmeta.columns
        assert columns["lastName"].dbName == "last_name"
        assert columns["wannahavefun"].dbName == "wannahavefun"
        if AutoTest._connection.dbName == 'mssql':
            use_microseconds(True)
Esempio n. 2
0
 def test_classCreate(self):
     if not supports('fromDatabase'):
         return
     class OldAutoTest(SQLObject):
         _connection = getConnection()
         class sqlmeta(sqlmeta):
             idName = 'auto_id'
             fromDatabase = True
     john = OldAutoTest(firstName='john',
                        lastName='doe',
                        age=10,
                        created=now(),
                        wannahavefun=False,
                        longField='x'*1000)
     jane = OldAutoTest(firstName='jane',
                        lastName='doe',
                        happy='N',
                        created=now(),
                        wannahavefun=True,
                        longField='x'*1000)
     assert not john.wannahavefun
     assert jane.wannahavefun
     assert john.longField == 'x'*1000
     assert jane.longField == 'x'*1000
     del classregistry.registry(
         OldAutoTest.sqlmeta.registry).classes['OldAutoTest']
Esempio n. 3
0
    def test_classCreate(self):
        class AutoTest(SQLObject):
            _connection = getConnection()
            class sqlmeta(sqlmeta):
                idName = 'auto_id'
                fromDatabase = True
        john = AutoTest(firstName='john',
                        lastName='doe',
                        age=10,
                        created=now(),
                        wannahavefun=False,
                        longField='x'*1000)
        jane = AutoTest(firstName='jane',
                        lastName='doe',
                        happy='N',
                        created=now(),
                        wannahavefun=True,
                        longField='x'*1000)
        assert not john.wannahavefun
        assert jane.wannahavefun
        assert john.longField == 'x'*1000
        assert jane.longField == 'x'*1000
        del classregistry.registry(
            AutoTest.sqlmeta.registry).classes['AutoTest']

        columns = AutoTest.sqlmeta.columns
        assert columns["lastName"].dbName == "last_name"
        assert columns["wannahavefun"].dbName == "wannahavefun"
Esempio n. 4
0
    def __init__(self, sourceClass, clause, clauseTables=None,
                 inheritedTables=None, **ops):
        if clause is None or isinstance(clause, str) and clause == 'all':
            clause = sqlbuilder.SQLTrueClause

        dbName = (ops.get('connection', None)
                  or sourceClass._connection).dbName

        tablesSet = tablesUsedSet(clause, dbName)
        tablesSet.add(str(sourceClass.sqlmeta.table))
        orderBy = ops.get('orderBy')
        if inheritedTables:
            for tableName in inheritedTables:
                tablesSet.add(str(tableName))
        if orderBy and not isinstance(orderBy, string_type):
            tablesSet.update(tablesUsedSet(orderBy, dbName))
        # DSM: if this class has a parent, we need to link it
        # DSM: and be sure the parent is in the table list.
        # DSM: The following code is before clauseTables
        # DSM: because if the user uses clauseTables
        # DSM: (and normal string SELECT), he must know what he wants
        # DSM: and will do himself the relationship between classes.
        if not isinstance(clause, str):
            tableRegistry = {}
            allClasses = classregistry.registry(
                sourceClass.sqlmeta.registry).allClasses()
            for registryClass in allClasses:
                if str(registryClass.sqlmeta.table) in tablesSet:
                    # DSM: By default, no parents are needed for the clauses
                    tableRegistry[registryClass] = registryClass
            tableRegistryCopy = tableRegistry.copy()
            for childClass in tableRegistryCopy:
                if childClass not in tableRegistry:
                    continue
                currentClass = childClass
                while currentClass:
                    if currentClass in tableRegistryCopy:
                        if currentClass in tableRegistry:
                            # DSM: Remove this class as it is a parent one
                            # DSM: of a needed children
                            del tableRegistry[currentClass]
                        # DSM: Must keep the last parent needed
                        # DSM: (to limit the number of join needed)
                        tableRegistry[childClass] = currentClass
                    currentClass = currentClass.sqlmeta.parentClass
            # DSM: Table registry contains only the last children
            # DSM: or standalone classes
            parentClause = []
            for (currentClass, minParentClass) in tableRegistry.items():
                while (currentClass != minParentClass) \
                        and currentClass.sqlmeta.parentClass:
                    parentClass = currentClass.sqlmeta.parentClass
                    parentClause.append(currentClass.q.id == parentClass.q.id)
                    currentClass = parentClass
                    tablesSet.add(str(currentClass.sqlmeta.table))
            clause = reduce(sqlbuilder.AND, parentClause, clause)

        super(InheritableSelectResults, self).__init__(
            sourceClass, clause, clauseTables, **ops)
Esempio n. 5
0
    def __init__(self, sourceClass, clause, clauseTables=None,
            inheritedTables=None, **ops):
        if clause is None or isinstance(clause, str) and clause == 'all':
            clause = sqlbuilder.SQLTrueClause

        dbName = (ops.get('connection',None) or sourceClass._connection).dbName

        tablesSet = tablesUsedSet(clause, dbName)
        tablesSet.add(str(sourceClass.sqlmeta.table))
        orderBy = ops.get('orderBy')
        if inheritedTables:
            for tableName in inheritedTables:
                tablesSet.add(str(tableName))
        if orderBy and not isinstance(orderBy, basestring):
            tablesSet.update(tablesUsedSet(orderBy, dbName))
        #DSM: if this class has a parent, we need to link it
        #DSM: and be sure the parent is in the table list.
        #DSM: The following code is before clauseTables
        #DSM: because if the user uses clauseTables
        #DSM: (and normal string SELECT), he must know what he wants
        #DSM: and will do himself the relationship between classes.
        if not isinstance(clause, str):
            tableRegistry = {}
            allClasses = classregistry.registry(
                sourceClass.sqlmeta.registry).allClasses()
            for registryClass in allClasses:
                if str(registryClass.sqlmeta.table) in tablesSet:
                    #DSM: By default, no parents are needed for the clauses
                    tableRegistry[registryClass] = registryClass
            tableRegistryCopy = tableRegistry.copy()
            for childClass in tableRegistryCopy:
                if childClass not in tableRegistry:
                    continue
                currentClass = childClass
                while currentClass:
                    if currentClass in tableRegistryCopy:
                        if currentClass in tableRegistry:
                            #DSM: Remove this class as it is a parent one
                            #DSM: of a needed children
                            del tableRegistry[currentClass]
                        #DSM: Must keep the last parent needed
                        #DSM: (to limit the number of join needed)
                        tableRegistry[childClass] = currentClass
                    currentClass = currentClass.sqlmeta.parentClass
            #DSM: Table registry contains only the last children
            #DSM: or standalone classes
            parentClause = []
            for (currentClass, minParentClass) in tableRegistry.items():
                while (currentClass != minParentClass) \
                and currentClass.sqlmeta.parentClass:
                    parentClass = currentClass.sqlmeta.parentClass
                    parentClause.append(currentClass.q.id == parentClass.q.id)
                    currentClass = parentClass
                    tablesSet.add(str(currentClass.sqlmeta.table))
            clause = reduce(sqlbuilder.AND, parentClause, clause)

        super(InheritableSelectResults, self).__init__(sourceClass,
            clause, clauseTables, **ops)
Esempio n. 6
0
    def __init__(self, sourceClass, clause, clauseTables=None,
                 **ops):
        if clause is None or isinstance(clause, str) and clause == 'all':
            clause = sqlbuilder.SQLTrueClause
        tablesDict = sqlbuilder.tablesUsedDict(clause)
        tablesDict[sourceClass.sqlmeta.table] = 1
        orderBy = ops.get('orderBy')
        if orderBy and not isinstance(orderBy, basestring):
            tablesDict.update(sqlbuilder.tablesUsedDict(orderBy))
        #DSM: if this class has a parent, we need to link it
        #DSM: and be sure the parent is in the table list.
        #DSM: The following code is before clauseTables
        #DSM: because if the user uses clauseTables
        #DSM: (and normal string SELECT), he must know what he wants
        #DSM: and will do himself the relationship between classes.
        if type(clause) is not str:
            tableRegistry = {}
            allClasses = classregistry.registry(
                sourceClass.sqlmeta.registry).allClasses()
            for registryClass in allClasses:
                if registryClass.sqlmeta.table in tablesDict:
                    #DSM: By default, no parents are needed for the clauses
                    tableRegistry[registryClass] = registryClass
            for registryClass in allClasses:
                if registryClass.sqlmeta.table in tablesDict:
                    currentClass = registryClass
                    while currentClass._parentClass:
                        currentClass = currentClass._parentClass
                        if tableRegistry.has_key(currentClass):
                            #DSM: Must keep the last parent needed
                            #DSM: (to limit the number of join needed)
                            tableRegistry[registryClass] = currentClass
                            #DSM: Remove this class as it is a parent one
                            #DSM: of a needed children
                            del tableRegistry[currentClass]
            #DSM: Table registry contains only the last children
            #DSM: or standalone classes
            parentClause = []
            for (currentClass, minParentClass) in tableRegistry.items():
                while currentClass != minParentClass and currentClass._parentClass:
                    parentClass = currentClass._parentClass
                    parentClause.append(currentClass.q.id == parentClass.q.id)
                    currentClass = parentClass
                    tablesDict[currentClass.sqlmeta.table] = 1
            clause = reduce(sqlbuilder.AND, parentClause, clause)

        super(InheritableSelectResults, self).__init__(sourceClass, clause, clauseTables,
                 **ops)
Esempio n. 7
0
 def testClassCreate(self):
     if not self.supportAuto:
         return
     class AutoTest(SQLObject):
         _fromDatabase = True
         _idName = 'auto_id'
         _connection = connection()
     john = AutoTest(firstName='john',
                     lastName='doe',
                     age=10,
                     created=now(),
                     wannahavefun=False)
     jane = AutoTest(firstName='jane',
                     lastName='doe',
                     happy='N',
                     created=now(),
                     wannahavefun=True)
     self.failIf(john.wannahavefun)
     self.failUnless(jane.wannahavefun)
     del classregistry.registry(AutoTest._registry).classes['AutoTest']
 def _init(self,*args,**kw):
     Persistent._init(self,*args,**kw)
     self.tableClass=classregistry.registry(None).getClass(self.name)
Esempio n. 9
0
 def _init(self, *args, **kw):
     Persistent._init(self, *args, **kw)
     self.tableClass = classregistry.registry(None).getClass(self.name)