LCOV - code coverage report
Current view: top level - tools/monetdbe - monetdbe_mapi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 152 0.0 %
Date: 2021-09-14 22:17:06 Functions: 0 17 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "stream.h"
      11             : #include "mapi.h"
      12             : #include "monetdbe_mapi.h"
      13             : #include "mal_exception.h"
      14             : 
      15             : #define MAPIalloc(sz) malloc(sz)
      16             : #define MAPIfree(p)   free(p)
      17             : 
      18             : monetdbe_MapiMsg
      19           0 : monetdbe_mapi_error(monetdbe_Mapi mid)
      20             : {
      21           0 :         if (mid->msg)
      22           0 :                 return MERROR;
      23             :         return MOK;
      24             : }
      25             : 
      26             : monetdbe_MapiHdl
      27           0 : monetdbe_mapi_query(monetdbe_Mapi mid, const char *query)
      28             : {
      29           0 :         monetdbe_MapiHdl mh = (monetdbe_MapiHdl)MAPIalloc(sizeof(struct monetdbe_MapiStatement));
      30             : 
      31           0 :         if (!mh)
      32             :                 return NULL;
      33           0 :         mh->mid = mid;
      34           0 :         mh->query = (char*)query;
      35           0 :         mh->msg = monetdbe_query(mh->mid->mdbe, mh->query, &mh->result, &mh->affected_rows);
      36           0 :         mh->current_row = 0;
      37           0 :         mh->mapi_row = NULL;
      38           0 :         if (mh->result && mh->result->ncols) {
      39           0 :                 mh->mapi_row = (char**)MAPIalloc(sizeof(char*)*mh->result->ncols);
      40           0 :                 if (!mh->mapi_row) {
      41           0 :                         mh->msg = "malloc failure";
      42           0 :                         return mh;
      43             :                 }
      44           0 :                 memset(mh->mapi_row, 0, sizeof(char*)*mh->result->ncols);
      45             :         }
      46             :         return mh;
      47             : }
      48             : 
      49             : monetdbe_MapiMsg
      50           0 : monetdbe_mapi_close_handle(monetdbe_MapiHdl hdl)
      51             : {
      52           0 :         if (hdl) {
      53             :                 char *msg = NULL;
      54           0 :                 if (hdl->result) {
      55           0 :                         if (hdl->mapi_row) {
      56           0 :                                 for (size_t i=0; i<hdl->result->ncols; i++) {
      57           0 :                                         if (hdl->mapi_row[i])
      58           0 :                                                 MAPIfree(hdl->mapi_row[i]);
      59             :                                 }
      60           0 :                                 MAPIfree(hdl->mapi_row);
      61             :                         }
      62           0 :                         msg = monetdbe_cleanup_result(hdl->mid->mdbe, hdl->result);
      63           0 :                         if (msg)
      64           0 :                                 hdl->mid->msg = msg;
      65             :                 }
      66           0 :                 MAPIfree(hdl);
      67             :         }
      68           0 :         return MOK;
      69             : }
      70             : 
      71             : int
      72           0 : monetdbe_mapi_fetch_row(monetdbe_MapiHdl hdl)
      73             : {
      74             :         int n = 0;
      75             : 
      76           0 :         if (hdl->result && hdl->current_row < hdl->result->nrows) {
      77           0 :                 n = (int) ++hdl->current_row;
      78             :         }
      79           0 :         return n;
      80             : }
      81             : 
      82             : #define SIMPLE_TYPE_SIZE 128
      83             : 
      84             : char *
      85           0 : monetdbe_mapi_fetch_field(monetdbe_MapiHdl hdl, int fnr)
      86             : {
      87           0 :         if (fnr < (int)hdl->result->ncols && hdl->current_row > 0 && hdl->current_row <= hdl->result->nrows) {
      88           0 :                 monetdbe_column *rcol = NULL;
      89           0 :                 if (monetdbe_result_fetch(hdl->result,  &rcol, fnr) == NULL) {
      90           0 :                         size_t r = (size_t) hdl->current_row - 1;
      91           0 :                         if (rcol->type != monetdbe_str && !hdl->mapi_row[fnr]) {
      92           0 :                                 hdl->mapi_row[fnr] = MAPIalloc(SIMPLE_TYPE_SIZE);
      93           0 :                                 if (!hdl->mapi_row[fnr]) {
      94           0 :                                         hdl->msg = "malloc failure";
      95           0 :                                         return NULL;
      96             :                                 }
      97             :                         }
      98           0 :                         switch(rcol->type) {
      99           0 :                         case monetdbe_bool: {
     100             :                                 monetdbe_column_bool *icol = (monetdbe_column_bool*)rcol;
     101           0 :                                 if (icol->data[r] == icol->null_value)
     102           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     103             :                                 else
     104           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%s", icol->data[r]==1?"true":"false");
     105           0 :                                 return hdl->mapi_row[fnr];
     106             :                         }
     107           0 :                         case monetdbe_int8_t: {
     108             :                                 monetdbe_column_int8_t *icol = (monetdbe_column_int8_t*)rcol;
     109           0 :                                 if (icol->data[r] == icol->null_value)
     110           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     111             :                                 else
     112           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%d", icol->data[r]);
     113           0 :                                 return hdl->mapi_row[fnr];
     114             :                         }
     115           0 :                         case monetdbe_int16_t: {
     116             :                                 monetdbe_column_int16_t *icol = (monetdbe_column_int16_t*)rcol;
     117           0 :                                 if (icol->data[r] == icol->null_value)
     118           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     119             :                                 else
     120           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%d", icol->data[r]);
     121           0 :                                 return hdl->mapi_row[fnr];
     122             :                         }
     123           0 :                         case monetdbe_int32_t: {
     124             :                                 monetdbe_column_int32_t *icol = (monetdbe_column_int32_t*)rcol;
     125           0 :                                 if (icol->data[r] == icol->null_value)
     126           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     127             :                                 else
     128           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%d", icol->data[r]);
     129           0 :                                 return hdl->mapi_row[fnr];
     130             :                         }
     131           0 :                         case monetdbe_int64_t: {
     132             :                                 monetdbe_column_int64_t *icol = (monetdbe_column_int64_t*)rcol;
     133           0 :                                 if (icol->data[r] == icol->null_value)
     134           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     135             :                                 else
     136           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%" PRId64, icol->data[r]);
     137           0 :                                 return hdl->mapi_row[fnr];
     138             :                         }
     139             : #ifdef HAVE_HGE
     140           0 :                         case monetdbe_int128_t: {
     141             :                                 monetdbe_column_int128_t *icol = (monetdbe_column_int128_t*)rcol;
     142           0 :                                 if (icol->data[r] == icol->null_value)
     143           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     144             :                                 else
     145           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%" PRId64, (int64_t)icol->data[r]);
     146           0 :                                 return hdl->mapi_row[fnr];
     147             :                         }
     148             : #endif
     149           0 :                         case monetdbe_float: {
     150             :                                 monetdbe_column_float *icol = (monetdbe_column_float*)rcol;
     151           0 :                                 if (icol->data[r] == icol->null_value)
     152           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     153             :                                 else
     154           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%f", icol->data[r]);
     155           0 :                                 return hdl->mapi_row[fnr];
     156             :                         }
     157           0 :                         case monetdbe_double: {
     158             :                                 monetdbe_column_double *icol = (monetdbe_column_double*)rcol;
     159           0 :                                 if (icol->data[r] == icol->null_value)
     160           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "NULL");
     161             :                                 else
     162           0 :                                         snprintf(hdl->mapi_row[fnr], SIMPLE_TYPE_SIZE, "%f", icol->data[r]);
     163           0 :                                 return hdl->mapi_row[fnr];
     164             :                         }
     165           0 :                         case monetdbe_str: {
     166             :                                 monetdbe_column_str *icol = (monetdbe_column_str*)rcol;
     167           0 :                                 return icol->data[r];
     168             :                         }
     169             :                         default:
     170             :                                 return NULL;
     171             :                         }
     172             :                 }
     173             :         }
     174             :         return NULL;
     175             : }
     176             : 
     177             : char *
     178           0 : monetdbe_mapi_get_type(monetdbe_MapiHdl hdl, int fnr)
     179             : {
     180           0 :         if (fnr < (int)hdl->result->ncols) {
     181           0 :                 monetdbe_column *rcol = NULL;
     182           0 :                 if (monetdbe_result_fetch(hdl->result,  &rcol, fnr) == NULL) {
     183           0 :                         switch(rcol->type) {
     184             :                         case monetdbe_bool:
     185           0 :                                 return "boolean";
     186           0 :                         case monetdbe_int8_t:
     187           0 :                                 return "tinyint";
     188           0 :                         case monetdbe_int16_t:
     189           0 :                                 return "smallint";
     190           0 :                         case monetdbe_int32_t:
     191           0 :                                 return "integer";
     192           0 :                         case monetdbe_int64_t:
     193           0 :                                 return "bigint";
     194             : #ifdef HAVE_HGE
     195           0 :                         case monetdbe_int128_t:
     196           0 :                                 return "hugeint";
     197             : #endif
     198           0 :                         case monetdbe_float:
     199           0 :                                 return "float";
     200           0 :                         case monetdbe_double:
     201           0 :                                 return "real";
     202           0 :                         case monetdbe_str:
     203           0 :                                 return "varchar";
     204           0 :                         default:
     205           0 :                                 return "unknown";
     206             :                         }
     207             :                 }
     208             :         }
     209             :         return NULL;
     210             : }
     211             : 
     212             : monetdbe_MapiMsg
     213           0 : monetdbe_mapi_seek_row(monetdbe_MapiHdl hdl, int64_t rowne, int whence)
     214             : {
     215           0 :         if (rowne == 0 && whence == MAPI_SEEK_SET) {
     216           0 :                 hdl->current_row = 0;
     217             :         }
     218           0 :         return MOK;
     219             : }
     220             : 
     221             : int64_t
     222           0 : monetdbe_mapi_get_row_count(monetdbe_MapiHdl hdl)
     223             : {
     224           0 :         return hdl->result->nrows;
     225             : }
     226             : 
     227             : int64_t
     228           0 : monetdbe_mapi_rows_affected(monetdbe_MapiHdl hdl)
     229             : {
     230           0 :         if (hdl->result)
     231           0 :                 return hdl->result->nrows;
     232           0 :         return hdl->affected_rows;
     233             : }
     234             : 
     235             : int
     236           0 : monetdbe_mapi_get_field_count(monetdbe_MapiHdl hdl)
     237             : {
     238           0 :         return (int) hdl->result->ncols;
     239             : }
     240             : 
     241             : const char *
     242           0 : monetdbe_mapi_result_error(monetdbe_MapiHdl hdl)
     243             : {
     244           0 :         if (hdl) {
     245           0 :                 return hdl->msg;
     246             :         }
     247             :         return NULL;
     248             : }
     249             : 
     250           0 : int monetdbe_mapi_get_len(monetdbe_MapiHdl hdl, int fnr)
     251             : {
     252             :         (void)hdl;
     253             :         (void)fnr;
     254           0 :         return 0;
     255             : }
     256             : 
     257             : /* implement these to make dump.c error's more informative */
     258           0 : void monetdbe_mapi_explain(monetdbe_Mapi mid, FILE *fd)
     259             : {
     260             :         (void)mid;
     261             :         (void)fd;
     262           0 : }
     263             : 
     264           0 : void monetdbe_mapi_explain_query(monetdbe_MapiHdl hdl, FILE *fd)
     265             : {
     266             :         (void)hdl;
     267             :         (void)fd;
     268           0 : }
     269             : 
     270           0 : void monetdbe_mapi_explain_result(monetdbe_MapiHdl hdl, FILE *fd)
     271             : {
     272             :         (void)hdl;
     273             :         (void)fd;
     274           0 : }
     275             : 
     276             : #define Mapi monetdbe_Mapi
     277             : #define MapiHdl monetdbe_MapiHdl
     278             : #define MapiHdl monetdbe_MapiHdl
     279             : #define MapiMsg monetdbe_MapiMsg
     280             : 
     281             : #include "msqldump.h"
     282             : 
     283             : char*
     284           0 : monetdbe_mapi_dump_database(monetdbe_database dbhdl, const char *filename)
     285             : {
     286             :         char* msg = NULL;
     287           0 :         struct monetdbe_MapiStruct mid = { .mdbe = dbhdl };
     288             : 
     289             :         /* open file stream */
     290           0 :         stream *fd = open_wastream(filename);
     291           0 :         if (fd) {
     292           0 :                 if (dump_database(&mid, fd, 0, 0, false)) {
     293           0 :                         if (mid.msg)
     294             :                                 msg = mid.msg;
     295             :                 }
     296           0 :                 close_stream(fd);
     297             :         } else {
     298           0 :                 return createException(MAL, "embedded.monetdbe_dump_database", "Unable to open file %s: %s", filename, mnstr_peek_error(NULL));
     299             :         }
     300           0 :         return msg;
     301             : }
     302             : 
     303             : char*
     304           0 : monetdbe_mapi_dump_table(monetdbe_database dbhdl, const char *sname, const char *tname, const char *filename)
     305             : {
     306             :         char* msg = NULL;
     307           0 :         struct monetdbe_MapiStruct mid = { .mdbe = dbhdl };
     308             : 
     309             :         /* open file stream */
     310           0 :         stream *fd = open_wastream(filename);
     311           0 :         if (fd) {
     312           0 :                 if (dump_table(&mid, sname, tname, fd, 0, 0, 0, 0, false)) {
     313           0 :                         if (mid.msg)
     314             :                                 msg = mid.msg;
     315             :                 }
     316           0 :                 close_stream(fd);
     317             :         } else {
     318           0 :                 return createException(MAL, "embedded.monetdbe_dump_table", "Unable to open file %s: %s", filename, mnstr_peek_error(NULL));
     319             :         }
     320           0 :         return msg;
     321             : }

Generated by: LCOV version 1.14