Initial Commit

This commit is contained in:
2016-07-21 16:55:03 -04:00
commit b6d5ec4ece
64 changed files with 7870 additions and 0 deletions

135
Systems/SQLCE/Credential.cs Normal file
View File

@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// Handles SQL CE Credentials, to be
/// used by all our SQL CE Functions
/// </summary>
public class SQLCECredential : IComparable, ICloneable, IConnectDb
{
#region IConnectDb
/// <summary>
/// Let Callers know this is a SQLCE Connection Object
/// </summary>
public DBSystem DBType { get { return DBSystem.SQL_CE; } }
/// <summary>
/// Check to see if the Credential Object consists of valid input
/// </summary>
public bool IsValid
{
get
{
if (!ValidationConsts.IsValidFileNameNPath(DataSource))
return false;
if (!String.IsNullOrEmpty(_UDL.Password) && !ValidationConsts.Generic.IsValidPassword(_UDL.Password))
return false;
return true;
}
}
/// <summary>
///
/// </summary>
public string ConnectionString { get { return _UDL.ConnectionString; } }
/// <summary>
/// SQL CE uses the File Name and Path as the Datasource
/// </summary>
public string DataSource { get { return _UDL.DataSource; } }
/// <summary>
/// SQL CE doesn't use User
/// </summary>
public string User { get { return _UDL.Username; } }
/// <summary>
/// SQL CE doesn't support DBMS like behaviors
/// </summary>
public bool SupportsDBMS { get { return false; } }
/// <summary>
/// SQL CE doesn't support DBMS like behaviors
/// </summary>
public IamDBMS DBMS { get { return null; } }
/// <summary>
/// SQL CE doesn't support Timeouts
/// </summary>
public bool SupportsTimeouts { get { return false; } }
/// <summary>
/// SQL CE doesn't support Timeouts
/// </summary>
public IsupportTimeouts Timeouts { get { return null; } }
#endregion
private UDL _UDL = null;
#region SQLCE Credential Constructors
/// <summary>
/// Create a SQL CE Connection from an UDL Object
/// </summary>
/// <param name="udl"></param>
internal SQLCECredential(UDL udl)
{
if (udl != null)
_UDL = udl;
}
/// <summary>
/// Create an Untrusted SQLCE Credential * No Encryption Used *
/// </summary>
public SQLCECredential(string FileNameNPath)
{
_UDL = new UDL(FileNameNPath, String.Empty);
}
/// <summary>
/// Create a Trusted SQLCE Credential * Encryption Used *
/// </summary>
public SQLCECredential(string FileNameNPath, string strPassword)
{
_UDL = new UDL(FileNameNPath, strPassword);
}
#endregion
#region ICloneable Members
public object Clone()
{
SQLCECredential credential = new SQLCECredential((UDL)this._UDL.Clone());
return credential;
}
#endregion
#region IComparable Members
public int CompareTo(object obj)
{
SQLCECredential otherCredential = obj as SQLCECredential;
if (otherCredential != null)
{
int nCompare = _UDL.CompareTo(otherCredential._UDL);
return nCompare;
}
else
{
throw new ArgumentException("Object is not a SQLCECredential");
}
}
#endregion
}
}

247
Systems/SQLCE/Database.cs Normal file
View File

