Пример #1
0
bool Db_Base::Output_Record (void)
{
	if (File_Format () == SQLITE3) {
		int i, j, num, lvalue;
		double dvalue;
		string svalue;
		Db_Field *fld;

		num = Num_Fields ();

		for (i=0, j=1; i < num; i++) {
			fld = Field (i);
			if (Nested () != fld->Nested ()) continue;

			if (Nested ()) {
				if (fld->Type () == DB_INTEGER) {
					Get_Field (i, lvalue);
					sqlite3_bind_int (insert_nest, j+1, lvalue);
				} else if (fld->Type () == DB_DOUBLE) {
					Get_Field (i, dvalue);
					sqlite3_bind_double (insert_nest, j+1, dvalue);
				} else {
					Get_Field (i, svalue);
					sqlite3_bind_text (insert_nest, j+1, svalue.c_str (), -1, SQLITE_TRANSIENT);
				}
			} else {
				if (fld->Type () == DB_INTEGER) {
					Get_Field (i, lvalue);
					sqlite3_bind_int (insert_stmt, j, lvalue);
				} else if (fld->Type () == DB_DOUBLE) {
					Get_Field (i, dvalue);
					sqlite3_bind_double (insert_stmt, j, dvalue);
				} else {
					Get_Field (i, svalue);
					sqlite3_bind_text (insert_stmt, j, svalue.c_str (), -1, SQLITE_TRANSIENT);
				}
			}
			j++;
			
		}
		if (Nested ()) {
			sqlite3_bind_int (insert_nest, 1, parent_id);

			if (sqlite3_step (insert_nest) != SQLITE_DONE) {
				exe->Warning ("Inserting Database: ") << sqlite3_errmsg (db_file);
			}
			if (sqlite3_reset (insert_nest) != SQLITE_OK) {
				exe->Warning ("Inserting Database: ") << sqlite3_errmsg (db_file);
			}
		} else {
			if (sqlite3_step (insert_stmt) != SQLITE_DONE) {
				exe->Warning ("Inserting Database: ") << sqlite3_errmsg (db_file);
			}
			parent_id = (int) sqlite3_last_insert_rowid (db_file);
			if (sqlite3_reset (insert_stmt) != SQLITE_OK) {
				exe->Warning ("Inserting Database: ") << sqlite3_errmsg (db_file);
			}
		}
	} else if (File_Format () == CSV_DELIMITED) {
		int i, j, max;
		string *ptr;
		char *delimiter, *record;
		bool first = true;
		Db_Field *fld;

		delimiter = Delimiters ();
		record = Record_String ();

		for (i=0; i < Num_Fields (); i++) {
			fld = Field (i);
			if (Nested () == fld->Nested ()) {
				ptr = &fld->Buffer ();
				max = (int) ptr->size ();
				if (max > fld->Width ()) max = fld->Width ();

				if (first) {
					first = false;
				} else {
					*record++ = *delimiter;
				}
				for (j=0; j < max; j++) {
					*record++ = (*ptr) [j];
				}
				*record = '\0';
			}
		}
		Set_Size ();
	}
	return (true);
}
Пример #2
0
bool Db_Base::Input_Record (void)
{
	if (File_Format () == SQLITE3) {
		int i, j, num, lvalue, result;
		const unsigned char *text;
		double dvalue;
		Db_Field *fld;

		//---- get the next record ----

		if (Nested ()) {
			if (new_nest_flag) {
				new_nest_flag = false;

				sqlite3_reset (read_nest);
				sqlite3_bind_int (read_nest, 1, parent_id);
			}
			result = sqlite3_step (read_nest);
		} else {
			Record_Number (0);
			parent_id = Record_Number ();
			new_nest_flag = true;

			sqlite3_reset (read_stmt);
			sqlite3_bind_int (read_stmt, 1, parent_id);
			result = sqlite3_step (read_stmt);
		}
		if (result == SQLITE_DONE) return (false);
		if (result != SQLITE_ROW) {
			exe->Warning ("SQLITE3 Read Problem: ") << sqlite3_errmsg (db_file);
		}
		num = Num_Fields ();

		for (i=0, j=0; i < num; i++) {
			fld = Field (i);
			if (Nested () != fld->Nested ()) continue;
			if (Nested ()) {
				if (fld->Type () == DB_INTEGER) {
					lvalue = sqlite3_column_int (read_nest, j+1);
					Put_Field (i, lvalue);
				} else if (fld->Type () == DB_DOUBLE) {
					dvalue = sqlite3_column_double (read_nest, j+1);
					Put_Field (i, dvalue);
				} else {
					text = sqlite3_column_text (read_nest, j+1);
					Put_Field (i, (char *) text);
				}
			} else {
				if (fld->Type () == DB_INTEGER) {
					lvalue = sqlite3_column_int (read_stmt, j);
					Put_Field (i, lvalue);
				} else if (fld->Type () == DB_DOUBLE) {
					dvalue = sqlite3_column_double (read_stmt, j);
					Put_Field (i, dvalue);
				} else {
					text = sqlite3_column_text (read_stmt, j);
					Put_Field (i, (char *) text);
				}
			}
			j++;
		}
	} else if (File_Format () == CSV_DELIMITED) {
		Strings fields;
		Db_Field *fld;

		int count = String (Record_String ()).Parse (fields, Delimiters ());

		for (int i=0; i < Num_Fields (); i++) {
			fld = Field (i);
			if (Nested () == fld->Nested ()) {
				if (fld->Offset () <= count) {
					fld->Buffer (fields [fld->Offset () - 1]);
				} else {
					fld->Buffer ("");
				}
			}
		}
	}
	return (true);
}