/RIOTDB/src/clib/handle_metadata.c

https://github.com/GunioRobot/riotdb · C · 300 lines · 209 code · 55 blank · 36 comment · 21 complexity · 874e8846f81b7154f37355b8ddc49ce7 MD5 · raw file

  1. /*****************************************************************************
  2. * Contains functions for managing the matadata table.
  3. *
  4. * Author: Herodotos Herodotou
  5. * Date: Sep 17, 2008
  6. ****************************************************************************/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <mysql.h>
  11. #include "basics.h"
  12. #include "handle_metadata.h"
  13. /* ----- Functions to keep track of table naming scheme ----- */
  14. int getNextTableID(MYSQL * sqlConn, int * next)
  15. {
  16. /* Execute the query */
  17. int success = mysql_query(sqlConn, sqlGetNextVectorID);
  18. if( success != 0 )
  19. {
  20. return 0;
  21. }
  22. /* Get the actual number */
  23. MYSQL_RES *sqlRes;
  24. MYSQL_ROW sqlRow;
  25. sqlRes = mysql_use_result(sqlConn);
  26. sqlRow = mysql_fetch_row(sqlRes);
  27. if( sqlRow != NULL )
  28. {
  29. *next = atoi(sqlRow[0]);
  30. success = 1;
  31. }
  32. /* C API requires to clean the result set */
  33. while( (sqlRow = mysql_fetch_row(sqlRes)) != NULL );
  34. mysql_free_result(sqlRes);
  35. return success;
  36. }
  37. int renameTable(MYSQL * sqlConn, char* oldName, char* newName)
  38. {
  39. /* Create the sql string */
  40. int length = strlen(sqlTemplateRenameTable) + strlen(oldName) +
  41. strlen(newName) + 1;
  42. char strRenameTableSQL[length];
  43. sprintf( strRenameTableSQL, sqlTemplateRenameTable, oldName, newName);
  44. int success = mysql_query(sqlConn, strRenameTableSQL);
  45. return ( success != 0 )? 0 : 1;
  46. }
  47. /* ------ Functions to handle the reference counter for Vectors -------- */
  48. int decrementRefCounter(MYSQL* sqlConn, int* newRefCounter, long int metadataID)
  49. {
  50. /* Get the counter */
  51. int refCounter = 0;
  52. if( !getRefCounter(sqlConn, metadataID, &refCounter) )
  53. return 0;
  54. /* Decrement the counter */
  55. refCounter = (refCounter <= 0)? 0 : (refCounter - 1);
  56. /* Set the new counter */
  57. *newRefCounter = refCounter;
  58. return setRefCounter(sqlConn, metadataID, refCounter);
  59. }
  60. int incrementRefCounter(MYSQL * sqlConn, int* newRefCounter, long int metadataID)
  61. {
  62. /* Get the counter */
  63. int refCounter = 0;
  64. if( !getRefCounter(sqlConn, metadataID, &refCounter) )
  65. return 0;
  66. /* Increment the counter */
  67. refCounter = (refCounter <= 0)? 1 : (refCounter + 1);
  68. /* Set the new counter */
  69. *newRefCounter = refCounter;
  70. return setRefCounter(sqlConn, metadataID, refCounter);
  71. }
  72. int getRefCounter(MYSQL * sqlConn, long int metadataID, int * refCounter)
  73. {
  74. /* Build the sql string */
  75. int length = strlen(sqlTemplateGetRefCounter) + MAX_INT_LENGTH + 1;
  76. char strGetRecCounterSQL[length];
  77. sprintf( strGetRecCounterSQL, sqlTemplateGetRefCounter, metadataID );
  78. /* Execute the query */
  79. int success = mysql_query(sqlConn, strGetRecCounterSQL);
  80. if( success != 0 )
  81. return 0;
  82. /* Get the reference counter */
  83. MYSQL_RES *sqlRes;
  84. MYSQL_ROW sqlRow;
  85. sqlRes = mysql_use_result(sqlConn);
  86. sqlRow = mysql_fetch_row(sqlRes);
  87. if( sqlRow != NULL )
  88. {
  89. *refCounter = atoi(sqlRow[0]);
  90. success = 1;
  91. }
  92. /* C API requires to clean the result set */
  93. while( (sqlRow = mysql_fetch_row(sqlRes)) != NULL );
  94. mysql_free_result(sqlRes);
  95. return success;
  96. }
  97. int setRefCounter(MYSQL * sqlConn, long int metadataID, int refCounter)
  98. {
  99. /* Store the new counter back into the metadata table */
  100. int length = strlen(sqlTemplateSetRefCounter) +2 * MAX_INT_LENGTH + 1;
  101. char strSetRecCounterSQL[length];
  102. sprintf( strSetRecCounterSQL, sqlTemplateSetRefCounter,
  103. refCounter, metadataID );
  104. int success = mysql_query(sqlConn, strSetRecCounterSQL);
  105. return ( success != 0 )? 0 : 1;
  106. }
  107. /* ----------------- Functions to handle Metadata ------------------ */
  108. int insertVectorMetadataInfo(MYSQL * sqlConn, rdbVector * vectorInfo)
  109. {
  110. /* Build the sql string */
  111. int length = strlen(sqlTemplateInsertMetadata) +
  112. strlen(vectorInfo->tableName) + 14*MAX_INT_LENGTH + 1;
  113. char strInsertMetadataInfo[length];
  114. char strSize[MAX_INT_LENGTH+1];
  115. sprintf( strSize, "%d", vectorInfo->size );
  116. sprintf( strInsertMetadataInfo, sqlTemplateInsertMetadata,
  117. vectorInfo->tableName,
  118. vectorInfo->dbSourceID,
  119. strSize,
  120. vectorInfo->isView,
  121. vectorInfo->refCounter,
  122. vectorInfo->sxp_type,
  123. vectorInfo->sxp_obj,
  124. vectorInfo->sxp_named,
  125. vectorInfo->sxp_gp,
  126. vectorInfo->sxp_mark,
  127. vectorInfo->sxp_debug,
  128. vectorInfo->sxp_trace,
  129. vectorInfo->sxp_spare,
  130. vectorInfo->sxp_gcgen,
  131. vectorInfo->sxp_gccls );
  132. /* Execute the query */
  133. int success = mysql_query(sqlConn, strInsertMetadataInfo);
  134. if( success) {
  135. fprintf(stderr, "insert metadata failed: %u (%s)\n",
  136. mysql_errno(sqlConn), mysql_error(sqlConn));
  137. return 0;
  138. }
  139. /* Get the metadata info index */
  140. vectorInfo->metadataID = (unsigned int)mysql_insert_id(sqlConn);
  141. return 1;
  142. }
  143. int updateVectorMetadataInfo(MYSQL * sqlConn, rdbVector * vectorInfo)
  144. {
  145. /* Build the sql string */
  146. int length = strlen(sqlTemplateUpdateMetadata) +
  147. strlen(vectorInfo->tableName) + 14*MAX_INT_LENGTH + 1;
  148. char strUpdateMetadataInfo[length];
  149. char strSize[MAX_INT_LENGTH+1];
  150. sprintf( strSize, "%d", vectorInfo->size );
  151. sprintf( strUpdateMetadataInfo, sqlTemplateUpdateMetadata,
  152. vectorInfo->tableName,
  153. vectorInfo->dbSourceID,
  154. strSize,
  155. vectorInfo->isView,
  156. vectorInfo->refCounter,
  157. vectorInfo->sxp_type,
  158. vectorInfo->sxp_obj,
  159. vectorInfo->sxp_named,
  160. vectorInfo->sxp_gp,
  161. vectorInfo->sxp_mark,
  162. vectorInfo->sxp_debug,
  163. vectorInfo->sxp_trace,
  164. vectorInfo->sxp_spare,
  165. vectorInfo->sxp_gcgen,
  166. vectorInfo->sxp_gccls,
  167. vectorInfo->metadataID );
  168. /* Execute the query */
  169. int success = mysql_query(sqlConn, strUpdateMetadataInfo);
  170. return ( success != 0 )? 0 : 1;
  171. }
  172. int insertMatrixMetadataInfo(MYSQL * sqlConn, rdbMatrix * matrixInfo)
  173. {
  174. /* Build the sql string */
  175. int length = strlen(sqlTemplateInsertMetadata) +
  176. strlen(matrixInfo->tableName) + 14*MAX_INT_LENGTH + 1;
  177. char strInsertMetadataInfo[length];
  178. char strSize[2*MAX_INT_LENGTH+2];
  179. sprintf( strSize, "%d,%d", matrixInfo->numRows, matrixInfo->numCols );
  180. sprintf( strInsertMetadataInfo, sqlTemplateInsertMetadata,
  181. matrixInfo->tableName,
  182. matrixInfo->dbSourceID,
  183. strSize,
  184. matrixInfo->isView,
  185. matrixInfo->refCounter,
  186. matrixInfo->sxp_type,
  187. matrixInfo->sxp_obj,
  188. matrixInfo->sxp_named,
  189. matrixInfo->sxp_gp,
  190. matrixInfo->sxp_mark,
  191. matrixInfo->sxp_debug,
  192. matrixInfo->sxp_trace,
  193. matrixInfo->sxp_spare,
  194. matrixInfo->sxp_gcgen,
  195. matrixInfo->sxp_gccls );
  196. /* Execute the query */
  197. int success = mysql_query(sqlConn, strInsertMetadataInfo);
  198. if( success) {
  199. fprintf(stderr, "insert metadata failed: %u (%s)\n",
  200. mysql_errno(sqlConn), mysql_error(sqlConn));
  201. return 0;
  202. }
  203. /* Get the metadata info index */
  204. matrixInfo->metadataID = (unsigned int)mysql_insert_id(sqlConn);
  205. return 1;
  206. }
  207. int updateMatrixMetadataInfo(MYSQL * sqlConn, rdbMatrix * matrixInfo)
  208. {
  209. /* Build the sql string */
  210. int length = strlen(sqlTemplateUpdateMetadata) +
  211. strlen(matrixInfo->tableName) + 14*MAX_INT_LENGTH + 1;
  212. char strUpdateMetadataInfo[length];
  213. char strSize[2*MAX_INT_LENGTH+2];
  214. sprintf( strSize, "%d,%d", matrixInfo->numRows, matrixInfo->numCols );
  215. sprintf( strUpdateMetadataInfo, sqlTemplateUpdateMetadata,
  216. matrixInfo->tableName,
  217. matrixInfo->dbSourceID,
  218. strSize,
  219. matrixInfo->isView,
  220. matrixInfo->refCounter,
  221. matrixInfo->sxp_type,
  222. matrixInfo->sxp_obj,
  223. matrixInfo->sxp_named,
  224. matrixInfo->sxp_gp,
  225. matrixInfo->sxp_mark,
  226. matrixInfo->sxp_debug,
  227. matrixInfo->sxp_trace,
  228. matrixInfo->sxp_spare,
  229. matrixInfo->sxp_gcgen,
  230. matrixInfo->sxp_gccls,
  231. matrixInfo->metadataID );
  232. /* Execute the query */
  233. int success = mysql_query(sqlConn, strUpdateMetadataInfo);
  234. return ( success != 0 )? 0 : 1;
  235. }
  236. int deleteMetadataInfo(MYSQL * sqlConn, long int metadataID)
  237. {
  238. /* Build the sql string */
  239. int length = strlen(sqlTemplateDeleteMetadata) + MAX_INT_LENGTH;
  240. char strDeleteMetadataInfo[length];
  241. sprintf( strDeleteMetadataInfo, sqlTemplateDeleteMetadata, metadataID);
  242. /* Execute the query */
  243. int success = mysql_query(sqlConn, strDeleteMetadataInfo);
  244. return (success != 0) ? 0 : 1;
  245. }