32
CSQL - SQLAPI Jitendra Lenka Developer - Lakshya Solutions Ltd. jitendra.lenka@lakshyas olutions.com

Sqlapi0.1

Embed Size (px)

DESCRIPTION

SQL API is the proprietary interface of CSQL to connect and execute DDL and DML opeartions in CSQL database.

Citation preview

Page 1: Sqlapi0.1

CSQL - SQLAPI

Jitendra Lenka Developer - Lakshya Solutions Ltd.

[email protected]

Page 2: Sqlapi0.1

Module Objectives

After completing this module you will be able to :

• Understand various interfaces in SQLAPI.

• Write application to access CSQL database

• Understand different SQL statements executed in CSQL database with parameter and projection value.

Page 3: Sqlapi0.1

The topics to be covered in this session are :-

• Introduction to CSQL main memory database cache.

• Overview of different Interfaces to client provided by CSQL.

• Important classes and functions for writing application in SQLAPI.

• Connection to the CSQL database and execute DDL and DML statements.

• Handling errors returned by various functions in SQLAPI.

• Know about non-primptive datatypes like Date, Time,TimeStamp.

Module Coverage

Page 4: Sqlapi0.1

• CSQL Database, which comprises suite of products such as Main Memory Database with complete ACID properties, Cache to leading disk based databases (MySQL, Postgres, and Oracle), and Replication, which provides high availability and load balancing cluster for MMDB.

• It is 30x faster than any leading database.

• CSQL is mainly developed to be used as a cache for existing disk based commercial databases, which delivers application response times in microseconds by bringing frequently accessed data closer to the application.

• CSQL is available as a open source product in(sourceforge.net) site, also available in official site(csqldb.com) as Proprietary license.

Download Enterprise version: http://csqldb.com/download.html

Open Source version: http://sourceforge.net/products/csql

What is CSQL?

Page 5: Sqlapi0.1

Referenced Manuals:

User Manual: Describes Concept, components and basic usages and useful for administrators.

Programmer Guide: It covers JDBC, ODBC and proprietary SQL interface and useful for application developers.

Cache Guide: Describes caching functionality of CSQL and configuration settings required to set up caching for MySQL, Postgres and Oracle

DBMS.

Documentation Downloading: http://www.csqldb.com/pro_documentation.html

CSQL Guides

Page 6: Sqlapi0.1

• ODBCC/C++ standard interface for SQL engine.

• JDBC Java standard interface for SQL engine.

• SQLAPI Proprietary interface for SQL engine.

• DBAPIProprietary interface for Storage engine

Overview

Page 7: Sqlapi0.1

• Proprietary C++ Interface• Important classes

• SqlFactory - create appropriate implementation for SQLAPI• AbsSqlConnection - It represents a database connection to sql engine. • AbsSqlStatement - Handle to the sql statement.

• Header Files• AbsSqlStatement.h• SqlFactory.h• Info.h

• Library• libcsqlsql.so

SQLAPI

Page 8: Sqlapi0.1

• DbRetVal : This enum is defined in ErrorType.h file and contain all the error codes returned by all the functions.

• Sample error codes :

OK = 0,

ErrSysFatal = -1,

ErrSysInit = -2,

ErrNoPrivilege = -3,

ErrSysInternal = -4,

ErrNoExists = -5,

ErrNoMemory = -6,

.

.

.

SplCase = -100

Error Handling

Page 9: Sqlapi0.1

This class is the entry point to access the database through the SQL engine.Each

connection has only one active transaction at any given point of time.

Member Functions :

DbRetVal connect( char *user, char *password ) Open connection to the sql engine.

DbRetVal close() Closes connection to the database and releases all the resources.

DbRetVal beginTrans( IsolationLevel isolationLevel = READ_COMMITTED ) Starts a transaction

AbsSqlConnection

Page 10: Sqlapi0.1

This class is the entry point to access the database through the SQL engine.Each

connection has only one active transaction at any given point of time.

Member Functions :

• DbRetVal commit()• Commits active transaction.

• DbRetVal rollback()• Abrots active transaction.

