Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 98 additions & 91 deletions mojoPortal.Data.SQLite/dbPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
using log4net;
using Mono.Data.Sqlite;

namespace mojoPortal.Data
namespace mojoPortal.Data
{
public static class DBPortal

public static class DBPortal
{
// Create a logger for use in this class
private static readonly ILog log = LogManager.GetLogger(typeof(DBPortal));
Expand All @@ -39,31 +39,31 @@ private static string GetConnectionString()
string connectionString = ConfigurationManager.AppSettings["SqliteConnectionString"];
if (connectionString == "defaultdblocation")
{
FileInfo theDb = new FileInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo.db.config"));
FileInfo seedDb = new FileInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo-seed.db.config"));
FileInfo theDb = new FileInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo.db.config"));
FileInfo seedDb = new FileInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo-seed.db.config"));

if (!theDb.Exists && seedDb.Exists && Convert.ToBoolean(ConfigurationManager.AppSettings["TryToCopySQLiteSeedDatabase"].ToString()))
{
seedDb.CopyTo("~/Data/sqlitedb/mojo.db.config");
}
connectionString = "version=3,URI=file:"
+ System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo.db.config");
}
if (!theDb.Exists && seedDb.Exists && Convert.ToBoolean(ConfigurationManager.AppSettings["TryToCopySQLiteSeedDatabase"].ToString()))
{
seedDb.CopyTo("~/Data/sqlitedb/mojo.db.config");
}

connectionString = "version=3,URI=file:"
+ System.Web.Hosting.HostingEnvironment.MapPath("~/Data/sqlitedb/mojo.db.config");
}

return connectionString;

}

public static void EnsureDatabase()
{
}


#region Versioning and Upgrade Helpers




#region Schema Table Methods

Expand Down Expand Up @@ -135,10 +135,10 @@ public static bool SchemaVersionAddSchemaVersion(
arParams[5].Value = revision;

int rowsAffected = SqliteHelper.ExecuteNonQuery(
GetConnectionString(),
sqlCommand.ToString(),
GetConnectionString(),
sqlCommand.ToString(),
arParams);

return (rowsAffected > 0);

}
Expand Down Expand Up @@ -270,7 +270,7 @@ public static IDataReader SchemaVersionGetNonCore()
sqlCommand.Append("ORDER BY ApplicationName ");
sqlCommand.Append(";");


