3 @brief Excecute a specified SQL query and return the results.
9 #include "opensrf/utils.h"
10 #include "opensrf/log.h"
11 #include "opensrf/string_array.h"
12 #include "opensrf/osrf_json.h"
13 #include "openils/oils_buildq.h"
15 static jsonObject* get_row( BuildSQLState* state );
16 static jsonObject* get_date_column( dbi_result result, int col_idx );
17 static int values_missing( BuildSQLState* state );
20 @brief Execute the current SQL statement and return the first row.
21 @param state Pointer to the query-building context.
22 @return Pointer to a newly-allocated jsonObject representing the row, if there is one; or
25 The returned row is a JSON_ARRAY of column values, of which each is a JSON_STRING,
26 JSON_NUMBER, or JSON_NULL.
28 jsonObject* oilsFirstRow( BuildSQLState* state ) {
33 // Make sure all the bind variables have values for them
34 if( !state->values_required && values_missing( state )) {
36 osrfLogError( OSRF_LOG_MARK, sqlAddMsg( state,
37 "Unable to execute query: values not available for all bind variables\n" ));
42 dbi_result_free( state->result );
45 state->result = dbi_conn_query( state->dbhandle, OSRF_BUFFER_C_STR( state->sql ));
46 if( !state->result ) {
49 (void) dbi_conn_error( state->dbhandle, &msg );
50 osrfLogError( OSRF_LOG_MARK, sqlAddMsg( state,
51 "Unable to execute query: %s",msg ? msg : "No description available" ));
56 if( dbi_result_first_row( state->result ))
57 return get_row( state );
59 dbi_result_free( state->result );
61 return NULL; // No rows returned
66 @brief Return the next row from a previously executed SQL statement.
67 @param state Pointer to the query-building context.
68 @return Pointer to a newly-allocated jsonObject representing the row, if there is one; or
71 The returned row is a JSON_ARRAY of column values, of which each is a JSON_STRING,
72 JSON_NUMBER, or JSON_NULL.
74 jsonObject* oilsNextRow( BuildSQLState* state ) {
76 if( !state || !state->result )
80 if( dbi_result_next_row( state->result ))
81 return get_row( state );
83 dbi_result_free( state->result );
85 return NULL; // No next row returned
90 @brief Construct a JSON representation of a returned row.
91 @param state Pointer to the query-building context.
92 @return Pointer to a newly-allocated jsonObject representing the row.
94 static jsonObject* get_row( BuildSQLState* state ) {
95 unsigned int col_count = dbi_result_get_numfields( state->result );
96 jsonObject* row = jsonNewObjectType( JSON_ARRAY );
99 for( i = 1; i <= col_count; ++i ) {
101 if( dbi_result_field_is_null_idx( state->result, i )) {
102 jsonObjectPush( row, jsonNewObjectType( JSON_NULL ));
103 continue; // Column is null
106 jsonObject* col_value = NULL;
107 int type = dbi_result_get_field_type_idx( state->result, i );
109 case DBI_TYPE_INTEGER : {
110 long long value = dbi_result_get_longlong_idx( state->result, i );
111 col_value = jsonNewNumberObject( (double) value );
114 case DBI_TYPE_DECIMAL : {
115 double value = dbi_result_get_double_idx( state->result, i );
116 col_value = jsonNewNumberObject( value );
119 case DBI_TYPE_STRING : {
120 const char* value = dbi_result_get_string_idx( state->result, i );
121 col_value = jsonNewObject( value );
124 case DBI_TYPE_BINARY : {
125 osrfLogError( OSRF_LOG_MARK, "Binary types not supported; column set to null" );
126 col_value = jsonNewObjectType( JSON_NULL );
129 case DBI_TYPE_DATETIME : {
130 col_value = get_date_column( state->result, i );
134 osrfLogError( OSRF_LOG_MARK,
135 "Unrecognized column type %d; column set to null", type );
136 col_value = jsonNewObjectType( JSON_NULL );
139 jsonObjectPush( row, col_value );
146 @brief Translate a date column into a string.
147 @param result Reference to the current returned row.
148 @param col_idx Column number (starting with 1) within the row.
149 @return Pointer to a newly-allocated JSON_STRING containing a formatted date string.
151 The calling code is responsible for freeing the returned jsonObject by calling
154 static jsonObject* get_date_column( dbi_result result, int col_idx ) {
156 time_t timestamp = dbi_result_get_datetime_idx( result, col_idx );
157 char timestring[ 256 ] = "";
158 int attr = dbi_result_get_field_attribs_idx( result, col_idx );
161 if( !( attr & DBI_DATETIME_DATE )) {
162 gmtime_r( ×tamp, &gmdt );
163 strftime( timestring, sizeof( timestring ), "%T", &gmdt );
164 } else if( !( attr & DBI_DATETIME_TIME )) {
165 localtime_r( ×tamp, &gmdt );
166 strftime( timestring, sizeof( timestring ), "%F", &gmdt );
168 localtime_r( ×tamp, &gmdt );
169 strftime( timestring, sizeof( timestring ), "%FT%T%z", &gmdt );
172 return jsonNewObject( timestring );
176 @brief Determine whether all bind variables have values supplied for them.
177 @param state Pointer to the query-building context.
178 @return The number of bind variables with no available value.
180 static int values_missing( BuildSQLState* state ) {
181 if( !state->bindvar_list || osrfHashGetCount( state->bindvar_list ) == 0 )
182 return 0; // Nothing to count
185 osrfHashIterator* iter = osrfNewHashIterator( state->bindvar_list );
187 BindVar* bind = NULL;
188 while(( bind = osrfHashIteratorNext( iter ))) {
189 if( !bind->actual_value && !bind->default_value ) {
190 sqlAddMsg( state, "No value for bind value \"%s\", with label \"%s\"",
191 bind->name, bind->label );
196 osrfHashIteratorFree( iter );