sábado, 19 de dezembro de 2009

Jade ORM ganha sinais de vida...

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Configuration;

using System.Data;

using System.Collections;

using System.Reflection;

namespace Jade.ORM

{

public static class DBNullConvert

{

public enum DateInterval { Second, Minute, Hour, Day, Week, Month, Quarter, Year }

public static bool ToBoolean(object value)

{

if (value is DBNull)

return false;

else

return Convert.ToBoolean(value);

}

public static string ToString(object value)

{

if (value is DBNull)

return string.Empty;

else

return value.ToString();

}

public static int ToInt(object value)

{

if (value is DBNull)

return 0;

else

return Convert.ToInt32(value);

}

public static float ToFloat(object Value)

{

if (Value is DBNull)

return 0;

else

return (float)Convert.ToDouble(Value);

}

public static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)

{

long lngDateDiffValue = 0;

System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);

switch (Interval)

{

case DateInterval.Day:

lngDateDiffValue = (long)TS.Days;

break;

case DateInterval.Hour:

lngDateDiffValue = (long)TS.TotalHours;

break;

case DateInterval.Minute:

lngDateDiffValue = (long)TS.TotalMinutes;

break;

case DateInterval.Month:

lngDateDiffValue = (long)(TS.Days / 30);

break;

case DateInterval.Quarter:

lngDateDiffValue = (long)((TS.Days / 30) / 3);

break;

case DateInterval.Second:

lngDateDiffValue = (long)TS.TotalSeconds;

break;

case DateInterval.Week:

lngDateDiffValue = (long)(TS.Days / 7);

break;

case DateInterval.Year:

lngDateDiffValue = (long)(TS.Days / 365);

break;

}

return lngDateDiffValue;

}

public static bool CompareData(DateTime dataIni, DateTime dataEnd, int Minutes)

{

dataIni = dataIni.AddMinutes(Minutes);

if (dataIni.Ticks <= dataEnd.Ticks)

return true;

else

return false;

}

}

///

/// Proxy to DataBase

///

public class DataBaseController

{

#region ShowErrorMessage

///

/// Shows the appropriate error message

///

///

///

public static string ShowErrorMessage(SqlException ex)

{

switch (ex.Number)

{

case 514:

return string.Format(Message.getMessage(Message.IDMessage.MSG_RECORD_DUPLICATE), ex.Number.ToString());

case 515:

return string.Format(Message.getMessage(Message.IDMessage.MSG_REQUERIED_FIELDS), ex.Number.ToString(), ex.Procedure);

case 2601:

return string.Format(Message.getMessage(Message.IDMessage.MSG_RECORD_DUPLICATE), ex.Number.ToString());

default:

return string.Format(Message.getMessage(Message.IDMessage.MSG_PROCEDURE_ERROR), ex.Number.ToString(), ex.Procedure);

}

}

#endregion

#region GetConnection

///

/// Get Connection

///

///

public static SqlConnection GetConnection()

{

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

return con;

}

#endregion

#region GetObjectParameters

///

/// Get Parameters

///

///

///

///

public static IDataParameter[] GetObjectParameters(string ProcedureTarget, object objBusiness)

{

ArrayList list = new ArrayList();

NullabledAttribute nullAtributte = null;

foreach (PropertyInfo info in objBusiness.GetType().GetProperties())

{

object[] customAttributes = info.GetCustomAttributes(typeof(NullabledAttribute), false);

if (customAttributes.Length > 0)

{

foreach (object oAtributte in customAttributes)

{

if (oAtributte is NullabledAttribute)

{

nullAtributte = (NullabledAttribute)oAtributte;

}

}

}

bool flag = true;

customAttributes = info.GetCustomAttributes(typeof(CmdProcedureTargetAttribute), false);

if (customAttributes.Length > 0)

{

foreach (object objcmdProcedureTarget in customAttributes)

{

if (objcmdProcedureTarget is CmdProcedureTargetAttribute)

{

flag = ((CmdProcedureTargetAttribute)objcmdProcedureTarget).ExistProcedure(ProcedureTarget);

}

}

}

if (flag)

{

customAttributes = info.GetCustomAttributes(typeof(CmdSQLParamAttribute), false);

if (customAttributes.Length > 0)

{

foreach (object objCustomAtributte in customAttributes)

{

if (objCustomAtributte is CmdSQLParamAttribute)

{

IDataParameter parameter = ((CmdSQLParamAttribute)objCustomAtributte).GetParameter();

parameter.Value = info.GetValue(objBusiness, null);

if ((nullAtributte != null) && nullAtributte.IsNull(parameter.Value))

{

parameter.Value = DBNull.Value;

}

list.Add(parameter);

}

}

}

}

}

if (list.Count > 0)

{

IDataParameter[] array = new IDataParameter[list.Count];

list.CopyTo(array, 0);

return array;

}

return null;

}

#endregion

#region Save

///

/// Save Object in Storage

///

///

public static int Save(string USP_Save, object baseObject)

{

Type tp = baseObject.GetType();

SqlConnection con = DataBaseController.GetConnection();

try

{

DataTable result = new DataTable(tp.Name);

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(USP_Save + tp.Name, con))

{

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

foreach (IDataParameter parameter2 in DataBaseController.GetObjectParameters(USP_Save, baseObject))

{

adapter.SelectCommand.Parameters.Add(parameter2);

}

adapter.Fill(result);

}

if (result.Rows.Count > 0)

return Convert.ToInt32(result.Rows[0]["id"]);

}

finally

{

if (con.State != ConnectionState.Closed)

con.Close();

}

return 0;

}

