# -*- coding: utf-8 -*-

import re

"""
建立相关表的字段
从源表创建指定的MySQL建表脚本

"""
# 目标表名称 lzb_decoration_houseinfo
table_name = 'lzb_decoration_audit'
# 原表名称
original_table_name="DecorationAudit"

print("#DROP TABLE IF EXISTS {0};".format(table_name))
print("CREATE TABLE {0} (".format(table_name))
print("id bigint(20) unsigned NOT NULL AUTO_INCREMENT")

with open('sql_server_table_source', encoding='UTF-8') as f:
for line in f.readlines():
#print(line)
if line=='\n':
continue

if re.match('.*(NOT.*NULL).*', line) is None:
if re.match('.*(.*NULL.*).*',line) is not None:
line = line.replace("NULL", " NOT NULL ")

#print(line)
# 获取注释
str_comment='未知'
list_comment=re.findall(r".*,(.*)",line)
if len(list_comment)==1:
str_comment=list_comment[0].strip()
# 获取注释后的字符
temp_line_list=re.findall(r"(.*),.*",line)
if temp_line_list is None or temp_line_list=="" or len(temp_line_list)==0:
continue

temp_line=temp_line_list[0]
# unrenow_reason varchar(512)
prefix_line=re.sub(r'(\[.*\])','',temp_line).strip()
#print(prefix_line)
#str_comment=re.sub("(\,)","",str_comment)
if re.match(r'.*int.*', prefix_line, flags=0) is not None:
print(","+prefix_line+" "+"NOT NULL "+"COMMENT '"+str_comment.strip()+"'")
continue

if re.match(r'.*decimal.*', prefix_line, flags=0) is not None:
print("," + prefix_line +"(18,2)"+ " " + "NOT NULL " + "COMMENT '" + str_comment.strip() + "'")
continue

if re.match(r'.*datetime.*', prefix_line, flags=0) is not None:
print("," + prefix_line + " " + "NOT NULL " + "COMMENT '" + str_comment.strip() + "'")
continue

if re.match(r'.*varchar.*', prefix_line, flags=0) is not None:
print("," + prefix_line + " " + "NOT NULL DEFAULT ''" + "COMMENT '" + str_comment.strip() + "'")
continue

print(",ts bigint NOT NULL DEFAULT 0 COMMENT '源表时间戳' ")
print(",sync_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '从原表同步到报表的时间'")
print(",modify_time datetime NOT NULL COMMENT '最后更新时间'")
print(",PRIMARY KEY (id)")
print(') ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;')

print("\n\n\n\n")

print("同步kettle脚本")

print("select ")
with open('sql_server_table_source', encoding='UTF-8') as f:
for line in f.readlines():
if line=='\n':
continue

str_original_field = ""
field_original_reg=re.findall(".*\[(.*)\].*",line)
if field_original_reg is not None:
str_original_field=field_original_reg[0]

result_field_nvarchar=re.findall('.*\](.*)\s*nvarchar.*',line)

is_nvarchar_field=False
is_bigint_field=False

str_target_field=""
if result_field_nvarchar is not None and len(result_field_nvarchar)>0:
str_target_field=result_field_nvarchar[0]
is_nvarchar_field=True
else:
result = re.findall('.*\](.*)(int|varchar|datetime|bigint|decimal|nvarchar).*', line)
if result is not None and len(result)!=0 and len(result[0])!=0:
str_target_field=result[0][0].strip()

if re.match('.*(bigint).*', line) is not None:
result_field_bigint = re.findall('.*\](.*)\s*bigint.*', line)
str_target_field=result_field_bigint[0].strip()

if is_nvarchar_field or re.match(r".*(varchar).*", line) is not None:
print("ISNULL(t.{0},'') AS {1} ,".format(str_original_field,str_target_field))
elif is_bigint_field:
print("ISNULL(t.{0},0) AS {1} ,".format(str_original_field, str_target_field))
elif re.match('.*(bigint).*',line) is not None or re.match(r".*(int|bigint|decimal).*",line) is not None:
print("ISNULL(t.{0},0) AS {1} ,".format(str_original_field, str_target_field))
elif re.match(r".*(datetime|date).*",line) is not None:
print("ISNULL(t.{0},'1970-01-01') AS {1} ,".format(str_original_field, str_target_field))
elif re.match(r".*(bit).*",line) is not None:
print("ISNULL(t.{0},0) AS {1} ,".format(str_original_field, str_target_field))
else :
print("error")