return SqliteHelper.ExecuteReader(
GetConnectionString(),
sqlCommand.ToString(),
Expand Down Expand Up @@ -474,8 +474,8 @@ public static bool SchemaScriptHistoryExists(Guid applicationId, String scriptFi

#endregion

#region DatabaseHelper

#region DatabaseHelper

public static DataTable GetTableFromDataReader(IDataReader reader)
{
Expand Down Expand Up @@ -618,7 +618,7 @@ public static bool DatabaseHelperCanAccessDatabase()

public static bool DatabaseHelperCanAlterSchema(String overrideConnectionInfo)
{

bool result = true;
// Make sure we can create, alter and drop tables

Expand Down Expand Up @@ -663,7 +663,7 @@ public static bool DatabaseHelperCanAlterSchema(String overrideConnectionInfo)
{
result = false;
}


sqlCommand = new StringBuilder();
sqlCommand.Append("DROP TABLE mp_Testdb;");
Expand All @@ -680,7 +680,7 @@ public static bool DatabaseHelperCanAlterSchema(String overrideConnectionInfo)
{
result = false;
}


return result;
}
Expand All @@ -697,7 +697,7 @@ public static bool DatabaseHelperCanCreateTemporaryTables()
{
DatabaseHelperRunScript(sqlCommand.ToString(), GetConnectionString());
}
catch
catch
{
result = false;
}
Expand Down Expand Up @@ -758,32 +758,39 @@ public static bool DatabaseHelperRunScript(String script, String overrideConnect
return result;
}

public static bool DatabaseHelperUpdateTableField(
String connectionString,
String tableName,
String keyFieldName,
String keyFieldValue,
String dataFieldName,
String dataFieldValue,
String additionalWhere)
{
bool result = false;

StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("UPDATE " + tableName + " ");
sqlCommand.Append(" SET " + dataFieldName + " = :fieldValue ");
sqlCommand.Append(" WHERE " + keyFieldName + " = " + keyFieldValue );
sqlCommand.Append(" " + additionalWhere + " ");
sqlCommand.Append(" ; ");

SqliteParameter[] arParams = new SqliteParameter[1];

arParams[0] = new SqliteParameter(":fieldValue", DbType.String);
arParams[0].Direction = ParameterDirection.Input;
arParams[0].Value = dataFieldValue;

SqliteConnection connection = new SqliteConnection(connectionString);
connection.Open();
public static bool DatabaseHelperUpdateTableField(
String connectionString,
String tableName,
String keyFieldName,
String keyFieldValue,
String dataFieldName,
String dataFieldValue,
String additionalWhere)
{
bool result = false;

var csSettings = ConfigurationManager.ConnectionStrings[connectionString];
if (csSettings == null)
{
throw new ArgumentException("Invalid connection string name.", nameof(connectionString));
}
string safeConnectionString = csSettings.ConnectionString;

StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("UPDATE " + tableName + " ");
sqlCommand.Append(" SET " + dataFieldName + " = :fieldValue ");
sqlCommand.Append(" WHERE " + keyFieldName + " = " + keyFieldValue);
sqlCommand.Append(" " + additionalWhere + " ");
sqlCommand.Append(" ; ");

SqliteParameter[] arParams = new SqliteParameter[1];

arParams[0] = new SqliteParameter(":fieldValue", DbType.String);
arParams[0].Direction = ParameterDirection.Input;
arParams[0].Value = dataFieldValue;

SqliteConnection connection = new SqliteConnection(safeConnectionString);
connection.Open();
try
{
int rowsAffected = SqliteHelper.ExecuteNonQuery(connection, sqlCommand.ToString(), arParams);
Expand All @@ -794,9 +801,9 @@ public static bool DatabaseHelperUpdateTableField(
connection.Close();
}

return result;
}
return result;

}

public static bool DatabaseHelperUpdateTableField(
String tableName,
Expand Down Expand Up @@ -837,22 +844,22 @@ public static bool DatabaseHelperUpdateTableField(

}

public static IDataReader DatabaseHelperGetReader(
String connectionString,
String tableName,
String whereClause)
{
StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("SELECT * ");
sqlCommand.Append("FROM " + tableName + " ");
sqlCommand.Append(whereClause);
sqlCommand.Append(" ; ");
public static IDataReader DatabaseHelperGetReader(
String connectionString,
String tableName,
String whereClause)
{
StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("SELECT * ");
sqlCommand.Append("FROM " + tableName + " ");
sqlCommand.Append(whereClause);
sqlCommand.Append(" ; ");

return SqliteHelper.ExecuteReader(
connectionString,
sqlCommand.ToString());
return SqliteHelper.ExecuteReader(
connectionString,
sqlCommand.ToString());

}
}

public static IDataReader DatabaseHelperGetReader(
string connectionString,
Expand Down Expand Up @@ -882,24 +889,24 @@ string query

}

public static DataTable DatabaseHelperGetTable(
String connectionString,
String tableName,
String whereClause)
{
StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("SELECT * ");
sqlCommand.Append("FROM " + tableName + " ");
sqlCommand.Append(whereClause);
sqlCommand.Append(" ; ");
public static DataTable DatabaseHelperGetTable(
String connectionString,
String tableName,
String whereClause)
{
StringBuilder sqlCommand = new StringBuilder();
sqlCommand.Append("SELECT * ");
sqlCommand.Append("FROM " + tableName + " ");
sqlCommand.Append(whereClause);
sqlCommand.Append(" ; ");

DataSet ds = SqliteHelper.ExecuteDataset(
connectionString,
sqlCommand.ToString());
DataSet ds = SqliteHelper.ExecuteDataset(
connectionString,
sqlCommand.ToString());

return ds.Tables[0];
return ds.Tables[0];

}
}

public static void DatabaseHelperDoForumVersion2202PostUpgradeTasks(
String overrideConnectionInfo)
Expand Down Expand Up @@ -1090,7 +1097,7 @@ public static void DatabaseHelperDoVersion2320PostUpgradeTasks(

foreach (DataRow row in dataTable.Rows)
{

DBLetterSubscription.Create(
Guid.NewGuid(),
new Guid(row["SiteGuid"].ToString()),
Expand Down Expand Up @@ -1431,11 +1438,11 @@ public static bool DatabaseHelperTableExists(string tableName)
return false;
}



#endregion


#endregion



//#region Private Message System

Expand Down Expand Up @@ -2237,9 +2244,9 @@ public static bool DatabaseHelperTableExists(string tableName)

//#endregion







}
}