def readExpression(self, parens): """ Read a given expression from a Parenthesis object. (This is currently a limited parser in support of simple CHECK constraints, not something suitable for a full WHERE Clause.) """ parens = iterSignificant(parens) expect(parens, ttype=Punctuation, value="(") nexttok = parens.next() if isinstance(nexttok, Comparison): lhs, op, rhs = list(iterSignificant(nexttok)) result = CompoundComparison(self.nameOrValue(lhs), op.value.encode("ascii"), self.nameOrValue(rhs)) elif isinstance(nexttok, Identifier): # our version of SQLParse seems to break down and not create a nice # "Comparison" object when a keyword is present. This is just a # simple workaround. lhs = self.nameOrValue(nexttok) op = expect(parens, ttype=CompTok).value.encode("ascii") funcName = expect(parens, ttype=Keyword).value.encode("ascii") rhs = FunctionSyntax(funcName)(*[ ColumnSyntax(self.table.columnNamed(x)) for x in namesInParens(expect(parens, cls=Parenthesis)) ]) result = CompoundComparison(lhs, op, rhs) expect(parens, ttype=Punctuation, value=")") return result
def _normalizeColumnUUIDs(txn, column): """ Upper-case the UUIDs in the given SQL DAL column. @param txn: The transaction. @type txn: L{CommonStoreTransaction} @param column: the column, which may contain UIDs, to normalize. @type column: L{ColumnSyntax} @return: A L{Deferred} that will fire when the UUID normalization of the given column has completed. """ tableModel = column.model.table # Get a primary key made of column syntax objects for querying and # comparison later. pkey = [ColumnSyntax(columnModel) for columnModel in tableModel.primaryKey] for row in (yield Select([column] + pkey, From=TableSyntax(tableModel)).on(txn)): before = row[0] pkeyparts = row[1:] after = normalizeUUIDOrNot(before) if after != before: where = _AndNothing # Build a where clause out of the primary key and the parts of the # primary key that were found. for pkeycol, pkeypart in zip(pkeyparts, pkey): where = where.And(pkeycol == pkeypart) yield Update({column: after}, Where=where).on(txn)
def nameOrValue(self, tok): """ Inspecting a token present in an expression (for a CHECK constraint on this table), return a L{twext.enterprise.dal.syntax} object for that value. """ if isinstance(tok, Identifier): return ColumnSyntax(self.table.columnNamed(tok.get_name())) elif tok.ttype == Number.Integer: return Constant(int(tok.value))
def checkOneConstraint(sqlText): s = self.schemaFromString(sqlText) table = s.tableNamed('sample') self.assertEquals(len(table.constraints), 1) expr = table.constraints[0].expression self.assertEquals(expr.a.model, table.columnNamed("example")) self.assertEquals(expr.op, "=") self.assertEquals(expr.b.function.name, "lower") self.assertEquals( expr.b.args, tuple([ColumnSyntax(table.columnNamed("example"))]))
def _primaryKeyExpression(cls): return Tuple([ColumnSyntax(c) for c in cls.table.model.primaryKey])