print(" GETDATE() as sync_time ,")
print(" GETDATE() as modify_time , ")
print("ISNULL(t.ts,0) AS ts ")
print(" from {0} as t with(nolock) where t.ts> ? ".format(original_table_name))

  

python 生产数据表脚本的更多相关文章

  1. python打印表格式数据,留出正确的空格和段落星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  2. python编写shell脚本详细讲解

    python编写shell脚本详细讲解 那,python可以做shell脚本吗? 首先介绍一个函数: os.system(command) 这个函数可以调用shell运行命令行command并且返回它 ...

  3. 用 Python 替代 Bash 脚本(转)

    add by zhj: 其实作者是想说用Python来做那些Bash实现起来比较麻烦的部分,即将Bash与Python结合使用. 英文原文:http://www.linuxjournal.com/co ...

  4. python打印表格式数据-星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  5. python注释、脚本参数、字节码

    python注释.脚本参数.字节码 --道心 python安装 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3. ...

  6. Python数据库备份脚本

    Python数据库备份脚本 #!/usr/bin/env python # author: liudong # -*- coding: utf-8 -*- # filename: db_bak.py ...

  7. SQL SERVER 生成建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MSSQL] Script Date: 06/15/2012 11:59:00 ***** ...

  8. SQL SERVER 生成MYSQL建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MYSQL] Script Date: 06/15/2012 13:05:14 ***** ...

  9. SQL SERVER 生成ORACLE建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...

随机推荐

  1. 微擎site.php函数以及路由连接

    任务1: 微擎模块设计: module.php 规则类: 会调用module.php manifest.xml中业务菜单对应的模块 如果在site.php中没有相应的函数 比如 /web/index. ...

  2. Math 数学类

    /* Math 数学类, 主要是提供了很多的数学公式. abs(double a) 获取绝对值 ceil(double a) 向上取整 floor(double a) 向下取整 round(float ...

  3. 生成树协议(STP)的精髓知识

    STP生成树协议   1.STP介绍 2.STP生成树算法 1.STP  -   Spanning tree protocol (生成树协议)是逻辑上断开环路,防止广播风暴的产生.当线路故障,阻塞接口 ...

  4. 利用redis+AOP简单处理MQ冥等问题

    思路: 1.利用redis内部的串行执行特性,使用getandset()处理分布式问题; 2.注解提供入参选择,通过数据抽取后计算MD5值,实现业务性值的冥等: 代码区: 1.注解 1 /** 2 * ...

  5. Node.js躬行记(15)——活动规则引擎

    在日常的业务开发中,会包含许多的业务规则,一般就是用if-else硬编码的方式实现,这样就会增加逻辑的维护成本,若无注释,可能都无法理解规则意图. 因为一旦规则有所改变,那么就需要修改代码再发布代码, ...

  6. MySQL中 BETWEEN ... AND ...

    MySQL中 BETWEEN ... AND ... 1. 准备测试数据 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...

  7. Puppeteer简介

    puppeteer常用API https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md Puppeteer是一个node库,他 ...

  8. 使用动态时间规整 (DTW) 解决时间序列相似性度量及河流上下游污染浓度相似性识别分析

    时间序列相似性度量方法 时间序列相似性度量常用方法为欧氏距离ED(Euclidean distance)和动态时间规整DTW(Dynamic Time Warping).总体被分为两类: 锁步度量(l ...

  9. zabbix密码复杂度有效期安全增强,符合三级等保要求。

    一.关于三级等保要求 1.zabbix默认没有设置密码复杂度功能,密码有效期功能. 2.zabbix具备失败处理功能,但是没有页面手动调试,需要修改源代码. 3.zabbix具备超时退出功能. 二.整 ...

  10. [c语言]左移和右移

    左移和右移都是位运算的概念.我们知道计算机是基于二进制保存数据的,因此左移和右移的概念十分重要.本文约定是32位的机器. [左移] 丢弃最高位,0补最低位 左移是把一个数按照二进制每位向左移动若干位, ...