2 using System.Collections.Generic;
6 using System.ComponentModel;
15 public static class EFLite
30 public static void Insert(
this Object obj)
32 obj.Insert(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table"));
40 public static void Insert(
this Object obj,
string _DataBase,
string _Table)
42 Tuple<string, string> ret = obj.GetNamesValues();
43 string lcSql =
string.Format(
"INSERT INTO " + DB.SQLDatabase(_DataBase, _Table) +
" ({0})" +
" VALUES ({1})",ret.Item1,ret.Item2);
50 public static void InsertOrUpdate(
this Object obj)
52 string _DataBase = obj.GetPropertyByName<
string>(
"_DataBase");
53 string _Table = obj.GetPropertyByName<
string>(
"_Table").ToString();
54 string where = obj.GetWhere();
56 if(obj.Exists(_DataBase, _Table, where))
58 if (!
string.IsNullOrEmpty(where))
60 obj.Update(_DataBase, _Table, where);
76 public static bool Exists(
this Object obj,
string _DataBase,
string _Table,
string where)
78 DataTable dt =
new DataTable();
79 string lcSql =
"SELECT count(*) as existe " +
" FROM " + DB.SQLDatabase(_DataBase, _Table) +
81 DB.SQLExec(lcSql, ref dt);
82 return Convert.ToInt32(dt.Rows[0][
"existe"]) > 0;
93 public static Int32 NewStrId(
this Object obj,
string field, Int32 inc = 1,
string where =
"")
97 string lcSql =
"Select max(CAST(" + field +
" AS INT)) as maximo from " +
98 DB.SQLDatabase(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table"));
99 if (!
string.IsNullOrEmpty(where))
101 lcSql +=
" WHERE " + where;
104 DataTable dt =
new DataTable();
105 DB.SQLExec(lcSql, ref dt);
106 if (dt.Rows.Count > 0)
108 if (dt.Rows[0][
"maximo"] == DBNull.Value)
114 ret += Convert.ToInt32(dt.Rows[0][
"maximo"]);
128 public static Int32 NewId(
this Object obj,
string field, Int32 inc=1,
string where=
"")
132 string lcSql =
"Select max(" + field +
") as maximo from " +
133 DB.SQLDatabase(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table"));
134 if (!
string.IsNullOrEmpty(where))
136 lcSql +=
" WHERE " + where;
139 DataTable dt =
new DataTable();
140 DB.SQLExec(lcSql, ref dt);
141 if (dt.Rows.Count > 0)
143 if (dt.Rows[0][
"maximo"] == DBNull.Value)
149 ret += Convert.ToInt32(dt.Rows[0][
"maximo"]);
160 public static bool IsNullOrEmpty(
this Object obj)
163 string myType = obj.GetType().Name.ToLower();
168 ret = obj == null || ((string)obj) ==
"";
173 ret = obj == null || ((DateTime)obj) == DateTime.MinValue;
193 public static void Delete(
this Object obj)
195 DataTable dt =
new DataTable();
196 string[] keys = obj.GetPropertyByName<
string[]>(
"_keys");
197 StringBuilder sb =
new StringBuilder();
198 List<string> _flds = (List<string>)obj.GetType().GetField(
"_flds").GetValue(obj);
200 foreach (
string key
in keys)
202 PropertyInfo prop = obj.GetPropertyInfoByName(key);
203 object valor = prop.GetValue(obj, null);
204 if (_flds.Contains(prop.Name))
206 sb.Append(prop.Name +
" = " + FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
" and ");
209 sb.Remove(sb.Length - 4, 4);
210 obj.Delete(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table"), sb.ToString());
219 public static void Delete(
this Object obj,
string _DataBase,
string _Table,
string where)
221 string lcSql =
"DELETE FROM " + DB.SQLDatabase(_DataBase, _Table);
223 if (!
string.IsNullOrEmpty(where))
225 lcSql +=
" WHERE " + where;
234 public static void Delete(
this Object obj,
string where)
236 obj.Delete(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table"), where);
245 public static void Update(
this Object obj,
string _DataBase,
string _Table,
string where)
247 string lcSql =
" UPDATE " + DB.SQLDatabase(_DataBase, _Table) +
248 " SET " +
"%%CAMPOSVALORES%%" +
251 StringBuilder sbCamposValores =
new StringBuilder();
252 Type myType = obj.GetType();
253 IList<PropertyInfo> props =
new List<PropertyInfo>(myType.GetProperties().Where(r => r.Name.Substring(0, 1) !=
"_"));
254 List<string> _flds = (List<string>)obj.GetType().GetField(
"_flds").GetValue(obj);
256 foreach (PropertyInfo prop
in props)
258 AttributeCollection attributes = TypeDescriptor.GetProperties(obj)[prop.Name].Attributes;
260 object valor = prop.GetValue(obj, null);
263 if (_flds.Contains(prop.Name))
265 sbCamposValores.Append(prop.Name +
" = " + FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
" , ");
283 sbCamposValores.Remove(sbCamposValores.Length - 2, 2);
284 lcSql = lcSql.Replace(
"%%CAMPOSVALORES%%", sbCamposValores.ToString());
293 public static void Update(
this Object obj)
295 string _DataBase = obj.GetPropertyByName<
string>(
"_DataBase");
296 string _Table = obj.GetPropertyByName<
string>(
"_Table").ToString();
297 string lcSql =
" UPDATE " + DB.SQLDatabase(_DataBase, _Table) +
298 " SET " +
"%%CAMPOSVALORES%%" +
301 StringBuilder sbKeys =
new StringBuilder();
302 string[] keys = GetPropertyByName<string[]>(obj,
"_keys");
303 if (keys.Length == 0)
305 List<string> _flds = (List<string>)obj.GetType().GetField(
"_flds").GetValue(obj);
309 StringBuilder sb =
new StringBuilder();
310 foreach (
string key
in keys)
312 PropertyInfo prop = obj.GetPropertyInfoByName(key);
313 object valor = prop.GetValue(obj, null);
315 if(_flds.Contains(prop.Name))
317 sb.Append(prop.Name +
" = " + FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
" and ");
320 sb.Remove(sb.Length - 4, 4);
321 obj.Update(_DataBase, _Table, sb.ToString());
328 public static void Load(
this Object obj, DataRow row)
330 foreach (DataColumn column
in row.Table.Columns)
332 Type _type = Type.GetType(obj.GetType().Namespace +
"." + obj.GetType().Name);
333 PropertyInfo _propertyInfo = _type.GetProperty(column.ColumnName.ToUpper());
335 if (_propertyInfo != null && row[column.ColumnName] != DBNull.Value)
337 object propertyValue =
System.Convert.ChangeType(row[column.ColumnName], _propertyInfo.PropertyType);
338 _propertyInfo.SetValue(obj, propertyValue, null);
349 public static bool Find(
this Object obj,
string where =
"")
351 DataTable dt =
new DataTable();
352 string lcSql =
" SELECT Count(*) as num FROM " + DB.SQLDatabase(obj.GetPropertyByName<
string>(
"_DataBase"), obj.GetPropertyByName<
string>(
"_Table")) +
" WHERE ";
353 if (
string.IsNullOrEmpty(where))
355 string[] keys = obj.GetPropertyByName<
string[]>(
"_keys");
356 StringBuilder sb =
new StringBuilder();
357 foreach (
string key
in keys)
359 PropertyInfo prop = obj.GetPropertyInfoByName(key);
360 object valor = prop.GetValue(obj, null);
361 if (!valor.IsNullOrEmpty())
363 sb.Append(prop.Name +
" = " + FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
" and ");
366 sb.Remove(sb.Length - 4, 4);
367 lcSql += sb.ToString();
373 DB.SQLExec(lcSql, ref dt);
375 return dt.Rows.Count > 0 && Convert.ToInt32(dt.Rows[0][
"num"]) > 0;
389 public static DataTable GetDataTable(
this Object obj,
string[] columns = null,
string where =
"",
string joins=
"",
string orderby =
"")
391 string _DataBase = obj.GetPropertyByName<
string>(
"_DataBase");
392 string _Table = obj.GetPropertyByName<
string>(
"_Table").ToString();
393 DataTable ret =
new DataTable();
394 StringBuilder sb =
new StringBuilder();
397 Type myType = obj.GetType();
398 IList<PropertyInfo> props =
new List<PropertyInfo>(myType.GetProperties().Where(r => r.Name.Substring(0, 1) !=
"_"));
400 foreach (PropertyInfo prop
in props)
402 sb.Append(prop.Name +
", ");
407 foreach (
string column
in columns)
409 sb.Append(column +
", ");
412 sb.Remove(sb.Length - 2, 2);
414 string lcSql =
"SELECT " + sb.ToString() +
" FROM " + DB.SQLDatabase(_DataBase, _Table) +
" A ";
415 if (!
string.IsNullOrEmpty(joins))
419 if (!
string.IsNullOrEmpty(where))
421 lcSql +=
" where " + where;
423 if (!
string.IsNullOrEmpty(orderby))
425 lcSql +=
" ORDER BY " + orderby;
427 DB.SQLExec(lcSql, ref ret);
439 public static BindingList<T> ToBindingList<T>(
this Object obj,
string[] columns = null,
string where =
"",
string join =
"")
441 BindingList<T> ret =
new BindingList<T>();
442 DataTable dt = obj.GetDataTable(columns, where, join );
443 List<T> s = dt.ToLista<T>();
444 return new BindingList<T>(s);
455 public static List<T> ToLista<T>(
this Object obj,
string[] columns = null,
string where =
"",
string join =
"")
457 BindingList<T> ret =
new BindingList<T>();
458 DataTable dt = obj.GetDataTable(columns, where, join);
460 return dt.ToLista<T>();
468 public static List<T> ToLista<T>(
this DataTable dt)
470 List<T> data =
new List<T>();
471 foreach (DataRow row
in dt.Rows)
473 T item = GetItem<T>(row);
484 public static T GetItem<T>(DataRow dr)
486 Type temp = typeof(T);
487 T obj = Activator.CreateInstance<T>();
489 foreach (DataColumn column
in dr.Table.Columns)
491 foreach (PropertyInfo pro
in temp.GetProperties())
493 if (pro.Name == column.ColumnName)
495 pro.SetValue(obj, (dr[column.ColumnName] == DBNull.Value?null: dr[column.ColumnName]), null);
504 private static string FormatValue(
string propertyType,
object propValue)
507 propertyType = propertyType.Replace(
"system.",
"");
508 if (propertyType ==
"int" ||
509 propertyType ==
"int32" ||
510 propertyType ==
"decimal" ||
511 propertyType ==
"numeric")
513 ret = propValue.ToString().Replace(
",",
".");
516 if (propertyType ==
"string" ||
517 propertyType ==
"datetime" ||
518 propertyType ==
"char" ||
519 propertyType ==
"smalldatetime" ||
520 propertyType ==
"text" ||
521 propertyType ==
"ntext")
523 ret = DB.SQLString(propValue);
526 if (propertyType ==
"bool" ||
527 propertyType ==
"boolean" ||
528 propertyType ==
"bit")
530 ret = Convert.ToBoolean(propValue) ?
"1" :
"0";
547 public static T GetPropertyByName<T>(
this object input,
string name)
552 .GetValue(input, null);
561 public static PropertyInfo GetPropertyInfoByName(
this object input,
string name)
565 IList<PropertyInfo> props =
new List<PropertyInfo>(input.GetType().GetProperties());
567 info = (PropertyInfo)props.Single(x => x.Name == name.Trim());
576 public static string GetWhere(
this Object obj)
578 string[] keys = GetPropertyByName<string[]>(obj,
"_keys");
579 if (keys.Length == 0)
583 StringBuilder sb =
new StringBuilder();
584 foreach (
string key
in keys)
586 PropertyInfo prop = obj.GetPropertyInfoByName(key);
587 object valor = prop.GetValue(obj, null);
588 if (!valor.IsNullOrEmpty())
590 sb.Append(prop.Name +
" = " + FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
" and ");
593 sb.Remove(sb.Length - 4, 4);
594 return sb.ToString();
601 public static string GetWhereExt(
this Object obj)
625 public static Tuple<string, string> GetNamesValues(
this Object obj)
627 StringBuilder sbNames =
new StringBuilder();
628 StringBuilder sbValues =
new StringBuilder();
629 Type myType = obj.GetType();
630 IList<PropertyInfo> props =
new List<PropertyInfo>(myType.GetProperties().Where(r => r.Name.Substring(0, 1) !=
"_"));
631 List<string> _flds = (List<string>)obj.GetType().GetField(
"_flds").GetValue(obj);
633 foreach (PropertyInfo prop
in props)
635 object valor = prop.GetValue(obj, null);
640 if (_flds.Contains(prop.Name))
642 sbNames.Append(prop.Name +
", ");
643 sbValues.Append(FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
", ");
648 switch (prop.PropertyType.Name)
652 DateTime dateTime =
new DateTime(1, 1, 1, 0, 0, 0);
653 if (prop.PropertyType.Name ==
"DateTime" && dateTime.CompareTo(Convert.ToDateTime(valor)) != 0)
655 sbNames.Append(prop.Name +
", ");
656 sbValues.Append(FormatValue(prop.PropertyType.ToString().ToLower(), valor) +
", ");
664 sbNames.Remove(sbNames.Length - 2, 2);
665 sbValues.Remove(sbValues.Length - 2, 2);
668 return new Tuple<string,string>(sbNames.ToString(), sbValues.ToString());
684 public static string InnerJoin(
this Object obj,
string asobj,
string rel1, Object obj2,
string asobj2,
string rel2)
700 public static T GetFieldValue<T>(
this Object obj,
string fieldName,
string where)
704 DataTable dt = obj.GetDataTable(
new string[] { fieldName }, where);
706 if (dt != null && dt.Rows.Count == 1)
708 ret = (T)Convert.ChangeType(dt.Rows[0][fieldName], typeof(T) );
719 public static void AddExt<T>(
this List<T> lista, T dato)
721 if (!lista.Contains(dato))
Es como el tipo de entrada asientos pero por negocio, sin formulario, pq quiero que me haga las propu...
Consulta de una declaración existente