1 34 package smallsql.database; 35 36 import java.io.ByteArrayOutputStream ; 37 38 39 42 public class ExpressionFunctionInsert extends ExpressionFunctionReturnP1StringAndBinary { 43 44 final int getFunction() { 45 return SQLTokenizer.INSERT; 46 } 47 48 49 final boolean isNull() throws Exception { 50 return param1.isNull() || param2.isNull() || param3.isNull() || param4.isNull(); 51 } 52 53 54 final byte[] getBytes() throws Exception { 55 if(isNull()) return null; 56 byte[] bytes = param1.getBytes(); 57 int start = Math.min(Math.max( 0, param2.getInt() - 1), bytes.length ); 58 int length = Math.min(param3.getInt(), bytes.length ); 59 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 60 buffer.write(bytes,0,start); 61 buffer.write(param4.getBytes()); 62 if(length < 0) 63 throw Utils.createSQLException("Invalid length '"+length + "' in function INSERT"); 64 buffer.write(bytes, start+length, bytes.length-start-length); 65 return buffer.toByteArray(); 66 } 67 68 69 final String getString() throws Exception { 70 if(isNull()) return null; 71 String str = param1.getString(); 72 int start = Math.min(Math.max( 0, param2.getInt() - 1), str.length() ); 73 int length = Math.min(param3.getInt(), str.length() ); 74 StringBuffer buffer = new StringBuffer (); 75 buffer.append(str.substring(0,start)); 76 buffer.append(param4.getString()); 77 if(length < 0) 78 throw Utils.createSQLException("Invalid length '"+length + "' in function INSERT"); 79 buffer.append(str.substring(start+length)); 80 return buffer.toString(); 81 } 82 83 84 int getPrecision() { 85 return param1.getPrecision()+param2.getPrecision(); 86 } 87 88 89 90 } 91 | Popular Tags |