SQLiteBooks, metodos hydrate y dehydrate

Share


En este post vamos a desglosar y analizar los metodos hydrate y dehydrate del ejemplo SQLiteBooks que talvez muchos no tenemos muy claro como funciona:
Estos métodos sirven para cargar y descargar nuestros datos de la memoria. Empecemos con hydrate:

En el ejemplo, tenemos un metodo llamado initWithPrimaryKey: que recupera el nombre del libro, con hydrate nos aseguramos de traer el resto de los datos a nuestro objeto.

Los argumentos que se pasan a sqlite3_prepare_v2 son: el nombre de la base de datos, la consulta, la longitud de la consulta en bytes, la consulta compilada y el puntero a una porción no usada de la consulta (en el ejemplo NULL).

C++:
  1. (void)hydrate {
  2.     // Verificamos si es necesario cargar los datos, si ya estan en memoria no se realiza ninguna acción.
  3.     if (hydrated) return;
  4.    
  5.     //Para que se ejecute una consulta es necesario compilarla previamente, para esto se emplea sqlite3_prepare_v2
  6.  
  7.     if (hydrate_statement == nil) {
  8.         const char *sql = "SELECT author, copyright FROM book WHERE pk=?";
  9.         if (sqlite3_prepare_v2(database, sql, -1, &hydrate_statement, NULL) != SQLITE_OK) {
  10.             NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
  11.         }
  12.     }
  13.  
  14.     // Enlazamos  el parametro "primaryKey" a la consulta
  15.     sqlite3_bind_int(hydrate_statement, 1, primaryKey);
  16.  
  17.     // Ejecutamos la consulta que nos devuelve SQLITE_ROW
  18.     int success =sqlite3_step(hydrate_statement);
  19.     if (success == SQLITE_ROW) {
  20.         char *str = (char *)sqlite3_column_text(hydrate_statement, 0);
  21.         self.author = (str) ? [NSString stringWithUTF8String:str] : @"";
  22.         self.copyright = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(hydrate_statement, 1)];
  23.     } else {
  24.         // Si la consulta no generó resultados
  25.         self.author = @"Unknown";
  26.         self.copyright = [NSDate date];
  27.     }
  28.     // Reseteamos la consulta para un proximo uso
  29.     sqlite3_reset(hydrate_statement);
  30.     // Actualizamos el estado del objeto a hydrated
  31.     hydrated = YES;
  32. }

El metodo dehydrate descarga de la memoria todos los datos excepto la llave primaria y nos permite realizar cualquier cambio en los datos:

