【動軟】批量生成mysql數據庫存儲過程的模板

人工敲代碼好累呀!經過近段時間的研究,發現程序可以有snippet來自定義,存儲過程卻不能得到有效解決,心裏很堵!

近兩天發現【動軟】可以生成項目,網上有很多人有教程,我就不寫了。不過還是推薦一下吧:能用官方的,儘量用官方的,畢竟權威一點!

研究發現官方有關於模板的說明,但沒有生成mysql存儲的模板,網上搜索了一下,有一個模板:mysql動軟生成模板,謝謝作者辛勤的勞動!

但是這個模板有點小問題,再加上幾個功能不適合我,於是自己再改了一下:

-- ------------------------------------------
-- MYSQL存儲過程【動軟】批量生成模板
-- 感謝【動軟】作者
-- 感謝 其他 免費奉獻代碼的朋友
-- 以下代碼大部分來自網上,但我寫了詳細的註釋,同時更改了一些錯誤的地方。
-- 希望可以解決大家的問題,同時有不清楚的請聯繫QQ:876067467,email:[email protected]
-- 我盡力給各位解答。

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>

-- -----------------------------------
-- 指定的3種類型是datetime,timestamp和bit。
-- datetime,timestamp這2種類型後不跟長度
-- bit這種類型長度獲取不了,而且將進行特殊處理
<#
	TableHost host = (TableHost)(Host);
	host.Fieldlist.Sort(CodeCommon.CompareByintOrder);

	int columnCount=host.Fieldlist.Count;
	string IdentityStr="id";
	string specStr="datetime";
	string specStr1="timestamp";
	string specType="bit";
	
	foreach (ColumnInfo c in host.Fieldlist)
	{
		if(c.IsIdentity)	
		{
			IdentityStr=c.ColumnName.ToString().ToLower();
			break;
		}
	}

#>
-- ------------------------------------------
-- 本表  最大的  ID_

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_GetMaxId`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_GetMaxId`()
COMMENT '本表  最大的  ID_'
BEGIN
SELECT MAX(Id) FROM <#= host.GetDALClass(host.TableName) #>;
END //
DELIMITER ;

-- ------------------------------------------
--  ID_ 是否存在

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_Exists`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_Exists`
($_Id int)
COMMENT ' ID_ 是否存在'
BEGIN
SELECT count(1) FROM <#= host.GetDALClass(host.TableName) #> WHERE Id=$_Id ;
END //
DELIMITER ;

-- ------------------------------------------
-- 表 添加記錄

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_ADD`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_ADD`(
<#int updateI=1; 
	foreach (ColumnInfo c in host.Fieldlist)
	{ 
		if(c.IsIdentity) { #>
		$_<#= c.ColumnName.ToString()#> <#=c.TypeName#> (<#=c.Length#>)
		<#if (updateI!=columnCount){#>,<#}#><# } 
		else{ #>
			<#if(c.TypeName==specStr || c.TypeName==specStr1){#>
				$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>
			<#}else if(c.TypeName==specType){#>
				$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>(1)	
			<#}else{#>
			$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>(<#=c.Length#>)
			<#}#>
			<#if (updateI!=columnCount){#>,<#}#>
		<#} 
		updateI++;
	} #>
)
COMMENT '表 添加記錄'
BEGIN 
INSERT INTO <#= host.GetDALClass(host.TableName) #>(<#updateI=1; foreach (ColumnInfo c in host.Fieldlist)
{ if(c.IsIdentity) { updateI++;continue;} #><#= c.ColumnName.ToString().ToLower()#><#if (updateI!=columnCount){#>,<#}#>
<# updateI++;} #>
)VALUES(
<# updateI=1; foreach (ColumnInfo c in host.Fieldlist)
{ if(c.IsIdentity) { updateI++;continue;} #>
$_<#= c.ColumnName.ToString().ToLower()#><#if (updateI!=columnCount){#>,<#}#>
<# updateI++;} #>
);
# set $_<#=IdentityStr#>=last_insert_id();
select last_insert_id();
END //
DELIMITER ;

-- ------------------------------------------
-- 表 更新記錄

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_Update`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_Update`(
<#updateI=1; 
  foreach (ColumnInfo c in host.Fieldlist)
	{ #>
		<#if(c.TypeName==specStr || c.TypeName==specStr1){#>
			$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>
		<#}else if(c.TypeName==specType){#>
			$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>(1)
		<#}else{#>
			$_<#= c.ColumnName.ToString() #> <#=c.TypeName#>(<#=c.Length#>)
		<#}#>
		<#if (updateI!=columnCount){#>,<#}#>
		<# updateI++;
	} 
#>)
COMMENT '更新記錄'
BEGIN
UPDATE <#= host.GetDALClass(host.TableName) #> SET 
<#updateI=1; foreach (ColumnInfo c in host.Fieldlist)
{ if(c.IsIdentity) { updateI++;continue;}#>
<#= c.ColumnName.ToString().ToLower()#>=$_<#= c.ColumnName.ToString().ToLower()#><#if (updateI!=columnCount){#>,<#}#>
<#updateI++; } #>	
WHERE Id=$_Id ;

END //
DELIMITER ;

-- ------------------------------------------
-- 表 刪除記錄

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_Delete`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_Delete`(
$_Id int)
COMMENT '刪除記錄,指定ID_  '
BEGIN
DELETE FROM <#= host.GetDALClass(host.TableName) #>
WHERE Id=$_Id ;

END //
DELIMITER ;

-- ------------------------------------------
-- 表 獲取某條記錄,指定ID_

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_GetModel`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_GetModel`(
$_Id int)
COMMENT '獲取某條記錄,指定ID_'
BEGIN 
SELECT 
<# updateI=1;foreach (ColumnInfo c in host.Fieldlist)
{ #>
<#= c.ColumnName.ToString().ToLower()#><#if (updateI!=columnCount){#>,<#}#>
<#updateI++; } #>

FROM <#= host.GetDALClass(host.TableName) #>
WHERE Id=$_Id ;

END //
DELIMITER ;

-- ------------------------------------------
-- 表 獲取字段信息

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_GetColumnInfo`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_GetColumnInfo`(
$_Id int,
$_ColumnName varchar(20))
COMMENT '獲取字段信息'
BEGIN 
set @sqlStr=CONCAT('SELECT ',$_ColumnName , ' FROM <#= host.GetDALClass(host.TableName) #> WHERE Id= ',$_Id);
PREPARE count_stmt FROM @sqlStr;
EXECUTE count_stmt;
END //
DELIMITER ;

-- ------------------------------------------
-- 表 獲取所有記錄

DELIMITER //
DROP procedure IF EXISTS `proc_<#= host.GetDALClass(host.TableName) #>_GetList`;
CREATE PROCEDURE `proc_<#= host.GetDALClass(host.TableName) #>_GetList`()
COMMENT '獲取所有記錄'
BEGIN 
SELECT 
<# updateI=1;foreach (ColumnInfo c in host.Fieldlist)
{ #>
<#= c.ColumnName.ToString().ToLower()#><#if (updateI!=columnCount){#>,<#}#>
<#updateI++; } #>

FROM <#= host.GetDALClass(host.TableName) #>;

END //
DELIMITER ;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章