|新しいページ|検索|ページ一覧|RSS|@ウィキご利用ガイド | 管理者にお問合せ
|ログイン|

SqlParser


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

WriteFile( DumpSqlNode( sn ) );
 
function DumpSqlNode( Sn, indent ){
	var indent = IsGiven(indent) ? indent+'	' : '';
 
	if( Sn.Type=='Bracket' ){
		return DumpBracket( Sn, indent );
	}
	else{
		return DumpElement( Sn, indent );
	}
 
	function toStringElement(e, indent){
		if( e instanceof SqlNode ){
			return DumpSqlNode( e, indent );
		}else{
			return indent + TrimLeft(e) ;
		}
	}
	function DumpElement( Sn, indent ){
		var yy = [];
		var ee = Sn.Elements;
 
		if( Sn.Type!=null ){
			yy.push( indent + Sn.Type );
		}
 
		if( !Sn.HasSqlNode() ){
			var zz = [];
			for( var i=0; i<ee.length; i++ ){
				zz.push( toStringElement(  ee[i], '' ) );
			}
			yy.push( zz.join(' ') );
		}else{
			for( var i=0; i<ee.length; i++ ){
				yy.push( toStringElement(  ee[i], indent ) );
			}
		}
		return yy.join( !Sn.HasSqlNode() ? ' ' : '\r\n' );
	}
	function DumpBracket( Sn, indent ){
		var yy = [];
		var ee = Sn.Elements;
 
		if( !Sn.HasSqlNode() ){
			yy.push( '(' );
			yy.push( ee.join(' ') );
			yy.push( ')' );
		}else{
			yy.push(indent + '(');
			for( var i=0; i<ee.length; i++ ){
				yy.push( toStringElement(  ee[i], indent ) );
			}
			yy.push(indent + ')');
		}
		return yy.join( !Sn.HasSqlNode() ? ' ' : '\r\n' );
	}
}
function SqlNode(Type_, Parent_){
	//Select, From, Where, OrderBy, GroupBy, Bracket
	this.Type     = Type_;
	this.Parent   = null;
	this.Elements = [];
 
	var ro;
	if(ro==null){
		ro = new RegExpOperator();
		ro.AddKey( {Name:'Select'         , RegExp:/Select /i} );
		ro.AddKey( {Name:'From'           , RegExp:/ From /i} );
		ro.AddKey( {Name:'Left Outer Join', RegExp:/Left +Outer +Join/i} );
		ro.AddKey( {Name:'Inner Join'     , RegExp:/Inner +Join/i} );
		ro.AddKey( {Name:'On'             , RegExp:/ On /i} );
		ro.AddKey( {Name:'Where'          , RegExp:/ Where /i} );
		ro.AddKey( {Name:'Order By'       , RegExp:/Order +By/i} );
		ro.AddKey( {Name:'Group By'       , RegExp:/Group +By/i} );
		ro.AddKey( {Name:'Bracket.From'   , RegExp:/\(/i} );
		ro.AddKey( {Name:'Bracket.To'     , RegExp:/\)/i} );
	}
 
	function Read( Sql ){
 
		Sql = Sql.replace(/  +/g, ' ');
 
		var r  = ro.Search(Sql);
		var rn = r.Name;
 
		if( rn!=null ){
 
			// --------sqlPrev--------r.From xxx r.To--------sqlNext--------
			var t       = this.Type;
			var sn      = new SqlNode( rn.split('.')[0] );
			var sqlPrev = Sql.substring( 0, r.From );
			var sqlNext = Sql.substring( r.To );
 
			this.AddElement( sqlPrev );
 
			if( rn=='Bracket.From' ){
				this.AddElement( sn );
			}
			else if( rn=='Bracket.To' ){
				sn = this.GetUpperContext();
			}
			else{
				if( t!=null && t!='Bracket' ){
					this.Parent.AddElement( sn );
				}else{
					this.AddElement( sn ); 
				}
			}
			sn.Read( sqlNext );
		}else{
			this.AddElement( Sql );
		}
	}
	function AddElement( e ){
		if( e!='' && (e+'').Rmv(' ')!='' ){
			if( e instanceof SqlNode ){
				e.Parent = this;
			}
			this.Elements.push( e );
			//ll.push( this.GetPath() +': '+ e.Type );
		}
	}
	function GetPath(){
		if( this.Parent!=null ){ return this.Parent.GetPath() +'.'+ this.Type; }
		return this.Type;
	}
	function GetUpperContext(){
		var p = this.Parent;
		return (this.Type=='Bracket') ? p : p.GetUpperContext();
	}
	function HasSqlNode(){
		var b = false;
		for( var i=0; i<this.Elements.length; i++ ){
			var e = this.Elements[i];
			if( e instanceof SqlNode ){
				b = b || ( e.Type=='Bracket' ? e.HasSqlNode() : true );
			}
		}
		//ll.push( this.GetPath() +': '+ b );
		return b;
	}
SqlNode.prototype.GetUpperContext = GetUpperContext;
SqlNode.prototype.GetPath    = GetPath;
SqlNode.prototype.Read       = Read;
SqlNode.prototype.AddElement = AddElement;
SqlNode.prototype.HasSqlNode = HasSqlNode;
}
 
function TrimLeft(xxx){
	if( xxx.charAt(0)==' ' ){
		return TrimLeft(xxx.substring(1));
	}
	return xxx;
}
 
function RegExpOperator(){
 
	this.RegExps = [];//{Name,RegExp}
 
	function AddKey( key ){
		this.RegExps.push(key);
	}
	function Search(text){
		var rn     = null;//Name
		var rpFrom = -1;//Position
		var rpTo   = -1;//Position
 
		for( var i=0; i<this.RegExps.length; i++ ){
			var r = this.RegExps[i]
			var p = text.search( r.RegExp );
			if( 0<=p ){
				if( rpFrom==-1 || p<rpFrom ){
					rn     = r.Name;
					rpFrom = RegExp.index;
					rpTo   = RegExp.lastIndex;
				}
			}
		}
		return { Name:rn, From:rpFrom, To:rpTo };
	}
RegExpOperator.prototype.AddKey = AddKey;
RegExpOperator.prototype.Search = Search;
}