Access MYSQL database with other sql languages(for example, ORACLE's sql)
MYSQL Client/Server Protocol
MYSQL Client/Server Protocol
is being optimizedMYSQL Client/Server Protocol
MYSQLC
libray) provides:
MYSQL Client/Server Protocol
prepare
and query
modelibmysqlclient
rlogon()
to parse connection strings and to do database loginopen()
to initialize the SQL and translate itreopen
a new SQL or execute
the old one againconnection string processor
which will test the inputed string
translation points
to being translated to MYSQL-style
syntaxdeflate
algorithm from zlibping
protocol to maintain the database connectionquery mode
with which all place holder values are embeded within the SQL statement string that will be sent to server by a single com_query
request, here’s the diagram:prepare mode
. The SQL execution progress is seperated into 2 requests:
com_stmt_prepare
to prepare resources and do checkings at server sidecom_stmt_execute
request to initiate the SQL execution
progress, and the binary/string/blob
type place-holders values should be sent by com_send_long_data
requests especially, Here’s the diagram:make clean install
under source root directorylibzas.a
, libzas.so
, and zas wrapper
library will be generated.a
or .so
library to your c++ applicationssrc
: core of zas librarytests
: test cases of zaswin
: vss project files of zas under windowswrapper
: library classes for java/python that encapsulates APIs onto ZASlibcwpr
that encapsulates a set of usage with ZAS APIs. see wrapper/cwpr.c for more detailslibcwpr
methods with JNI interfaceslibcwpr
In c++, one should access MYSQL with ZAS like this:
/* initialize connection object and login to database with MYSQL driver */
zas_connect cnn(tnsFilePath,dal_mysql) ;
/* initialize stream object with SQL-prepare mode */
zas_stream stream(cnn,true);
/* initialize the ORACLE-style SQL */
stream.open(0,"select id,nvl(name),price from test_db.test_tbl "
"where id>=:f1<unsigned int> and id<:f2<int,in>");
/* insert place holders and execute the SQL */
streams<<1;
streams<<5;
/* fetch results */
while (!streams[strs].eof()) {
int id=0;
char name[256] = "";
float point = 0.0;
long size = 0L;
streams>>id ;
streams>>name ;
streams>>point ;
streams>>size ;
streams.flush();
printd("%d: name %s, point %f, size %ld\n",
id, name, point, size);
}
In java, one should set correct 'CLASSPATH' of ZAS java wrapper class and play like this:
public class test_cases {
public static void main(String[] args) throws Exception {
/* initialize instance of zas.class and do initializations with it */
zas mz = (zas)Class.forName("zas").newInstance();
/* login to MYSQL */
mz.login("localhost",3306,"root","123","");
/* initialize the ORACLE-style SQL */
mz.prepare("select id,nvl(name),price,size from test_db.test_tbl where id<:f1<int>");
/* insert placeholder and execute SQL */
mz.insertInt(10);
/* fetch results */
while (!mz.isEof()) {
Integer id = 0;
String name = "" ;
Double price = 0.0;
Long size = 0L;
id = mz.fetchInt();
name = mz.fetchStr();
price = mz.fetchDouble();
size = mz.fetchLong();
System.out.println(id + ": name: " + name + ", point: " +
price + ", size: " + size + "\n");
}
}
}
In python, one should load ZAS python wrapper class and play like this:
# path of ZAS python wrapper class
sys.path.append(zasPythonWrapperPath)
# load the wrapper class
from python.zas import *
def main():
# do initializations
mz = zas()
# login to database
if mz.login("localhost",3306,"root","123","")!=0 :
print("login fail\n")
exit(-1)
# initialize the ORACLE-style SQL
if mz.prepare('select nvl(id),name,price,size from test_db.test_tbl where id <:f1<int>')!=0 :
print("prepare fail\n")
exit(-1)
# insert placeholder and execute SQL
if mz.insert_int(10)!=0 :
exit(-1)
# fetch results
while mz.is_eof()==0 :
id = mz.fetch_int()
name = mz.fetch_str()
price = mz.fetch_double()
size = mz.fetch_long()
print("{0}: name: {1}, point: {2}, size: {3}\n".format(id,name,price,size))