PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/error.c

https://bitbucket.org/drjokepu/libpx
C | 141 lines | 109 code | 25 blank | 7 comment | 16 complexity | 2aca466f31c00efee5525c1661a3d8ea MD5 | raw file
  1//
  2//  error.c
  3//  libpx
  4//
  5//  Created by Tamas Czinege on 08/05/2012.
  6//  Copyright (c) 2012 Tamas Czinege. All rights reserved.
  7//
  8
  9#include <stdio.h>
 10#include <stdlib.h>
 11#include <string.h>
 12#include "error.h"
 13#include "response.h"
 14#include "utility.h"
 15
 16static void px_error_delete_string(char *str);
 17
 18px_error *px_error_new(const px_response *restrict response)
 19{
 20    px_error *error = calloc(1, sizeof(px_error));
 21    
 22    error->severity = px_copy_string(response->response_data.error.severity);
 23    error->sqlState = px_copy_string(response->response_data.error.sqlstate);
 24    error->message = px_copy_string(response->response_data.error.message);
 25    error->detail = px_copy_string(response->response_data.error.detail);
 26    error->hint = px_copy_string(response->response_data.error.hint);
 27    error->position = px_copy_string(response->response_data.error.position);
 28    error->internal_position = px_copy_string(response->response_data.error.internal_position);
 29    error->internal_query = px_copy_string(response->response_data.error.internal_query);
 30    error->where = px_copy_string(response->response_data.error.where);
 31    error->file = px_copy_string(response->response_data.error.file);
 32    error->line = px_copy_string(response->response_data.error.line);
 33    error->routine = px_copy_string(response->response_data.error.routine);
 34    
 35    return error;
 36}
 37
 38px_error *px_error_new_custom(const char *sqlState, const char *message)
 39{
 40    px_error *error = calloc(1, sizeof(px_error));
 41    error->sqlState = px_copy_string(sqlState);
 42    error->message = px_copy_string(message);
 43    
 44    return error;
 45}
 46
 47void px_error_delete(px_error *error)
 48{
 49    if (error == NULL) return;
 50    
 51    px_error_delete_string(error->severity);
 52    px_error_delete_string(error->sqlState);
 53    px_error_delete_string(error->message);
 54    px_error_delete_string(error->detail);
 55    px_error_delete_string(error->hint);
 56    px_error_delete_string(error->position);
 57    px_error_delete_string(error->internal_position);
 58    px_error_delete_string(error->internal_query);
 59    px_error_delete_string(error->where);
 60    px_error_delete_string(error->file);
 61    px_error_delete_string(error->line);
 62    px_error_delete_string(error->routine);
 63    
 64    free(error);
 65}
 66
 67static void px_error_delete_string(char *str)
 68{
 69    if (str != NULL)
 70        free(str);
 71}
 72
 73const char *px_error_get_severity(const px_error *restrict error)
 74{
 75    return error == NULL ? "" : px_null_coalesce(error->severity);
 76}
 77
 78const char *px_error_get_sqlstate(const px_error *restrict error)
 79{
 80    return error == NULL ? "" : px_null_coalesce(error->sqlState);
 81}
 82
 83const char *px_error_get_message(const px_error *restrict error)
 84{
 85    return error == NULL ? "" : px_null_coalesce(error->message);
 86}
 87
 88const char *px_error_get_detail(const px_error *restrict error)
 89{
 90    return error == NULL ? "" : px_null_coalesce(error->detail);
 91}
 92
 93const char *px_error_get_hint(const px_error *restrict error)
 94{
 95    return error == NULL ? "" : px_null_coalesce(error->hint);
 96}
 97
 98const char *px_error_get_position(const px_error *restrict error)
 99{
100    return error == NULL ? "" : px_null_coalesce(error->position);
101}
102
103const char *px_error_get_internal_position(const px_error *restrict error)
104{
105    return error == NULL ? "" : px_null_coalesce(error->internal_position);
106}
107
108const char *px_error_get_internal_query(const px_error *restrict error)
109{
110    return error == NULL ? "" : px_null_coalesce(error->internal_query);
111}
112
113const char *px_error_get_where(const px_error *restrict error)
114{
115    return error == NULL ? "" : px_null_coalesce(error->where);
116}
117
118const char *px_error_get_file(const px_error *restrict error)
119{
120    return error == NULL ? "" : px_null_coalesce(error->file);
121}
122
123const char *px_error_get_line(const px_error *restrict error)
124{
125    return error == NULL ? "" : px_null_coalesce(error->line);
126}
127
128const char *px_error_get_routine(const px_error *restrict error)
129{
130    return error == NULL ? "" : px_null_coalesce(error->routine);
131}
132
133px_error *px_error_new_authentication_failure()
134{
135    return px_error_new_custom("28P01", "authentication failure");
136}
137
138px_error *px_error_new_io_error()
139{
140    return px_error_new_custom("58030", "io error");
141}