/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: booklist.ctr
*/

/**	@file booklist.c The booklist module.
*/


#line 1 "booklist.ctr"


#include "dk3all.h"

#if DK3_HAVE_MYSQL_MYSQL_H
#
#include <mysql/mysql.h>

/**	Author record.
*/
typedef struct {
  unsigned long		 pk;	/**< Primary key. */
  char const		*sn;	/**< Surname. */
  char const		*in;	/**< Initials. */
  char const		*na;	/**< Name. */
} author_t;

/**	Book record.
*/
typedef structt {
  author_t		*au;	/**< Author. */
  char const		*ti;	/**< Book title. */
  char const		*isbn;	/**< ISBN number. */
} book_t;

/**	Compare two author records.
	@param	l	Left record.
	@param	r	Right record.
	@param	cr	Comparison criteria (0=record/record by index,
	1=record/int by index, 2=record/record by name)
*/
static
int
booklist_compare_authors(void *l, void *r, int cr)
{
  int back = 0;
  author_t	*pl;
  author_t	*pr;
  int		*ir;
  int		 i;
  if(l) {
    if(r) {
      pl = (author_t *)l;
      switch(cr) {
        case 2: {
	  pr = (author_t *)r;
	  if(pl->na) {
	    if(pr->na) {
	      back = dk3str_c8_cmp(pl->na, pr->na);
	    } else back = 1;
	  } else {
	    if(pr->na) back = -1;
	  }
	  if(back == 0) {
	    if(pl->in) {
	      if(pr->in) {
	        back = dk3str_c8_cmp(pl->in, pr->in);
	      } else back = 1;
	    } else {
	      if(pr->in) back = -1;
	    }
	  }
	  if(back == 0) {
	    if(pl->sn) {
	      if(pr->sn) {
	        back = dk3str_c8_cmp(pl->sn, pr->sn);
	      } else back = 1;
	    } else {
	      if(pr->sn) back = -1;
	    }
	  }
	  if(back == 0) {
	    if(pl->pk > pr->pk) {
	      back = 1;
	    } else {
	      if(pl->pk < pr->pk) {
	        back = -1;
	      }
	    }
	  }
	} break;
	case 1: {
	  ir = (int *)r; i = *ir;
	  if(pl->pk > i) {
	    back = 1;
	  } else {
	    if(pl->pk < i) {
	      back = -1;
	    }
	  }
	} break;
	default: {
	  pr = (author_t *)r;
	  if(pl->pk > pr->pk) {
	    back = 1;
	  } else {
	    if(pl->pk < pr->pk) {
	      back = -1;
	    }
	  }
	} break;
      }
    } else back = 1;
  } else {
    if(r) back = -1;
  }
  return back;
}


/**	Compare two book records.
	@param	l	Left object.
	@param	r	Right object.
	@param	cr	Comparison criteria (ignored).
	@return	Comparison result.
*/
static
int
booklist_compare_books(void *l, void *r, int cr)
{
  book_t	*pl;
  book_t	*pr;
  int		 back = 0;
  if(l) {
    if(r) {
      pl = (book_t *)l; pr = (book_t *)r;
      back = booklist_compare_authors((void *)(pl->au), (void *)(pr->au), 2);
      if(back == 0) {
        if(pl->ti) {
	  if(pr->ti) {
	    back = dk3str_c8_cmp(pl->ti, pr->ti);
	  } else back = 1;
	} else {
	  if(pr->ti) back = -1;
	}
      }
      if(back == 0) {
        if(pl->isbn) {
	  if(pr->isbn) {
	    back= dk3str_c8_cmp(pl->isbn, pr->isbn);
	  } else back = 1;
	} else {
	  if(pr->isbn) back = -1;
	}
      }
    } else back = 1;
  } else {
    if(r) back = -1;
  }
  return back;
}


static
void
author_free(author_t *author)
{
  if(author) {
    author->pk = 0UL;
    dk3_release(author->sn)
    dk3_release(author->in)
    dk3_release(author->na)
    dk3_delete(author)
  }
}



