2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
CResultField.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CRESULTFIELD_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include "CResultField.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int valid_result_field(CRESULTFIELD *_object)
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
return !THIS->result || !THIS->result->conn || !THIS->result->conn->db.handle;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CRESULTFIELD_find(CRESULT *result, const char *name, bool error)
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
int index;
|
|
|
|
char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!name || !*name)
|
|
|
|
return -1;
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
index = strtol(name, &p, 10);
|
|
|
|
if (*name && *p == 0)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= result->info.nfield)
|
2014-10-18 15:23:29 +02:00
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
if (error)
|
|
|
|
GB.Error("Bad field index");
|
|
|
|
index = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (result->handle)
|
|
|
|
index = result->driver->Result.Field.Index(result->handle, (char *)name, &result->conn->db);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (index = 0; index < result->info.nfield; index++)
|
2014-10-18 15:23:29 +02:00
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
if (strcasecmp(name, result->info.field[index].name) == 0)
|
|
|
|
break;
|
2014-10-18 15:23:29 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if (index < 0 || index >= result->info.nfield)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
GB.Error("Unknown field: &1", name);
|
|
|
|
index = -1;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
return index;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static CRESULTFIELD *make_result_field(CRESULT *result, int index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
CRESULTFIELD *_object;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
_object = GB.New(GB.FindClass("ResultField"), NULL, NULL);
|
|
|
|
THIS->result = result;
|
|
|
|
THIS->driver = result->conn->driver;
|
|
|
|
THIS->index = index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
return _object;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *CRESULTFIELD_get(CRESULT *result, const char *name)
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
int index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if ((intptr_t)name >> 16)
|
|
|
|
index = CRESULTFIELD_find(result, name, TRUE);
|
|
|
|
else
|
|
|
|
index = (int)(intptr_t)name;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if (index < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return make_result_field(result, index);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CRESULTFIELD_exist(CRESULT *result, const char *name)
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
return CRESULTFIELD_find(result, name, FALSE) >= 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
char *CRESULTFIELD_key(CRESULT *result, int index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
if (result->handle)
|
|
|
|
return result->driver->Result.Field.Name(result->handle, index);
|
|
|
|
else
|
|
|
|
return result->info.field[index].name;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRESULTFIELD_release(CRESULT *result, void *_object)
|
|
|
|
{
|
|
|
|
THIS->result = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
ResultField
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CRESULTFIELD_free)
|
|
|
|
|
|
|
|
if (!valid_result_field(THIS))
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_SubCollectionRemove(THIS->result->fields, CRESULTFIELD_key(THIS->result, THIS->index), 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CRESULTFIELD_name)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB.ReturnNewZeroString(CRESULTFIELD_key(THIS->result, THIS->index));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CRESULTFIELD_type)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
CRESULT *result = THIS->result;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if (result->handle)
|
|
|
|
GB.ReturnInteger(result->driver->Result.Field.Type(result->handle, THIS->index));
|
|
|
|
else
|
|
|
|
GB.ReturnInteger(result->info.field[THIS->index].type);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CRESULTFIELD_length)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
CRESULT *result = THIS->result;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if (result->handle)
|
|
|
|
GB.ReturnInteger(result->driver->Result.Field.Length(result->handle, THIS->index));
|
|
|
|
else
|
|
|
|
GB.ReturnInteger(result->info.field[THIS->index].length);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
/*BEGIN_PROPERTY(CRESULTFIELD_default)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB.Error("No default value");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY*/
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CRESULTFIELD_result)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB.ReturnObject(THIS->result);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CResultFieldDesc[] =
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_DECLARE("ResultField", sizeof(CRESULTFIELD)),
|
|
|
|
GB_NOT_CREATABLE(),
|
|
|
|
GB_HOOK_CHECK(valid_result_field),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_METHOD("_free", NULL, CRESULTFIELD_free, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_PROPERTY_READ("Name", "s", CRESULTFIELD_name),
|
|
|
|
GB_PROPERTY_READ("Type", "i", CRESULTFIELD_type),
|
|
|
|
GB_PROPERTY_READ("Length", "i", CRESULTFIELD_length),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_PROPERTY_READ("Result", "Result", CRESULTFIELD_result),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
.Result.Fields
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#undef THIS
|
2012-11-14 23:24:15 +01:00
|
|
|
#define THIS ((CSUBCOLLECTION *)_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
BEGIN_PROPERTY(CRESULTFIELD_count)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
CRESULT *result = GB_SubCollectionContainer(THIS);
|
|
|
|
GB.ReturnInteger(result->info.nfield);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CRESULTFIELD_next)
|
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
CRESULT *result = GB_SubCollectionContainer(THIS);
|
|
|
|
int *index = (int *)GB.GetEnum();
|
|
|
|
CRESULTFIELD *rf;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
if (*index >= result->info.nfield)
|
|
|
|
GB.StopEnum();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rf = GB_SubCollectionGet(THIS, CRESULTFIELD_key(result, *index), 0);
|
|
|
|
(*index)++;
|
|
|
|
GB.ReturnObject(rf);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CResultFieldsDesc[] =
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_DECLARE(".Result.Fields", 0), GB_INHERITS(".SubCollection"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_PROPERTY_READ("Count", "i", CRESULTFIELD_count),
|
|
|
|
//GB_PROPERTY_READ("Length", "i", CRESULTFIELD_count),
|
|
|
|
GB_METHOD("_next", "ResultField", CRESULTFIELD_next, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-01-08 13:12:15 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|