odoo 是怎样格式化sql 查询语句的
odoo 是怎样格式化sql 查询语句的
主要还是使用format 来替换变量
# generic INSERT and UPDATE queriesINSERT_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()]
懂得,原来世界如此简单!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~