package com.moanoit.belote.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.moanoit.belote.Constantes;
/**
* Classe de gestion de la base de donnes.
*
* @author bdelbos
*
*/
public class BeloteDbAdapter {
/**
* tag pour traces.
*/
private static final String TAG = "BeloteDbAdapter";
/**
* Connexion la base de donnes.
*/
private DatabaseHelper mDbHelper;
/**
* Base de donnes.
*/
private SQLiteDatabase mDb;
/**
* Contexte d'execution.
*/
private final Context mCtx;
/**
* Constructor - takes the context to allow the database to be
* opened/created.
*
* @param ctx
* the Context within which to work
*/
public BeloteDbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the teams database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws Exception
* if the database could be neither opened or created
*/
public final BeloteDbAdapter open() throws Exception {
DatabaseHelperFactory dataBaseFactory = null;
try {
//mDbHelper = new DatabaseHelper(mCtx);
// get the DBManager
dataBaseFactory = DatabaseHelperFactory.getInstance();
mDbHelper = dataBaseFactory.getDBManager(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
throw e;
}
}
/**
* Closes connection to database.
*/
public final void close() {
mDbHelper.close();
}
/**
* Create a new team using the name and players provided. If the team is
* successfully created return the new rowId for that team, otherwise return
* a -1 to indicate failure.
*
* @param name
* the name of the team
* @param player1Name
* the name of the first player
* @param player2Name
* the name of the second player
* @return the id of the team
*/
public final long createTeam(String name, String player1Name,
String player2Name) {
try {
ContentValues initialValues = new ContentValues();
initialValues.put(Constantes.KEY_TEAM_NAME, name);
initialValues.put(Constantes.KEY_TEAM_PLAYER1, player1Name);
initialValues.put(Constantes.KEY_TEAM_PLAYER2, player2Name);
return mDb.insert(ConstantesDataBase.TABLE_TEAMS,
null,
initialValues);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Delete the team with the given rowId.
*
* @param rowId id of team to delete
* @return true if deleted, false otherwise
* @throws SQLException if the database could be neither opened or created
*/
public final boolean deleteTeam(long rowId) throws SQLException {
try {
return mDb.delete(ConstantesDataBase.TABLE_TEAMS,
Constantes.KEY_TEAM_ROWID + "=" + rowId,
null) > 0;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor over the list of all teams in the database.
*
* @return Cursor over all teams
*/
public final Cursor fetchAllTeams() {
try {
return mDb.query(ConstantesDataBase.TABLE_TEAMS, new String[] {
Constantes.KEY_TEAM_ROWID, Constantes.KEY_TEAM_NAME,
Constantes.KEY_TEAM_PLAYER1, Constantes.KEY_TEAM_PLAYER2 },
null, null, null, null, null);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor over the list of all teams in the database. Except the
* one team with given id.
*
* @param rowId
* of the excluded team
* @return Cursor over all teams
*/
public final Cursor fetchAllTeams(long rowId) {
try {
return mDb.query(ConstantesDataBase.TABLE_TEAMS, new String[] {
Constantes.KEY_TEAM_ROWID, Constantes.KEY_TEAM_NAME,
Constantes.KEY_TEAM_PLAYER1, Constantes.KEY_TEAM_PLAYER2 },
Constantes.KEY_TEAM_ROWID + "!=" + rowId, null, null, null,
null);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor positioned at the team that matches the given rowId.
*
* @param rowId
* id of team to retrieve
* @return Cursor positioned to matching team, if found
* @throws SQLException
* if team could not be found/retrieved
*/
public final Cursor fetchTeam(long rowId) throws SQLException {
try {
Cursor mCursor = mDb.query(true, ConstantesDataBase.TABLE_TEAMS,
new String[] {
Constantes.KEY_TEAM_ROWID,
Constantes.KEY_TEAM_NAME,
Constantes.KEY_TEAM_PLAYER1,
Constantes.KEY_TEAM_PLAYER2 },
Constantes.KEY_TEAM_ROWID + "=" + rowId, null, null, null,
null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Update the team using the details provided. The team to be updated is
* specified using the rowId, and it is altered to use the name and players
* values passed in
*
* @param rowId
* id of team to update
* @param name
* value to set team name to
* @param player1Name
* value to set player1 name to
* @param player2Name
* value to set player2 name to
* @return true if the team was successfully updated, false otherwise
*/
public final boolean updateTeam(long rowId, String name, String player1Name,
String player2Name) {
try {
ContentValues args = new ContentValues();
args.put(Constantes.KEY_TEAM_NAME, name);
args.put(Constantes.KEY_TEAM_PLAYER1, player1Name);
args.put(Constantes.KEY_TEAM_PLAYER2, player2Name);
return mDb.update(ConstantesDataBase.TABLE_TEAMS,
args,
Constantes.KEY_TEAM_ROWID + "=" + rowId,
null) > 0;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Create a new party using the id of teams provided. If the party is
* successfully created return the new rowId for that team, otherwise
* return. a -1 to indicate failure.
*
* @param team1Id
* the id of the first team
* @param team2Id
* the id of the second team
* @return the id of the party
*/
public final long createParty(long team1Id, long team2Id) {
try {
ContentValues initialValues = new ContentValues();
initialValues.put(Constantes.KEY_PARTY_TEAM1ID, team1Id);
initialValues.put(Constantes.KEY_PARTY_TEAM2ID, team2Id);
initialValues.put(Constantes.KEY_PARTY_SCORE1, 0);
initialValues.put(Constantes.KEY_PARTY_SCORE2, 0);
initialValues.put(Constantes.KEY_PARTY_CURRENT, true);
return mDb.insert(ConstantesDataBase.TABLE_MANCHES,
null,
initialValues);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Update a party using the id of the party provided. If the party is
* successfully updated return the number of updated rows, otherwise return.
* a -1 to indicate failure.
*
* @param partyId
* the id of the party
* @param score1
* the score of the second team
* @param score2
* the score of the second team
* @return the id of the party
*/
public final long updatePartyScore(long partyId, int score1, int score2) {
try {
ContentValues updateValues = new ContentValues();
updateValues.put(Constantes.KEY_PARTY_SCORE1, score1);
updateValues.put(Constantes.KEY_PARTY_SCORE2, score2);
int nb = mDb.update(ConstantesDataBase.TABLE_MANCHES, updateValues,
Constantes.KEY_PARTY_ROWID + "=" + partyId, null);
if (nb == 1) {
return nb;
} else {
// TODO : traitement d'erreur
return -1;
}
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Ends a party using the id of the party provided. If the party is
* successfully updated return the number of updated rows, otherwise return
* a -1 to indicate failure.
*
* @param partyId
* the id of the party
* @param score1
* the score of the second team
* @param score2
* the score of the second team
* @return the id of the party
*/
public final long endParty(long partyId, int score1, int score2) {
try {
ContentValues updateValues = new ContentValues();
updateValues.put(Constantes.KEY_PARTY_SCORE1, score1);
updateValues.put(Constantes.KEY_PARTY_SCORE2, score2);
updateValues.put(Constantes.KEY_PARTY_CURRENT, false);
Log.d(TAG, "endParty: " + Constantes.KEY_PARTY_ROWID + " : "
+ partyId + "; " + Constantes.KEY_PARTY_SCORE1 + " : "
+ score1 + "; " + Constantes.KEY_PARTY_SCORE2 + " : "
+ score2 + "; ");
int nb = mDb.update(ConstantesDataBase.TABLE_MANCHES, updateValues,
Constantes.KEY_PARTY_ROWID + "=" + partyId, null);
if (nb == 1) {
return nb;
} else {
Log.e(TAG, "endParty: nb " + ConstantesDataBase.TABLE_MANCHES
+ " table entries updated");
// TODO : traitement d'erreur
return -1;
}
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor positioned at the party that matches the given rowId.
*
* @param rowId
* id of party to retrieve
* @return Cursor positioned to matching party, if found
* @throws SQLException
* if party could not be found/retrieved
*/
public final Cursor fetchPartyById(long rowId) throws SQLException {
try {
Cursor mCursor =
mDb.query(true, ConstantesDataBase.TABLE_MANCHES,
new String[] {
Constantes.KEY_PARTY_ROWID,
Constantes.KEY_PARTY_TEAM1ID,
Constantes.KEY_PARTY_TEAM2ID,
Constantes.KEY_PARTY_SCORE1,
Constantes.KEY_PARTY_SCORE2 },
Constantes.KEY_PARTY_ROWID
+ "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor positioned at the party that matches the given teamId.
*
* @param teamId
* id of team to retrieve
* @return Cursor positioned to matching party, if found
* @throws SQLException
* if party could not be found/retrieved
*/
public final Cursor fetchPartyByTeamId(long teamId) throws SQLException {
try {
Cursor mCursor =
mDb.query(true, ConstantesDataBase.TABLE_MANCHES, new String[] {
Constantes.KEY_PARTY_ROWID,
Constantes.KEY_PARTY_TEAM1ID,
Constantes.KEY_PARTY_TEAM2ID,
Constantes.KEY_PARTY_SCORE1,
Constantes.KEY_PARTY_SCORE2 },
Constantes.KEY_PARTY_TEAM1ID
+ "=" + teamId + " OR " + Constantes.KEY_PARTY_TEAM2ID
+ "=" + teamId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Return a Cursor positioned at the current party.
*
* @return Cursor positioned to matching party, if found
* @throws SQLException
* if party could not be found/retrieved
*/
public final Cursor fetchCurrentParty() throws SQLException {
try {
Cursor mCursor =
mDb.query(true, ConstantesDataBase.TABLE_MANCHES, new String[] {
Constantes.KEY_PARTY_ROWID,
Constantes.KEY_PARTY_TEAM1ID,
Constantes.KEY_PARTY_TEAM2ID,
Constantes.KEY_PARTY_SCORE1,
Constantes.KEY_PARTY_SCORE2 },
Constantes.KEY_PARTY_CURRENT
+ "=1", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Create a new player using the id of teams provided. If the party is
* successfully created return the new rowId for that team, otherwise return
* a -1 to indicate failure.
*
* @param name
* the name of the player
* @return the id of the player
*/
public final long createPlayer(String name) {
try {
ContentValues initialValues = new ContentValues();
initialValues.put(Constantes.KEY_PLAYER_NAME, name);
return mDb.insert(ConstantesDataBase.TABLE_PLAYERS,
null,
initialValues);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Update the player using the details provided. The player to be updated is
* specified using the rowId, and it is altered to use the name.
*
* @param rowId
* id of player to update
* @param name
* value to set player name to
* @return true if the player was successfully updated, false otherwise
*/
public final boolean updatePlayer(long rowId, String name) {
try {
ContentValues args = new ContentValues();
args.put(Constantes.KEY_PLAYER_NAME, name);
return mDb.update(ConstantesDataBase.TABLE_PLAYERS,
args,
Constantes.KEY_PLAYER_ROWID + "=" + rowId,
null) > 0;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Delete the player with the given rowId.
*
* @param rowId
* id of player to delete
* @return true if deleted, false otherwise
*/
public final boolean deletePlayer(long rowId) {
try {
return mDb.delete(ConstantesDataBase.TABLE_PLAYERS,
Constantes.KEY_PLAYER_ROWID + "=" + rowId,
null) > 0;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Create a new prise using the id of teams and scores provided. If the
* prise is successfully created return the new rowId for that prise,
* otherwise return a -1 to indicate failure.
*
* @param partyId
* id of the party
* @param number
* prise number in the party
* @param team1Id
* the id of the first player
* @param team2Id
* the id of the second player
* @param score1
* score of first player
* @param score2
* score of second player
* @return the id of the player
*/
public final long createPrise(long partyId, int number, long team1Id,
long team2Id, int score1, int score2) {
try {
ContentValues initialValues = new ContentValues();
initialValues.put(Constantes.KEY_PARTY_NUMBER, number);
initialValues.put(Constantes.KEY_PARTY_PARTYID, partyId);
initialValues.put(Constantes.KEY_PARTY_TEAM1ID, team1Id);
initialValues.put(Constantes.KEY_PARTY_TEAM2ID, team2Id);
initialValues.put(Constantes.KEY_PARTY_SCORE1, score1);
initialValues.put(Constantes.KEY_PARTY_SCORE2, score2);
Log.d(TAG, "createPrise: " + Constantes.KEY_PARTY_NUMBER + " : "
+ number + "; " + Constantes.KEY_PARTY_PARTYID + " : "
+ partyId + "; " + Constantes.KEY_PARTY_TEAM1ID + " : "
+ team1Id + "; " + Constantes.KEY_PARTY_TEAM2ID + " : "
+ team2Id + "; " + Constantes.KEY_PARTY_SCORE1 + " : "
+ score1 + "; " + Constantes.KEY_PARTY_SCORE2 + " : "
+ score2 + "; ");
return mDb.insert(ConstantesDataBase.TABLE_PRISES,
null,
initialValues);
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Renvoie les prises associes une manche.
* @param mancheId identifiant de partie.
* @return curseur sur bdd
* @throws SQLException if the database could be neither opened or created
*/
public final Cursor fetchPrisesByManchesId(long mancheId) throws SQLException {
try {
Cursor mCursor = mDb.query(true,
ConstantesDataBase.TABLE_PRISES,
new String[] {
Constantes.KEY_PRISE_ROWID,
Constantes.KEY_PRISE_NUMBER,
Constantes.KEY_PRISE_PARTYID,
Constantes.KEY_PARTY_TEAM1ID,
Constantes.KEY_PARTY_TEAM2ID,
Constantes.KEY_PARTY_SCORE1,
Constantes.KEY_PARTY_SCORE2 },
Constantes.KEY_PRISE_PARTYID
+ "=" + mancheId, null, null, null,
Constantes.KEY_PRISE_NUMBER, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
/**
* Resets current party.
*
* @return true if deleted, false otherwise
*/
public final boolean resetCurrentParty() {
try {
return mDb.delete(ConstantesDataBase.TABLE_PRISES, null, null) > 0;
} catch (SQLException e) {
Log.e(TAG, "SQLException: " + e.getMessage());
throw e;
}
}
}
|