@@ -0,0 +1,247 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlServerCe;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// Specific actions for SQL CE Databases
/// </summary>
public class SQLCEDatabase
{
#region Database Create / Exists
/// <summary>
/// Checks to see if we can query the specified server for the Database (Existence check)
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <param name="bExists">Returns true if the Database Exists, false otherwise</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseExists(IConnectDb credential, out bool bExists)
{
bExists = false;
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Check for File Existence
if (File.Exists(credential.DataSource))
{
bExists = true;
return dbError;
}
else
return DBError.Create("Database File does not exist");
}
/// <summary>
/// Create a new Database with the specified Credentials
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseCreate(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// First Create the Directory if it doesn't exists
string path = Path.GetDirectoryName(credential.DataSource);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
// Now try to create the database if it doesn't exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (!bExists)
{
try
{
using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
{
engine.CreateDatabase();
}
}
catch (SqlCeException e)
{
dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
}
}
return dbError;
}
#endregion
#region Database Copy / Delete
/// <summary>
/// Copies a specified SQL CE Database to a new specified Database
/// </summary>
/// <param name="SourceCredential">SQL CE Credentials of Source Database</param>
/// <param name="DestinationCredential">SQLCE Credential of Destination Database To be copied To</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseCopy(IConnectDb SourceCredential, IConnectDb DestinationCredential)
{
DBError dbError = ValidationConsts.IsCredentialValid(SourceCredential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
dbError = ValidationConsts.IsCredentialValid(DestinationCredential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Make sure the Source Database exists
bool bExists = false;
dbError = DatabaseExists(SourceCredential, out bExists);
if (!bExists)
return dbError;
// Make sure the Destination Database does NOT Exist
bExists = false;
dbError = DatabaseExists(DestinationCredential, out bExists);
if (bExists)
return DBError.Create("Destination Credential Database already exists");
try
{
File.Copy(SourceCredential.DataSource, DestinationCredential.DataSource);
}
catch (Exception ex)
{
dbError = DBError.Create(ex.Message);
}
return dbError;
}
/// <summary>
/// Delete a specified SQLCE Database
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseDelete(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Make sure the Source Database exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (!bExists)
return dbError;
try
{
File.Delete(credential.DataSource);
}
catch (Exception ex)
{
dbError = DBError.Create(ex.Message);
}
return dbError;
}
#endregion
#region Database Integrity
/// <summary>
/// Shrinks the specified Database to reclaim wasted space
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseShrink(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Now try to shrink the database if it exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (bExists)
{
try
{
using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
{
engine.Shrink();
}
}
catch (SqlCeException e)
{
dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
}
}
return dbError;
}
/// <summary>
/// Verifies the specified Database in case it is corrupted
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseVerify(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Now try to repair the database if it exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (bExists)
{
try
{
using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
{
engine.Verify();
}
}
catch (SqlCeException e)
{
dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
}
}
return dbError;
}
/// <summary>
/// Repairs the specified Database in case it is corrupted
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError DatabaseRepair(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Now try to repair the database if it exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (bExists)
{
try
{
using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
{
// Specify null destination connection string for in-place repair
engine.Repair(null, RepairOption.RecoverAllPossibleRows);
}
}
catch (SqlCeException e)
{
dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
}
}
return dbError;
}
#endregion
}
}

41
Systems/SQLCE/General.cs Normal file
View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// General SQL CE Functions
/// </summary>
public class SQLCEGeneral
{
/// <summary>
/// Quick Check function to see if the passed in credential allows us to connect to the database,
/// It also verifies that there are any Tables in the Database
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <param name="CanConnect">Returns true if successfull in connecting to the database</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError SQLCECheckConnection(IConnectDb credential, out bool CanConnect)
{
CanConnect = false;
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// First Verify that the Database Exists
bool bExists = false;
dbError = SQLCEDatabase.DatabaseExists(credential, out bExists);
if (!bExists)
return dbError;
////
// TODO: Make sure that we can read any tables from the db
////
return null;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// SQL CE Database Error Constants to Use to handle specific Errors
/// </summary>
internal static class ErrorConst
{
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sdaleo.Systems;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// SQL CE Custom Validation
/// </summary>
internal static class Validation
{
/// <summary>
/// validate SQL CE Table Names
/// </summary>
/// <param name="strTableName"></param>
/// <returns></returns>
public static DBError IsValidTableName(string strTableName)
{
if (string.IsNullOrEmpty(strTableName))
{
return DBError.Create("TableName is null");
}
else if (strTableName.Length < 1 || strTableName.Length > 52) // limit Table Name length
{
return DBError.Create("TableName length is invalid");
}
else if (!ValidationConsts.ContainsOnlyLegalChars(strTableName, ValidationConsts.CharSet_AllowedTableNames))
{
return DBError.Create("TableName contains illegal characters");
}
else
{
return new DBError();
}
}
}
}

95
Systems/SQLCE/Table.cs Normal file
View File

@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Sdaleo.Systems.SQLCE
{
/// <summary>
/// Specific actions for SQL CE Tables in a Database
/// </summary>
public class SQLCETable
{
/// <summary>
/// Returns all the Table Names for the specified SQL CE Credential
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <param name="Tables">array of table names, or null if error occured</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError Tables(IConnectDb credential, out string[] Tables)
{
Tables = null;
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Execute the Query
string sql = "Select [TABLE_NAME] From INFORMATION_SCHEMA.TABLES Order By [TABLE_NAME]";
DB db = DB.Create(credential);
DBRetVal retVal = db.FillDataTable(sql);
if (retVal.IsValid)
{
List<String> retList = new List<string>();
foreach (DataRow row in retVal.GetDataTableRetVal().Rows)
retList.Add(row["TABLE_NAME"].ToString());
Tables = retList.ToArray();
}
return retVal;
}
/// <summary>
/// Check to see if the Table Exists on the specified SQL CE Credentials
/// </summary>
/// <param name="credential">SQL CE Credentials</param>
/// <param name="TableName">Specify the TableName to check for (required)</param>
/// <param name="bExists">true if exists, false otherwise</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError TableExists(IConnectDb credential, string TableName, out bool bExists)
{
bExists = false;
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
dbError = Validation.IsValidTableName(TableName);
if (dbError.ErrorOccured)
return dbError;
// Execute the Query
string sql = String.Format("Select [TABLE_NAME] From INFORMATION_SCHEMA.TABLES Where [TABLE_NAME] = '{0}' Order By [TABLE_NAME]", TableName);
DB db = DB.Create(credential);
DBRetVal retVal = db.ExecuteScalar(sql);
bExists = !retVal.IsValid;
return retVal;
}
/// <summary>
/// Changes the Table Name for the specified Table
/// </summary>
/// <param name="credential">SSQL CE Credentials</param>
/// <param name="TableName">Name of Table (required)</param>
/// <param name="TableNameNew">Name of Table New (required)</param>
/// <returns>DBError Object with ErrorOccured, if error Occured</returns>
public static DBError TableRename(IConnectDb credential, string TableName, string TableNameNew)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
dbError = Validation.IsValidTableName(TableName);
if (dbError.ErrorOccured)
return dbError;
dbError = Validation.IsValidTableName(TableNameNew);
if (dbError.ErrorOccured)
return dbError;
// Execute the Query
string sql = String.Format("sp_rename @objname = '{0}', @newname = '{1}', @objtype = 'OBJECT'", TableName, TableNameNew);
DB db = DB.Create(credential);
DBRetVal retVal = db.ExecuteNonQuery(sql);
return retVal;
}
}
}