Introducing WADL#

Of late I have been working on a small project which deals with C# code generation. I named it WADL# by the way most of the C#/.NET projects are names. If Apple products can start with i , GNOME with g, KDE with K and Windows with Win , then  I think WADL# is an apt name.

For all the people who have been working on .NET and web-services must be diving deep into SOAP based web-services. Right? How many of you know that SOAP based web services are not the only type of web-service which exist on this planet? How many of you have heard about RESTful web services? I know the fight between SOAP and REST is just like Emacs v/s Vim and GNOME v/s KDE and SQL v/s NoSQL. If someone worked on SOAP based web service, they must be familiar with WSDL. They added a “Web reference” and voila – Your IDE generated the client proxy for you.

For people in the RESTful world, don’t have something called WSDL. Sun came up with something called WADL. It is again a XML based representation of operations offered by the web-service. WADL doesn’t get so much of love since RESTful people are opposed to anything such as WADL. Whatever may be the case, WADL is still pending with W3C and not yet finalized. This makes the situation more complex for people from WSDL background who have been in a habit of getting their client proxy code. Hardly anyone looks at the actual XML. Why does it make the life tough? This is because I along with many people could not find any such code generators for WADL. After days of hunting I finally gave up.

I actually needed the WADL -> C# code generator as I wanted to write a client side proxy for Launchpad. After figuring out that LP provides a WADL, I started researching how to use it, but only in vain since no such code generators exist. This inspired me to write one of my own. I got the WADL schema, read the reference, sent some mails to launchpad-dev mailing list asking for clarification and finally came up with v0.1 of WADL#, download the assembly file.

I am still learning WADL and not everything is clear. There are many conflicts which might take time to grasp, but the current assembly works flawlessly for Launchpad WADL. Let me know if it fails for some WADL file which you provided. Take the pains to mail me the WADL file too. My email is mail at manishsinha dot net

Documentation:

This is no magic or rocket science. All you need to do it to call the Convert method of WadlConverter which implements IConverter interface. This means you need to use LpNet.WadlSharp.Common.WadlConverter class and call it’s Convert method. The signature of the method is

void Convert(string inputFileName, string outputFileName, string rootNamespace)

The explanation of the method:

  1. The first argument is the filename of the WADL file.
  2. Second argument is the filename of the .cs file. If the file does not exist, it would be created. If it does exist, it would be truncated and then overwritten.
  3. The third parameter is the name of the namespace which you would like the generated code to have.

Languages Supported:

WADL# uses CodeDom which is provided both in .NET and also Mono. WADL# can theoretically gen rate code for all languages which CodeDom can.

In reality, there is a small problem in the file Customizer.cs at line 215 where there is a hard coding for C#. Next release of WADL# will remove that.

Code:

WADL# uses Launchpad for development using the Bzr source control system. Get the code from trunk, make changes, push it and add it to merge queue. I love contribution and patches. 🙂

License:

WADL# is an open-source project released under MIT/X11/Expat License. This means you can use it freely any way you like it provided you do not remove the Copyright notice. Know more about MIT License.

Just now I got a mail from Softpedia informing me that they have added WADL# to their repository of Linux Softwares.

Please don’t start with Mono and patents talk. Please point your browser to Slashdot, Hacker News, OSNews and Reddit for such discussions. You’ll get better responses in such places.

Please let me know if there is any mistakes on this post which includes typos, grammatical and factual ones. I would correct it at the earliest.

Update: I just got a backlink from a blog at weblogs.java.net on a post titled Bumper Crop of WADL Tools

Which Programmer are you?

It’s written just for humour, no offence meant to any language

*

You know you are a Python programmer……. when one of your finger is always on the Tab key

*

You know you are a PHP Programmer…….. when you always have an urge to type $_FOO[‘BLAH’] every now and then

*

You know you are a Java Programmer……  when you have a fetish for looong looong identifier names like ArrayIndexOutOfBoundsException

*

You know you are a C programmer…………. when you think pointers is the only true thing that exists on this world.

*

You know you are a VB programmer……….. when you think it is the only real programming language which exists and you recommend it to a lot of newbie who are fresh into programming

*

You know you are a JavaScript programmer…………. when you think that JavaScript is the future of programming languages and you eat, sleep and dream of DOM.

*

You know you are a VC++ programmer………. if you don’t hate VC++

*

You know you are a Perl programmer…………    if half of your code contains only regex

*

By Ankur Sethi:

You know you are a Cocoa/ObjC programmer……… when you have this in mind: [anExtremelyLongIdentifier someRandomArgument:foo anotherRandomArgument:bar];

SQLite with C

‘C’ has always been my favourite language due to simple facts that it is beautiful and low level in nature. I don’t claim that am a ‘Geek’ in this language, its just my love that pulls me towards it. Let’s have a look at the other languages usually  liked by the public – VB, Java, Perl , Python. All of them may be good in their own ways but C kicks ass. VB?? urgh… Sorry! I vow not to code in it. It’s syntax is very unusual and every Tom,Dick and Harry claims to be a champ of that language.

