All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yx.db.visit.Visitors Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/**
 * Copyright (C) 2016 - 2030 youtongluan.
 *
 * 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 org.yx.db.visit;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.yx.db.DBType;
import org.yx.db.conn.ConnectionPool;
import org.yx.db.event.EventLane;
import org.yx.db.sql.InsertResult;
import org.yx.db.sql.MapedSql;
import org.yx.db.sql.PojoMeta;
import org.yx.db.sql.SelectBuilder;
import org.yx.db.sql.SqlBuilder;

public final class Visitors {

	private static interface Transform {
		T transFrom(ResultSet ret) throws Exception;
	}

	private static class QueryVisitor implements SumkDbVisitor {
		private final Transform transform;

		private QueryVisitor(Transform transform) {
			this.transform = transform;
		}

		@Override
		public T visit(SqlBuilder builder) throws Exception {
			MapedSql maped = builder.toMapedSql();
			Connection conn = ConnectionPool.get().connection(DBType.ANY);
			SumkStatement statement = SumkStatement.create(conn, maped);
			ResultSet ret = statement.executeQuery();
			T list = this.transform.transFrom(ret);
			statement.close();
			return list;
		}

	}

	public static final SumkDbVisitor modifyVisitor = builder -> {
		Connection conn = ConnectionPool.get().connection(DBType.WRITE);
		MapedSql maped = builder.toMapedSql();
		SumkStatement statement = SumkStatement.create(conn, maped);
		int ret = statement.executeUpdate();
		statement.close();
		EventLane.pubuish(conn, maped.getEvent());
		return ret;
	};

	public static final SumkDbVisitor insertWithAutoGeneratedKeysVisitor = builder -> {
		Connection conn = ConnectionPool.get().connection(DBType.WRITE);
		MapedSql maped = builder.toMapedSql();
		SumkStatement statement = SumkStatement.createAutoGenerateKeyStatement(conn, maped);
		int count = statement.executeUpdate();
		ResultSet rs = statement.getGeneratedKeys();
		List keys = new ArrayList<>();
		InsertResult result = new InsertResult(count, keys);
		if (rs != null && rs.getMetaData().getColumnCount() > 0) {
			if (rs.next()) {
				keys.add(rs.getLong(1));
			}
		}
		rs.close();
		statement.close();

		return result;
	};

	public static final SumkDbVisitor>> queryVisitorForORM = builder -> {
		MapedSql maped = builder.toMapedSql();
		Connection conn = ConnectionPool.get().connection(DBType.ANY);
		SumkStatement statement = SumkStatement.create(conn, maped);
		ResultSet ret = statement.executeQuery();
		PojoMeta pm = ((SelectBuilder) builder).parsePojoMeta(true);
		List> list = ResultSetUtils.toMapList(ret, pm);
		statement.close();
		return list;
	};

	public static final SumkDbVisitor>> queryVisitor = new QueryVisitor<>(
			ResultSetUtils::toMapList);

	public static final SumkDbVisitor> singleListQueryVisitor = new QueryVisitor<>(ResultSetUtils::toList);

	public static final SumkDbVisitor> arrayListQueryVisitor = new QueryVisitor<>(
			ResultSetUtils::toObjectArrayList);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy