当前位置: 首页 > 工具软件 > Dao Script > 使用案例 >

Dto Dao 一括作成用script

晋越彬
2023-12-01
/**********
//変数設定
***********/
//***************************************
//↓↓↓ ツール使用前に設定してください ↓↓↓
//a5erのパス
var a5erPath = './arm.a5er';
//Author
var authorName = '稻草人';
//DSN
var dsn = 'arm';
//ローカルパス
var path = "D:\\temp\\";
//↑↑↑ ツール使用前に設定してください ↑↑↑
//*************************************** 
var a5er = $("<dir></dir>").load(a5erPath ,function(response, status, xhr) {
					a5er = a5er.text();
				});


$(document).ready(function(){
					   
	var formatDate = function (date, format) {
		if (!format) format = 'YYYY-MM-DD hh:mm:ss.SSS';
			format = format.replace(/YYYY/g, date.getFullYear());
			format = format.replace(/MM/g, ('0' + (date.getMonth() + 1)).slice(-2));
			format = format.replace(/DD/g, ('0' + date.getDate()).slice(-2));
			format = format.replace(/hh/g, ('0' + date.getHours()).slice(-2));
			format = format.replace(/mm/g, ('0' + date.getMinutes()).slice(-2));
			format = format.replace(/ss/g, ('0' + date.getSeconds()).slice(-2));
			if (format.match(/S/g)) {
			var milliSeconds = ('00' + date.getMilliseconds()).slice(-3);
			var length = format.match(/S/g).length;
			for (var i = 0; i < length; i++) format = format.replace(/S/, milliSeconds.substring(i, i + 1));
		}
		return format;
	};
	
	var getValue = function(str, key){
		var startIndex = str.indexOf(key);
		var endIndex = str.indexOf('\n',startIndex);
		if(startIndex == -1){
			return "";	
		}
		if(endIndex == -1){
			endIndex = str.length;
		}
		var keyValue = str.substring(startIndex, endIndex).split("=");
		return keyValue[1].trim();
	}
	
	var types={"@INT":["numeric","cf_sql_integer","0"],
			"INT":["numeric","cf_sql_integer","0"],
			"NVARCHAR":["string","cf_sql_varchar",""],
			"@CHAR":["string","cf_sql_varchar",""],
			"VARCHAR":["string","cf_sql_varchar",""],
			"@DATETIME":["date","cf_sql_timestamp","#Now()#"],
			"DATETIME":["date","cf_sql_timestamp","#Now()#"],
			"@DATE":["date","cf_sql_timestamp","#Variables.MIN_DATE#"],
			"DATE":["date","cf_sql_timestamp","#Variables.MIN_DATE#"],
			"@TIME":["string","cf_sql_time","00:00:00"],
			"@VARCHAR":["string","cf_sql_varchar",""],
			"@DECIMAL":["numeric","cf_sql_decimal","0"],
			"DECIMAL":["numeric","cf_sql_decimal","0"]
	}

	
	
	$('#GoBtn').click(function(){

		
		var data_array = a5er.split(/\r\n|\r|\n/);
		var tableName = "";
		var tableList = [];
		//テーブル名取得
		$.each(data_array,function(index,val){
			
			if ( val.match(/PName=/)) {
				//strにhogeを含む場合の処理
			 	//console.log(val);
				tmp = val.replace("PName=", "");
				if(tmp != '') tableList.push(tmp);		
			}
		});
		//ソート
		tableList = tableList.sort();
		
		var flag = true;
		for (var i in tableList){
			console.log(tableList[i]);
			
					
				var tableName =tableList[i];
			
				var startIndex = a5er.indexOf('PName=' + tableName + '\n');
				if(startIndex == -1){
					startIndex = a5er.indexOf('PName=' + tableName + ' ');
				}
				if(startIndex == -1){
					startIndex = a5er.indexOf('PName=' + tableName.toUpperCase() + '\n');
				}
				if(startIndex == -1){
					startIndex = a5er.indexOf('PName=' + tableName.toUpperCase() + ' ');
				}
				
				if(startIndex == -1){
					alert("該当テーブルが見つかりません!" + tableName);
					flag = false;
					break;
				}else{
					var endIndex = a5er.indexOf('\n[',startIndex);
					var tableInfo = a5er.substring(startIndex, endIndex).split("\n");
					
					generateDTO(tableInfo);
					generateDAO(tableInfo);
				}
			
		}
		if(flag){
			alert('成功');
			
		}
		
		
		

	});
	
	//テーブル名表示ボタン
	$('#tbBtn').click(function(){

		var data_array = a5er.split(/\r\n|\r|\n/);
		var tableName = "";
		var tableList = [];
		//テーブル名取得
		$.each(data_array,function(index,val){
			
			if ( val.match(/PName=/)) {
				//strにhogeを含む場合の処理
			 	//console.log(val);
				tmp = val.replace("PName=", "");
				if(tmp != '') tableList.push(tmp);		
			}
		});
		//ソート
		tableList = tableList.sort();
		
		console.log(tableList);
		//テーブル名一覧作成
		$.each(tableList,function(index,val){
			tableName += val + '\n';					   
		});
		
		$('#t_area').val(tableName);
		
	});

	
	
	var generateDTO = function(params){
		
		var className = "";
		var tableName = "";
		var tableLName = "";
		for(var i in params){
			tableName = getValue(params[i],"PName");
			if(tableName != ""){
				break;	
			}
		}
		for(var i in params){
			tableLName = getValue(params[i],"LName");
			if(tableLName != ""){
				break;	
			}
		}
		
		
		var tmp = tableName.split("_");
		
		for (var i = 0;i<tmp.length; i++) {
			className+=tmp[i].replace(/\b\w+\b/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1).toLowerCase();} );
		}
		
		className+="DTO";
		
		var paramters = "";
		var setName = "";
		var setGetMethord = "";
		var nowDate = new Date();
		nowDate = formatDate(nowDate,'YYYY/MM/DD');
		
		for(var i in params){
			var field = getValue(params[i],"Field");
			if(field != ""){
				var fieldInfo = field.split(",");
				var value = fieldInfo[1].substring(1,fieldInfo[1].length-1);
				var hint = fieldInfo[0].substring(1,fieldInfo[0].length-1);
				var type = (fieldInfo[2].substring(1, fieldInfo[2].indexOf('(')==-1?fieldInfo[2].length -1:fieldInfo[2].indexOf('('))).toUpperCase();
				
				if(types[type] == null){
					alert('タイプ「'+type+'」はまだ対応されていません!');
				}
				
				paramters += '		<cfargument name="' + value + '" type="' + types[type][0] + '" required="no" default="'+ types[type][2] + '" hint="' + hint + '" />\n';
				
				var tmp = value.split("_");
				var setN = "";
				for (var i = 0;i<tmp.length; i++) {
					setN+=tmp[i].replace(/\b\w+\b/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1);} );
				}
				setName += '		<cfset set' + setN + '(arguments.'+ value + ') />\n';
				
				setGetMethord += '\n	<!--- '+ value +' --->\n	<cffunction name="set' + setN + '" access="private" output="false" returntype="void">\n'
							+ '		<cfargument name="' + value 
							+ '" type="' + types[type][0] + '" required="yes" hint="' + hint + '" />\n		<cfset variables.instance.' + value 
							+ ' = arguments.' + value 
							+ ' />\n	</cffunction>\n\n	<cffunction name="get' + setN 
							+ '" access="public" output="false" returntype="' + types[type][0] + '">\n		<cfreturn variables.instance.'+ value 
							+ ' />\n	</cffunction>\n';
			}
		}

		
		var strDTO='<!---\n	Name         : '+dsn+'_cfc/dao/'+ className + '.cfc\n	Author       : '+authorName+'\n	Created      : ' + nowDate + '\n	Last Updated : ' + nowDate + '\n	History      : \n	Purpose		 : ' + tableLName + 'のDTO(Data Transfer Object)\n\n--->\n<cfcomponent>\n	<cfprocessingdirective pageencoding="UTF-8">\n	<cfprocessingdirective suppresswhitespace="yes">\n	\n	<cfscript>\n	Variables.SA = "";	\n	Variables.StringUtil = "";\n	</cfscript>		\n	\n	<cffunction name="init" access="public" returntype="' + className + '">		\n'+
		paramters +
		'\n		<cfargument name="COMMON_SERVICE_ADDRESS" type="string" required="yes" hint="Application.COMMON_SERVICE_ADDRESS" />\n				\n\n		<!--- 文字列ユーティリティーの生成 --->		\n		<cfset Variables.CSA = arguments.COMMON_SERVICE_ADDRESS />		\n		<cfset Variables.StringUtil = 	CreateObject("component","#Variables.CSA#.StringUtil").init() />\n				\n		<!--- 変数をプロテクト --->		\n		<cfset variables.instance = structNew() />	\n'+ 
		setName +
		'		<cfreturn this />\n	</cffunction>	\n'+ 
		setGetMethord +
		'\n	</cfprocessingdirective>\n</cfcomponent>';
		
		createFile(className,strDTO);
		
	}
	
	var generateDAO = function(params){
		
		var className = "";
		var tableName = "";
		var tableLName = "";
		for(var i in params){
			tableName = getValue(params[i],"PName");
			if(tableName != ""){
				break;	
			}
		}
		for(var i in params){
			tableLName = getValue(params[i],"LName");
			if(tableLName != ""){
				break;	
			}
		}
		
		
		var tmp = tableName.split("_");
		//className+=tmp[0];
		
		for (var i = 0;i<tmp.length; i++) {
			className+=tmp[i].replace(/\b\w+\b/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1).toLowerCase();} );
		}
		
		var paramters = "",
			setName = "",
			setGetMethord = "",
			nowDate = new Date(),
			fields = "",
			fieldValue = "",
			fieldValueUp = "",
			whereField = "",
			createField = "",
			readStr1 = "";
		
		nowDate = formatDate(nowDate,'YYYY/MM/DD');
		
		for(var i in params){
			var field = getValue(params[i],"Field");
			if(field != ""){
				var fieldInfo = field.split(",");
				var value = fieldInfo[1].substring(1,fieldInfo[1].length-1);
				var hint = fieldInfo[0].substring(1,fieldInfo[0].length-1);
				var type = fieldInfo[2].substring(1, fieldInfo[2].indexOf('(')==-1?fieldInfo[2].length -1:fieldInfo[2].indexOf('(')).toUpperCase();
				var isKey = (fieldInfo[4] != "");
				
				if(types[type] == null){
					alert('タイプ「'+type+'」はまだ対応されていません!');
				}
				
				var tmp = value.split("_");
				var setN = "";
				for (var i = 0;i<tmp.length; i++) {
					setN+=tmp[i].replace(/\b\w+\b/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1);} );
				}
				
				readStr1 += '				'+ value +' = qRead.'+ value +',\n';
				fields += '				'+ value +',\n';
				fieldValue += '				'+ value +' = <cfqueryparam value="#arguments.dto.get' + setN + '()#" cfsqltype="' + types[type][1] + '" />,\n';
				
				//更新時は「entry_date」の項目削除
				if(value != 'entry_date'){
					fieldValueUp += '				'+ value +' = <cfqueryparam value="#arguments.dto.get' + setN + '()#" cfsqltype="' + types[type][1] + '" />,\n';
				}
				if(isKey){
					whereField  += '				AND '+ value +' = <cfqueryparam value="#arguments.dto.get' + setN + '()#" cfsqltype="' + types[type][1] + '" />\n';
				}
				createField += '				<cfqueryparam value="#arguments.dto.get' + setN + '()#" cfsqltype="' + types[type][1] + '" />,\n';
			}
		}
		
		fields = fields.substring(0,fields.length -2);
		fieldValue = fieldValue.substring(0,fieldValue.length -2);
		fieldValueUp = fieldValueUp.substring(0,fieldValueUp.length -2);
		whereField = '				' + whereField.substring(8,whereField.length-1);
		createField = createField.substring(0,createField.length -2);
		
		//読み込み
		var readStr = '\n	<!--- 読み込み --->\n	<cffunction name="read" access="public" output="false" returntype="boolean">\n		<cfargument name="dto" required="yes" type="' + className + 'DTO" hint="このオブジェクトに結果が格納される。" />\n		<cfscript>\n		var qRead = "";\n		</cfscript>\n		<cfquery name="qRead" datasource="#Variables.DSN#">\n			SELECT\n';
		readStr += fields + '\n			FROM\n				' + tableName + '\n			WHERE\n' + whereField + '\n		</cfquery>\n		<cfif qRead.recordCount eq 1>\n			<cfset arguments.dto.init(\n' + readStr1 + '				COMMON_SERVICE_ADDRESS=Variables.CSA\n			) />\n			<cfreturn true />\n		</cfif>\n		<cfreturn false />\n	</cffunction>\n';
		
		//新規
		var createStr = '\n	<!--- 新規 --->\n	<cffunction name="create" access="public" output="false" returntype="boolean">\n		<cfargument name="dto" required="yes" type="' + className + 'DTO" />\n		<cfscript>\n		var qCreate = "";\n		</cfscript>\n		<cfquery name="qCreate" datasource="#Variables.DSN#">\n			INSERT INTO\n				' + tableName + '\n				(\n';
		createStr += fields + '\n				)\n			VALUES\n				(\n' + createField + '\n				)\n		</cfquery>\n		<cfreturn true />\n	</cffunction>\n';
		
		//更新
		var updateStr = '\n	<!--- 更新 --->\n	<cffunction name="update" access="public" output="true" returntype="boolean">\n		<cfargument name="dto" required="yes" type="' + className + 'DTO" />\n		<cfscript>\n		var qUpdate = "";\n		</cfscript>\n		<cfquery name="qUpdate" datasource="#Variables.DSN#">\n			UPDATE\n				' + tableName + '\n			SET\n'; 
		updateStr += fieldValueUp + '\n			WHERE\n' + whereField + '\n		</cfquery>\n		<cfreturn true />\n	</cffunction>\n';
		
		//削除
		var deleteStr = '\n	<!--- 削除 --->\n	<cffunction name="delete" access="public" output="true" returntype="boolean">\n		<cfargument name="dto" required="yes" type="' + className + 'DTO" />\n		<cfscript>\n		var qDelete = "";\n		</cfscript>\n		<cfquery name="qDelete" datasource="#Variables.DSN#">\n			DELETE\n			FROM\n				' + tableName + '\n			WHERE\n';
		deleteStr += whereField + '\n		</cfquery>\n		<cfreturn true />\n	</cffunction>\n';
		

		className += "DAO";
		
		var strDAO = '<!---\n	Name         : '+dsn+'_cfc/dao/' + className + '.cfc\n	Author       : '+authorName+'\n	Created      : ' + nowDate + '\n	Last Updated : ' + nowDate + '\n	History      : \n	Purpose		 : ' + tableLName + 'のDAO(Data Access Object)\n\n--->\n<cfcomponent>\n	<cfprocessingdirective pageencoding="UTF-8">\n	<cfprocessingdirective suppresswhitespace="yes">\n	\	<cffunction name="init" access="public" returntype="'+ className +'" hint="コンストラクタ">\n		<cfargument name="DSN" required="yes" type="string" hint="データソース" />\n		<cfargument name="COMMON_SERVICE_ADDRESS" required="yes" type="string" hint="サービスアドレス" />\n			<cfset Variables.DSN = arguments.DSN />\n			<cfset Variables.CSA = arguments.COMMON_SERVICE_ADDRESS />\n		<cfreturn this />\n	</cffunction>\n'
		+ readStr
		+ createStr
		+ updateStr
		+ deleteStr
		+ '\n	</cfprocessingdirective>\n</cfcomponent>';

		createFile(className,strDAO);
		
	}
	
	function createFile(tableName, contents){
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var temp = path+tableName+'.cfc';
		var folder = fso.GetParentFolderName(temp);
		
		if (!fso.FolderExists(folder))
		{
			fso.CreateFolder(folder);
		}
		
		var s = fso.CreateTextFile(temp, true);
		s.WriteLine(contents);
		s.Close();   

	}
	
	

});

 类似资料: