package daruma.sql;

import java.lang.StringBuilder;

import java.io.ByteArrayOutputStream;


public class SQLStringConverter
{
	private	SQLStringConverter()
	{
	}

	/*
	public	static	String	escape( String  str )
	{
		// XXX: optimizable

		// XXX: check and test

		// XXX: upper/lower case
		return( str
			.replace( "%"     , "%25" )
			.replace( "."     , "%2e" )
			.replace( "/"     , "%2f" )
			.replace( "\\"    , "%5c" )
			.replace( "`"     , "%60" )
			.replace( "\""    , "%22" )
			.replace( "'"     , "%27" )
			.replace( ":"     , "%3a" )
			.replace( "\0x00" , "%00" )
			.replace( "\0xff" , "%ff" )
		      );
	}
	*/

	/*
	public	static	String	encodeStringValue( String  str )
	{
		StringBuilder	buf = new StringBuilder();

		for ( int  i = 0  ;  i < str.length()  ;  ++ i )
		{
			char	ch = str.charAt(i);

			if ( ch == '"'
			  || ch == '\''
			  || ch == '\\'
			  || ch == '\r'
			  || ch == '\n'
			  || ch == 0x1a // Control-Z
			  || ch == '\0' )
			{
				buf.append('\\');
			}

			buf.append(ch);
		}

		return( buf.toString() );
	}
	*/

	/*
	public	static	String	decodeStringValue( String  str )
	{
		StringBuilder	buf = new StringBuilder();

		boolean	escaped = false;

		for ( int  i = 0  ;  i < str.length()  ;  ++ i )
		{
			char	ch = str.charAt(i);

			if ( escaped )
			{
				escaped = false;

				buf.append(ch);
			}
			else if ( ch == '\\' )
			{
				escaped = true;
			}
			else
			{
				buf.append(ch);
			}
		}

		return( buf.toString() );
	}

	public	static	byte[]	encodeBlobValue( byte[]  data )
	{
		ByteArrayOutputStream	buf = new ByteArrayOutputStream();

		for ( int  i = 0  ;  i < data.length  ;  ++ i )
		{
			byte	b = data[i];

			if ( b == 0x22 // "
			  || b == 0x27 // '
			  || b == 0x5c // '\'
			  || b == 0x0d // CR
			  || b == 0x0a // LF
			  || b == 0x1a // Control-Z
			  || b == 0x00 )
			{
				buf.write(0x5c); // backslash
			}

			buf.write(b);
		}

		return( buf.toByteArray() );
	}
	*/
}
