All Projects → mhewedy → spwrap

mhewedy / spwrap

Licence: Apache-2.0 license
Simple Stored Procedure call wrapper with no framework dependencies.

Programming Languages

java
68154 projects - #9 most used programming language
groovy
2714 projects
SQLPL
141 projects
PLpgSQL
1095 projects
PLSQL
303 projects
shell
77523 projects

Projects that are alternatives of or similar to spwrap

ksql-jdbc-driver
JDBC driver for Apache Kafka
Stars: ✭ 85 (+254.17%)
Mutual labels:  jdbc
mongodb-jdbc-driver
MongoDB JDBC Driver | DbSchema MongoDB Designer
Stars: ✭ 47 (+95.83%)
Mutual labels:  jdbc
tamer
Standalone alternatives to Kafka Connect Connectors
Stars: ✭ 42 (+75%)
Mutual labels:  jdbc
blade-jdbc
🐜 move to https://github.com/biezhi/anima
Stars: ✭ 36 (+50%)
Mutual labels:  jdbc
apache-flink-jdbc-streaming
Sample project for Apache Flink with Streaming Engine and JDBC Sink
Stars: ✭ 22 (-8.33%)
Mutual labels:  jdbc
dice-fairlink
JDBC Driver for read-only connections on AWS RDS Clusters
Stars: ✭ 33 (+37.5%)
Mutual labels:  jdbc
shoppingPro
此项目是橘子网上商城的功能,按照我自己的想法实现的一个自营模式的商城应用。 目前实现了前台的功能,后台的功能,主要有登录,注册,商品展示,购物车等模块。后台面有添加商品,删除商品,商品上下架,订单管理,管理员管理,实现了E支付功能和百度地图功能.为了便于此项目日后的完善和扩展, 我采用了前后端分离的架构设计。前端主要使用javaScript,jquery,AjAX,JSP构建的单页web应用, 通过使用指令,服务,模板,控制器完成前端的MVC分层,各个模板页面的效果实现则使用了基本的CSS3等技术, 结合JS完成页面的基本交互效果。由于有实现数据库和后台模块等功能,通过AJax请求完成整个前台功能的数据交互, 后期再考虑实现一个后台管理系统及数据库系统,还有前端页面资源的异步加载及优化。
Stars: ✭ 28 (+16.67%)
Mutual labels:  jdbc
KBC--Kaun-Banega-Crorepati
It is Core Java based Game based on Indian television game show having best animation as possible in Core java 5000+ lines
Stars: ✭ 38 (+58.33%)
Mutual labels:  jdbc
babashka-sql-pods
Babashka pods for SQL databases
Stars: ✭ 64 (+166.67%)
Mutual labels:  jdbc
NiuBi
👊 一个Java文件也能干大事系列
Stars: ✭ 34 (+41.67%)
Mutual labels:  jdbc
APICorePayLots
Web API designed in Asp.NET Core 3.1, using Dapper and Entity Framework Core, Repository Pattern, Identity
Stars: ✭ 13 (-45.83%)
Mutual labels:  stored-procedures
nifi-sqllookup-services-bundle
NIFI controllers for SQL record and attributes lookups with built-in caching
Stars: ✭ 18 (-25%)
Mutual labels:  jdbc
soda-for-java
SODA (Simple Oracle Document Access) for Java is an Oracle library for writing Java apps that work with JSON (and not only JSON!) in the Oracle Database. SODA allows your Java app to use the Oracle Database as a NoSQL document store.
Stars: ✭ 61 (+154.17%)
Mutual labels:  jdbc
neo4j-jdbc
JDBC driver for Neo4j
Stars: ✭ 110 (+358.33%)
Mutual labels:  jdbc
table2pojo
Generate POJOs for database table/columns
Stars: ✭ 16 (-33.33%)
Mutual labels:  jdbc
hasor
Hasor是一套基于 Java 语言的开发框架,区别于其它框架的是 Hasor 有着自己一套完整的体系,同时还可以和先有技术体系做到完美融合。它包含:IoC/Aop容器框架、Web框架、Jdbc框架、RSF分布式RPC框架、DataQL引擎,等几块。
Stars: ✭ 938 (+3808.33%)
Mutual labels:  jdbc
person-directory
A framework for resolving persons and attributes from a variety of underlying sources.
Stars: ✭ 26 (+8.33%)
Mutual labels:  jdbc
Neo
Orm框架:基于ActiveRecord思想开发的至简化的java的Orm框架
Stars: ✭ 35 (+45.83%)
Mutual labels:  jdbc
zuul-route-jdbc-spring-cloud-starter
No description or website provided.
Stars: ✭ 23 (-4.17%)
Mutual labels:  jdbc
eth-jdbc-connector
Ethereum JDBC driver implements a pure java, type 4 JDBC driver that executes SQL queries on Ethereum Blockchain.
Stars: ✭ 19 (-20.83%)
Mutual labels:  jdbc

spwrap

Stored Procedure caller; simply execute stored procedure from java code.
Compatible with jdk >= 1.5, with only single dependency (slf4j-api)

Build Status Coverage Status

Step 0: Create Stored Procedures:

Suppose you have 3 Stored Procedures to save customer to database, get customer by id and list all customer.

For example here's SP code using HSQL:

CREATE PROCEDURE create_customer(firstname VARCHAR(50), lastname VARCHAR(50), OUT custId INT, 
        OUT code SMALLINT, OUT msg VARCHAR(50))
    MODIFIES SQL DATA DYNAMIC RESULT SETS 1
    BEGIN ATOMIC
        INSERT INTO CUSTOMERS VALUES (DEFAULT, firstname, lastname);
        SET custId = IDENTITY();
        SET code = 0 -- success;
    END

