maxfarnham / PharmManager

UIC CS342 Pharmacy Inventory management project
0 stars 0 forks source link

What are the return types of the execute functions? #5

Open justodiaz opened 10 years ago

justodiaz commented 10 years ago

If we execute a non-scalar query, to get all medicines in the table for example, what is it returning? I know in 341 we worked with DataSets and adapters.

maxfarnham commented 10 years ago

That'd what we're trying to figure out, @kungpaojake and me were talking about returning a list of dictionaries, but we'll have to see how the sqlite library returns the result set.

Dhruv, Jake and chris are working on this portion so they could probably hop in here.

kungpaojake commented 10 years ago

Just pushed/committed a version using a new test class object and ArrayList of the test object. I tried to use HashMap but couldn't get it to work. Maybe I'm missing something...but I'll try again later.

kungpaojake commented 10 years ago

edit: using { and } instead of less than and greater than signs since git tries to interpret those as html tags (I think)

tldr: executeNonQuery(String) returns 1 on success, 0 on fail executeScalar(String) returns Object that can be type casted by the caller executeNonScalar(String) returns ArrayList{ArrayList{Object}} by using exampleList.get(row).get(col) we can pull any value out using row/column numbering (depending on your sql query)

Ok I finished up the execute methods (non-query, scalar and non-scalar) earlier. Right now the returns are 1 or 0 for non-query (1 = success, 0 = fail), scalar returns an object (depending on the query, you'll type cast the object), and non-scalar returns an ArrayList{ArrayList{Object}}. I notice that there are some references to non-scalar returning a ResultSet but I don't think that will work. ResultSet closes/frees when the method returns resulting in an empty ResultSet.

So an example using the ArrayList{ArrayList{Object}} we have the current call in pharmacyManager

sql = "SELECT LowStockThreshold, OverstockThreshold " + "FROM Medications " + "WHERE Name = " + name + ";";
rs = datatier.executeQuery(sql); rs.next();
//set thresholds lowFlag = rs.getInt("LowStockThreshold"); overFlag = rs.getInt("OverstockThreshold");

can be rewritten as:

// (sql query is the same) sql = "SELECT LowStockThreshold, OverstockThreshold " + "FROM Medications " + "WHERE Name = " + name + ";"; ArrayList{ArrayList{Object}} testList = executeNonScalar(sql); int i; for(i = 0; i < testList.size(); i++){ lowFlag = (int)testList.get(i).get(0); overFlag = (int)testList.get(i).get(1); }

We use get(0) and get(1) because we know there are only two columns from our sql query. We cast them to int's because it returns objects and we're expecting int's. The "width" or number of columns will always be the number of items we "select" and in order that we select them while number of rows will vary depending on how many results we have.

Hopefully this made sense! Let me know if you would like me to make changes to the return types and accesses.