garten

changeset 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 13c6828bd4a5
children 8db6497d6065
files clock.c db.h environment.c game.h growth.c weather.c
diffstat 6 files changed, 96 insertions(+), 96 deletions(-) [+]
line diff
     1.1 --- a/clock.c	Wed Jul 23 11:40:45 2008 +0200
     1.2 +++ b/clock.c	Wed Jul 23 11:41:38 2008 +0200
     1.3 @@ -1,6 +1,5 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 -#include <mysql.h>
     1.7  
     1.8  #include "db.h"
     1.9  #include "game.h"
    1.10 @@ -8,28 +7,27 @@
    1.11  
    1.12  
    1.13  void inc_time() {
    1.14 -
    1.15  	/* get current time */
    1.16 -	sprintf(query, "select time from game where name = '%s' ", gamename);
    1.17 +	sprintf(query, "select time from game;");
    1.18  	db_query(query);
    1.19 -	result = mysql_store_result(conn);
    1.20 -	if (mysql_num_rows(result)) {
    1.21 -		row = mysql_fetch_row(result);
    1.22 -		gametime = atoi(row[0]);
    1.23 +	if (sqlite3_step(stmt) == SQLITE_ROW) {
    1.24 +		gametime = sqlite3_column_int(stmt, 0);
    1.25 +		printf("gametime: %d\n", gametime);
    1.26 +	} else {
    1.27 +		fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
    1.28  	}
    1.29 -	mysql_free_result(result);
    1.30 -
    1.31 +	sqlite3_finalize(stmt);
    1.32  
    1.33  	/* increment time */
    1.34 -	sprintf(query, " update game set time = '%d' where name = '%s' ", ++gametime, gamename);
    1.35 +	sprintf(query, " update game set time = '%d';", ++gametime);
    1.36  	db_query(query);
    1.37 -	if (mysql_affected_rows(conn) > 0) {
    1.38 +	if (sqlite3_step(stmt) == SQLITE_DONE) {
    1.39  		printf("time update successful\n");
    1.40  		printf("virtual time: %d\n", gametime);
    1.41  	} else {
    1.42 -		printf("E: time update failed\n");
    1.43 +		printf("error: time update failed: %s\n", sqlite3_errmsg(db));
    1.44  	}
    1.45 -
    1.46 +	sqlite3_finalize(stmt);
    1.47  }
    1.48  
    1.49  
    1.50 @@ -41,10 +39,10 @@
    1.51  		printf("usage: %s <game>\n", argv[0]);
    1.52  		exit(1);
    1.53  	}
    1.54 -	gamename = argv[1];
    1.55 +	database = argv[1];
    1.56  
    1.57  	db_connect();
    1.58 -	check_game();
    1.59 +	read_time();
    1.60  
    1.61  	inc_time();
    1.62  
     2.1 --- a/db.h	Wed Jul 23 11:40:45 2008 +0200
     2.2 +++ b/db.h	Wed Jul 23 11:41:38 2008 +0200
     2.3 @@ -2,39 +2,51 @@
     2.4   * data for the database connection
     2.5   */
     2.6  
     2.7 -char* server = "localhost";
     2.8 -char* user = "garten";
     2.9 -char* password = "gras";
    2.10 -char* database = "garten";
    2.11 +#include <sqlite3.h>
    2.12  
    2.13 -MYSQL* conn;
    2.14 -MYSQL_RES* result;
    2.15 -MYSQL_ROW row;
    2.16 +char* database;
    2.17 +char query[1024];
    2.18 +sqlite3* db;
    2.19 +sqlite3_stmt* stmt;
    2.20 +
    2.21  
    2.22  
    2.23  void db_connect() {
    2.24 -	conn = mysql_init(NULL);
    2.25 -
    2.26 -	/* Connect to database */
    2.27 -	if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
    2.28 -		fprintf(stderr, "%s\n", mysql_error(conn));
    2.29 +	if (sqlite3_open(database, &db) != SQLITE_OK) {
    2.30 +		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    2.31 +		sqlite3_close(db);
    2.32  		exit(1);
    2.33  	}
    2.34  }
    2.35  
    2.36  
    2.37  void db_close() {
    2.38 -	/* close connection */
    2.39 -	mysql_close(conn);
    2.40 +	sqlite3_close(db);
    2.41  }
    2.42  
    2.43  
    2.44 +/*
    2.45 +static int callback(void* NotUsed, int argc, char* argv[], char* azColName[]){
    2.46 +	NotUsed = NULL;
    2.47 +	int i;
    2.48 +	for (i = 0; i < argc; i++) {
    2.49 +		printf("%s = %s\n", azColName[i], argv[i] ? argv[i]: "NULL");
    2.50 +	}
    2.51 +	printf("\n");
    2.52 +	return 0;
    2.53 +}
    2.54 +
    2.55 +int rc;
    2.56 +char* zErrMsg = 0;
    2.57 +rc = sqlite3_exec(db, query, callback, 0, &zErrMsg);
    2.58 +*/
    2.59 +
    2.60 +
    2.61  void db_query(char* query) {
    2.62  	int error;
    2.63 -	/* send SQL query */
    2.64 -	error = mysql_query(conn, query);
    2.65 -	/*printf("query: %s\nerror: %d / %d\n", query, error, mysql_errno(conn));*/
    2.66 -	if (error != 0) {
    2.67 -		fprintf(stderr, "E: %s\n", mysql_error(conn));
    2.68 +
    2.69 +	error = sqlite3_prepare(db, query, -1, &stmt, NULL);
    2.70 +	if (error) {
    2.71 +		fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
    2.72  	}
    2.73  }
     3.1 --- a/environment.c	Wed Jul 23 11:40:45 2008 +0200
     3.2 +++ b/environment.c	Wed Jul 23 11:41:38 2008 +0200
     3.3 @@ -1,6 +1,5 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6 -#include <mysql.h>
     3.7  
     3.8  #include "db.h"
     3.9  #include "game.h"
    3.10 @@ -12,7 +11,7 @@
    3.11  	int groundwater, slugs, earthworms;
    3.12  
    3.13  	/* get weather and last environments to calculate the next one *
    3.14 -	sprintf(query, "select time from simulation where name = '%s' ", gamename);
    3.15 +	sprintf(query, "select time from simulation");
    3.16  	db_query(query);
    3.17  	result = mysql_store_result(conn);
    3.18  	if (mysql_num_rows(result)) {
    3.19 @@ -29,14 +28,14 @@
    3.20  
    3.21  	/* set weather */
    3.22  	sprintf(query, " insert into environment \
    3.23 -			(tick, game_id, groundwater, slugs, earthworms) \
    3.24 -			values ('%d', '%d', '%d', '%d', '%d') ",
    3.25 -			gametime, gameid, groundwater, slugs, earthworms);
    3.26 +			(tick, groundwater, slugs, earthworms) \
    3.27 +			values ('%d', '%d', '%d', '%d') ",
    3.28 +			gametime, groundwater, slugs, earthworms);
    3.29  	db_query(query);
    3.30 -	if (mysql_affected_rows(conn) > 0) {
    3.31 +	if (sqlite3_step(stmt) == SQLITE_DONE) {
    3.32  		printf("environment successful inserted\n");
    3.33  	} else {
    3.34 -		printf("E: environment insertion failed\n");
    3.35 +		printf("error: environment insertion failed: %s\n", sqlite3_errmsg(db));
    3.36  	}
    3.37  
    3.38  }
    3.39 @@ -47,13 +46,13 @@
    3.40  
    3.41  	/* init */
    3.42  	if (argc != 2) {
    3.43 -		printf("usage: %s <game>\n", argv[0]);
    3.44 +		printf("usage: %s <database>\n", argv[0]);
    3.45  		exit(1);
    3.46  	}
    3.47 -	gamename = argv[1];
    3.48 +	database = argv[1];
    3.49  
    3.50  	db_connect();
    3.51 -	check_game();
    3.52 +	read_time();
    3.53  
    3.54  	set_environment();
    3.55  
     4.1 --- a/game.h	Wed Jul 23 11:40:45 2008 +0200
     4.2 +++ b/game.h	Wed Jul 23 11:41:38 2008 +0200
     4.3 @@ -1,25 +1,15 @@
     4.4 -char query[1024];
     4.5 -char* gamename;
     4.6 -int gameid;
     4.7  int gametime;
     4.8  
     4.9  
    4.10 -void check_game() {
    4.11 -	int rows;
    4.12 -	MYSQL_ROW row;
    4.13 -
    4.14 -	sprintf(query, "select id, time from game where name = '%s' ", gamename);
    4.15 +int read_time() {
    4.16 +	sprintf(query, "select time from game;");
    4.17  	db_query(query);
    4.18 -	result = mysql_store_result(conn);
    4.19 -	rows = mysql_num_rows(result);
    4.20 -	row = mysql_fetch_row(result);
    4.21 -	gameid = atoi(row[0]);
    4.22 -	gametime = atoi(row[1]);
    4.23 -	mysql_free_result(result);
    4.24 -	
    4.25 -	if (rows != 1) {
    4.26 -		printf("game '%s' does not exist\n", gamename);
    4.27 +	if (sqlite3_step(stmt) != SQLITE_ROW) {
    4.28 +		printf("error: %s\n", sqlite3_errmsg(db));
    4.29  		exit(1);
    4.30  	}
    4.31 +	gametime = sqlite3_column_int(stmt, 0);
    4.32 +	sqlite3_finalize(stmt);
    4.33 +	return gametime;
    4.34  }
    4.35  
     5.1 --- a/growth.c	Wed Jul 23 11:40:45 2008 +0200
     5.2 +++ b/growth.c	Wed Jul 23 11:41:38 2008 +0200
     5.3 @@ -1,6 +1,5 @@
     5.4  #include <stdio.h>
     5.5  #include <stdlib.h>
     5.6 -#include <mysql.h>
     5.7  
     5.8  #include "db.h"
     5.9  #include "game.h"
    5.10 @@ -16,15 +15,12 @@
    5.11  	sprintf(query, "select\
    5.12  			f.id, f.size, f.age, p.size, p.growspeed, p.age\
    5.13  			from field f\
    5.14 -			join plant p on f.plant_id = p.id\
    5.15 -			where f.game_id = '%d' ",
    5.16 -			gameid);
    5.17 +			join plant p on f.plant_id = p.id ");
    5.18  	db_query(query);
    5.19 -	result = mysql_store_result(conn);
    5.20 -	printf("number of plants to process: %d\n", (int)mysql_num_rows(result));
    5.21 -	while ((row = mysql_fetch_row(result)) != NULL) {
    5.22 +	/* printf("number of plants to process: %d\n", (int)mysql_num_rows(result)); */
    5.23 +	while (sqlite3_step(stmt) == SQLITE_ROW) {
    5.24  		for (i = 0; i < 6; i++) {
    5.25 -			r[i] = atoi(row[i]);
    5.26 +			r[i] = (char) sqlite3_column_int(stmt, i);
    5.27  		}
    5.28  
    5.29  		sprintf(query, "update field set \
    5.30 @@ -32,15 +28,16 @@
    5.31  				where id = '%d' ",
    5.32  				(r[1] + 1), r[2]+1, r[0]);
    5.33  		db_query(query);
    5.34 +		;
    5.35  
    5.36 -		if (mysql_affected_rows(conn) > 0) {
    5.37 +		if (sqlite3_step(stmt) == SQLITE_DONE) {
    5.38  			printf("update successful  ++  %s\n", query);
    5.39  		} else {
    5.40 -			printf("E: update failed  ++  %s\n", query);
    5.41 +			printf("E: update failed  ++  %s\n", sqlite3_errmsg(db));
    5.42  		}
    5.43  
    5.44  	}
    5.45 -	mysql_free_result(result);
    5.46 +	sqlite3_finalize(stmt);
    5.47  
    5.48  
    5.49  
    5.50 @@ -53,13 +50,13 @@
    5.51  
    5.52  	/* init */
    5.53  	if (argc != 2) {
    5.54 -		printf("usage: %s <game>\n", argv[0]);
    5.55 +		printf("usage: %s <database>\n", argv[0]);
    5.56  		exit(1);
    5.57  	}
    5.58 -	gamename = argv[1];
    5.59 +	database = argv[1];
    5.60  
    5.61  	db_connect();
    5.62 -	check_game();
    5.63 +	read_time();
    5.64  
    5.65  	grow_plants();
    5.66  
     6.1 --- a/weather.c	Wed Jul 23 11:40:45 2008 +0200
     6.2 +++ b/weather.c	Wed Jul 23 11:41:38 2008 +0200
     6.3 @@ -1,6 +1,5 @@
     6.4  #include <stdio.h>
     6.5  #include <stdlib.h>
     6.6 -#include <mysql.h>
     6.7  #include <time.h>
     6.8  
     6.9  #include "db.h"
    6.10 @@ -45,11 +44,20 @@
    6.11  
    6.12  	sprintf(query,
    6.13  			" select temp, sun, rain, wind, hum from weather "
    6.14 -			" where game_id = '%d' "
    6.15  			" order by tick desc "
    6.16  			" limit %d "
    6.17 -			, gameid, nlast);
    6.18 +			, nlast);
    6.19  	db_query(query);
    6.20 +	for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
    6.21 +		lastw[i].temp = sqlite3_column_double(stmt, 0);
    6.22 +		lastw[i].sun = sqlite3_column_double(stmt, 1);
    6.23 +		lastw[i].rain = sqlite3_column_double(stmt, 2);
    6.24 +		lastw[i].wind = sqlite3_column_double(stmt, 3);
    6.25 +		lastw[i].hum = sqlite3_column_double(stmt, 4);
    6.26 +	}
    6.27 +	sqlite3_finalize(stmt);
    6.28 +
    6.29 +	/*
    6.30  	result = mysql_store_result(conn);
    6.31  	for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
    6.32  		lastw[i].temp = atof(row[0]);
    6.33 @@ -59,6 +67,7 @@
    6.34  		lastw[i].hum = atof(row[4]);
    6.35  	}
    6.36  	mysql_free_result(result);
    6.37 +	*/
    6.38  
    6.39  }
    6.40  
    6.41 @@ -72,16 +81,20 @@
    6.42  }
    6.43  
    6.44  
    6.45 -int setweather(struct weather* w) {
    6.46 +void setweather(struct weather* w) {
    6.47  	char query[512];
    6.48  	sprintf(query,
    6.49  			" insert into weather "
    6.50 -			" (tick, game_id, temp, sun, rain, wind, hum) "
    6.51 -			" values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') "
    6.52 -			, gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum);
    6.53 +			" (tick, temp, sun, rain, wind, hum) "
    6.54 +			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
    6.55 +			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
    6.56  	db_query(query);
    6.57  	puts(query);
    6.58 -	return mysql_affected_rows(conn);
    6.59 +	if (sqlite3_step(stmt) == SQLITE_DONE) {
    6.60 +		printf("weather successful inserted\n");
    6.61 +	} else {
    6.62 +		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
    6.63 +	}
    6.64  }
    6.65  
    6.66  
    6.67 @@ -90,13 +103,13 @@
    6.68  
    6.69  	/* init */
    6.70  	if (argc != 2) {
    6.71 -		printf("usage: %s <game>\n", argv[0]);
    6.72 +		printf("usage: %s <database>\n", argv[0]);
    6.73  		exit(1);
    6.74  	}
    6.75 -	gamename = argv[1];
    6.76 +	database = argv[1];
    6.77  
    6.78  	db_connect();
    6.79 -	check_game();
    6.80 +	read_time();
    6.81  
    6.82  	srand((unsigned int) time(NULL));
    6.83  
    6.84 @@ -105,17 +118,8 @@
    6.85  	struct weather lastn[Nlast];
    6.86  
    6.87  	getlastweather(lastn, Nlast);
    6.88 -
    6.89  	genweather(&w, lastn, Nlast, May);
    6.90 -
    6.91 -	if (setweather(&w) > 0) {
    6.92 -		printf("weather successful inserted\n");
    6.93 -	} else {
    6.94 -		printf("E: weather insertion failed\n");
    6.95 -	}
    6.96 -
    6.97 -/* 	set_weather(); */
    6.98 -
    6.99 +	setweather(&w);
   6.100  
   6.101  	db_close();
   6.102