• odoo 是怎样格式化sql 查询语句的


    主要还是使用format 来替换变量

    # generic INSERT and UPDATE queries
    INSERT_QUERY = "INSERT INTO {table} ({cols}) VALUES {rows} RETURNING id"
    UPDATE_QUERY = "UPDATE {table} SET {assignment} WHERE {condition} RETURNING id"
    
    def query_insert(cr, table, rows):
        """ Insert rows in a table. ``rows`` is a list of dicts, all with the same
            set of keys. Return the ids of the new rows.
        """
        if isinstance(rows, Mapping):
            rows = [rows]
        cols = list(rows[0])
        query = INSERT_QUERY.format(
            table=table,
            cols=",".join(cols),
            rows=",".join("%s" for row in rows),
        )
        params = [tuple(row[col] for col in cols) for row in rows]
        cr.execute(query, params)
        return [row[0] for row in cr.fetchall()]
    
    def query_update(cr, table, values, selectors):
        """ Update the table with the given values (dict), and use the columns in
            ``selectors`` to select the rows to update.
        """
        setters = set(values) - set(selectors)
        query = UPDATE_QUERY.format(
            table=table,
            assignment=",".join("{0}=%({0})s".format(s) for s in setters),
            condition=" AND ".join("{0}=%({0})s".format(s) for s in selectors),
        )
        cr.execute(query, values)
        return [row[0] for row in cr.fetchall()]
    
  • 相关阅读:
    js Array的方法及属性总结
    js 继承
    js 判断数据类型
    序列化和反序列化
    express 常用方法和插件
    node 常用的对象
    node.js 守护进程
    CentOS7安装Python3.8.1和ansible
    MAC终端终极美化方案
    Linux之top命令详解
  • 原文地址:https://www.cnblogs.com/qianxunman/p/13359505.html
Copyright © 2020-2023  润新知