uint process_event(DeviceInfo info, MYSQL *mysql, int trigger_type, int last_status, int situation, long int last_triggered, string name) { MYSQL_RES *mysql_res; MYSQL_ROW mysql_row; uint status = 0; string query = "SELECT `value`, `value_type`, `condition`, `logic_condition` FROM `conditions` WHERE `event_id` = '" + inttostr(info.event_id) + "' ORDER BY `id`"; mysql_res = db_query(mysql, &info, query); for (uint i = 0; i < mysql_num_rows(mysql_res); i++) { mysql_row = mysql_fetch_row(mysql_res); if (i == 0) { status = process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2])); } else { switch (strtoint(mysql_row[3])) { case 0: status = status && process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2])); break; case 1: status = status || process_condition(info, strtoint(mysql_row[0]), strtoint(mysql_row[1]), strtoint(mysql_row[2])); break; } } } mysql_free_result(mysql_res); if (status == 0) { debuglogger(DEBUG_EVENT, LEVEL_INFO, &info, "Not Triggered."); db_update(mysql, &info, "UPDATE events SET last_status=0 WHERE id=" + inttostr(info.event_id)); return 0; } else { debuglogger(DEBUG_EVENT, LEVEL_INFO, &info, "Triggered."); // setup parameters for the response to use. info.parameters.push_front(ValuePair("event_name", name)); info.parameters.push_front(ValuePair("situation", situations[situation])); if ((uint) last_status != status) { db_update(mysql, &info, "UPDATE events SET last_triggered=UNIX_TIMESTAMP(NOW()), last_status=1 WHERE id=" + inttostr(info.event_id)); db_update(mysql, &info, "INSERT INTO event_log SET date=UNIX_TIMESTAMP(NOW()), time_since_last_change=UNIX_TIMESTAMP(NOW())-" + inttostr(last_triggered) + ", event_id=" + inttostr(info.event_id)); process_responses(info, mysql); } return 1; } } // end process_event()
int updateStatus (int port, bool portStatus){ //6 = HUMO - 21/22 //5 = PRTA - 23 - NC - LOW //2 = TALT - 24 // DETECTA VALOR -10 => PUERTA ABIERTA 0 // DETECTA VALOR -20 => PUERTA CERRADA 1 if (!mysql_connect ()){ if (portStatus){ db_insert (5,0,puerta_abierta); printf ("PA\n"); } else { db_insert (5,0,puerta_cerrada); printf ("PC\n"); } db_update (5,0,0); mysql_disconnect (); } return 0; }
/* inserts into database a cpl script in XML format(xml) along with its binary * format (bin) * Returns: 1 - success * -1 - error */ int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin) { db_key_t keys[] = {"user","cpl_xml","cpl_bin"}; db_val_t vals[3]; db_res_t *res; /* lets see if the user is already in database */ vals[0].type = DB_STRING; vals[0].nul = 0; vals[0].val.string_val = usr; if (db_query(db_con, keys, 0, vals, keys, 1, 1, NULL, &res) < 0) { LOG(L_ERR,"ERROR:cpl:write_to_db: db_query failed\n"); goto error; } if (res->n>1) { LOG(L_ERR,"ERROR:cpl:write_to_db: Incosistent CPL database:" " %d records for user %s\n",res->n,usr); goto error; } /* username */ vals[0].type = DB_STRING; vals[0].nul = 0; vals[0].val.string_val = usr; /* cpl text */ vals[1].type = DB_BLOB; vals[1].nul = 0; vals[1].val.blob_val.s = xml->s; vals[1].val.blob_val.len = xml->len; /* cpl bin */ vals[2].type = DB_BLOB; vals[2].nul = 0; vals[2].val.blob_val.s = bin->s; vals[2].val.blob_val.len = bin->len; /* insert or update ? */ if (res->n==0) { DBG("DEBUG:cpl:write_to_db:No user %s in CPL databse->insert\n",usr); if (db_insert(db_con, keys, vals, 3) < 0) { LOG(L_ERR,"ERROR:cpl:write_to_db: insert failed !\n"); goto error; } } else { DBG("DEBUG:cpl:write_to_db:User %s already in CPL database ->" " update\n",usr); if (db_update(db_con, keys, 0, vals, keys+1, vals+1, 1, 2) < 0) { LOG(L_ERR,"ERROR:cpl:write_to_db: updare failed !\n"); goto error; } } return 1; error: return -1; }
int ul_db_update(str * table, str * first, str * second, db_key_t* _k, db_op_t * _op, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { ul_db_handle_t * handle; if(!db_write){ LM_ERR("not allowed in read only mode, abort.\n"); return -1; } if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_update(handle, table, _k, _op, _v, _uk, _uv, _n, _un); }
int do_migrate(int migrate_limit) { C c; R r; int id = 0; int count = 0; DbmailMessage *m; qprintf ("Mirgrate legacy 2.2.x messageblks to mimeparts...\n"); if (!yes_to_all) { qprintf ("\tmigration skipped. Use -y option to perform mirgration.\n"); return 0; } qprintf ("Preparing to migrate %d physmessages.\n", migrate_limit); c = db_con_get(); TRY r = db_query(c, "SELECT DISTINCT(physmessage_id) FROM %smessageblks LIMIT %d", DBPFX, migrate_limit); qprintf ("Migrating %d physmessages...\n", migrate_limit); while (db_result_next(r)) { count++; id = db_result_get_u64(r,0); m = dbmail_message_new(); m = dbmail_message_retrieve(m, id, DBMAIL_MESSAGE_FILTER_FULL); if(!dm_message_store(m)) { if(verbose) qprintf ("%d ",id); db_update("DELETE FROM %smessageblks WHERE physmessage_id = %d", DBPFX, id); } else { if(!verbose) qprintf ("migrating physmessage_id: %d\n",id); qprintf ("failed\n"); return -1; } dbmail_message_free(m); } CATCH(SQLException) LOG_SQLERROR; return -1; FINALLY db_con_close(c); END_TRY; qprintf ("Migration complete. Migrated %d physmessages.\n", count); return 0; }
void update_monitor_db(DeviceInfo info, MYSQL *mysql, RRDInfo rrd) { if (info.curr_val == "U") { info.curr_val = "NULL"; } if (info.delta_val == "U") { info.delta_val = "NULL"; } db_update(mysql, &info, "UPDATE monitors SET tuned=1, last_val=" + info.curr_val + ", delta_val=" + info.delta_val + ", delta_time=UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_time), "+ "last_time=NOW(), status=" + inttostr(info.status) + " WHERE id=" + inttostr(info.monitor_id)); }
static void destroy(void) { LM_DBG("destroying module ...\n"); if (puacb_list) destroy_puacb_list(); if(pua_db && HashT) db_update(0,0); if(HashT) destroy_htable(); if(pua_db) pua_dbf.close(pua_db); if(pua_evlist) destroy_pua_evlist(); return ; }
int do_migrate(int migrate_limit) { Connection_T c; ResultSet_T r; int id = 0; volatile int count = 0; DbmailMessage *m; qprintf ("Migrate legacy 2.2.x messageblks to mimeparts...\n"); if (!yes_to_all) { qprintf ("\tmigration skipped. Use -y option to perform migration.\n"); return 0; } qprintf ("Preparing to migrate up to %d physmessages.\n", migrate_limit); c = db_con_get(); TRY db_begin_transaction(c); r = db_query(c, "SELECT DISTINCT(physmessage_id) FROM %smessageblks LIMIT %d", DBPFX, migrate_limit); while (db_result_next(r)) { count++; id = db_result_get_u64(r,0); m = dbmail_message_new(NULL); m = dbmail_message_retrieve(m, id); if(! dm_message_store(m)) { if(verbose) qprintf ("%d ",id); db_update("DELETE FROM %smessageblks WHERE physmessage_id = %d", DBPFX, id); } dbmail_message_free(m); } db_commit_transaction(c); CATCH(SQLException) LOG_SQLERROR; db_rollback_transaction(c); return -1; FINALLY db_con_close(c); END_TRY; qprintf ("Migration complete. Migrated %d physmessages.\n", count); return 0; }
/* encrypt an existing note, or create a new encrypted note */ int db_encrypt(char* search) { /* search by name */ sql_result *result; char* crypt; int r; result = db_get("SELECT * FROM nodau WHERE name = '%s'",search); /* there's already a note with that name */ if (result->num_rows) { char* name; char* text; /* get the data */ name = result->data[COLUMN(0,COL_NAME)]; text = result->data[COLUMN(0,COL_TEXT)]; crypt = result->data[COLUMN(0,COL_CRYPT)]; /* encrypt it if it's not already */ if (!strcmp(crypt,"false")) { crypt = crypt_get_key(); r = db_update(name,text); } else { printf("Note '%s' is already encrypted\n",search); } db_result_free(result); return r; } /* free the search result */ db_result_free(result); /* create the new entry */ db_insert(search,"new entry"); if (error_msg) fprintf(stderr,"%s\n",error_msg); crypt = crypt_get_key(); /* open for editing */ return db_edit(search); }
static void destroy(void) { if (puacb_list) destroy_puacb_list(); /* if dbmode is PUA_DB_ONLY, then HashT will be NULL so db_update and destroy_htable won't get called */ if(pua_db && HashT) db_update(0,0); if(HashT) destroy_htable(); if(pua_db) pua_dbf.close(pua_db); if(pua_evlist) destroy_pua_evlist(); return ; }
/* decrypt an existing note or create a new encrypted note */ int db_decrypt(char* search) { /* search by name */ sql_result *result; int r; result = db_get("SELECT * FROM nodau WHERE name = '%s'",search); /* found the note */ if (result->num_rows) { char* text; char* crypt; /* get the data */ text = result->data[COLUMN(0,COL_TEXT)]; crypt = result->data[COLUMN(0,COL_CRYPT)]; /* decrypt it if it is encrypted */ if (!strcmp(crypt,"true")) { char* t; crypt = crypt_get_key(); t = note_decrypt(text,crypt); if (!t) return 1; free(crypt_key); crypt_key = NULL; r = db_update(search,t); db_result_free(result); return r; } else { printf("Note '%s' is not encrypted\n",search); db_result_free(result); } return 0; } printf("No notes matches '%s'\n",search); db_result_free(result); return 0; }
static int db_set_deleted(void) { return db_update("UPDATE %smessages SET status = %d WHERE status = %d", DBPFX, MESSAGE_STATUS_PURGE, MESSAGE_STATUS_DELETE); }
static int db_cleanup_replycache(timestring_t lasttokeep) { field_t to_date_str; char2date_str(lasttokeep, &to_date_str); return db_update("DELETE FROM %sreplycache WHERE lastseen < %s", DBPFX, to_date_str); }
static int db_cleanup_iplog(timestring_t lasttokeep) { field_t to_date_str; char2date_str(lasttokeep, &to_date_str); return db_update("DELETE FROM %spbsp WHERE since < %s", DBPFX, to_date_str); }
int main(int argc, char* argv[]) { sqlite3 * db; int i; COLUMN column[10]; pTable_name = table_name; strcpy(pTable_name, "test_table"); ret = sqlite3_open("student.db", &db); if(ret != SQLITE_OK) { perror("slqite3_open"); exit(1); } while(choice != 0) { printf("please input your choise:\n"); printf("-------------------------------------\n"); printf("|0.exit|1.create|2.show|3.insert|4.update|5.delete|6.empty|7.drop|\n"); printf("-------------------------------------\n"); scanf("%d", &choice); switch(choice) { case 0: printf("you choise leave, y or n?\n"); setbuf(stdin, NULL); scanf("%c", &c); setbuf(stdin, NULL); if(c == 'y') { if(test_flag == 0) { db_drop(db, "test_table", &test_flag, &create_flag); } printf("goodbye!\n"); sqlite3_close(db); return 0; } else { choice = -1; } break; case 1: printf("we will create a table for you, please input the name of your table:\n"); scanf("%s",pTable_name); printf("please input the number of column:\n"); scanf("%d", &column_num); printf("please input column_name column_type:\n"); for(i = 0; i < column_num; i++) scanf("%s %s", column[i].column_name, column[i].column_type); db_create(db, table_name, column_num, column, &create_flag); break; case 2: db_flag(db, &create_flag, &test_flag, table_name); db_show(db, table_name); break; case 3: db_flag(db, &create_flag, &test_flag, table_name); db_insert(db, table_name, column_num, column, &test_flag, &create_flag); break; case 4: db_flag(db, &create_flag, &test_flag, table_name); db_update(db, table_name, column_num, column, &test_flag, &create_flag); break; case 5: db_flag(db, &create_flag, &test_flag, table_name); db_delete(db, table_name); break; case 6: db_flag(db, &create_flag, &test_flag, table_name); db_empty(db, table_name); break; case 7: db_flag(db, &create_flag, &test_flag, table_name); db_drop(db, table_name, &test_flag, &create_flag); break; default: printf("your choice is not exist!\n"); break; } } sqlite3_close(db); return 0; }
void data_update_test(MEM_POOL* mem_pool) { system("rm -rf /tmp/binlog"); system("rm -rf /tmp/log"); system("rm -rf /tmp/storage"); db_initialize(mem_pool); uint16 field_count = 3; enum field_types types[5] = {HI_TYPE_STRING,HI_TYPE_LONG,HI_TYPE_TINY,HI_TYPE_LONGLONG,HI_TYPE_DOUBLE}; //构造数据 uint16 sid = 0; uint32 docid = 0; int32 ret; ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); struct low_data_struct* new_data = (struct low_data_struct*)mem_pool_malloc(mem_pool,sizeof(struct low_data_struct)); get_low_data(new_data,HI_TYPE_STRING,mem_pool); struct low_data_struct* old_data = NULL; memset(new_data->field_name,0,20); strcpy(new_data->field_name,"HI_TYPE_LONG"); //更新已有的列 ret = db_update("hello",sid,docid,new_data,&old_data,mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); ASSERT_EQ(1,verify_low_data(old_data,HI_TYPE_LONG)); //查询一列 struct low_data_struct* query_col_ret = NULL; query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONG",docid,mem_pool); ASSERT_STREQ(query_col_ret->field_name,"HI_TYPE_LONG"); query_col_ret->field_name = (char*)mem_pool_malloc(mem_pool,20); strcpy(query_col_ret->field_name,"HI_TYPE_STRING"); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_STRING)); //更新不存在的列 query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(0,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); get_low_data(new_data,HI_TYPE_LONGLONG,mem_pool); ret = db_update("hello",sid,docid,new_data,&old_data,mem_pool); query_col_ret = db_data_query_col("hello",sid,"HI_TYPE_LONGLONG",docid,mem_pool); ASSERT_EQ(1,verify_low_data(query_col_ret,HI_TYPE_LONGLONG)); //更新不存在的段 ret = db_update("hello",sid+1,docid,new_data,&old_data,mem_pool); ASSERT_EQ(ERROR_SEGMENT_NOT_INIT,ret); //更新不存在的docid ret = db_update("hello",sid,docid+1,new_data,&old_data,mem_pool); ASSERT_EQ(ERROR_EXCEED_CURRENT,ret); ASSERT_EQ(0,verify_low_data(old_data,HI_TYPE_STRING)); //更新不存在的表名 ret = db_update("world",sid,docid,new_data,&old_data,mem_pool); ASSERT_EQ(ret,ERROR_TABLE_NOT_EXIT); db_release(); }
static int do_export(char *user, char *base_mailbox, char *basedir, char *outfile, char *search, int delete_after_dump, int recursive) { u64_t user_idnr = 0, owner_idnr = 0, mailbox_idnr = 0; char *dumpfile = NULL, *mailbox = NULL, *search_mailbox = NULL, *dir = NULL; GList *children = NULL; int result = 0; /* Verify the existence of this user */ if (! auth_user_exists(user, &user_idnr)) { qerrorf("Error: user [%s] does not exist.\n", user); result = -1; goto cleanup; } mailbox = g_new0(char, IMAP_MAX_MAILBOX_NAMELEN); if (!base_mailbox) { /* Always recursive without a mailbox base */ search_mailbox = g_strdup("*"); } else if (recursive) { /* Base and everything below */ search_mailbox = g_strdup_printf("%s*", base_mailbox); } else if (!recursive) { /* Should yield same results as plain db_findmailbox */ search_mailbox = g_strdup_printf("%s", base_mailbox); } /* FIXME: What are the possible error conditions here? */ db_findmailbox_by_regex(user_idnr, search_mailbox, &children, 0); /* Decision process for basedir vs. outfile: * If we're dumping one mailbox for one user, it goes to * stdout. If we've been given -o -, dump everything to * stdout (e.g., one giant mbox). If we've been given foo */ if (!outfile && !basedir) { /* Default is to use basedir of . */ basedir = "."; } else if (outfile) { /* Everything goes into this one file */ dumpfile = outfile; } children = g_list_first(children); qerrorf("Exporting [%u] mailboxes for [%s]\n", g_list_length(children), user); while (children) { mailbox_idnr = *(u64_t *)children->data; db_getmailboxname(mailbox_idnr, user_idnr, mailbox); if (! db_get_mailbox_owner(mailbox_idnr, &owner_idnr)) { qerrorf("Error checking mailbox ownership"); goto cleanup; } if (owner_idnr == user_idnr) { if (basedir) { /* Prepare the directory */ dumpfile = g_strdup_printf("%s/%s/%s.mbox", basedir, user, mailbox); dir = g_path_get_dirname(dumpfile); if (g_mkdir_with_parents(dir, 0700)) { qerrorf("can't create directory [%s]\n", dir); result = -1; goto cleanup; } } qerrorf(" export mailbox %s -> %s\n", mailbox, dumpfile); if ((result = mailbox_dump(mailbox_idnr, dumpfile, search, delete_after_dump)) != 0) { qerrorf("error exporting mailbox %s -> %s\n", mailbox, dumpfile); goto cleanup; } if (delete_after_dump) db_update("UPDATE %smailboxes SET seq=seq+1 WHERE mailbox_idnr=%d",DBPFX,mailbox_idnr); if (basedir) { g_free(dir); g_free(dumpfile); } } if (! g_list_next(children)) break; children = g_list_next(children); } cleanup: g_list_destroy(children); g_free(search_mailbox); g_free(mailbox); return result; }
///////////////////////////////////////////////////////////////////////////////// //++++++++++++++++++++++ START READ PORTS FUNCTION ++++++++++++++++++++++++++++++ ///////////////////////////////////////////////////////////////////////////////// int readActivePorts (void){ int size = sizeof (activePorts)/ sizeof (int); int result; int lcontador = 0; if (!mysql_connect ()){ if (!digitalRead (24)) volts = 0; db_insert (2,0,volts); db_update (2,0,0); if (!digitalRead (21)) humo = 70; db_insert (6,0,humo); db_update (6,0,0); humo = 20; if (!digitalRead (22)) humo = 70; db_insert (6,1,humo); db_update (6,1,0); fileRead (); for (int i=0 ; ((i < MAXPORTS) && (i < size)) ; i++){ result = displayDHTData (activePorts[i]); if(!result){ db_insert (0,i,temperature); db_update (0,i,0); fprintf(fr, "%2.1f\t", temperature); db_insert (1,i,humidity); db_update (1,i,0); fprintf(fr, "%2.1f\t", humidity); db_insert (4,i,dewPoint); db_update (4,i,0); fprintf(fr, "%2.1f\n", dewPoint); //lcontador = 0; } //else { //if ((result == 20) && (lcontador < MAX_ERRORS)){ // ++lcontador; // --i; //} else if (result == 20){ for (int j = 1; j < i; j++){ fscanf(fr, "%*f %*f %*f"); //printf ("J:%d\n",j); //printf ("I:%d\n",i); } fscanf(fr, "%f %f %f", &temperature, &humidity, &dewPoint); fprintf(stderr, "New value T %2.1f\n", temperature); fprintf(stderr, "New value H %2.1f\n", humidity); fprintf(stderr, "New value D %2.1f\n", dewPoint); db_insert (0,i,temperature); db_update (0,i,0); db_insert (1,i,humidity); db_update (1,i,0); db_insert (4,i,dewPoint); db_update (4,i,0); // lcontador = 0; } else if (result == 10){ db_update (0,i,result); db_update (1,i,result); db_update (4,i,result); //lcontador = 0; } } //fflush (fr); fclose (fr); printf("File Closed\n"); mysql_disconnect (); } return 0; }
static db_result_t upgrade_to_schema_v2( db_t hdb ) { db_result_t rc = DB_OK; /* 1. Convert string type v1's 'sex_title' column to int-type 'sex' column, as v2 schema declares */ /* 1.1. Append a new uint8 type 'sex' column */ db_fielddef_t sex = { CONTACT_NFIELDS, "sex", DB_COLTYPE_UINT8, 0, 0, DB_NULLABLE, 0 }; /* 1.2. Fill 'sex' column with data according to schema: Mr->0, Mrs->1,Other->2 */ // Use absolute bound fields ( see ittiadb/manuals/users-guide/api-c-database-access.html#absolute-bound-fields ) db_ansi_t sex_old[ MAX_SEX_TITLE + 1 ]; db_len_t sex_ind = DB_FIELD_NULL, sex_ind_new = DB_FIELD_NULL; uint8_t sex_new = 0; db_bind_t binds_def[] = { {CONTACT_SEX, DB_VARTYPE_ANSISTR, DB_BIND_ADDRESS(sex_old), sizeof(sex_old), DB_BIND_ADDRESS(&sex_ind), DB_BIND_ABSOLUTE}, {CONTACT_NFIELDS, DB_VARTYPE_UINT8, DB_BIND_ADDRESS(&sex_new), sizeof(sex_new), DB_BIND_ADDRESS(&sex_ind_new), DB_BIND_ABSOLUTE}, }; db_row_t row = db_alloc_row( binds_def, 2 ); db_table_cursor_t p = { NULL, //< No index DB_CAN_MODIFY | DB_LOCK_EXCLUSIVE }; db_cursor_t c; rc = db_add_field( hdb, CONTACT_TABLE, &sex ); if( DB_OK != rc ) { print_error_message( "Couldn't append 'sex' column on v1->v2 upgrade", 0 ); return DB_FAIL; } c = db_open_table_cursor(hdb, CONTACT_TABLE, &p); db_begin_tx( hdb, DB_DEFAULT_ISOLATION | DB_LOCK_DEFAULT ); for ( rc = db_seek_first( c ); DB_OK == rc && !db_eof( c ); db_seek_next( c ) ) { rc = db_fetch( c, row, 0 ); if( 0 == strcmp( "Mr", sex_old ) ) { sex_new = 0; } else if( 0 == strcmp( "Mrs", sex_old ) ) { sex_new = 1; } else { sex_new = 2; } sex_ind_new = 0; rc = db_update( c, row, 0 ); } db_commit_tx( hdb, DB_DEFAULT_COMPLETION ); if( DB_OK != rc ) { print_error_message( "Couldn't fill appended 'sex' column with data", c ); } db_free_row( row ); db_close_cursor( c ); /* 1.3 Drop v1 sex_title column. */ /* Note: Dropping columns isn't available in ITTIA evaluation version. */ rc = db_drop_field( hdb, CONTACT_TABLE, "sex_title" ); if( DB_OK != rc ) { if( DB_EEVALUATION == get_db_error() ) { rc = DB_OK; fprintf( stdout, "No db_drop_field() feature supported in ITTIA Evaluation version\n" ); } else { print_error_message( "Couldn't drop 'sex_title' v1 schema column", c ); } } /* 2. Indexes modifications */ // 2.1. drop old pkey rc = db_drop_index( hdb, CONTACT_TABLE, CONTACT_BY_ID ); if ( DB_OK != rc ) { print_error_message( "Couldn't drop v1 pkey index", c ); } // 2.2. create a new one // 2.2.1. Set NOT NULL property on ring_id field // Note: this feature isn't supported in evaluation version of ittiadb. { extern db_fielddef_t contact_fields[]; //< Defined in schema_upgrade_schema_v2.c rc = db_update_field( hdb, CONTACT_TABLE, "ring_id", &contact_fields[ CONTACT_RING_ID ] ); if ( DB_OK != rc ) { print_error_message( "Couldn't set not null on ring_id column", c ); } } // 2.2.2. Create PKey index { extern db_indexdef_t contact_indexes[]; //< Defined in schema_upgrade_schema_v2.c rc = db_create_index( hdb, CONTACT_TABLE, CONTACT_BY_ID_RING_ID, &contact_indexes[0] ); if ( DB_OK != rc ) { print_error_message( "Couldn't create v2 pkey index", c ); } } }
static INT32 contact_provider_update(ContentProvider* thiz,ContentProviderDestination dest, ContentValues* values, DatabaseWhereClause* filter) { ContactProvider* sub_provider =(ContactProvider*) thiz->priv_data; const CHAR* table = NULL; ContentValuesItor itor; ContentValues *values_me, *values_ext; CHAR* values_ret = NULL; INT32 db_ret=DB_OK; CHAR* statement; CHAR statement_ext[50]; DatabaseWhereClause* clause; DatabaseHandle* db_handle = contact_base_provider_obtain_db_handle(); return_val_if_fail(sub_provider&&db_handle, DB_ERROR); switch (dest) { case CONTACT_PROVIDER_DESTINATION_CONTACTS: table = CONTACT_ME_CONTACTS_TABLE_NAME; break; case CONTACT_PROVIDER_DESTINATION_GROUP: table = CONTACT_ME_GROUP_TABLE_NAME; break; case CONTACT_PROVIDER_DESTINATION_EXTENSION_NUMBER: table = CONTACT_EXTENSION_TABLE_NUMBER_NAME; break; case CONTACT_PROVIDER_DESTINATION_EXTENSION_OTHER: table = CONTACT_EXTENSION_TABLE_OTHER_NAME; break; case CONTACT_PROVIDER_DESTINATION_SIM: table = CONTACT_SIM_TABLE_NAME; break; } if (CONTACT_PROVIDER_DESTINATION_CONTACTS==dest) { INT64 value_group_id,value_main_num_tag; WCHAR* str_name=NULL; values_me = content_values_create(); content_values_get_int(values,"group_id",&value_group_id); content_values_delete_key(values,"group_id"); content_values_put_int(values_me,"group_id",value_group_id); content_values_get_int(values,"main_num_tag",&value_main_num_tag); content_values_delete_key(values,"main_num_tag"); content_values_put_int(values_me,"main_num_tag",value_main_num_tag); content_values_get_string16(values,"name",&str_name); content_values_delete_key(values,"name"); content_values_put_string_16(values_me,"name",str_name); db_update(db_handle,table,values_me,filter); content_values_destroy(values_me); TG_FREE(str_name); for (itor=content_values_first(values);itor;itor=content_values_next(values,itor)) { const CHAR* key = content_values_get_key(itor); content_values_get_string(values,key,&values_ret); values_ext = content_values_create(); if (0 == strcmp(key,"mobile")) { content_values_put_string(values_ext,"value",values_ret); statement = (CHAR*)db_clause_get_statement(filter); sprintf(statement_ext,"%s AND tag_id=%d",statement,CONTACT_EXTENSION_TAG_MOBILE); clause = db_clause_create(DB_WHERE_CLAUSE,statement_ext,0); db_update(db_handle,CONTACT_EXTENSION_TABLE_NUMBER_NAME,values_ext,clause); db_clause_destroy(clause); } else if (0 == strcmp(key,"home")) { content_values_put_string(values_ext,"value",values_ret); statement = (CHAR*)db_clause_get_statement(filter); sprintf(statement_ext,"%s AND tag_id=%d",statement,CONTACT_EXTENSION_TAG_HOME); clause = db_clause_create(DB_WHERE_CLAUSE,statement_ext,0); db_update(db_handle,CONTACT_EXTENSION_TABLE_NUMBER_NAME,values_ext,clause); db_clause_destroy(clause); } else if (0 == strcmp(key,"office")) { content_values_put_string(values_ext,"value",values_ret); statement = (CHAR*)db_clause_get_statement(filter); sprintf(statement_ext,"%s AND tag_id=%d",statement,CONTACT_EXTENSION_TAG_OFFICE); clause = db_clause_create(DB_WHERE_CLAUSE,statement_ext,0); db_update(db_handle,CONTACT_EXTENSION_TABLE_NUMBER_NAME,values_ext,clause); db_clause_destroy(clause); } else if (0 == strcmp(key,"email")) { content_values_put_string(values_ext,"value",values_ret); statement = (CHAR*)db_clause_get_statement(filter); sprintf(statement_ext,"%s AND tag_id=%d",statement,CONTACT_EXTENSION_TAG_EMAIL); clause = db_clause_create(DB_WHERE_CLAUSE,statement_ext,0); db_update(db_handle,CONTACT_EXTENSION_TABLE_OTHER_NAME,values_ext,clause); db_clause_destroy(clause); } content_values_destroy(values_ext); TG_FREE(values_ret); } } else { db_ret = db_update(db_handle ,table,values,filter); } contact_base_provider_release_db_handle(); return db_ret; }
static void update_func(uint64_t index) { db_update(test_data[index].id, test_data[index].value + 1); }
static int db_deleted_purge(void) { return db_update("DELETE FROM %smessages WHERE status=%d", DBPFX, MESSAGE_STATUS_PURGE); }
/* * Update contact in the database */ int db_update_ucontact(ucontact_t* _c) { char b[256]; char* dom; db_key_t keys1[3]; db_val_t vals1[3]; db_key_t keys2[7]; db_val_t vals2[7]; keys1[0] = user_col; keys1[1] = contact_col; keys1[2] = domain_col; keys2[0] = expires_col; keys2[1] = q_col; keys2[2] = callid_col; keys2[3] = cseq_col; keys2[4] = replicate_col; keys2[5] = state_col; keys2[6] = flags_col; vals1[0].type = DB_STR; vals1[0].nul = 0; vals1[0].val.str_val = *_c->aor; vals1[1].type = DB_STR; vals1[1].nul = 0; vals1[1].val.str_val = _c->c; vals2[0].type = DB_DATETIME; vals2[0].nul = 0; vals2[0].val.time_val = _c->expires; vals2[1].type = DB_DOUBLE; vals2[1].nul = 0; vals2[1].val.double_val = _c->q; vals2[2].type = DB_STR; vals2[2].nul = 0; vals2[2].val.str_val = _c->callid; vals2[3].type = DB_INT; vals2[3].nul = 0; vals2[3].val.int_val = _c->cseq; vals2[4].type = DB_INT; vals2[4].nul = 0; vals2[4].val.int_val = _c->replicate; vals2[5].type = DB_INT; vals2[5].nul = 0; if (_c->state < CS_ZOMBIE_N) { vals2[5].val.int_val = 0; } else { vals2[5].val.int_val = 1; } vals2[6].type = DB_INT; vals2[6].nul = 0; vals2[6].val.bitmap_val = _c->flags; if (use_domain) { dom = q_memchr(_c->aor->s, '@', _c->aor->len); vals1[0].val.str_val.len = dom - _c->aor->s; vals1[2].type = DB_STR; vals1[2].nul = 0; vals1[2].val.str_val.s = dom + 1; vals1[2].val.str_val.len = _c->aor->s + _c->aor->len - dom - 1; } /* FIXME */ memcpy(b, _c->domain->s, _c->domain->len); b[_c->domain->len] = '\0'; db_use_table(db, b); if (db_update(db, keys1, 0, vals1, keys2, vals2, (use_domain) ? (3) : (2), 7) < 0) { LOG(L_ERR, "db_upd_ucontact(): Error while updating database\n"); return -1; } return 0; }
ret = db_insert("hello", &sid, &docid,get_row_data(field_count,types,mem_pool), DOCID_BY_SELF, mem_pool); ASSERT_EQ(MILE_RETURN_SUCCESS,ret); struct list_head* index_equal_ret = NULL; index_equal_ret = db_index_equal_query("hello",get_time_hint(mem_pool),new_data,mem_pool); struct segment_query_rowids* node; list_for_each_entry(node,index_equal_ret,rowids_list){ ASSERT_EQ(node->sid,0); ASSERT_EQ(node->rowids->rowid_num,1); ASSERT_EQ(node->rowids->head->rowid_array[0],0); } //尝试更新会被拒绝 struct low_data_struct* old_data = NULL; ret = db_update("hello",sid,docid,new_data,&old_data,mem_pool); ASSERT_EQ(ret,ERROR_ONLY_FILTER_SUPPORT); //在hash列上调用fiter索引的查询接口 struct low_data_struct* index_value_ret = NULL; index_value_ret = db_index_value_query("hello",sid,"HI_TYPE_STRING",docid,mem_pool); ASSERT_EQ(0,verify_low_data(index_value_ret,HI_TYPE_STRING)); //在同一列上建立filter索引 ret = db_ensure_index("hello","HI_TYPE_STRING",HI_KEY_ALG_FILTER,HI_TYPE_STRING,mem_pool); ASSERT_EQ(ret,MILE_RETURN_SUCCESS); ret = db_ensure_index("hello","HI_TYPE_LONG",HI_KEY_ALG_FILTER,HI_TYPE_LONG,mem_pool); index_value_ret = db_index_value_query("hello",sid,"HI_TYPE_STRING",docid,mem_pool);
int auth_change_clientid(uint64_t user_idnr, uint64_t new_cid) { return db_update("UPDATE %susers SET client_idnr = %" PRIu64 " WHERE user_idnr=%" PRIu64 "", DBPFX, new_cid, user_idnr); }