良好的设计,用于修改表中各种列的组合 [英] Good design for modifying various combination of columns in a table

查看:128
本文介绍了良好的设计,用于修改表中各种列的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个(〜15)列的数据库表。



我有要求在哪里连续更新数据,列的组合,给予主键。



例如

 更新Col1,Col2,Col3其中PK是

更新Col4其中PK是

更新Col5,Col7,Col9,Col10其中PK是

更新Col5,Col10其中PK是

我用Python编写了原始查询来处理这种情况。我写了单独的功能来管理每个要求。他们看起来像这样:

  requirements1(Col5,val1,Col7,val2,Col9,val3,Col10,val4)
{
更新TABLE SET(Col5,Col7,Col9,Col10)VALUES(val1,val2,val3,val4)
}

requirements2(Col5,val1,Col10,val2 )
{
更新TABLE SET(Col5,Col10)VALUES(val1,val2)
}

等。



但是,这种情况的数量已经增加到太多。有一个好的设计来处理这个吗?



像普通功能一样:

  UpdateTableValue(ColumnName1,ColumnValue1,ColumnName2,ColumnValue2等)
{
更新TABLE SET(ColumnName1,ColumnName2 ..)VALUES(ColumnValue1,ColumnValue2 ..)
}

然后用户可以打电话:

  UpdateTableValue(Col5,val1,Col7,val2,Col9,val3,Col10,val4)
UpdateTableValue(Col7,val,Col3,val2)

PS我不是要求一个好的DB设计来处理这个(所以没有OR映射等)。我想从查询包装器的角度来理解一个可以处理这个的好的设计。

解决方案

/ p>

  def update_table_value(* args):
sql =更新TABLE SET({})VALUES({})
sql.format(,。join([args [0 :: 2]]),
,。join([args [1 :: 2]]))
返回sql

请注意:




  • args将所有参数添加到函数中,并创建一个名为args的列表

  • args [0 :: 2]选择第二个条目列表


I have a database table with multiple (~ 15) columns.

I have requirement where where data is updated in a row, in combinations of columns, for the given primary key.

Eg.

Update Col1,Col2,Col3 where PK is something

Update Col4 where PK is something

Update Col5,Col7,Col9,Col10 where PK is something

Update Col5,Col10 where PK is something

I used Python and wrote raw queries to handle such cases. I wrote separate functions to manage each requirements. They look like this :

requirement1( Col5,val1,Col7,val2,Col9,val3,Col10,val4 )
{
    Update TABLE SET (Col5,Col7,Col9,Col10) VALUES (val1,val2,val3,val4)
}

requirement2( Col5,val1,Col10,val2 )
{
    Update TABLE SET (Col5,Col10) VALUES (val1,val2)
}

etc.

But the number of such cases has increased to too many. Is there a good design to handle this ?

Something like a general function :

UpdateTableValue(ColumnName1,ColumnValue1,ColumnName2,ColumnValue2, and so on )
{
    Update TABLE SET (ColumnName1,ColumnName2.. ) VALUES (ColumnValue1,ColumnValue2 .. )
}

And then user could just call :

UpdateTableValue(Col5,val1,Col7,val2,Col9,val3,Col10,val4)
UpdateTableValue(Col7,val,Col3,val2)

P.S. I am not asking for a good DB design to handle this (so no OR mapping etc.). I want to understand a good design from the query wrapper point of view which could handle this.

解决方案

What about something like:

def update_table_value(*args):
    sql = "Update TABLE SET ({}) VALUES ({}) "
    sql.format(",".join([args[0::2]]),
               ",".join([args[1::2]]))
return sql

note that:

  • args takes all the arguments that you put into the function and creates a list out of them named args
  • args[0::2] picks ever 2nd entry in the list

这篇关于良好的设计,用于修改表中各种列的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