Beispiel #1
0
//static kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs)
kqcur_t *MYSQL_query(CTX ctx, kconn_t *hdr, kbytes_t sql, kResultSet *rs)
{
    MYSQL_RES *res = NULL;
    MYSQL *db = (MYSQL*)hdr;
    if (db == NULL) {
        /* return NULL */
    }
    else if (rs == NULL) {
        /* Connection.exec */
        int r = mysql_query(db, sql.text);
        KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db))));
        //if(r > 0) {
        //	knh_mysql_perror(ctx, (MYSQL*)db, r);
        //}
    }
    else {
        /* Connection.query */
        int r = mysql_query((MYSQL*)db, sql.text);
        KNH_NTRACE2(ctx, "mysql_query", (r == 0) ? K_OK : K_FAILED, KNH_LDATA(
                        LOG_s("query", sql.text), LOG_i("errno", mysql_errno(db)),
                        LOG_s("error", mysql_error(db))
                    ));
        if (r == 0) {
            res = mysql_store_result((MYSQL*)db);
            if (res == NULL) { // NULL RESULT
                if (mysql_errno(db) != 0) {
                    mysql_free_result(res);
                    KNH_NTRACE2(ctx, "mysql_store_result", K_FAILED, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db))));
                } else {
                    KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db))));
                }
            }
            else {
                knh_ResultSet_initColumn(ctx, rs, (size_t)mysql_num_fields(res));
                KNH_NTRACE2(ctx, "mysql_store_result", K_OK, KNH_LDATA(LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db))));
                int i = 0;
                MYSQL_FIELD *field = NULL;
                while((field = mysql_fetch_field(res))) {
                    DP(rs)->column[i].dbtype = field->type;
                    kString *s = new_String(ctx, field->name);
                    ResultSet_setName(ctx, rs, i, s);
                    i++;
                }
            }
        }
    }
    return (kqcur_t *) res;
}
Beispiel #2
0
static
knh_dbcur_t *knh_dbquery__sqlite3(Ctx *ctx, knh_db_t *hdr, knh_bytes_t sql, ResultSet *rs)
{
	if(rs == NULL) {
		int r = sqlite3_exec((sqlite3*)hdr, (const char*)sql.buf, NULL, NULL, NULL);
		if(r != SQLITE_OK) {
			knh_sqlite3_perror(ctx, (sqlite3*)hdr, r);
		}
		return NULL;
	}
	else {
		sqlite3_stmt *stmt = NULL;
		sqlite3_prepare((sqlite3*)hdr, (char*)sql.buf, sql.len, &stmt, NULL);
//	if (r != SQLITE_OK) {
//		sqlite3_finalize(stmt);
//		DBG2_P("msg='%s', sqlite3_errmsg((sqlite3)hdr));
//		return NULL;
//	}
//		r = sqlite3_reset(stmt);
//	if(r != SQLITE_OK) {
//		sqlite3_finalize(stmt);
//		return NULL;
//	}
		size_t column_size = (size_t)sqlite3_column_count(stmt);
		//DBG2_P("column_size=%d", column_size);
		knh_ResultSet_initColumn(ctx, rs, column_size);
		if(column_size > 0) {
			size_t i;
			for(i = 0; i < DP(rs)->column_size; i++) {
				char *n = (char*)sqlite3_column_name(stmt, i);
				//DBG2_P("(%d) name = '%s'", i, n);
				if(n != NULL) {
					knh_ResultSet_setName(ctx, rs, i, new_String(ctx, B(n), NULL));
				}
			}
		}
		return (knh_dbcur_t*)stmt;
	}
}