Commit 1de792ca authored by Elemer Lelik's avatar Elemer Lelik
Browse files

Titan SQLasp TP Initial Contribution

parent bd2619c2
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2014 Ericsson
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
File: SQLasp_CNL113760.tpd
Description: tpd project file
Rev: R4A
Prodnr: CNL 113 760
Updated: 2014-09-24
Contact: http://ttcn.ericsson.se
-->
<TITAN_Project_File_Information version="1.0">
<ProjectName>SQLasp_CNL113760</ProjectName>
<Folders>
<FolderResource projectRelativePath="src" relativeURI="src"/>
</Folders>
<Files>
<FileResource projectRelativePath="src/SQL.grp" relativeURI="src/SQL.grp"/>
<FileResource projectRelativePath="src/SQL_Functions.ttcn" relativeURI="src/SQL_Functions.ttcn"/>
<FileResource projectRelativePath="src/SQL_MySQL_interface.cc" relativeURI="src/SQL_MySQL_interface.cc"/>
<FileResource projectRelativePath="src/SQL_PortTypes.ttcn" relativeURI="src/SQL_PortTypes.ttcn"/>
<FileResource projectRelativePath="src/SQL_SQLite_interface.cc" relativeURI="src/SQL_SQLite_interface.cc"/>
<FileResource projectRelativePath="src/SQL_interface.hh" relativeURI="src/SQL_interface.hh"/>
<FileResource projectRelativePath="src/SQL_parser.l" relativeURI="src/SQL_parser.l"/>
<FileResource projectRelativePath="src/SQL_parser.y" relativeURI="src/SQL_parser.y"/>
<FileResource projectRelativePath="src/SQL_parser_.tab.c" relativeURI="src/SQL_parser_.tab.c"/>
<FileResource projectRelativePath="src/SQL_parser_.tab.h" relativeURI="src/SQL_parser_.tab.h"/>
<FileResource projectRelativePath="src/SQLasp_PT.cc" relativeURI="src/SQLasp_PT.cc"/>
<FileResource projectRelativePath="src/SQLasp_PT.hh" relativeURI="src/SQLasp_PT.hh"/>
<FileResource projectRelativePath="src/lex.SQL_parser_.c" relativeURI="src/lex.SQL_parser_.c"/>
</Files>
<ActiveConfiguration>Default</ActiveConfiguration>
<Configurations>
<Configuration name="Default">
<ProjectProperties>
<MakefileSettings>
<generateInternalMakefile>true</generateInternalMakefile>
<GNUMake>true</GNUMake>
<incrementalDependencyRefresh>true</incrementalDependencyRefresh>
<targetExecutable>bin/SQLasp_CNL113760</targetExecutable>
<addSourceLineInfo>true</addSourceLineInfo>
<linkerLibraries>
<listItem>mysqlclient</listItem>
<listItem>sqlite3</listItem>
</linkerLibraries>
</MakefileSettings>
<LocalBuildSettings>
<workingDirectory>bin</workingDirectory>
</LocalBuildSettings>
</ProjectProperties>
</Configuration>
</Configurations>
</TITAN_Project_File_Information>
/******************************************************************************
* Copyright (c) 2012, 2014 Ericsson AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gabor Szalai - initial implementation and initial documentation
* Zoltan Medve
* Janos Kovesdi
******************************************************************************/
module SQL_Parser {
import from SQL_PortTypes all;
import from SQL_Functions all;
modulepar {
charstring tsp_sqlfile := "example.sql";
}
type component SQL_CT {
port SQLasp_PT v_mysql_PT;
var SQL_Result v_result;
var SQL_Query_result v_query_result;
var SQL_exception v_exception;
var SQL_Rollback_Queries v_rollback_queries;
var SQL_Query_Result_Record v_result_record;
var SQL_Connection v_connection := {
MySQL_engine,
{
{"host", "127.0.0.1"},
{"port", "3306"},
{"user", "username"},
{"password", "password"},
{"database", "database"},
{"char_set", "utf8"},
{"auto_reconnect", "true"}
}
}//connection
}//SQL_CT
template SQL_Rollback_Queries t_SQL_Rollback_Queries := {
"INSERT INTO parsertest VALUES(2,'john doe 2',25,'message available');",
pattern "DELETE FROM parsertest WHERE id=\d+;",
pattern "UPDATE parsertest SET id=\d+,name='john doe 2',age=11,message=NULL WHERE id=\d+;"
}//t_SQL_Rollback_Queries
template SQL_Query_result t_initial_state := {
connId := ?,
affected_rows := 2,
data := {
{
{field_name := "id", field_data := {int_data := 1}},
{field_name := "name" , field_data := {text := "anonymous"}},
{field_name := "age" , field_data := {int_data := 20}},
{field_name := "message" , field_data := {null_value := {}}}
},
{
{field_name := omit, field_data := {int_data := 2}},
{field_name := omit, field_data := {text := "john doe"}},
{field_name := omit, field_data := {int_data := 25}},
{field_name := omit, field_data := {text := "message available"}}
}
}
}//t_initial_state
template SQL_Query_Result_Record t_expected_result_record := {
{
//DELETE FROM parsertest WHERE age > 20 AND age <= 25 ORDER BY id ASC;
connId := ?,
affected_rows := 1,
data := omit
},
{
//INSERT INTO parsertest (name,age, message) VALUES('john doe 2',11, NULL);
connId := ?,
affected_rows := 1,
data := omit
},
{
//UPDATE parsertest SET name='john doe 3' WHERE name='john doe 2';
connId := ?,
affected_rows := 1,
data := omit
}
}//t_expected_result_record
testcase tc_demo() runs on SQL_CT {
map(self:v_mysql_PT, system: v_mysql_PT);
if(f_connect(v_mysql_PT, v_connection, v_result, v_exception)){
log("Query: ", f_query(v_mysql_PT, v_result.connId, "SELECT * FROM parsertest;", v_query_result, v_exception));
log("Result: ", v_query_result);
if(match(v_query_result, t_initial_state)) {
log("Initial state is ok");
} else {
log(match(v_query_result, t_initial_state));
setverdict(fail);
}
var SQL_Primary_Keys vl_keys := {{"parsertest", "id"}};
if(ef_SQL_Execute(tsp_sqlfile, v_mysql_PT, v_result.connId, vl_keys, v_rollback_queries, v_result_record)) {
log("Rollback queries: ", v_rollback_queries);
log("Result record: ", v_result_record);
} else {
setverdict(fail);
}
if(match(v_rollback_queries, t_SQL_Rollback_Queries)) {
log("RollbackQueries ok.");
f_SQL_Rollback(v_mysql_PT, v_result.connId, v_rollback_queries);
f_query(v_mysql_PT, v_result.connId, "SELECT * FROM parsertest;", v_query_result, v_exception);
if(match(v_query_result, t_initial_state)) {
log("Initial state is back");
} else {
log("", match(v_query_result, t_initial_state));
setverdict(fail);
}
if(match(v_result_record, t_expected_result_record)) {
log("Result record is ok");
} else {
log(match(v_result_record, t_expected_result_record));
setverdict(fail);
}
} else {
log(match(v_rollback_queries, t_SQL_Rollback_Queries));
setverdict(fail);
}
log("Disconnect: ", f_disconnect(v_mysql_PT, v_result.connId, v_result, v_exception));
}//if connect
else {
setverdict(fail);
}
setverdict(pass);
unmap(self:v_mysql_PT, system: v_mysql_PT);
}//tc_demo
control {
execute(tc_demo());
}
} // end of module
/******************************************************************************
* Copyright (c) 2012, 2014 Ericsson AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gabor Szalai - initial implementation and initial documentation
* Zoltan Medve
* Janos Kovesdi
******************************************************************************/
module SQL_demo {
import from SQL_PortTypes all;
type component MySQL_CT{
port SQLasp_PT MySQL_PT;
var SQL_Connection vl_conn:={
MySQL_engine,
{
{"host","demohost"},
{"user","demo"},
{"password","demopassword"},
{"database","demo"},
{"port","3306"},
{"char_set","utf8"}
}
};
var SQL_Result v_result;
var SQL_Query_result v_query_result;
var SQL_exception v_exception;
var integer v_connId;
var universal charstring v_sql;
}//MySQL_CT
type component SQLite_CT {
port SQLasp_PT SQLite_PT;
var SQL_Connection v_connection := {
SQlite_engine,
{
{"database", "./example.db"}
}
};
var SQL_Result v_result;
var SQL_Query_result v_query_result;
var SQL_exception v_exception;
var integer v_connId;
var universal charstring v_sql;
}//SQLite_CT
function f_sqlite_connect() runs on SQLite_CT return boolean{
SQLite_PT.call( S_SQL_connect:{v_connection} ){
[] SQLite_PT.getreply(S_SQL_connect:{?}) -> value v_result{
v_connId := v_result.connId;
log("ConnID: ",v_connId);
}
[] SQLite_PT.catch( S_SQL_connect, SQL_exception:?) -> value v_exception{
log("Conn exception: ", v_exception);
return false;
}
}
return true;
}
function f_sqlite_query(in charstring pl_query) runs on SQLite_CT return boolean{
SQLite_PT.call( S_SQL_Query:{v_connId, pl_query}){
[] SQLite_PT.getreply(S_SQL_Query:{?,?}) -> value v_query_result{
log("Query result: ", v_query_result);
}
[] SQLite_PT.catch(S_SQL_Query, SQL_exception:?) -> value v_exception {
log("Query exception: ", v_exception);
return false;
}
}
return true;
}
function f_sqlite_disconnect() runs on SQLite_CT return boolean{
SQLite_PT.call( S_SQL_disconnect:{v_connId}){
[] SQLite_PT.getreply(S_SQL_disconnect:{?}) -> value v_result{
log("Disconnect result: ", v_result);
}
[] SQLite_PT.catch(S_SQL_disconnect, SQL_exception:?) -> value v_exception {
log("Disconnect exception: ", v_exception);
return false;
}
}
return true
}
function f_mysql_connect() runs on MySQL_CT return boolean{
MySQL_PT.call( S_SQL_connect:{vl_conn} ){
[] MySQL_PT.getreply(S_SQL_connect:{?}) -> value v_result{
v_connId := v_result.connId;
log("ConnID: ",v_connId);
}
[] MySQL_PT.catch( S_SQL_connect, SQL_exception:?) -> value v_exception{
log("Conn exception: ", v_exception);
return false;
}
}
return true;
}
function f_mysql_query(in charstring pl_query) runs on MySQL_CT return boolean{
MySQL_PT.call( S_SQL_Query:{v_connId, pl_query}){
[] MySQL_PT.getreply(S_SQL_Query:{?,?}) -> value v_query_result{
log("Query result: ", v_query_result);
}
[] MySQL_PT.catch(S_SQL_Query, SQL_exception:?) -> value v_exception {
log("Query exception: ", v_exception);
return false;
}
}
return true;
}
function f_mysql_disconnect() runs on MySQL_CT return boolean{
MySQL_PT.call( S_SQL_disconnect:{v_connId}){
[] MySQL_PT.getreply(S_SQL_disconnect:{?}) -> value v_result{
log("Disconnect result: ", v_result);
}
[] MySQL_PT.catch(S_SQL_disconnect, SQL_exception:?) -> value v_exception {
log("Disconnect exception: ", v_exception);
return false;
}
}
return true
}
testcase MySQL_Test_Demo() runs on MySQL_CT {
map(self:MySQL_PT,system:MySQL_PT);
if(not f_mysql_connect()){
setverdict(fail);
}
f_mysql_query("SELECT * FROM demo_table");
log(v_query_result);
f_mysql_disconnect();
unmap(self:MySQL_PT,system:MySQL_PT);
setverdict(pass);
}//MySQL_Test_Demo
testcase SQLite_Test_Demo() runs on SQLite_CT {
map(self:SQLite_PT,system:SQLite_PT);
if(not f_sqlite_connect()){
setverdict(fail);
}
f_sqlite_query("SELECT * FROM demo_table");
log(v_query_result);
f_sqlite_disconnect();
unmap(self:SQLite_PT,system:SQLite_PT);
setverdict(pass);
}//SQLite_Test_Demo
} // end of module
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2014 Ericsson
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->
<TITAN_Project_File_Information version="1.0">
<ProjectName>SQLasp_CNL113760_demo</ProjectName>
<ReferencedProjects>
<ReferencedProject name="SQLasp_CNL113760" projectLocationURI="../SQLasp_CNL113760.tpd"/>
</ReferencedProjects>
<Folders>
<FolderResource projectRelativePath="demo" relativeURI=""/>
</Folders>
<Files>
<FileResource projectRelativePath="demo/SQL_Parser.ttcn" relativeURI="SQL_Parser.ttcn"/>
<FileResource projectRelativePath="demo/SQL_demo.ttcn" relativeURI="SQL_demo.ttcn"/>
</Files>
<ActiveConfiguration>Default</ActiveConfiguration>
<Configurations>
<Configuration name="Default">
<ProjectProperties>
<MakefileSettings>
<generateInternalMakefile>true</generateInternalMakefile>
<GNUMake>true</GNUMake>
<incrementalDependencyRefresh>true</incrementalDependencyRefresh>
<targetExecutable>bin/SQLasp_CNL113760_demo</targetExecutable>
</MakefileSettings>
<LocalBuildSettings>
<workingDirectory>bin</workingDirectory>
</LocalBuildSettings>
<NamingCoventions>
<enableProjectSpecificSettings>true</enableProjectSpecificSettings>
<externalFunction>f_.*</externalFunction>
<testcase>.*</testcase>
<localVariable>.*</localVariable>
<formalParameter>.*</formalParameter>
<componentVariable>.*</componentVariable>
</NamingCoventions>
</ProjectProperties>
</Configuration>
</Configurations>
</TITAN_Project_File_Information>
<!--
/******************************************************************************
* Copyright (c) 2012, 2014 Ericsson AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gabor Szalai - initial implementation and initial documentation
* Zoltan Medve
* Janos Kovesdi
******************************************************************************/
//
// File: SQL.grp
// Description:
// Rev: R4A
// Prodnr: CNL 113 760
//
//
-->
<!DOCTYPE TITAN_GUI_FileGroup_file>
<FileGroup TITAN_version="3.1.pl0" >
<File_Group name="SQL" >
<File path="lex.SQL_parser_.c" />
<File path="SQL_Functions.ttcn" />
<File path="SQL_interface.hh" />
<File path="SQL_MySQL_interface.cc" />
<File path="SQL_parser_.tab.c" />
<File path="SQL_parser_.tab.h" />
<File path="SQL_PortTypes.ttcn" />
<File path="SQL_SQLite_interface.cc" />
<File path="SQLasp_PT.cc" />
<File path="SQLasp_PT.hh" />
</File_Group>
</FileGroup>
/******************************************************************************
* Copyright (c) 2012, 2014 Ericsson AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gabor Szalai - initial implementation and initial documentation
* Zoltan Medve
* Janos Kovesdi
******************************************************************************/
//
// File: SQL_Functions.ttcn
// Description:
// Rev: R4A
// Prodnr: CNL 113 760
//
//
module SQL_Functions {
import from SQL_PortTypes all;
external function ef_SQL_Execute (in charstring pl_sql_file,
inout SQLasp_PT pl_sqlPort,
in integer pl_connId,
in SQL_Primary_Keys pl_primary_keys,
out SQL_Rollback_Queries pl_result,
out SQL_Query_Result_Record pl_result_record) return boolean;
private type record SQL_Rollback_Parameters {
integer connId,
SQL_Query select_query,
SQL_Query original_query,
SQL_Primary_Keys primary_keys,
universal charstring table_name
}
private type record of charstring CharstringList;
//from the last
function f_SQL_Rollback(inout SQLasp_PT pl_sqlPort,
in integer pl_connId,
inout SQL_Rollback_Queries pl_queries) {
var SQL_Rollback_Queries vl_result := {};
var integer vl_i := 0;
var integer vl_size := sizeof(pl_queries);
var SQL_exception vl_exception;
for(vl_i := vl_size-1; vl_i >= 0; vl_i := vl_i - 1) {
pl_sqlPort.call(S_SQL_Query:{pl_connId, pl_queries[vl_i]}) {
[] pl_sqlPort.getreply(S_SQL_Query:{?,?});
[] pl_sqlPort.catch (S_SQL_Query, SQL_exception:?) -> value vl_exception {
var integer vl_j;
log("Rollback exception: ", vl_exception);
log("Query: ", pl_queries[vl_i]);
log("Your database is inconsistent. You should backup with mysqldump or try again with the remaining queries");
for (vl_j := vl_i; vl_j >= 0; vl_j := vl_j - 1){
vl_result[sizeof(vl_result)] := pl_queries[vl_j];
}
}
}
}
pl_queries := vl_result;
}//f_SQL_Rollback
function f_connect(
inout SQLasp_PT pl_port,
in SQL_Connection pl_connection,
out SQL_Result pl_result,
out SQL_exception pl_exception
) return boolean {
pl_port.call( S_SQL_connect:{pl_connection} ){
[] pl_port.getreply(S_SQL_connect:{?}) -> value pl_result{}
[] pl_port.catch( S_SQL_connect, SQL_exception:?) -> value pl_exception{
return false;
}
}
return true;
}//f_connect
function f_query(
inout SQLasp_PT pl_port,
in integer pl_connId,
in SQL_Query pl_query,
out SQL_Query_result pl_result,
out SQL_exception pl_exception
) return boolean {
pl_port.call( S_SQL_Query:{pl_connId, pl_query}){
[] pl_port.getreply(S_SQL_Query:{?,?}) -> value pl_result{}
[] pl_port.catch(S_SQL_Query, SQL_exception:?) -> value pl_exception {
return false;
}
}
return true;
}//f_query
function f_disconnect(
inout SQLasp_PT pl_port,
in integer pl_connId,
out SQL_Result pl_result,
out SQL_exception pl_exception
) return boolean {
pl_port.call( S_SQL_disconnect:{pl_connId}){
[] pl_port.getreply(S_SQL_disconnect:{?}) -> value pl_result{}
[] pl_port.catch(S_SQL_disconnect, SQL_exception:?) -> value pl_exception {
return false;
}
}
return true
}//f_disconnect
private function f_getRollback_Delete(
inout SQLasp_PT pl_port,
in SQL_Rollback_Parameters pl_parameters,
inout SQL_Rollback_Queries pl_result,
inout SQL_Query_Result_Record pl_result_record,
inout boolean pl_exception_received
) {
var SQL_Query_result vl_select_result;
var SQL_Query_result vl_delete_result;
var SQL_exception vl_exception;
var integer vl_result_size := sizeof(pl_result);
var integer vl_result_record_size := sizeof(pl_result_record);
var integer vl_connId := pl_parameters.connId;
if(f_query(pl_port, vl_connId, pl_parameters.select_query, vl_select_result, vl_exception)){
if(ispresent(vl_select_result.data)){
if(f_query(pl_port, vl_connId, pl_parameters.original_query, vl_delete_result, vl_exception) ) {