• DbRetVal isConnectionOpen()• Checks whether a connection is open or closed.

AbsSqlConnection

Page 11: Sqlapi0.1

This class is to create appropriate implementation of SQLAPI.

Member Functions :

static AbsSqlConnection *createConnection( SqlApiImplType implFlag )

• Creates appropriate implementation of AbsSqlConenction based on implFlag passed .

• Argument : implFlag { CSql = 1,CSqlAdapter = 2,CSqlGateway = 3 , ... }

• Return Type : AbsSqlConenction

static AbsSqlStatement *createStatement( SqlApiImplType implFlag )

• Creates appropriate implementation of AbsSqlStatement based on implFlag passed .

• Argument : implFlag { CSql = 1,CSqlAdapter = 2,CSqlGateway = 3, … }

• Return Type : AbsSqlConnection

SqlFactory

Page 12: Sqlapi0.1

EXAMPLE : Below snippet code shows how to open and close a connection.

#include<AbsSqlConnection.h>#include<SqlFactory.h>int main( ) {

DbRetVal rv = OK ;AbsSqlConnection *con = SqlFactory :: createConnection( CSql ) ;rv = con->connect( “root” , “manager” ) ;if( rv != OK ) return 1; printf(“Connection opened”) ;

rv = con -> close( );if( rv !=OK ) return 2 ; printf(“close the connection”) ;

}

Connection

Page 13: Sqlapi0.1

Primptive Non-Primptive

typeInt typeByteInt

typeLongLong typeDate

typeShort typeTime

typeFloat typeTimeStamp

typeDouble

typeString

Data Type

Page 14: Sqlapi0.1

This class is working as a handle to the SqlStatement. It is used to execute queries and return the values from the database.

Member Functions :

• void setConnection( AbsSqlConnection *con) • Sets connection handle to be used for subsequent operations.

• DbRetVal prepare( char *statement ) • Compiles the sql statement.

• DbRetVal execute( int &rowsAffect ) • Execute the sql statement

AbsSqlStatement

Page 15: Sqlapi0.1

This class is working as a handle to the SqlStatement. It is used to execute queries and return the values from the database.

Member Functions :

• DbRetVal bindField( int pos , void *val ) – Binds application buffer to the specified field position of the projection list in the select query

or for fields in the insert statement.

• void *fetch( ) – Fetches the next tuple from the result of the execution of sql select query.

• void *fetchAndPrint( bool sql ) – Fetches the next tuple from the result of the execution of sql select query and prints it to

stdout.

AbsSqlStatement

Page 16: Sqlapi0.1

This class is working as a handle to the SqlStatement. It is used to execute queries and return the values from the database.

Member Functions :

• DbRetVal free( ) – Frees all the resources held for the sql statement.

• int noOfProjFields( ) – Retrieves the total number of projection fields in the statement.

• int noOfParamFields( )– Retrieves the total number of parameters in the statement.

AbsSqlStatement

Page 17: Sqlapi0.1

This class is working as a handle to the SqlStatement. It is used to execute queries and return the values from the database.

Member Functions :

• DbRetVal getProjFldInfo( int ProjPos , FieldInfo *&info )– Retrieves the field info for the required projection field position in statement

• DbRetVal paramFldInfo( int ParamPos , FieldInfo *&info )– Retrieves the field info for the required parameter position in statement.

• bool isSelect( ) – Returns whether the statement prepared is select statement.

• DbRetVal close( )– Closes the iterator & makes the statement ready for another execution.

AbsSqlStatement

Page 18: Sqlapi0.1

This class is working as a handle to the SqlStatement. It is used to execute queries and return the values from the database.

Member Functions :

• void set<Type>Param( int ParamPos , [ Type ] value ) – Sets the value for the required parameter position in the statement.“ Type “ could be any

type of datatype.

– Type : int,Short,Long,LongLong,Byte,Float,Double,String,Date,Time.

– Example : setShortParam( int ParamPos , short value ) ,

setDateParam( int ParamPos , date value ) , etc .

AbsSqlStatement

Page 19: Sqlapi0.1

