Dynamic Data Access API


Dynamic data-access API is designed and implemented to enable the software developers and another API’s to be able to make database operations without the need of writing static SQL statements and even knowing about its syntax. With this API, the develop can call straight forward API’s that will be generated full dynamic SQL statements later.

To be able to use this API, the developer should include its dependency in the maven project as follows:

<!-- https://mvnrepository.com/artifact/com.jalalkiswani/jk-db-dynamic -->
<dependency>
 <groupId>com.jalalkiswani</groupId>
 <artifactId>jk-db-dynamic</artifactId>
 <version>0.0.2-1</version>
</dependency>

Usage:

Below are some usage examples for this API:

package com.jk.db.dynamic.test;
import java.util.List;
import java.util.Vector;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.jk.db.dynamic.dataaccess.DynamicDataAccess;
import com.jk.db.dynamic.dataaccess.DynamicDataAccessFactory;
import com.jk.db.test.AbstractDBTest;
import com.jk.metadata.db.meta.Record;
import com.jk.util.JK;

public class DynamicDaoTest {
 static DynamicDataAccess dao = DynamicDataAccessFactory.createDynamicDao("hr_employees");

 public void insertRecord() {
 Record record = createMockUpRecord();
 record.setIdValue(ID);
 dao.insertRecord(record);
 JK.print("insertRecord()");
 }

 public void insertRecords() {
 List<Record> records = new Vector<>();
 for (int i = 0; i < 100; i++) {
 records.add(createMockUpRecord());
 }
 dao.insertRecords(records);
 JK.print("insertRecords()");
 }

 public void updateRecord() {
 Record record = dao.findRecord(ID);
 record.setFieldValue("emp_name", "Updated name");
 dao.updateRecord(record);
 JK.print("updateRecord()");
 }

 public void isIdExists() {
 boolean idExists = dao.isIdExists("-1");
 boolean idExists2 = dao.isIdExists(ID);
 JK.print("isIdExists()", idExists, idExists2);
 }

 public void lstRecords1() {
 List<Record> records = dao.lstRecords();
 for (int i = 0; i < records.size(); i++) {
 JK.print(records.get(i).getFieldsValues());
 }
 JK.print("lstRecords1()");
 }


 public void saveRecord() {
 Record record = createMockUpRecord();
 // should insert
 record.setModified(true);
 record.setFieldValue("emp_salary", 888);
 dao.saveRecord(record);
 record.setFieldValue("emp_salary", 999);
 record.setModified(true);
 dao.saveRecord(record);
 record.setDeleted(true);
 // delete
 dao.saveRecord(record);
 JK.print("saveRecord()");
 }

 public void deleteAllRecords() {
 dao.deleteAllRecords();
 JK.print("deleteAllRecords()");
 }

 public void deleteByFieldValue() {
 dao.deleteByFieldValue("dep_id", 2);
 JK.print("deleteByFieldValue()");
 }

 protected Record createMockUpRecord() {
 Record record = dao.createEmptyRecord(true);
 record.setFieldValue("emp_name", "Name-" + JK.randomNumber());
 record.setFieldValue("emp_salary", JK.randomNumber(100, 3000));
 record.setFieldValue("dep_id", JK.randomNumber(1, 5));
 return record;
 }
}