powerdesigner统一添加默认字段(创建时间、创建人等)

使用powerdesigner设计表结构时,每个表都需要设置一些默认字段,
如都有创建时间,创建人等,每次都写会感觉比较麻烦。所以就有一些方式可以自动生成这些默认字段。

方式有三种:

1.第一张表先设计好这些默认字段,直接复制这个表即可。

2.添加脚本,在导出sql脚本时会默认添加
这样不需要在表模型中添加默认字段,在导出sql脚本时会默认添加。方法如下:

1.打开Powerdesigner工具,依次点击Database -> Edit Current DBMS…。
2.在弹出窗口,依次点击Script->Objects->Table->Create。
3.在右侧窗口的 Value 中填入以下内容:(如果想要重置,可以create处右键,选择restore value)
create table[%R%?[ if not exists]] [%QUALIFIER%]%TABLE%[
(
   %TABLDEFN%, 
   create_user      bigint comment '创建人',
   create_time      datetime default CURRENT_TIMESTAMP comment '创建时间',
   update_user      bigint comment '修改人',
   update_time      datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间',
)][%R%?[comment = %.q:TLABL%]]
[%OPTIONS%]

3.创建所有表模型后执行脚本语句
这种方式可以在所有表模型都创建好后,统一添加默认字段。
操作如下:
tools - execute commands - edit/ run Scripe 打开窗口输入vb脚本命令

对于需要排除在外的表,可添加在下面代码中:
if instr(",需要排除的表1,需要排除的表2,",","+CurrentObject.Code+",")>0 then exit sub
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
 

Dim mdl 
Set mdl = ActiveModel
If (mdl Is Nothing) Then
   MsgBox "There is no Active Model"
Else
   ListObjects(mdl)
End If
 
 

Private Sub ListObjects(fldr) 
   output "Scanning " & fldr.code
   Dim obj 
   For Each obj In fldr.children
      
      TableSetComment obj
   Next
   		
   Dim f 
   For Each f In fldr.Packages 
     
      ListObjects f
   Next
End Sub
 
Private Sub TableSetComment(CurrentObject)
   if not CurrentObject.Iskindof(cls_Table) then exit sub
   // 添加你需要排除的表
      if instr(",standard_data_field,",","+CurrentObject.Code+",")>0 then exit sub
      if not CurrentObject.isShortcut then
        
         Dim col  
         Dim num
         //定义你需要添加的字段;
         dim create_user_id_
         dim create_time_
         dim modify_user_id_
         dim modify_time_
         dim is_delete_
         dim is_final_
         dim tenant_id_
         dim team_id_
        //给字段定义一个初始值;
        create_user_id_=1
        modify_user_id_=1
        create_time_=1
        modify_time_=1
        is_delete_=1
		is_final_ = 1
		tenant_id_ = 1
		team_id_ = 1
        //循环获取到最大列号;
         for each col in CurrentObject.columns
            
              num= num + 1
       //如果列中有此字段,赋个其他值;(可能是,实际操作如果表中有此字段,会报错)
            if col.Code="create_user_id_" then
               create_user_id_=100
            end if
            
            if col.Code="create_time_" then
               create_time_=100
              
            end if
            
            if col.Code="modify_user_id_" then
               modify_user_id_=100
             
            end if
            
            if col.Code="modify_time_" then
               modify_time_=100
 
            end if
            
            if col.Code="is_delete_" then
               is_delete_=100
 
            end if
            
            
         next
         
		//下面的操作是进行赋默认值,根据需要进行设计即可。
         if create_user_id_=1 then 
         CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="创建人"
                 	col.Code="create_user_id_"
                 	col.Comment="创建人"
                 	col.DataType="bigint(20)"
					col.Mandatory=1
            end if 
            next
            num=num+1
         end if
         
         if create_time_=1 then 
         CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
           
            if col.DataType="" then 
                 	col.Name="创建时间"
                 	col.Code="create_time_"
                 	col.Comment="创建时间"
                 	col.DataType="datetime"
		col.DefaultValue="CURRENT_TIMESTAMP"
            end if 
            next
            num=num+1
         end if
         

         
         if modify_user_id_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="修改人"
                 	col.Code="modify_user_id"
                 	col.Comment="修改人"
                 	col.DataType="bigint(20)"     
            end if 
            next
            num=num+1
         end if
         
         
         if modify_time_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="修改时间"
                 	col.Code="modify_time_"
                 	col.Comment="修改时间"
                 	col.DataType="datetime"
		col.DefaultValue="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
            end if 
            next
            num=num+1
         end if
         

		 if is_delete_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="删除标志"
                 	col.Code="is_delete_"
                 	col.Comment="删除标识(1:已删除;0:正常)"
                 	col.DataType="tinyint(1)"
		col.DefaultValue="0"
            end if 
            next
            num=num+1
         end if
				 
      if is_final_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="修改标志"
                 	col.Code="is_final_"
                 	col.Comment="是否不可修改(1:不可修改;0:可修改)"
                 	col.DataType="tinyint(1)"
		col.DefaultValue="0"
            end if 
            next
            num=num+1
         end if
         
      if tenant_id_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="租户id"
                 	col.Code="tenant_id_"
                 	col.Comment="租户id"
                 	col.DataType="bigint(20)"
		col.DefaultValue="-1"
            end if 
            next
            num=num+1
         end if
         
      if team_id_=1 then 
            CurrentObject.columns.CreateNewAt(num)
            for each col in CurrentObject.columns 
          
            if col.DataType="" then 
                 	col.Name="团队id"
                 	col.Code="team_id_"
                 	col.Comment="团队id"
                 	col.DataType="bigint(20)"
            end if 
            next
            num=num+1
         end if
         
      end if      
End Sub

参考链接:
https://colourful.blog.csdn.net/article/details/111546398

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值