Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[80966b]: src / core / modules / db_mysql / res.c Maximize Restore History

Download this file

res.c    154 lines (132 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* $Id: res.c 5901 2009-07-21 07:45:05Z bogdan_iancu $
*
* MySQL module result related functions
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2007-2008 1&1 Internet AG
*
* This file is part of opensips, a free SIP server.
*
* opensips 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 of the License, or
* (at your option) any later version
*
* opensips 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.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <mysql/mysql.h>
#include "../../db/db_res.h"
#include "../../mem/mem.h"
#include "../../log.h"
#include "row.h"
#include "my_con.h"
#include "res.h"
/**
* Get and convert columns from a result
*/
int db_mysql_get_columns(const db_con_t* _h, db_res_t* _r)
{
int col;
MYSQL_FIELD* fields;
if ((!_h) || (!_r)) {
LM_ERR("invalid parameter\n");
return -1;
}
if (CON_HAS_PS(_h)) {
RES_COL_N(_r) = CON_MYSQL_PS(_h)->cols_out;
} else {
RES_COL_N(_r) = mysql_field_count(CON_CONNECTION(_h));
}
if (!RES_COL_N(_r)) {
LM_ERR("no columns returned from the query\n");
return -2;
} else {
LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
}
if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
LM_ERR("could not allocate columns\n");
return -3;
}
fields = mysql_fetch_fields(CON_RESULT(_h));
for(col = 0; col < RES_COL_N(_r); col++) {
/* The pointer that is here returned is part of the result structure */
RES_NAMES(_r)[col]->s = fields[col].name;
RES_NAMES(_r)[col]->len = strlen(fields[col].name);
LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
switch(fields[col].type) {
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_DECIMAL:
#if MYSQL_VERSION_ID > 49999
case MYSQL_TYPE_NEWDECIMAL:
#endif
case MYSQL_TYPE_TIMESTAMP:
LM_DBG("use DB_INT result type\n");
RES_TYPES(_r)[col] = DB_INT;
break;
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
LM_DBG("use DB_DOUBLE result type\n");
RES_TYPES(_r)[col] = DB_DOUBLE;
break;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_DATE:
LM_DBG("use DB_DATETIME result type\n");
RES_TYPES(_r)[col] = DB_DATETIME;
break;
case MYSQL_TYPE_BLOB:
LM_DBG("use DB_BLOB result type\n");
RES_TYPES(_r)[col] = DB_BLOB;
break;
case FIELD_TYPE_SET:
LM_DBG("use DB_BITMAP result type\n");
RES_TYPES(_r)[col] = DB_BITMAP;
break;
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
LM_DBG("use DB_STRING result type\n");
RES_TYPES(_r)[col] = DB_STRING;
break;
default:
LM_WARN("unhandled data type column (%.*s) type id (%d), "
"use DB_STRING as default\n", RES_NAMES(_r)[col]->len,
RES_NAMES(_r)[col]->s, fields[col].type);
RES_TYPES(_r)[col] = DB_STRING;
break;
}
}
return 0;
}
/**
* Fill the structure with data from database
*/
int db_mysql_fill_result(const db_con_t* _h, db_res_t* _r)
{
if ((!_h) || (!_r)) {
LM_ERR("invalid parameter\n");
return -1;
}
if (db_mysql_get_columns(_h, _r) < 0) {
LM_ERR("error while getting column names\n");
return -2;
}
if (CON_HAS_PS(_h)) {
RES_NUM_ROWS(_r) = mysql_stmt_num_rows(CON_PS_STMT(_h));
} else {
RES_NUM_ROWS(_r) = mysql_num_rows(CON_RESULT(_h));
}
return 0;
}