cn.xdf.thinkutils.db2.util.sql.InsertSqlBuilder.java Source code

Java tutorial

Introduction

Here is the source code for cn.xdf.thinkutils.db2.util.sql.InsertSqlBuilder.java

Source

/*
 * Copyright (C) 2013  WhiteCat  (www.thinkandroid.cn)
 *
 * Licensed 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 cn.xdf.thinkutils.db2.util.sql;

import java.lang.reflect.Field;

import org.apache.http.NameValuePair;

import cn.xdf.thinkutils.db2.annotation.PrimaryKey;
import cn.xdf.thinkutils.db2.entity.ArrayListEx;
import cn.xdf.thinkutils.db2.util.DBUtils;
import cn.xdf.thinkutils.exception.DBException;
import cn.xdf.thinkutils.util.file.StringUtils;

/**
 * @Title TASqlBuilder
 * @Package com.ta.util.db.util.sql
 * @Description ?sql?
 * @author 
 * @date 2013-1-20
 * @version V1.0
 */
public class InsertSqlBuilder extends SqlBuilder {
    @Override
    public void onPreGetStatement() throws DBException, IllegalArgumentException, IllegalAccessException {
        // TODO Auto-generated method stub
        if (getUpdateFields() == null) {
            setUpdateFields(getFieldsAndValue(entity));
        }
        super.onPreGetStatement();
    }

    @Override
    public String buildSql() throws DBException, IllegalArgumentException, IllegalAccessException {
        // TODO Auto-generated method stub
        StringBuilder columns = new StringBuilder(256);
        StringBuilder values = new StringBuilder(256);
        columns.append("INSERT INTO ");
        columns.append(tableName).append(" (");
        values.append("(");
        ArrayListEx updateFields = getUpdateFields();
        if (updateFields != null) {
            for (int i = 0; i < updateFields.size(); i++) {
                NameValuePair nameValuePair = updateFields.get(i);
                columns.append(nameValuePair.getName());
                values.append(StringUtils
                        .isNumeric(nameValuePair.getValue() != null ? nameValuePair.getValue().toString() : "")
                                ? nameValuePair.getValue()
                                : "'" + nameValuePair.getValue() + "'");
                if (i + 1 < updateFields.size()) {
                    columns.append(", ");
                    values.append(", ");
                }
            }
        } else {
            throw new DBException("???");
        }
        columns.append(") values ");
        values.append(")");
        columns.append(values);
        return columns.toString();
    }

    /**
     * ,?
     * 
     * @return
     * @throws DBException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static ArrayListEx getFieldsAndValue(Object entity)
            throws DBException, IllegalArgumentException, IllegalAccessException {
        // TODO Auto-generated method stub
        ArrayListEx arrayList = new ArrayListEx();
        if (entity == null) {
            throw new DBException("?");
        }
        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {

            if (!DBUtils.isTransient(field)) {
                if (DBUtils.isBaseDateType(field)) {
                    PrimaryKey annotation = field.getAnnotation(PrimaryKey.class);
                    if (annotation != null && annotation.autoIncrement()) {

                    } else {
                        String columnName = DBUtils.getColumnByField(field);
                        field.setAccessible(true);
                        arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                                field.get(entity) == null ? null : field.get(entity).toString());
                    }

                }
            }
        }
        return arrayList;
    }
}