Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #include "cassandra.h"
  7.  
  8. struct Users_ {
  9. const char* lastname;
  10. cass_int32_t age;
  11. const char* city;
  12. const char* email;
  13. const char* firstname;
  14. };
  15.  
  16. typedef struct Users_ Users;
  17.  
  18. void print_error(CassFuture* future) {
  19. const char* message;
  20. size_t message_length;
  21. cass_future_error_message(future, &message, &message_length);
  22. fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
  23. }
  24.  
  25. CassCluster* create_cluster(const char* hosts) {
  26. CassCluster* cluster = cass_cluster_new();
  27. cass_cluster_set_contact_points(cluster, hosts);
  28. return cluster;
  29. }
  30.  
  31. CassError connect_session(CassSession* session, const CassCluster* cluster) {
  32. CassError rc = CASS_OK;
  33. CassFuture* future = cass_session_connect(session, cluster);
  34.  
  35. cass_future_wait(future);
  36. rc = cass_future_error_code(future);
  37. if (rc != CASS_OK) {
  38. print_error(future);
  39. }
  40. cass_future_free(future);
  41.  
  42. return rc;
  43. }
  44.  
  45. CassError insert_user(CassSession* session, const Users* users) {
  46. CassError rc = CASS_OK;
  47. CassStatement* statement = NULL;
  48. CassFuture* future = NULL;
  49.  
  50. const char* query = "INSERT INTO demo.users (lastname, age, city, email, firstname) VALUES (?, ?, ?, ?, ?)";
  51.  
  52. statement = cass_statement_new(query, 5);
  53.  
  54. cass_statement_bind_string(statement, 0, users->lastname);
  55. cass_statement_bind_int32(statement, 1, users->age);
  56. cass_statement_bind_string(statement, 2, users->city);
  57. cass_statement_bind_string(statement, 3, users->email);
  58. cass_statement_bind_string(statement, 4, users->firstname);
  59.  
  60. future = cass_session_execute(session, statement);
  61.  
  62. cass_future_wait(future);
  63.  
  64. rc = cass_future_error_code(future);
  65. if (rc != CASS_OK) {
  66. print_error(future);
  67. }
  68.  
  69. cass_future_free(future);
  70. cass_statement_free(statement);
  71.  
  72. return rc;
  73. }
  74.  
  75.  
  76. CassError select_user(CassSession* session, const Users* users) {
  77. CassError rc = CASS_OK;
  78. CassStatement* statement = NULL;
  79. CassFuture* future = NULL;
  80.  
  81. const char* query = "SELECT * FROM demo.users WHERE lastname=?";
  82.  
  83. statement = cass_statement_new(query, 1);
  84.  
  85. cass_statement_bind_string(statement, 0, users->lastname);
  86.  
  87. future = cass_session_execute(session, statement);
  88. cass_future_wait(future);
  89.  
  90. rc = cass_future_error_code(future);
  91. if (rc != CASS_OK) {
  92. print_error(future);
  93.  
  94. } else {
  95. const CassResult* result = cass_future_get_result(future);
  96. const CassRow* row = cass_result_first_row(result);
  97.  
  98. if (row) {
  99. const CassValue* firstvalue = cass_row_get_column_by_name(row, "firstname");
  100. const char* firstname;
  101. size_t firstname_length;
  102. cass_value_get_string(firstvalue, &firstname, &firstname_length);
  103.  
  104. const CassValue* secondvalue = cass_row_get_column_by_name(row, "age");
  105. cass_int32_t age;
  106. cass_value_get_int32(secondvalue, &age);
  107.  
  108. printf("firstname: '%.*s' age: %d\n", (int)firstname_length, firstname, age);
  109. }
  110.  
  111. cass_statement_free(statement);
  112. cass_future_free(future);
  113.  
  114. }
  115.  
  116. return rc;
  117. }
  118.  
  119.  
  120. CassError update_user(CassSession* session, const Users* users) {
  121. CassError rc = CASS_OK;
  122. CassStatement* statement = NULL;
  123. CassFuture* future = NULL;
  124.  
  125. const char* query = "UPDATE demo.users SET age =? WHERE lastname =? ";
  126.  
  127. statement = cass_statement_new(query, 2);
  128.  
  129. cass_statement_bind_int32(statement, 0, users->age);
  130. cass_statement_bind_string(statement, 1, users->lastname);
  131.  
  132. future = cass_session_execute(session, statement);
  133.  
  134. cass_future_wait(future);
  135.  
  136. rc = cass_future_error_code(future);
  137. if (rc != CASS_OK) {
  138. print_error(future);
  139. }
  140.  
  141. cass_future_free(future);
  142. cass_statement_free(statement);
  143.  
  144. return rc;
  145. }
  146.  
  147. CassError delete_user(CassSession* session, const Users* users) {
  148. CassError rc = CASS_OK;
  149. CassStatement* statement = NULL;
  150. CassFuture* future = NULL;
  151.  
  152. const char* query = "DELETE FROM demo.users WHERE lastname=?";
  153.  
  154. statement = cass_statement_new(query, 1);
  155.  
  156. cass_statement_bind_string(statement, 0, users->lastname);
  157.  
  158. future = cass_session_execute(session, statement);
  159.  
  160. cass_future_wait(future);
  161.  
  162. rc = cass_future_error_code(future);
  163. if (rc != CASS_OK) {
  164. print_error(future);
  165. }
  166.  
  167. cass_future_free(future);
  168. cass_statement_free(statement);
  169.  
  170. return rc;
  171. }
  172.  
  173. int main(int argc, char* argv[]) {
  174. CassCluster* cluster = NULL;
  175. CassSession* session = cass_session_new();
  176. Users input = { "Jones", 39, "Austin", "bob@example.com", "Bob" };
  177. Users select = { "Jones", NULL, NULL, NULL, NULL};
  178. Users update = { "Jones", 40, NULL, NULL, NULL};
  179. Users deletion = { "Jones", NULL, NULL, NULL, NULL};
  180.  
  181. char* hosts = "127.0.0.1";
  182. if (argc > 1) {
  183. hosts = argv[1];
  184. }
  185. cluster = create_cluster(hosts);
  186.  
  187. if (connect_session(session, cluster) != CASS_OK) {
  188. cass_cluster_free(cluster);
  189. cass_session_free(session);
  190. return -1;
  191. }
  192.  
  193. insert_user(session, &input);
  194. select_user(session, &select);
  195. update_user(session, &update);
  196. delete_user(session, &deletion);
  197.  
  198.  
  199. cass_cluster_free(cluster);
  200. cass_session_free(session);
  201.  
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement