Python peewee.Expression() Examples

The following are 19 code examples of peewee.Expression(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module peewee , or try the search function .
Example #1
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains(self, value):
        if isinstance(value, dict):
            return Expression(self, OP.HCONTAINS_DICT, Param(value))
        elif isinstance(value, (list, tuple)):
            return Expression(self, OP.HCONTAINS_KEYS, Param(value))
        return Expression(self, OP.HCONTAINS_KEY, value) 
Example #2
Source File: peeweedbevolve.py    From peewee-db-evolve with GNU Lesser General Public License v3.0 5 votes vote down vote up
def set_default(db, migrator, table_name, column_name, field):
    default = field.default
    if callable(default): default = default()
    migration = ( migrator.make_context()
      .literal('UPDATE ').sql(pw.Entity(table_name))
      .literal(' SET ').sql(pw.Expression(pw.Entity(column_name), pw.OP.EQ, field.db_value(default), flat=True))
      .literal(' WHERE ').sql(pw.Expression(pw.Entity(column_name), pw.OP.IS, pw.SQL('NULL'), flat=True))
    )
    return extract_query_from_migration(migration) 
Example #3
Source File: sql.py    From rowboat with MIT License 5 votes vote down vote up
def pg_regex_i(lhs, rhs):
    return Expression(lhs, OP.IRGX, rhs) 
Example #4
Source File: test_shortcuts.py    From aiopeewee with MIT License 5 votes vote down vote up
def title(self):
        return Expression(
            fn.UPPER(fn.SUBSTR(self.username, 1, 1)),
            OP_CONCAT,
            fn.SUBSTR(self.username, 2)) 
Example #5
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def match(self, query):
        return Expression(self, OP.TS_MATCH, fn.to_tsquery(query)) 
Example #6
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains_all(self, *items):
        return Expression(
            self,
            OP.JSONB_CONTAINS_ALL_KEYS,
            Passthrough(list(items))) 
Example #7
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains_any(self, *items):
        return Expression(
            self,
            OP.JSONB_CONTAINS_ANY_KEY,
            Passthrough(list(items))) 
Example #8
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contained_by(self, other):
        return Expression(self, OP.JSONB_CONTAINED_BY, Json(other)) 
Example #9
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains(self, other):
        if isinstance(other, (list, dict)):
            return Expression(self, OP.JSONB_CONTAINS, Json(other))
        return Expression(self, OP.JSONB_EXISTS, Passthrough(other)) 
Example #10
Source File: sqlite_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def match(lhs, rhs):
    return Expression(lhs, OP.MATCH, rhs) 
Example #11
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def update(self, **data):
        return Expression(self, OP.HUPDATE, data) 
Example #12
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def __getitem__(self, key):
        return Expression(self, OP.HKEY, Param(key)) 
Example #13
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains_any(self, *items):
        return Expression(self, OP.ACONTAINS_ANY, _Array(self, list(items))) 
Example #14
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains(self, *items):
        return Expression(self, OP.ACONTAINS, _Array(self, list(items))) 
Example #15
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains_any(self, *keys):
        return Expression(
            self.as_json(True),
            OP.JSONB_CONTAINS_ANY_KEY,
            Passthrough(list(keys))) 
Example #16
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def contains(self, other):
        clone = self.as_json(True)
        if isinstance(other, (list, dict)):
            return Expression(clone, OP.JSONB_CONTAINS, Json(other))
        return Expression(clone, OP.JSONB_EXISTS, other) 
Example #17
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def cast(self, as_type):
        return Expression(Clause(self, parens=True), OP.CAST, SQL(as_type)) 
Example #18
Source File: postgres_ext.py    From Quiver-alfred with MIT License 5 votes vote down vote up
def cast(self, as_type):
    return Expression(self, OP.CAST, SQL(as_type)) 
Example #19
Source File: sqlfuncs.py    From slim with zlib License 4 votes vote down vote up
def update(self, records: Iterable[DataRecord], values: SQLValuesToWrite, returning=False) -> Union[int, Iterable[DataRecord]]:
        new_vals = {}
        model = self.vcls.model
        db = self.vcls.model._meta.database
        fields = self.vcls._peewee_fields
        cond = self._build_write_condition(records)

        for k, v in values.items():
            if k in fields:
                field = fields[k]
                is_array_field = isinstance(field, ArrayField)

                if is_array_field:
                    if k in values.set_add_fields:
                        # 这里需要加 [v] 的原因是,params需要数组,举例来说为,[v1,v2,v3]
                        # v = SQL('%s || %%s' % field.column_name, [v])
                        v = SQL('(select ARRAY((select unnest(%s)) union (select unnest(%%s))))' % field.column_name, [v])

                    if k in values.set_remove_fields:
                        v = SQL('(select ARRAY((select unnest(%s)) except (select unnest(%%s))))' % field.column_name, [v])

                    # 尚未启用
                    # if k in values.array_append:
                    #     v = SQL('array_append(%s, %%s)' % field.column_name, [v])

                    # if k in values.array_remove:
                    #     v = SQL('array_remove(%s, %%s)' % field.column_name, [v])

                else:
                    if k in values.incr_fields:
                        v = field + v
                    if k in values.decr_fields:
                        v = field - v

                new_vals[k] = v

        with db.atomic(), PeeweeContext(db):
            if isinstance(db, peewee.PostgresqlDatabase):
                q = model.update(**new_vals).where(cond)
                if returning:
                    # cond: peewee.Expression
                    ret = q.returning(*model._meta.fields.values()).execute()
                    to_record = lambda x: PeeweeDataRecord(None, x, view=self.vcls)
                    items = map(to_record, ret)
                    return list(items)
                else:
                    count = q.execute()
                    return count
            else:
                count = model.update(**new_vals).where(cond).execute()
                if not returning: return count

                to_record = lambda x: PeeweeDataRecord(None, x, view=self.vcls)
                return list(map(to_record, model.select().where(cond).execute()))