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

org.yx.db.mapper.NamedExecutor 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.mapper;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yx.db.sql.InsertResult;
import org.yx.db.sql.MapedSql;
import org.yx.db.sql.SqlBuilder;
import org.yx.db.visit.Visitors;
import org.yx.exception.SumkException;

/**
 * 这里的sql,占位符不是?,而是#{**},里面的name,与map的key要一一对应,忽略大小写
* 大小写敏感 * * @author 游夏 * */ public final class NamedExecutor { private static class InnerSqlBuilder implements SqlBuilder { private final Map map; private final SqlParser sqlParser; InnerSqlBuilder(SqlParser sql, Map map) { this.sqlParser = sql; this.map = map == null ? Collections.emptyMap() : new HashMap<>(map); } @Override public MapedSql toMapedSql() throws Exception { return sqlParser.toMapedSql(map); } } private static InnerSqlBuilder createSqlBuilder(SqlParser sql, Map map) { return new InnerSqlBuilder(sql, map); } public static int execute(SqlParser sql, Map map) { try { return Visitors.modifyVisitor.visit(createSqlBuilder(sql, map)); } catch (Exception e) { throw SumkException.create(e); } } public static InsertResult insertWithAutoGeneratedKeys(SqlParser sql, Map map) { try { return Visitors.insertWithAutoGeneratedKeysVisitor.visit(createSqlBuilder(sql, map)); } catch (Exception e) { throw SumkException.create(e); } } public static List> list(SqlParser sql, Map map) { try { return Visitors.queryVisitor.visit(createSqlBuilder(sql, map)); } catch (Exception e) { throw SumkException.create(e); } } public static List listInArray(SqlParser sql, Map map) { try { return Visitors.arrayListQueryVisitor.visit(createSqlBuilder(sql, map)); } catch (Exception e) { throw SumkException.create(e); } } public static List singleColumnList(SqlParser sql, Map map) { try { return Visitors.singleListQueryVisitor.visit(createSqlBuilder(sql, map)); } catch (Exception e) { throw SumkException.create(e); } } public static int count(SqlParser sql, Map map) { try { List list = Visitors.singleListQueryVisitor.visit(createSqlBuilder(sql, map)); Number n = (Number) list.get(0); return n.intValue(); } catch (Exception e) { throw SumkException.create(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy