1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * ExpressionFunctionInsert.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | * Created on 17.06.2006 |
33 | */ |
34 | package smallsql.database; |
35 | |
36 | import java.io.ByteArrayOutputStream; |
37 | |
38 | |
39 | /** |
40 | * @author Volker Berlin |
41 | */ |
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 | } |