Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package com.aliyun.odps.jdbc;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.aliyun.odps.OdpsException;
import com.aliyun.odps.data.Binary;
import com.aliyun.odps.data.Char;
import com.aliyun.odps.data.Varchar;
import com.aliyun.odps.jdbc.utils.JdbcColumn;
import com.aliyun.odps.sqa.commandapi.utils.SqlParserUtil;
import com.aliyun.odps.tunnel.TunnelException;
public class OdpsPreparedStatement extends AbstractOdpsPreparedStatement {
private final String TABLE_NAME = "((\\w+\\.)?\\w+)"; // "proj.name" or "name"
private final String PREP_VALUES = "\\((\\s*\\?\\s*)(,\\s*\\?\\s*)*\\)"; // "(?)" or "(?,?,...)"
private final String SPEC_COLUMN = "(\\([^()]*\\))?"; // "" or "(a,b,c)"
private final String
SPEC_PARTITION =
"\\((\\s*\\w+\\s*=\\s*(\\w+|'\\w+')(\\s*,\\s*\\w+\\s*=\\s*(\\w+|'\\w+')\\s*)*\\s*)\\)";
// (p1=a) or (p1=a,p2=b,...) or (p1='a') ...
private final String PREP_INSERT_WITH_SPEC_PARTITION =
"(?i)^" + "\\s*" + "insert" + "\\s+" + "into" + "\\s+" + TABLE_NAME + "\\s*" + SPEC_COLUMN
+ "\\s+" + "partition" + SPEC_PARTITION + "\\s+" + "values" + "\\s*" + PREP_VALUES + "\\s*"
+ ";?\\s*$";
private final String EXAMPLE =
"INSERT INTO table [(c1, c2)] [partition(p1=a,p2=b,...)] VALUES (?, ?);";
private final String PREP_INSERT_WITHOUT_SPEC_PARTITION =
"(?i)^" + "\\s*" + "insert" + "\\s+" + "into" + "\\s+" + TABLE_NAME + "\\s*" + SPEC_COLUMN
+ "\\s+" + "values" + "\\s*" + PREP_VALUES + "\\s*" + ";?\\s*$";
private static final String
SQL_REGEX =
"(')|(--)|(/\\*(?:.|[\\n\\r])*?\\*/)|(\\b(select|update|and|or|delete|insert|trancate|char|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
private static final Pattern SQL_PATTERN = Pattern.compile(SQL_REGEX, Pattern.CASE_INSENSITIVE);
static ThreadLocal
DATETIME_FORMAT =
ThreadLocal.withInitial(() -> new SimpleDateFormat(JdbcColumn.ODPS_DATETIME_FORMAT));
static ThreadLocal
DATE_FORMAT =
ThreadLocal.withInitial(() -> new SimpleDateFormat(JdbcColumn.ODPS_DATE_FORMAT));
static ThreadLocal
LOCAL_DATE_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_DATE_FORMAT)
.withZone(ZoneId.systemDefault()));
static ThreadLocal
ZONED_DATETIME_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_DATETIME_FORMAT)
.withZone(ZoneId.systemDefault()));
static ThreadLocal
ZONED_TIMESTAMP_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_TIMESTAMP_FORMAT)
.withZone(ZoneId.systemDefault()));
/**
* The prepared sql template (immutable). e.g. insert into table FOO select * from BAR where id =
* ? and weight = ?
*/
private final String sql;
private boolean parsed = false;
private String tableBatchInsertTo;
private String projectName;
private String schemaName;
private String tableName;
private String partitionSpec;
private List specificColumns;
private int parametersNum;
private DataUploader uploader;
/**
* The parameters for the prepared sql (index=>parameter). The parameter is stored as Java objects
* and lazily casted into String when submitting sql. The executeBatch() call will utilize it to
* upload data to ODPS via tunnel.
*/
private HashMap parameters = new HashMap<>();
// When addBatch(), compress the parameters into a row
private List