EXAMPLE : commit or rollback a transaction.// include the necessary header files

int main() { DbRetVal rv = OK ;AbsSqlConnection *con = SqlFactory :: createConnection( CSql ) ;rv = con->connect( “root” , “manager” ) ;

AbsSqlStatement *stmt = SqlFactory :: createStatement( CSql ) ;stmt -> setConnection(con) ;

con -> beginTrans( ) ;// DML operations

con -> commit( ) ; // or you can specify rollback( ) here also . stmt->free();delete stmt ; delete con ; return 0;

}

Isolation level : To specify consistency and concurrency of TransactionsValues : READ COMMITTED | READ UNCOMMITTED | READ REPEATABLE

Transaction

Page 20: Sqlapi0.1

EXAMPLE : create table emp( eid int, ename char(20), doj date) ;// include the necessary header files

int main() { DbRetVal rv = OK ;AbsSqlConnection *con = SqlFactory :: createConnection( CSql ) ;rv = con->connect( “root” , “manager” ) ;

AbsSqlStatement *stmt = SqlFactory :: createStatement( CSql ) ;stmt -> setConnection(con) ;

char statement[100] ; strcpy(statement , ”create table emp(eid int, ename char(20), doj date) ; ” ) ;int rows;

stmt->prepare(statement);stmt->execute(rows);

stmt->free();delete stmt ; delete con ; return 0;

}

Table Creation

Page 21: Sqlapi0.1

EXAMPLE : insert into T1 values(?,?) ;// assumed that table T1 is already created with F1 integer and F2 char(20) fields.

// include the necessary header filesint main() {

// connect to the database and get the statement handle. int rows; int id1 ; char name[20] ; // buffer stmt->prepare(“insert itno T1 values( ? , ? ) ; ” ) ;

for( int i=0 ; i < 10 ; i++) {con->beginTrans(); // transaction begins herestmt->setIntParam( 1 , id1 ) ;stmt->setStringParam( 2, name) ;

stmt->execute(rows) ;con->commit() ; count++ ; id++;

}stmt->free();delete stmt ; delete con ; return 0;

}

Table - Insert

Page 22: Sqlapi0.1

EXAMPLE : select * from T1 ;// assumed that Table T1 (F1 int, F2 char(20)) already created with some values.

// include the necessary header filesint main() {

// connect to the database and get the statement handle.int rows; int id1 ; char name[20]; stmt->prepare(“select * from T1 ; ” ) ;

stmt->bindField( 1 , &id );stmt->bindField( 2 , name );

con -> beginTrans( ); // transaction begins herestmt->execute(rows) ;

while( stmt -> fetch( ) != NULL ) {printf(“F1=%d F2=%s”, id1, name) ; count ++ ;

}stmt->close(); stmt->commit();stmt->free();delete stmt ; delete con ; return 0;

}

Table - Select

Page 23: Sqlapi0.1

CREATED TABLE : T1( F1 int primary key, F2 int) ;

EXAMPLE : select * from T1 where F1= ? (0 to 99);

// include the necessary header filesint main() {

// prepare the statememnt and bind the two fields with ‘id’ and ‘id1’ variable.

int var1=0;for( int i=0 ; i<100 ; i++) {

con->beginTrans(); // transaction begins herevar1 = i ;setIntParam(1,var1) ; stmt->execute(rows) ;

while( stmt -> fetch( ) != NULL ) {printf(“F1=%d F2=%d”, id, id1) ; count ++ ;

}stmt->close(); stmt->commit();

}stmt->free();delete stmt ; delete con ; return 0;

}

Table – Select – Where - Parameter

Page 24: Sqlapi0.1

CREATED TABLE : T1( F1 int , F2 char(20)) ;

EXAMPLE : update T1 set F1 = ? ;

// include the necessary header filesint main() {

stmt->prepare( “ update T1 set F1 = ?”;) ;int var1 = 10;

for( int i=0 ; i<10 ; i++) {con->beginTrans(); // transaction begins herevar1 ++ ;

stmt -> setIntParam(1,var1) ; stmt->execute(rows) ;

stmt->commit();}stmt->free();delete stmt ; delete con ; return 0;

}