#endregion

#region Pick

///

/// Pick Object

///

///

///

public static void Pick(string USP_Pick, object baseObject)

{

SqlConnection con = DataBaseController.GetConnection();

Type tp = baseObject.GetType();

try

{

using (DataTable dataTableResult = new DataTable(tp.Name))

{

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(USP_Pick + tp.Name, con))

{

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

foreach (IDataParameter parameter2 in DataBaseController.GetObjectParameters(USP_Pick, baseObject))

{

adapter.SelectCommand.Parameters.Add(parameter2);

}

adapter.Fill(dataTableResult);

}

if (dataTableResult.Rows.Count > 0)

{

foreach (PropertyInfo info in baseObject.GetType().GetProperties())

{

if (dataTableResult.Columns[info.Name]!=null)

info.SetValue(baseObject, dataTableResult.Rows[0][info.Name], null);

}

}

}

}

finally

{

if (con.State != ConnectionState.Closed)

con.Close();

}

}

#endregion

#region Search

///

/// Search Object

///

///

public static List Search(string USP_Search, object baseObject)

{

List oList = new List();

Type tp = baseObject.GetType();

using (DataTable dataTableResult = SearchTable(USP_Search,baseObject))

{

if (dataTableResult.Rows.Count > 0)

foreach (DataRow row in dataTableResult.Rows)

{

System.Object obj = Activator.CreateInstance(tp);

foreach (PropertyInfo info in tp.GetProperties())

{

object[] customAttributes = info.GetCustomAttributes(typeof(CmdSQLParamAttribute), false);

if (customAttributes.Length > 0)

{

foreach (object objcmdCustomAtributte in customAttributes)

{

if (objcmdCustomAtributte is CmdSQLParamAttribute)

{

IDataParameter parameter = ((CmdSQLParamAttribute)objcmdCustomAtributte).GetParameter();

if (dataTableResult.Columns[parameter.ParameterName.Replace("@", string.Empty)] != null)

info.SetValue(obj, row[parameter.ParameterName.Replace("@", string.Empty)], null);

}

}

}

}

oList.Add((T)obj);

}

}

return oList;

}

#endregion

#region Search

///

/// Search Object

///

///

public static DataTable LookupTable(string USP_LookUp, object baseObject)

{

Type tp = baseObject.GetType();

SqlConnection con = DataBaseController.GetConnection();

try

{

using (DataTable dataTableResult = new DataTable(tp.Name))

{

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(USP_LookUp + tp.Name, con))

{

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

adapter.Fill(dataTableResult);

return dataTableResult;

}

}

}

finally

{

if (con.State != ConnectionState.Closed)

con.Close();

}

}

#endregion

#region Search

///

/// Search Object

///

///

public static DataTable SearchTable(string USP_Search, object baseObject)

{

Type tp = baseObject.GetType();

SqlConnection con = DataBaseController.GetConnection();

try

{

using (DataTable dataTableResult = new DataTable(tp.Name))

{

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(USP_Search + tp.Name, con))

{

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

foreach (IDataParameter parameter2 in DataBaseController.GetObjectParameters(USP_Search, baseObject))

{

adapter.SelectCommand.Parameters.Add(parameter2);

}

adapter.Fill(dataTableResult);

return dataTableResult;

}

}

}

finally

{

if (con.State != ConnectionState.Closed)

con.Close();

}

}

#endregion

#region Delete

///

/// Logical Delete Current Object

///

///

public static bool Delete(string USP_Delete, object baseObject)

{

Type tp = baseObject.GetType();

SqlConnection con = DataBaseController.GetConnection();

try

{

DataTable result = new DataTable(tp.Name);

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(USP_Delete + tp.Name, con))

{

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

foreach (IDataParameter parameter2 in DataBaseController.GetObjectParameters(USP_Delete, baseObject))

{

adapter.SelectCommand.Parameters.Add(parameter2);

}

adapter.Fill(result);

}

if (result.Rows.Count > 0)

return Convert.ToBoolean(result.Rows[0]["ret"]);

}

finally

{

if (con.State != ConnectionState.Closed)

con.Close();

}

return false;

}

#endregion

}

}

Postar um comentário