static
void
book_free(book_t *book)
{
  if(book) {
    book->au = NULL;
    dk3_release(book->ti)
    dk3_release(book->isbn)
    dk3_delete(book)
  }
}


/**	Exit status code.
*/
static int exval	=	1;

static dk3_sto_t	*s_au = NULL;

static dk3_sto_it_t	*i_au = NULL;

static dk3_sto_t	*s_bo = NULL;

static dk3_sto_it_t	*i_bo = NULL;


/**	Configuration section name "booklist".
*/
static char booklist_arg_0[] = {
  'b', 'o', 'o', 'k', 'l', 'i', 's', 't', '\0'
};


/**	Configuration section name "client".
*/
static char booklist_gr_client[] = {
  'c', 'l', 'i', 'e', 'n', 't', '\0'
};


/**	Argv array to initialize the library.
*/
static char *booklist_argv[] = {
  booklist_arg_0,
  NULL
};


/**	Groups (configuration file section) names.
*/
static char *booklist_groups[] = {
  booklist_gr_client,
  booklist_arg_0,
  NULL
};



static
int
booklist_read_authors(MYSQL *mysql)
{
  MYSQL_RES	*mr;		/* Results set */
  int		 res;		/* Query result. */
  unsigned int	 nf;		/* Number of fields in response. */
  res = mysql_query(mysql, "select * from autoren");
  if(0 == res) {
    mr = mysql_use_result(mysql);
    if(mr) {
      nf = mysql_num_fields(mr);
      mysql_free_result(mr);
    } else {			

#line 241 "booklist.ctr"
    }
  } else {			

#line 243 "booklist.ctr"
  }
}



static
int
booklist_read_books(MYSQL *mysql)
{
}


/**	The main() function.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, any other value indicates an error.
*/
int main(int argc, char *argv[])
{
  book_t	*book;
  author_t	*author;
  MYSQL		*re1;
  MYSQL		*res;
  s_au = dk3sto_open();
  if(s_au) {
    dk3sto_set_comp(s_au, booklist_compare_authors, 0);
    i_au = dk3sto_it_open(s_au);
  }
  s_bo = dk3sto_open();
  if(s_bo) {
    dk3sto_set_comp(s_bo, booklist_compare_books, 0);
    i_bo = dk3sto_it_open(s_bo);
  }
  if((s_au) && (i_au) && (s_bo) && (i_bo)) {
    if(0 == mysql_library_init(-1, booklist_argv, booklist_groups)) {
      re1 = mysql_init(NULL);
      if(re1) {
        res = mysql_real_connect(re1,"localhost",NULL,NULL,"buecher",0,NULL,0);
        if(res) {
	  if(mysql_set_character_set(res, "utf8")) {	

#line 283 "booklist.ctr"
	  }
	  if(booklist_read_authors(res)) {
	    if(booklist_read_books(res)) {
	    } else {			

#line 287 "booklist.ctr"
	    }
	  } else {			

#line 289 "booklist.ctr"
	  }
        } else {			

#line 291 "booklist.ctr"
        }
        mysql_close(re1);
      } else {				

#line 294 "booklist.ctr"
      }
      mysql_library_end();
    } else {				

#line 297 "booklist.ctr"
    }
  } else {				

#line 299 "booklist.ctr"
  }
  if(i_bo) {
    dk3sto_it_reset(i_bo);
    while(NULL != (book = (book_t *)dk3sto_it_next(i_bo))) {
      book_free(book);
    }
  } i_bo = NULL;
  if(s_bo) {
    dk3sto_close(s_bo);
  } s_bo = NULL;
  if(i_au) {
    dk3sto_it_reset(i_au);
    while(NULL != (author = (author_t *)dk3sto_it_next(i_au))) {
      author_free(author);
    }
  } i_au = NULL;
  if(s_au) {
    dk3sto_close(s_au);
  } s_au = NULL;
  exit(exval);
  return exval;
}


#else
#error	"This program requires a MySQL database!"
#endif