Table - Update

Page 25: Sqlapi0.1

CREATED TABLE : T1( F1 int , F2 char(20)) ;

EXAMPLE : update T1 set F1 = ? ;

// include the necessary header filesint main() {

stmt->prepare( “ delete from T1 where F1 = ?”;) ; // f1=0,1,2…….9int id = 0;

for( int i=0 ; i<10 ; i++) {con->beginTrans(); // transaction begins here

stmt -> setIntParam(1 , id) ; stmt->execute(rows) ; stmt->commit();id++ ;

}stmt->free();delete stmt ; delete con ; return 0;

}

Table - Delete

Page 26: Sqlapi0.1

Date is a non primptive data type.

Functions :

Date( int year , int month , int day ) int set( int year, int month, int day ) int get( int &year, int &month, int &day) int dayOfMonth() int month() int year() const char *monthName()

january, february,…

const char *dayOfWeekName()Sunday, Monday, …

int parseFrom(const char *s)

Format : “mm/dd/yyyy”

Date

Page 27: Sqlapi0.1

CREATED TABLE : T1( F1 date) ;

EXAMPLE : insert into T1 values(?)

// include the necessary header files

int main() { Date dtIn ; dtIn . Set(2007,01,02) ;

stmt->prepare(“insert into T1 values(?) ; “) ;con -> beginTrans() ;

stmt->setDateParam(1,dtIn) ;

stmt -> execute(rows) ;con -> commit( ) ;

// close the connection.return 0;

}

Date - Insert

Page 28: Sqlapi0.1

CREATED TABLE : T1( F1 date) .

EXAMPLE : select * from T1 ;

int main() {

// get the connection and connect to the database and get the statement handle

Date dtOut ;

stmt -> bindField( 1, dtOut ) ;

stmt->prepare(“ select * from T1 ; “) ;

con -> beginTrans() ;

stmt->execute(rows) ;

while( stmt -> fetch() !=NULL ) {

printf(“Year=%d Month=%d Day=%d” , dtOut.year() , dtOut.month() , dtOut. dayOfMonth() ) ;

}

stmt->close( ) ;

con -> commit( ) ;

// close the connection.

return 0;

}

Date - Select

Page 29: Sqlapi0.1

Time is a non primptive data type.

Functions :

Time( int hour , int mint , int secs, int usec=0 ) int set( int hour, int mint, int secs, int usec=0 ) int get( int &hour, int &mints, int &secs) Int usec() int seconds() int minutes() int hours()

int parseFrom(const char *s)

Format : “hh:mm:ss”

Time

Page 30: Sqlapi0.1

CREATED TABLE : T1( F1 time) ;

EXAMPLE : insert into T1 values(?)

// include the necessary header files

int main() {

Time inTime ;inTime . Set(12,29,30) ;

stmt->prepare(“insert into T1 values(?) ; “) ;con -> beginTrans() ;

stmt->setTimeParam(1 , inTime) ;

stmt -> execute(rows) ;con -> commit( ) ;

// close the connection.return 0;

}

Time - Insert

Page 31: Sqlapi0.1

CREATED TABLE : T1( F1 time) .

EXAMPLE : select * from T1 ;

int main() {

// get the connection and connect to the database and get the statement handleDate outTime ; stmt -> bindField( 1, outTime ) ;

stmt->prepare(“ select * from T1 ; “) ;con -> beginTrans() ;stmt->execute(rows) ;

while( stmt -> fetch() !=NULL ) { printf(“Hour=%d Mimute=%d Second=%d” , outTime . hours() , outTime.minutes() , outTime. seconds() ) ;}

stmt->close( ) ;con -> commit( ) ;// close the connection.return 0;

}

Time - Select

Page 32: Sqlapi0.1

Below directories contain the test cases for SQLAPI.

$CSQL/examples/sqlapi/sqlapiexample.c$CSQL/sqlapi/Connect/*.c$CSQL/sqlapi/Select/*.c

Date - Select