The biggest problem which I face in C is storing data or in short making data persistent. One way is to write the required to a file on the disk in a fixed format. This stored data can then be read and parsed as per requirement. This approach is good for small amount of data, but what about huge amount of data? You would spend a big share of your time just for structured file I/O. Finally you would land up writing a small module for this work. Why not use any such existing database software for the same? Here comes SQLite for rescue.

I have seen a lot of tutorials on the net, they are very good but none of them suited my needs. The requirement was to explain a sample code line by line. After lots of googling and tea, I managed to make it work! The code snippet which I made is able to create new database if it does not exist, create a table if it does not exist, enter two rows and then fetch those two rows and print them on the screen. Check the code which I have committed the code to my personal google code repository.

Let me explain the code. Sorry for not aligning it. Please download the raw file.

#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>


int
main(int argc, char** args)
{
// Create an int variable for storing the return code for each call
int retval;

Include stdio.h, sqlite3.h and stdlib.h , stdlib.h is for malloc and sqlite3.h contains the standard function declarations needed for the required functionality.

// The number of queries to be handled,size of each query and pointer
int q_cnt = 5,q_size = 150,ind = 0;
char **queries = malloc(sizeof(char) * q_cnt * q_size);

q_cnt stored the number of queries we may want to do, q_size stores the max size of a SQL query, ind is the index.

**queries is a double array or matrix which stores the multiple queries. The total amount of storage to be allocated is sizeof(char) * q_cnt * q_size

// A prepered statement for fetching tables
sqlite3_stmt *stmt;

// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;

// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
retval = sqlite3_open(“sampledb.sqlite3”,&handle);
// If connection failed, handle returns NULL
if(retval)
{
printf(“Database connection failed\n”);
return -1;
}
printf(“Connection successful\n”);

We need to create a pointer to sqlite3 and sqlite3_stmt structures. sqlite3 is the structure which is to hold the database connection handle. sqlite3_stmt is just like a cursor to a database.

sqlite3_open function needs the address of the sqlite3 database instance on the disk. The second parameter is the pointer to the pointer to sqlite3 structure. One mistake which I stumbled upon was to create a sqlite3 ** handle and then pass it to this function. The correct way is to create a sqlite3* handle and then pass the pointer to it using the & operator

// Create the SQL query for creating a table
char create_table[100] = “CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)”;

// Execute the query for creating the table
retval = sqlite3_exec(handle,create_table,0,0,0);

// Insert first row and second row
queries[ind++] = “INSERT INTO users VALUES(‘manish’,’manish’,1)”;
retval = sqlite3_exec(handle,queries[ind-1],0,0,0);
queries[ind++] = “INSERT INTO users VALUES(‘mehul’,’pulsar’,0)”;
retval = sqlite3_exec(handle,queries[ind-1],0,0,0);

Create a table if it does not exist and then insert two rows. Note that sqlite3 does not support inserting two rows in one single query. Maybe I need to confirm this fact again, but I never worked for me ever.

// select those rows from the table
queries[ind++] = “SELECT * from users”;
retval = sqlite3_prepare_v2(handle,queries[ind-1],-1,&stmt,0);
if(retval)
{
printf(“Selecting data from DB Failed\n”);
return1;
}

// Read the number of rows fetched
int cols = sqlite3_column_count(stmt);

Create a prepared statement for fetching data from the database using sqlite3_prepare_v2 function call. The first parameter is the database handle itself which is a sqlite3* pointer. The second parameter is the SQL statement which needs to be executed. The third parameter tells upto how long the second parameter to be read. Pass -1 to make it read till line terminator. Fourth statement is the pointer to pointer to prepared statement structure. Take care of the pointer concept as I told about sqlite3 structure. The fifth parameter is filled with the unused portion of the query. Have a look at the official documentation.

sqlite3_column_count function gets the number of columns for the result fetched.

while(1)
{
// fetch a row’s status
retval = sqlite3_step(stmt);

if(retval == SQLITE_ROW)
{
// SQLITE_ROW means fetched a row

// sqlite3_column_text returns a const void* , typecast it to const char*
for(int col=0 ; col<cols;col++)
{
const char *val = (const char*)sqlite3_column_text(stmt,col);
printf(“%s = %s\t”,sqlite3_column_name(stmt,col),val);
}
printf(“\n”);
}
else if(retval == SQLITE_DONE)
{
// All rows finished
printf(“All rows fetched\n”);
break;
}
else
{
// Some error encountered
printf(“Some error encountered\n”);
return1;
}
}

We have put this code in infinite while loop as we are not sure how much rows it contains. Usually, the table returns n+1 rows, where 1 extra row is for telling that all rows have been fetched. sqlite3_step returns the status which is actually an enumeration. Check all the results contants here. Two most used are SQLITE_DONE, SQLITE_ROW. The former tells that all the rows have been fetched, now the user can come out of this loop and continue. SQLITE_ROW tells that a valid row has been fetched.

// Close the handle to free memory
sqlite3_close(handle);
return 0;
}

sqlite3_close simply closes the database connection.

Save the code in a file named, say dataman.c , compile it using the command

$ gcc dataman.c -o dataman -l sqlite –std=c99

You obviously need to have sqlite development headers installed for compiling the same. The name of the package on Ubuntu is libsqlite3-dev

Official SQLite Documentation

That’s all Folks! Enjoy 🙂