Debian Codegraph @

src/main/org/h2/expression/function/table/CSVReadFunction.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.expression.function.table;
7
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.engine.SessionLocal;
13 import org.h2.expression.Expression;
14 import org.h2.expression.function.CSVWriteFunction;
15 import org.h2.message.DbException;
16 import org.h2.result.ResultInterface;
17 import org.h2.schema.FunctionAlias.JavaMethod;
18 import org.h2.tools.Csv;
19 import org.h2.util.StringUtils;
20
21 /**
22 * A CSVREAD function.
23 */
24 public final class CSVReadFunction extends TableFunction {
25
26 public CSVReadFunction() {
27 super(new Expression[4]);
28 }
29
30 @Override
31 public ResultInterface getValue(SessionLocal session) {
32 session.getUser().checkAdmin();
33 String fileName = getValue(session, 0);
34 String columnList = getValue(session, 1);
35 Csv csv = new Csv();
36 String options = getValue(session, 2);
37 String charset = null;
38 if (options != null && options.indexOf('=') >= 0) {
39 charset = csv.setOptions(options);
40 } else {
41 charset = options;
42 String fieldSeparatorRead = getValue(session, 3);
43 String fieldDelimiter = getValue(session, 4);
44 String escapeCharacter = getValue(session, 5);
45 String nullString = getValue(session, 6);
46 CSVWriteFunction.setCsvDelimiterEscape(csv, fieldSeparatorRead, fieldDelimiter, escapeCharacter);
47 csv.setNullString(nullString);
48 }
49 char fieldSeparator = csv.getFieldSeparatorRead();
50 String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true);
51 try {
52 // TODO create result directly
53 return JavaMethod.resultSetToResult(session, csv.read(fileName, columns, charset), Integer.MAX_VALUE);
54 } catch (SQLException e) {
55 throw DbException.convert(e);
56 }
57 }
58
59 private String getValue(SessionLocal session, int index) {
60 return getValue(session, args, index);
61 }
62
63 @Override
64 public void optimize(SessionLocal session) {
65 super.optimize(session);
66 int len = args.length;
67 if (len < 1 || len > 7) {
68 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), "1..7");
69 }
70 }
71
72 @Override
73 public ResultInterface getValueTemplate(SessionLocal session) {
74 session.getUser().checkAdmin();
75 String fileName = getValue(session, args, 0);
76 if (fileName == null) {
77 throw DbException.get(ErrorCode.PARAMETER_NOT_SET_1, "fileName");
78 }
79 String columnList = getValue(session, args, 1);
80 Csv csv = new Csv();
81 String options = getValue(session, args, 2);
82 String charset = null;
83 if (options != null && options.indexOf('=') >= 0) {
84 charset = csv.setOptions(options);
85 } else {
86 charset = options;
87 String fieldSeparatorRead = getValue(session, args, 3);
88 String fieldDelimiter = getValue(session, args, 4);
89 String escapeCharacter = getValue(session, args, 5);
90 CSVWriteFunction.setCsvDelimiterEscape(csv, fieldSeparatorRead, fieldDelimiter, escapeCharacter);
91 }
92 char fieldSeparator = csv.getFieldSeparatorRead();
93 String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true);
94 ResultInterface result;
95 try (ResultSet rs = csv.read(fileName, columns, charset)) {
96 result = JavaMethod.resultSetToResult(session, rs, 0);
97 } catch (SQLException e) {
98 throw DbException.convert(e);
99 } finally {
100 csv.close();
101 }
102 return result;
103 }
104
105 private static String getValue(SessionLocal session, Expression[] args, int index) {
106 return index < args.length ? args[index].getValue(session).getString() : null;
107 }
108
109 @Override
110 public String getName() {
111 return "CSVREAD";
112 }
113
114 @Override
115 public boolean isDeterministic() {
116 return false;
117 }
118
119 }