C++:
  1. - (void)dehydrate {
  2.     if (dirty) {
  3.         ////Para que se ejecute una consulta es necesario compilarla previamente, para esto se emplea sqlite3_prepare_v2
  4.         if (dehydrate_statement == nil) {
  5.             const char *sql = "UPDATE book SET title=?, author=?, copyright=? WHERE pk=?";
  6.             if (sqlite3_prepare_v2(database, sql, -1, &dehydrate_statement, NULL) != SQLITE_OK) {
  7.                 NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
  8.             }
  9.         }
  10.         // Enlazamos las variables de la consulta
  11.         sqlite3_bind_text(dehydrate_statement, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
  12.         sqlite3_bind_text(dehydrate_statement, 2, [author UTF8String], -1, SQLITE_TRANSIENT);
  13.         sqlite3_bind_double(dehydrate_statement, 3, [copyright timeIntervalSince1970]);
  14.         sqlite3_bind_int(dehydrate_statement, 4, primaryKey);
  15.         // Ejecutamos la consulta
  16.         int success = sqlite3_step(dehydrate_statement);
  17.         // Reseteamos la consulta para un proximo uso
  18.         sqlite3_reset(dehydrate_statement);
  19.         // Manejamos los errores
  20.         if (success != SQLITE_DONE) {
  21.             NSAssert1(0, @"Error: failed to dehydrate with message '%s'.", sqlite3_errmsg(database));
  22.         }
  23.         // Actualizamos el estado del objeto respecto a los cambios
  24.         dirty = NO;
  25.     }
  26.    
  27.    //Finalmente liberamos nuestras variables de la memoria
  28.     [author release];
  29.     author = nil;
  30.     [copyright release];
  31.     copyright = nil;
  32.     [data release];
  33.     data = nil;
  34.     // Actualizamos el estado del objeto a hydrated = NO
  35.     hydrated = NO;
  36. }

Espero que el post haya servido para aclarar algunas dudas y bueno cualquier comentario o pregunta son bienvenidos.

banner ad

Leave a Reply

  • Buy Cheapest new fat binding weight loss pill Now Best Prices. Discount Online Pharmacy.
  • Buy Cheapest medco pharmacy Online Drugs, Health And Beauty. Best Prices.
  • Buy Cheap cialis europe Online Cheap Online Pharmacy. Online Medical Shop.
  • Buy Cheap weight loss medication Now Pharmacy Store. Pharmacy At The Best Price!
  • Buy Cheapest prescribed weight loss pills Now Cheap Prescription Drugs. Pharmacy Store.
  • Buy Cheapest adult acne Online Cheap Online Pharmacy. Best Online.
  • Buy Cheap where to order fioricet with codeine Now Free Viagra Pills! Drugs, Health And Beauty.
  • Buy Cheapest uk drug prices Now Guaranteed Shipping. Pharmacy Store.
  • Buy Cheapest otc weight loss supplements Online Best Internet. Free Viagra Pills!
  • Buy Cheapest diabetic impotence Online Cheap Online Pharmacy. Best Drugstore.
  • Buy Cheapest pills for bipolar disorder Now Best Drugstore. 24/Online Pharmacy.
  • Buy Cheapest valtrex commercials Now WorldWide Shipping. No Prescription Needed.
  • Buy Cheap online pharmacy no prescriptions Now Best Prices. 100% Satisfaction Guaranteed.
  • usa discount pharmacy Online Without Prescription Low Prices. Free Viagra Pills!
  • Buy Cheap buy cheap alprazolam Online WorldWide Shipping. Buy Medications Online.
  • Buy Cheapest phentermine no perscription Online Pharmacy Store. Guaranteed Shipping.
  • Buy Cheap doctor diet pills Online Pharmacy Store. Cheap Online Pharmacy.
  • Buy Cheapest pay pal buy diflucan Now Pharmacy At The Best Price! Low Prices.
  • Buy Cheapest effects nexium Now Online Medical Shop. Guaranteed Shipping.
  • Buy Cheap levitra use Now Free Viagra Pills! Buy Medications Online.
  • Buy Cheap online pain doctors Now Online Prices For online pain doctors! Top Online Pharmacy.
  • Buy Cheap rapid weight loss pills Online The Largest Internet Pharmacy. Best Online.
  • Buy Cheapest buy ativan from europe Online Best Prices. Buy Medications Online.
  • Buy Cheapest strongest pain medications Now Online Medical Shop. Cheap Online Pharmacy.
  • Buy Cheapest appetite suppressant medication Online Best Online. No Prescription Needed.
  • Buy Cheap phentermine for less Now Guaranteed Shipping. Online Medical Shop.
  • Buy Cheapest cost of prescription for chlamydia Now Best Online. Order Cheap Meds Without Rx.
  • Buy Cheapest xanax dosage Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest ultram withdrawl Now Best Internet. No Prescription Needed.
  • Buy Cheapest canadian prescription medications Online Best Prices. Discount Pharmacy Online.
  • Buy Cheapest very cheap tramadol Online Discount Online Pharmacy. Best Internet.
  • Buy Cheapest xanax pregnancy Online Guaranteed Shipping. Free Viagra Pills!
  • Buy Cheap do diet pills work Online Top Online Pharmacy. Best Drugstore.
  • Buy Cheapest long term use of ambien Online Online Prices For long term use of ambien! Low Prices.
  • Buy Cheap zithromax 500 mg Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheapest list of vitamin supplements Online Drugs, Health And Beauty. Best Prices.
  • Buy Cheapest atorvastatin Now Best Online. Discount Pharmacy Online.
  • Buy Cheap viagra cheapest online sellers Now Top Online Pharmacy. Drugs, Health And Beauty.
  • Buy Cheap where to buy levitra Now Buy Medications Online. Cheap Online Pharmacy.
  • Buy Cheap pfizer viagra dosage 100 mg Online Guaranteed Shipping. Pharmacy Store.
  • Buy Cheapest diazepam anxiety Now 24/Online Pharmacy. WorldWide Shipping.
  • Buy Cheap viagra testosterone Online Low Prices. Top Online Pharmacy Supplier.
  • Buy Cheap ambien canadian pharmacy Now Pharmacy Store. Discount Pharmacy Online.
  • Buy codeine pills Without Prescription Doctor. Internet Prices For codeine pills!
  • Buy Cheapest canadian pharmacy meds Online Guaranteed Shipping. Best Prices.
  • Buy Cheapest most effective prescription sleep aids Online Cheap Online Pharmacy. Best Drugstore.
  • Buy Cheap reliable online pharmacies Online Drugs, Health And Beauty. Best Online.
  • Buy Cheap alprazolam generic for xanax Online Online Medical Shop. Pharmacy Store.
  • Buy Cheapest cost of chlamydia medication Online Best Prices. No Prescription Needed.
  • Buy Cheap pmdd medicine Online Pharmacy Store. No Prescription Needed.
  • Buy Cheap diet pill phentermine Now Special Prices For diet pill phentermine! Best Prices.
  • Buy Cheap valtrex cheap Now Online Prices For valtrex cheap! Top Online Pharmacy.
  • Buy alprazolam pills Online Without Prescription. Best Prices. Best Online.
  • Buy Cheap safe generic cialis Now Best Prices. Order Cheap Meds Without Rx.
  • Buy Cheap buspirone hcl Now Cheap Prescription Drugs. Top Online Pharmacy.
  • Buy Cheap stress valium Now Best Internet. Pharmacy At The Best Price!
  • Buy Cheap phentermine 37.5 diet pills Now Pharmacy At The Best Price! Best Internet.
  • Buy Cheapest cialis soft tab description Now Cheap Pharmacy Online. WorldWide Shipping.
  • Buy Cheapest order pain medicine online Now Internet Prices For order pain medicine online! Pharmacy Store.
  • Buy Cheap buy propecia where Online No Prescription Needed. Best Prices.
  • Buy Cheap best levitra price Now Online Medical Shop. Free Viagra Pills!
  • Buy Cheap viagra from uk Online Best Prices. Cheap Prescription Drugs.
  • Buy Cheap valium 5mg Now Best Internet. Top Online Pharmacy Supplier.
  • Buy Cheap buy online zoloft Now Buy Medications Online. Top Online Pharmacy.
  • Buy Cheapest natural appetite suppressants Now Top Online Pharmacy. WorldWide Shipping.
  • Buy Cheap narcotic pain pills without a prescription Online Online Medical Shop. Guaranteed Shipping.
  • weight loss herbs Online Without Prescription Low Prices. WorldWide Shipping.
  • Buy Cheap weight loss remedies Now Guaranteed Shipping. Discount Online Pharmacy.
  • Buy viagra dosing Online Without Prescription. Best Prices. Best Online.
  • Buy Cheap fat burning diet Now Pharmacy Store. Order Cheap Meds Without Rx.
  • Buy Cheap ambien side effect Online Cheap Online Pharmacy. Pharmacy Store.
  • Buy Cheap tramadol dosage Online Special Prices For tramadol dosage! Best Online.
  • Buy Cheap medical weight loss Online Online Medical Shop. Top Online Pharmacy.
  • Buy Cheap order viagra jelly Now Best Online. The Largest Internet Pharmacy.
  • Buy Cheapest free trial diet pills Now Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheapest online pet pharmacy Online Best Drugstore. Cheap Pharmacy Online.
  • top 10 diet pills Online Without Prescription Free Viagra Pills! Best Online.
  • Buy Cheapest azithromycin bronchitis Online Best Internet. Discount Online Pharmacy.
  • Buy Cheap drugs for schizophrenia Now 24/Online Pharmacy. WorldWide Shipping.
  • Buy Cheap buy pheromone to attract woman Online Best Prices. No Prescription Needed.
  • Buy Cheap viagra substites Now Pharmacy Store. Order Cheap Meds Without Rx.
  • Buy Cheap proscar 5 mg Now Pharmacy Store. Top Online Pharmacy Supplier.
  • Buy Cheapest cheap lexapro Now Top Online Pharmacy. Cheap Pharmacy Online.
  • buy valium cheap Online Without Prescription Free Viagra Pills! Low Prices.
  • Buy Cheap top 10 diet pills Online Best Online. Pharmacy At The Best Price!
  • Buy Cheapest how fast does valium work Now Best Internet. Special Prices For how fast does valium work!
  • Buy Cheapest on line cialis Now Top Online Pharmacy Supplier. Best Online.
  • Buying Cheap zyban how to use. Mexican Pharmacy, Good Prices. Best Drugstore.
  • Buy Cheap purpose of xanax Now Best Online. All Medications Are Certificated!
  • Buy Cheapest bayer levitra professional pro Online Best Drugstore. Free Viagra Pills!
  • Buy Cheap klonopin and pregnancy Now Best Drugstore. Discount Online Pharmacy.
  • Buy Cheap cheap drugs Online Best Online. Internet Prices For cheap drugs!
  • Buy Cheapest geodon and codeine Online Online Medical Shop. WorldWide Shipping.
  • Buy Cheapest discount erectile dysfunction medicatin Online WorldWide Shipping. Best Drugstore.
  • Buy Cheap us based viagra Online 100% Satisfaction Guaranteed. Best Prices.
  • Buy prescription drugs generic medicines brand product Online Without Prescription. Pharmacy Store. Low Prices.
  • Buy Cheap trazodone sleep aid Online Pharmacy Store. Guaranteed Shipping.
  • Buy Cheapest tramadol doses Online 24/Online Pharmacy. Best Internet.
  • Buy Cheapest clomid take Now Top Online Pharmacy. Free Viagra Pills!
  • Buy Cheapest herbal based diet products Now Best Online. Special Prices For herbal based diet products!