package com.refresh.pos.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class InventoryDaoAndroid extends SQLiteOpenHelper implements Dao {

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "pos_database2";

	public InventoryDaoAndroid(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {

		database.execSQL("CREATE TABLE product_catalog"
				+ "(_id INTEGER PRIMARY KEY," + "name TEXT(100),"
				+ "barcode TEXT(100)," +
				" sale_price DOUBLE );");

		database.execSQL("CREATE TABLE stock" + "(_id INTEGER PRIMARY KEY,"
				+ "product_id INTEGER," + "amount INTEGER," + "cost DOUBLE,"
				+ "date_added DATETIME);");


		Log.d("CREATE DATABASE", "Create Database Successfully.");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

	}

	@Override
	public List<Object> select(String queryString) {
		try {
			SQLiteDatabase database = this.getWritableDatabase();
			List<Object> list = new ArrayList<Object>();
			Cursor cursor = database.rawQuery(queryString, null);

			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						ContentValues content = new ContentValues();
						content.put("_id",
								cursor.getInt(cursor.getColumnIndex("_id")));
						content.put("name",
								cursor.getString(cursor.getColumnIndex("name")));
						content.put("barcode", cursor.getString(cursor
								.getColumnIndex("barcode")));
						content.put("sale_price", cursor.getDouble(cursor
								.getColumnIndex("sale_price")));
						list.add(content);
					} while (cursor.moveToNext());
				}
			}
			cursor.close();
			database.close();
			return list;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public long insert(String tableName, Object content) {
		try {
			SQLiteDatabase database = this.getWritableDatabase();
			long rows = database.insert(tableName, null,
					(ContentValues) content);
			database.close();
			return rows;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

	}

	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete() {
		// TODO Auto-generated method stub
		return false;
	}

	public long getSize() {
		SQLiteDatabase database = this.getWritableDatabase();
		SQLiteStatement byteStatement = database
				.compileStatement("SELECT SUM(LENGTH(_id)) FROM product_catalog");
		long bytes = byteStatement.simpleQueryForLong();
		return bytes;
	}


}

@@ -1548,165 +1548,103 @@
 %09%09%09%09
-content.put(%22_id%22,%0A%09%09%09%09%09%09%09%09cursor.getInt(cursor.getColumnIndex(%22_id%22)));%0A%09%09%09%09%09%09content.put(%22name%22,%0A%09%09%09%09%09%09%09%09cursor.getString(cursor.getColumnIndex(%22name%22)));%0A
+String%5B%5D columnNames = cursor.getColumnNames();%0A%09%09%09%09%09%09for (String columnName : columnNames) %7B%0A%09
 %09%09%09%09
@@ -1657,25 +1657,26 @@
 ent.put(
-%22barcode%22
+columnName
 , cursor
@@ -1688,33 +1688,24 @@
 tring(cursor
-%0A%09%09%09%09%09%09%09%09
 .getColumnIn
@@ -1712,17 +1712,18 @@
 dex(
-%22barcode%22
+columnName
 )));
@@ -1733,98 +1733,9 @@
 %09%09%09%09
-content.put(%22sale_price%22, cursor.getDouble(cursor%0A%09%09%09%09%09%09%09%09.getColumnIndex(%22sale_price%22)));
+%7D
 %0A%09%09%09

edit select method