zoukankan      html  css  js  c++  java
  • petapoco IsNew

    Singleton<SqlServer2012DatabaseType>.Instance
    SqlClientFactory.Instance
    SqlServerDatabase

    public Database(string connectionString, DatabaseType databaseType, DbProviderFactory provider, IsolationLevel? isolationLevel = null, bool enableAutoSelect = DefaultEnableAutoSelect)
    {
    EnableAutoSelect = enableAutoSelect;
    KeepConnectionAlive = false;

    _sharedConnection = default!;
    _connectionString = connectionString;
    _factory = provider;
    _dbType = databaseType ?? DatabaseType.Resolve(_factory.GetType().Name, null);
    _providerName = _dbType.GetProviderName();
    _isolationLevel = isolationLevel ?? _dbType.GetDefaultTransactionIsolationLevel();
    _paramPrefix = _dbType.GetParameterPrefix(_connectionString);
    }


    // Check if a poco represents a new record public bool IsNew(string primaryKeyName, object poco) { var pd = PocoData.ForObject(poco, primaryKeyName); object pk; PocoColumn pc; if (pd.Columns.TryGetValue(primaryKeyName, out pc)) { pk = pc.GetValue(poco); } #if !PETAPOCO_NO_DYNAMIC else if (poco.GetType() == typeof(System.Dynamic.ExpandoObject)) { return true; } #endif else if (primaryKeyName.Contains(",")) { return primaryKeyName.Split(',') .Select(pkPart => GetValue(pkPart, poco)) .Any(pkValue => IsDefaultOrNull(pkValue)); } else { pk = GetValue(primaryKeyName, poco); } return IsDefaultOrNull(pk); } private static object GetValue(string primaryKeyName, object poco) { object pk; var pi = poco.GetType().GetProperty(primaryKeyName); if (pi == null) throw new ArgumentException( string.Format("The object doesn't have a property matching the primary key column name '{0}'", primaryKeyName)); pk = pi.GetValue(poco, null); return pk; } private static bool IsDefaultOrNull(object pk) { if (pk == null) return true; var type = pk.GetType(); if (type.IsValueType) { // Common primary key types if (type == typeof(long)) return (long)pk == default(long); else if (type == typeof(ulong)) return (ulong)pk == default(ulong); else if (type == typeof(int)) return (int)pk == default(int); else if (type == typeof(uint)) return (uint)pk == default(uint); else if (type == typeof(Guid)) return (Guid)pk == default(Guid); // Create a default instance and compare return pk == Activator.CreateInstance(pk.GetType()); } else { return pk == null; } }
  • 相关阅读:
    asp.net生命周期
    中国互联网公司数据库访问现状
    console在文件中
    2011程序员薪资调查报告全文发布
    Centos上搭建能用于ok6410开发板的tftp服务器
    Centos 上搭建nfs且可挂载到6410开发板
    linux下软件的卸载与安装
    基于ok6410的韦东山驱动视频简要分析lcd驱动
    6410上移植uboot
    编译可加载触摸屏驱动的uImage内核。
  • 原文地址:https://www.cnblogs.com/zwei1121/p/5201678.html
Copyright © 2011-2022 走看看