Browse Source

- Support for raw sql queries

Jan Janak 18 years ago
parent
commit
2cd3e836e9
3 changed files with 28 additions and 0 deletions
  1. 24 0
      modules/db_mysql/my_cmd.c
  2. 3 0
      modules/db_mysql/my_cmd.h
  3. 1 0
      modules/db_mysql/mysql_mod.c

+ 24 - 0
modules/db_mysql/my_cmd.c

@@ -466,6 +466,19 @@ int my_cmd_read(db_res_t* res, db_cmd_t* cmd)
 }
 
 
+int my_cmd_sql(db_res_t* res, db_cmd_t* cmd)
+{
+	struct my_cmd* mcmd;
+   
+	mcmd = DB_GET_PAYLOAD(cmd);
+	if (mysql_stmt_execute(mcmd->st)) {
+		ERR("Error while executing query: %s\n", mysql_stmt_error(mcmd->st));
+		return -1;
+	}
+	return 0;
+}
+
+
 static int bind_params(MYSQL_STMT* st, db_fld_t* fld)
 {
 	int i, n;
@@ -712,6 +725,17 @@ int my_cmd(db_cmd_t* cmd)
 		}
 		if (bind_result(res->st, cmd->result) < 0) goto error;
 		break;
+
+	case DB_SQL:
+		if (mysql_stmt_prepare(res->st, cmd->table.s, cmd->table.len)) {
+			ERR("Error while preparing raw SQL query: %s\n",
+				mysql_stmt_error(res->st));
+			goto error;
+		}
+		if (!DB_FLD_EMPTY(cmd->result)) {
+			if (bind_result(res->st, cmd->result) < 0) goto error;
+		}
+		break;
 	}
 
 	DB_SET_PAYLOAD(cmd, res);

+ 3 - 0
modules/db_mysql/my_cmd.h

@@ -48,6 +48,9 @@ int my_cmd_read(db_res_t* res, db_cmd_t* cmd);
 /* Runtime execution function for DB_PUT and DB_DEL */
 int my_cmd_write(db_res_t* res, db_cmd_t* cmd);
 
+/* Raw SQL query */
+int my_cmd_sql(db_res_t* res, db_cmd_t* cmd);
+
 int my_cmd_first(db_res_t* res);
 
 int my_cmd_next(db_res_t* res);

+ 1 - 0
modules/db_mysql/mysql_mod.c

@@ -61,6 +61,7 @@ static cmd_export_t cmds[] = {
 	{"db_put",         (cmd_function)my_cmd_write, 0, 0, 0},
 	{"db_del",         (cmd_function)my_cmd_write, 0, 0, 0},
 	{"db_get",         (cmd_function)my_cmd_read, 0, 0, 0},
+	{"db_sql",         (cmd_function)my_cmd_sql, 0, 0, 0},
 	{"db_res",         (cmd_function)my_res,  0, 0, 0},
 	{"db_fld",         (cmd_function)my_fld,  0, 0, 0},
 	{"db_first",       (cmd_function)my_cmd_next, 0, 0, 0},