# -*- 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. 一张图让你看懂 iPhone 各种分辨率问题! #DF

    话不多说,直接看图! Source: paintcodeapp.com

  2. android中listView下拉刷新

    Android的ListView是应用最广的一个组件,功能强大,扩展性灵活(不局限于ListView本身一个类),前面的文章有介绍分组,拖拽,3D立体,游标,圆角,而今天我们要介绍的是另外一个扩展Li ...

  3. 一键部署nginx 服务

    一键部署nginx服务 老样子安装包都拖到opt里! 脚本如下 source 执行! #!/bin/bash #======编译安装nginx服务====== #安装所需开发包和编译环境.编译器 yu ...

  4. SpringBoot源码解读系列三——引导注解

    我们再来看下SpringBoot应用的启动类: 查看代码 import org.springframework.boot.SpringApplication; import org.springfra ...

  5. Spring IOC-基于XML配置的容器

    Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...

  6. Spring Boot对Spring Data JPA的支持

    前两篇介绍了Spring Data JPA的基本使用,本篇介绍Spring Boot 对JPA的支持.如下: 1)导入坐标 2)注解配置 其他配置同Spring Data JPA应用之常规CRUD操作 ...

  7. IDEA2019.2.2激活码,亲测可用

    3AGXEJXFK9-eyJsaWNlbnNlSWQiOiIzQUdYRUpYRks5IiwibGljZW5zZWVOYW1lIjoiaHR0cHM6Ly96aGlsZS5pbyIsImFzc2lnb ...

  8. [LeetCode]1389. 按既定顺序创建目标数组

    给你两个整数数组 nums 和 index.你需要按照以下规则创建目标数组: 目标数组 target 最初为空. 按从左到右的顺序依次读取 nums[i] 和 index[i],在 target 数组 ...

  9. maven私有仓库从搭建到使用

    因工作需要,需要搭建公司自己的私有仓库,存储自己的私有jar包,所以研究了下 一.环境准备 1.下载并安装nexus,然后启动项目,这部分攻略网上很多,而且基本上都是正确的,此处不做梳理 2.登录12 ...

  10. 国内外主流5款doshboard软件比较和对比

    大数据行业随着互联网的蓬勃发展中也越来越被人们看好,但是从事大数据行业的数据分析师经常会谈到dashboard,很多人就会疑惑什么是dashboard,下面就来了解一下Doshboard的发展. da ...