• 一个用于将sql脚本转换成实体类的js代码


    以前写过一段C#,苦于编译才能用。这样的小工具最好是用脚本语言来编写,易于执行,也易于修改。

    js 代码 convert.js

    --------------------------------------------------

    String.prototype.trim=function(){
      return this.replace(/(^s*)|(s*$)/g, "");
    }
    String.prototype.ltrim=function(){
      return this.replace(/(^s*)/g,"");
    }
    String.prototype.rtrim=function(){
      return this.replace(/(s*$)/g,"");
    }
    String.prototype.startWith=function(str){
      var reg = new RegExp("^" + str);
      return reg.test(this);
    }
    
    
    var ConvertCode = {
        content: "",
        run: function(){
            console.writeline(this.content);
            
            if( !this.content) return;
            
            var lines = this.content.split('
    ');
            var ret = "";
            for(var i=0; i<lines.length; i++)
            {
                var dataInfo = this.convertLine(lines[i]);
                if(!dataInfo) continue;
                
                ret = ret + "///<summary>
    ";
                ret = ret + "///
    ";
                ret = ret + "///<summary>
    ";
                ret = ret + '[DBColumn("' + dataInfo.column + '")]
    ';
                ret = ret + "public " + dataInfo.dataType + " " + dataInfo.name + "{get;set;}
    
    ";
            }
            
            return ret;
        },
        
        convertLine: function(line){
            line = line.trim();
            if(line.length == 0) return null;
            
            var tokens = line.split(/s+/);
            if(tokens.length <= 1) return null;
            
            return {
                column: tokens[0],
                name: this.toNormalName(tokens[0]),
                dataType: this.toDataType(tokens[1])
            };
        },
        
        toNormalName: function(raw) {
            var tokens = raw.split('_');
            
            var ret = "";
            for(var i=0; i<tokens.length; i++)
            {
                ret = ret + tokens[i].substr(0,1).toUpperCase() + tokens[i].substr(1);
            }
            
            return ret;
        },
        
        toDataType: function(d) {
            if(!d) return null;
            
            if(d.startWith("varchar") || d.startWith("char")) return "string";
            if(d.startWith("int")) return "int";
            if(d.startWith("number")) return "decimal";
            if(d.startWith("date")) return "DateTime";
            return null;
        }
    }

    html 代码用于调用它
    ----------------------------------

    <html>
    <head>
        <meta charset='gbk' />
        <script type='text/javascript' src="convert.js"></script>
        <script language="javascript" type='text/javascript'>
        
        var doparse = function(){
            var parser = ConvertCode;
            parser.content = document.getElementById('content').value;
            
            var result = parser.run(content);
    
            document.getElementById('divResult').value = result;
            console.writeline( document.getElementById('divResult').value);
        }
        </script>
    </head>
    <body>
        <div><input type="button" value="转换" onclick="javascript:doparse();" style="50px; height: 30px;"/></div>
        <div style="float:left;  500px;">
            <textarea id='content' style="500px; height:600px;"></textarea>
        </div>
        <div style="float:left; 500px; margin-left:30px;" >
            <textarea id="divResult" style="500px; height:600px;"></textarea>
        </div>
        
    </body>
    </html>
  • 相关阅读:
    小知识
    对NSArray中自定义的对象进行排序
    照片浏览滑动效果UIScrollView和UIPageControl组合
    label 设置行间距 字间距
    即使通讯聊天界面搭建----iOS
    ios8推送新增
    UITableviewCell滑动出现多级的控制按钮
    iOS 去掉html标签 留下原本的字符串
    平时常用的小知识点 (不断更新中)
    IOS UI多线程 NSThread 下载并显示图片到UIImageView
  • 原文地址:https://www.cnblogs.com/qkhh/p/4188006.html
Copyright © 2020-2023  润新知