CREATE PROCEDURE get_customer(IN custId INT, OUT firstname VARCHAR(50), OUT lastname VARCHAR(50), 
        OUT code SMALLINT, OUT msg VARCHAR(50)) 
    READS SQL DATA
    BEGIN ATOMIC
        SELECT first_name, last_name INTO firstname, lastname FROM customers WHERE id = custId;
        SET code = 0 -- success;
    END

CREATE PROCEDURE list_customers(OUT code SMALLINT, OUT msg VARCHAR(50))
    READS SQL DATA DYNAMIC RESULT SETS 1
    BEGIN ATOMIC
        DECLARE result CURSOR FOR SELECT id, first_name firstname, last_name lastname FROM CUSTOMERS;
        OPEN result;
        SET code = 0 -- success;
    END

NOTE: Every Stored Procedure by default need to have 2 additional Output Parameters at the end of its parameter list. One of type SMALLINT and the other of type VARCHAR for result code and message respectively, where result code 0 means success. You can override the 0 value or remove this default behviour at all, see the configuration wiki page.

Step 1: Create The DAO interface:

public interface CustomerDAO {

    @StoredProc("create_customer")
    void createCustomer(@Param(VARCHAR) String firstName, @Param(VARCHAR) String lastName);

    @StoredProc("get_customer")
    Customer getCustomer(@Param(INTEGER) Integer id);	
	
    @StoredProc("list_customers")
    List<Customer> listCustomers();
}

Step 2: Map Output parameters and Result set (if any):

Before start using the CustomerDAO interface, one last step is required, to map the result of the get_customer and list_customers stored procedures.

  • get_customer stored procs returns the result as Output Parameters, so you need to have a class to implement TypedOutputParamMapper interface.
  • list_customers stored proc returns the result as Result Set, so you need to have a class to implement ResultSetMapper interface.

Let's create Customer class to implement both interfaces (for getCustomer and listCustomers):

public class Customer implements TypedOutputParamMapper<Customer>, ResultSetMapper<Customer> {

	private Integer id;
	private String firstName, lastName;

	public Customer() {
	}

	public Customer(Integer id, String firstName, String lastName) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer id() {
		return id;
	}

	public String firstName() {
		return firstName;
	}

	public String lastName() {
		return lastName;
	}
	
	 //You can acess result set columns/output parameters by name as well
	@Override
	public Customer map(Result<?> result) {
		if (result.isResultSet()) {
			return new Customer(result.getInt(1), result.getString(2), result.getString(3));
		} else {
			return new Customer(null, result.getString(1), result.getString(2));
		}
	}

	// for TypedOutputParamMapper
	@Override
	public List<Integer> getTypes() {
		return Arrays.asList(VARCHAR, VARCHAR);
	}
}

See more examples on spwrap-examples github project and read more about Mappers in the wiki.

NOTE: If your stored procedure returns a single output parameter with no result set, then you can use the @Scalar annotation and you will not need to provide a Mapper class yourself, the mapping will done for you. see wiki page about scalars for more

NOTE: You can use @AutoMappers to do the mapping for you instead of Mapping the Result object into your domain object yourself.

Step 3: Using the DAO interface:

Now you can start using the interface to call the stored procedures:

DataSource dataSource = ...
DAO dao = new DAO.Builder(dataSource).build();
CustomerDAO customerDao = dao.create(CustomerDAO.class);

customerDao.createCustomer("Abdullah", "Muhammad");
Customer customer = customerDao.getCustomer1(1);
Assert.assertEquals("Abdullah", customer.firstName());

Installation

Gradle:

compile group: 'com.github.mhewedy', name: 'spwrap', version: '0.0.20'

Maven:

<dependency>
    <groupId>com.github.mhewedy</groupId>
    <artifactId>spwrap</artifactId>
    <version>0.0.20</version>
</dependency>

Additional staff:

  • If you don't supply the stored procedure name to @StoredProc, it will use the method name by default.

  • @Param annotation should used for ALL method parameters and accepts the SQL Type per java.sql.Types.

  • If you don't want to tie your Domain Object with spwrap as of step 3 above, you can have another class to implement the Mapper interfaces (TypedOutputParamMapper and ResultSetMapper) and pass it to the annotaion @Mapper like:

@Mapper(CustomResultSetMapper.class)
@StoredProc("list_customers")
List<Customer> listCustomers();
  • @Mapper annotation overrides the mapping specified by the return type object, i.e. spwrap extract Mapping infromation from the return type class, and then override it with the classes set by @Mapper annotation if found.

  • Your Stored procedure can return output parameter as well as One Result set in one call, to achieve this use Tuple return type:

@Mapper({MyResultSetMapper.class, MyOutputParameterMapper.class})
@StoredProc("list_customers_with_date")
Tuple<Customer, Date> listCustomersWithDate();

Limitations:

  • spwrap doesn't support INOUT parameters.

  • spwrap doesn't support returning multi-result sets from the stored procedure.

  • When the Stored procedure have input and output parameters, input parameters should come first and then the output parameters.

Database Support:

Because spwrap is based on JDBC API, theoretically it should support any Database Management System with a JDBC Driver, However it is tested on HSQL, MySQL, SQL Server and Oracle with jdk 1.6, 1.7 and 1.8 (1.7 and 1.8 are remove to reduce build time). (Plan to test againest: Postgresql, Sybase, DB2)

See wiki page for more info and test cases/spwrap-examples for more usage scenarios.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].