Jenkins基础篇 系列之-—08 实现SQL脚本批量执行补充

本篇文章是对前一篇的优化。在上一篇中 Jenkins基础篇 系列之-—07 实现SQL脚本批量执行 备份操作是在模块全部执行成功后进行的,当开发提交的sql脚本质量较差时,执行成功的脚本需人为介入改脚本后缀,避免二次执行;本篇文章实现单脚本执行成功就及时备份。

Ant知识扩展

官网说明
在ant中,无法实现常见的字符重操作,比如截取、替换。我们可以借助ant-contrib中的propertyregex任务实现字符串的截取、替换

1、字符串的截取

<!--${file.separator} 文件分割符 ,windows  \ ,linux  /  -->

 <propertyregex property="result"
              input="D:\apache-ant-1.9.6-bin\lib\a.txt"
              regexp="([^\*]*)\${file.separator}"
              select="\1"
              casesensitive="false" />
 <echo>文件绝对路径:${result}</echo>
 <propertyregex property="result" 
            input="D:\apache-ant-1.9.6-bin\lib\a.txt" 
            regexp="[^\${file.separator}]+$" select="\0" 
            casesensitive="false"/>
 <echo>文件名:${result}</echo>

2、字符串的替换

比如:替换字符串root:password@127.0.0.1为root:pwd@127.0.0.1

 <propertyregex property="${svr1}" input="${svr}" regexp='password' replace="pwd"/>

最终对Target节点进行优化

 <target name="runSqlInFolder">
	<echo>Run the SQL at Folder: ${sqlfolder}</echo>
	<echo>DB Host: ${v7testdb.host}</echo>
	<echo>DB Name: ${v7testdb.name}</echo>
	<echo>DB User: ${v7testdb.user}</echo>
	<trycatch property="errMsg">
		<try>
			<for param="file">
				<path>
					<sort xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators">
						<fileset dir="${sqlfolder}" includes="*/*/*.sql" casesensitive="false"/>
					</sort>
				</path>
				<sequential>
					<echo>SQL: @{file}</echo>
					<propertyregex override="yes" property="sqlFolderName" input="@{file}"
						regexp="${sqlfolder}${file.separator}([^\*]*)\${file.separator}" select="\1"
					casesensitive="false" />
					<propertyregex override="yes" property="sqlFileName" input="@{file}"
						regexp="[^\${file.separator}]+$" select="\0"
					casesensitive="false"/>
						<execsql
							dbhost="${v7testdb.host}"	
							dbport="${v7testdb.port}"	
							dbname="${v7testdb.name}" 
							dbuser="${v7testdb.user}" 
							dbpwd="${v7testdb.pwd}"
							sqlfile="@{file}"
							logfile="${Sqllogfile}"/>
					<move file="@{file}" todir="${sqlbakdir}${file.separator}${sqlFolderName}"/>
				</sequential>
			</for>
			<echo>Finished running all SQL</echo>
			<echo>Files moved to backup folder:${sqlbakdir}</echo>
		</try>
		<catch>
			<echo>Error found when running SQL</echo>
			<echo>Log file can be found in:</echo>
			<echo>${sqlbakdir}/err</echo>
			<move file="${Sqllogfile}" todir="${sqlbakdir}/err"/>
			<fail>Error Occur</fail>
		</catch>
		<finally>
		</finally>
	</trycatch>
 </target>

问题补充

用ojdbc5执行sql,脚本是数据库导出来的,碰到timestamp(6)格式时,插入会失败??

timestamp(6)是时间戳类型,参数6指的是表示秒的数字的小数点右边可以存储6位数字,最多9位。

1、时间戳是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
2、在oracle使用时间戳,一般都是为了方便计算时间差的,要知道oracle中的date类型想减是不能友好的得到时间的差值的。所以使用时间戳来得到两个时间差。

posted @ 2020-04-29 12:59  雨 燕  阅读(424)  评论(0编辑  收藏  举报