码迷,mamicode.com
首页 > 数据库 > 详细

Jenkins基础篇 系列之-—11 实现SQL脚本批量执行2

时间:2020-05-29 13:31:42      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:pass   port   regexp   window   ref   eset   字符   folder   sep   

对前一篇的优化,实现单脚本执行成功即备份
Jenkins基础篇 系列之-—07 实现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>

Jenkins基础篇 系列之-—11 实现SQL脚本批量执行2

标签:pass   port   regexp   window   ref   eset   字符   folder   sep   

原文地址:https://www.cnblogs.com/liuyitan/p/12982854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!