Browse code

- support for 'last_id' getopt, which retrieves the unique id of the last inserted row

Jan Janak authored on 14/02/2008 15:40:44
Showing 1 changed files
... ...
@@ -1261,11 +1261,31 @@ int my_cmd_next(db_res_t* res)
1261 1261
 int my_getopt(db_cmd_t* cmd, char* optname, va_list ap)
1262 1262
 {
1263 1263
 	struct my_cmd* mcmd;
1264
+	long long* id;
1264 1265
 	int* val;
1265 1266
 
1266 1267
 	mcmd = (struct my_cmd*)DB_GET_PAYLOAD(cmd);
1267 1268
 
1268
-	if (!strcasecmp("fetch_all", optname)) {
1269
+	if (!strcasecmp("last_id", optname)) {
1270
+		id = va_arg(ap, long long*);
1271
+		if (id == NULL) {
1272
+			BUG("mysql: NULL pointer passed to 'last_id' option\n");
1273
+			goto error;
1274
+		}
1275
+
1276
+		if (mcmd->st->last_errno != 0) {
1277
+			BUG("mysql: Option 'last_id' called but previous command failed, "
1278
+				"check your code\n");
1279
+			return -1;
1280
+		}
1281
+
1282
+		*id = mysql_stmt_insert_id(mcmd->st);
1283
+		if ((*id) == 0) {
1284
+			BUG("mysql: Option 'last_id' called but there is no auto-increment"
1285
+				" column in table, SQL command: %.*s\n", STR_FMT(&mcmd->sql_cmd));
1286
+			return -1;
1287
+		}
1288
+	} else if (!strcasecmp("fetch_all", optname)) {
1269 1289
 		val = va_arg(ap, int*);
1270 1290
 		if (val == NULL) {
1271 1291
 			BUG("mysql: NULL pointer passed to 'fetch_all' DB option\n");