Esempio n. 1
0
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
	cur_data *cur = getcursor (L);
	MYSQL_RES *res = cur->my_res;
	unsigned long *lengths;
	MYSQL_ROW row = mysql_fetch_row(res);
	if (row == NULL) {
		lua_pushnil(L);  /* no more results */
		return 1;
	}
	lengths = mysql_fetch_lengths(res);

	if (lua_istable (L, 2)) {
		const char *opts = luaL_optstring (L, 3, "n");
		if (strchr (opts, 'n') != NULL) {
			/* Copy values to numerical indices */
			int i;
			for (i = 0; i < cur->numcols; i++) {
				pushvalue (L, row[i], lengths[i]);
				lua_rawseti (L, 2, i+1);
			}
		}
		if (strchr (opts, 'a') != NULL) {
			int i;
			/* Check if colnames exists */
			if (cur->colnames == LUA_NOREF)
		        create_colinfo(L, cur);
			lua_rawgeti (L, LUA_REGISTRYINDEX, cur->colnames);/* Push colnames*/
	
			/* Copy values to alphanumerical indices */
			for (i = 0; i < cur->numcols; i++) {
				lua_rawgeti(L, -1, i+1); /* push the field name */

				/* Actually push the value */
				pushvalue (L, row[i], lengths[i]);
				lua_rawset (L, 2);
			}
			/* lua_pop(L, 1);  Pops colnames table. Not needed */
		}
		lua_pushvalue(L, 2);
		return 1; /* return table */
	}
	else {
		int i;
		luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
		for (i = 0; i < cur->numcols; i++)
			pushvalue (L, row[i], lengths[i]);
		return cur->numcols; /* return #numcols values */
	}
}
Esempio n. 2
0
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
	cur_data *cur = getcursor (L);
	sword status = OCIStmtFetch (cur->stmthp, cur->errhp, 1,
		OCI_FETCH_NEXT, OCI_DEFAULT);

	if (status == OCI_NO_DATA) {
		/* No more rows */
		lua_pushnil (L);
		return 1;
	} else if (status != OCI_SUCCESS) {
		/* Error */
		return checkerr (L, status, cur->errhp);
	}

	if (lua_istable (L, 2)) {
		int i;
		const char *opts = luaL_optstring (L, 3, "n");
		if (strchr (opts, 'n') != NULL)
			/* Copy values to numerical indices */
			for (i = 1; i <= cur->numcols; i++) {
				int ret = pushvalue (L, cur, i);
				if (ret != 1)
					return ret;
				lua_rawseti (L, 2, i);
			}
		if (strchr (opts, 'a') != NULL)
			/* Copy values to alphanumerical indices */
			for (i = 1; i <= cur->numcols; i++) {
				column_data *col = &(cur->cols[i-1]);
				int ret;
				lua_pushlstring (L, col->name, col->namelen);
				if ((ret = pushvalue (L, cur, i)) != 1)
					return ret;
				lua_rawset (L, 2);
			}
		lua_pushvalue(L, 2);
		return 1; /* return table */
	}
	else {
		int i;
		luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
		for (i = 1; i <= cur->numcols; i++) {
			int ret = pushvalue (L, cur, i);
			if (ret != 1)
				return ret;
		}
		return cur->numcols; /* return #numcols values */
	}
}
Esempio n. 3
0
		bool operator!=(const functor<Ret>& rhs) const
		{
			if (!ref_.is_valid() || !rhs.ref_.is_valid()) return true;
			pushvalue();
			rhs.pushvalue();
			bool result = lua_equal(L_, -1, -2) == 0;
			lua_pop(L_, 2);
			return result;
		}
Esempio n. 4
0
static void
copytable(lua_State *L, int tbl, struct proxy *p) {
	const struct document * doc = (const struct document *)p->data; 
	if (p->index < 0 || p->index >= doc->n) {
		luaL_error(L, "Invalid proxy (index = %d, total = %d)", p->index, (int)doc->n);
	}
	const struct table * t = gettable(doc, p->index);
	if (t == NULL) {
		luaL_error(L, "Invalid proxy (index = %d)", p->index);
	}
	const uint32_t * v = (const uint32_t *)((const char *)t + sizeof(uint32_t) + ((t->dict*2+ 3) & ~3));
	int i;
	for (i=0;i<t->dict;i++) {
		pushvalue(L, v++, t->type[2*i], doc);
		pushvalue(L, v++, t->type[2*i+1], doc);
		lua_rawset(L, tbl);
	}
}
Esempio n. 5
0
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
	cur_data *cur = getcursor (L);
	PGresult *res = cur->pg_res;
	int tuple = cur->curr_tuple;

	if (tuple >= PQntuples(cur->pg_res)) {
		cur_nullify (L, cur);
		lua_pushnil(L);  /* no more results */
		return 1;
	}

	cur->curr_tuple++;
	if (lua_istable (L, 2)) {
		int i;
		const char *opts = luaL_optstring (L, 3, "n");
		if (strchr (opts, 'n') != NULL)
			/* Copy values to numerical indices */
			for (i = 1; i <= cur->numcols; i++) {
				pushvalue (L, res, tuple, i);
				lua_rawseti (L, 2, i);
			}
		if (strchr (opts, 'a') != NULL)
			/* Copy values to alphanumerical indices */
			for (i = 1; i <= cur->numcols; i++) {
				lua_pushstring (L, PQfname (res, i-1));
				pushvalue (L, res, tuple, i);
				lua_rawset (L, 2);
			}
		lua_pushvalue(L, 2);
		return 1; /* return table */
	}
	else {
		int i;
		luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
		for (i = 1; i <= cur->numcols; i++)
			pushvalue (L, res, tuple, i);
		return cur->numcols; /* return #numcols values */
	}
}
Esempio n. 6
0
static void result_to_table(lua_State* L, Cursor* cur, MYSQL_ROW row,
                            unsigned long* lengths, int alpha_idx)
{
    assert(L && cur && lengths);
    if (alpha_idx)
    {
        lua_createtable(L, 0, cur->numcols);
        for (unsigned i = 0; i < cur->numcols; i++)
        {
            pushvalue(L, cur->fields[i].type, row[i], lengths[i]);
            lua_setfield(L, -2, cur->fields[i].name);
        }
    }
    else // numeric index
    {
        lua_createtable(L, cur->numcols, 0);
        for (unsigned i = 0; i < cur->numcols; i++)
        {
            pushvalue(L, cur->fields[i].type, row[i], lengths[i]);
            lua_rawseti(L, -2, i + 1);
        }
    }
}