Advertisement
Guest User

Untitled

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