Debian Codegraph @

src/main/org/h2/expression/function/CSVWriteFunction.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;
7
8 import java.sql.Connection;
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.ExpressionVisitor;
15 import org.h2.message.DbException;
16 import org.h2.tools.Csv;
17 import org.h2.value.TypeInfo;
18 import org.h2.value.Value;
19 import org.h2.value.ValueInteger;
20
21 /**
22 * A CSVWRITE function.
23 */
24 public final class CSVWriteFunction extends FunctionN {
25
26 public CSVWriteFunction() {
27 super(new Expression[4]);
28 }
29
30 @Override
31 public Value getValue(SessionLocal session) {
32 session.getUser().checkAdmin();
33 Connection conn = session.createConnection(false);
34 Csv csv = new Csv();
35 String options = getValue(session, 2);
36 String charset = null;
37 if (options != null && options.indexOf('=') >= 0) {
38 charset = csv.setOptions(options);
39 } else {
40 charset = options;
41 String fieldSeparatorWrite = getValue(session, 3);
42 String fieldDelimiter = getValue(session, 4);
43 String escapeCharacter = getValue(session, 5);
44 String nullString = getValue(session, 6);
45 String lineSeparator = getValue(session, 7);
46 setCsvDelimiterEscape(csv, fieldSeparatorWrite, fieldDelimiter, escapeCharacter);
47 csv.setNullString(nullString);
48 if (lineSeparator != null) {
49 csv.setLineSeparator(lineSeparator);
50 }
51 }
52 try {
53 return ValueInteger.get(csv.write(conn, args[0].getValue(session).getString(),
54 args[1].getValue(session).getString(), charset));
55 } catch (SQLException e) {
56 throw DbException.convert(e);
57 }
58 }
59
60 private String getValue(SessionLocal session, int index) {
61 return index < args.length ? args[index].getValue(session).getString() : null;
62 }
63
64 /**
65 * Sets delimiter options.
66 *
67 * @param csv
68 * the CSV utility instance
69 * @param fieldSeparator
70 * the field separator
71 * @param fieldDelimiter
72 * the field delimiter
73 * @param escapeCharacter
74 * the escape character
75 */
76 public static void setCsvDelimiterEscape(Csv csv, String fieldSeparator, String fieldDelimiter,
77 String escapeCharacter) {
78 if (fieldSeparator != null) {
79 csv.setFieldSeparatorWrite(fieldSeparator);
80 if (!fieldSeparator.isEmpty()) {
81 char fs = fieldSeparator.charAt(0);
82 csv.setFieldSeparatorRead(fs);
83 }
84 }
85 if (fieldDelimiter != null) {
86 char fd = fieldDelimiter.isEmpty() ? 0 : fieldDelimiter.charAt(0);
87 csv.setFieldDelimiter(fd);
88 }
89 if (escapeCharacter != null) {
90 char ec = escapeCharacter.isEmpty() ? 0 : escapeCharacter.charAt(0);
91 csv.setEscapeCharacter(ec);
92 }
93 }
94
95 @Override
96 public Expression optimize(SessionLocal session) {
97 optimizeArguments(session, false);
98 int len = args.length;
99 if (len < 2 || len > 8) {
100 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), "2..8");
101 }
102 type = TypeInfo.TYPE_INTEGER;
103 return this;
104 }
105
106 @Override
107 public String getName() {
108 return "CSVWRITE";
109 }
110
111 @Override
112 public boolean isEverything(ExpressionVisitor visitor) {
113 if (!super.isEverything(visitor)) {
114 return false;
115 }
116 switch (visitor.getType()) {
117 case ExpressionVisitor.DETERMINISTIC:
118 case ExpressionVisitor.QUERY_COMPARABLE:
119 case ExpressionVisitor.READONLY:
120 return false;
121 default:
122 return true;
123 }
124 }
125
126 }