コード例 #1
0
ファイル: NPocoSchema.cs プロジェクト: dgodard/NPoco.T4
		string CheckNullable(Column col)
		{
			string result="";
			if(col.IsNullable && 
				col.PropertyType !="byte[]" && 
				col.PropertyType !="string" &&
				col.PropertyType !="Microsoft.SqlServer.Types.SqlGeography" &&
				col.PropertyType !="Microsoft.SqlServer.Types.SqlGeometry"
				)
				result="?";
			return result;
		}
コード例 #2
0
ファイル: NPocoSchema.cs プロジェクト: dgodard/NPoco.T4
			List<Column> LoadColumns(Table tbl)
			{
				using (var cmd = _factory.CreateCommand())
				{
					cmd.Connection = _connection;
					cmd.CommandText = COLUMN_SQL;

					var p = cmd.CreateParameter();
					p.ParameterName = "@tableName";
					p.Value = tbl.Name;
					cmd.Parameters.Add(p);

					p = cmd.CreateParameter();
					p.ParameterName = "@schemaName";
					p.Value = tbl.Schema;
					cmd.Parameters.Add(p);

					var result=new List<Column>();
					using (IDataReader rdr=cmd.ExecuteReader())
					{
						while(rdr.Read())
						{
							Column col=new Column();
							col.Name=rdr["ColumnName"].ToString();
							col.PropertyName = CleanUp(col.Name);
							col.IsNullable = rdr["IsNullable"].ToString() == "YES";
							col.PropertyType = GetPropertyType(rdr["DataType"].ToString(), col.IsNullable);
							int.TryParse(rdr["MaxLength"].ToString(), out col.StringLength);
							col.IsAutoIncrement = ((int)rdr["IsIdentity"]) == 1;
							result.Add(col);
						}
					}

					return result;
				}
			}
コード例 #3
0
		public static void BuildCurrentPocos()
		{
			var tbls = new Tables();

			var iobj = new MyApp.Models.IdentityObject();
			var asm = Assembly.GetAssembly(iobj.GetType());

			Type[] types = asm.GetTypes();

			foreach (Type t in types)
			{
				var tbl = new Table();
				tbl.Columns = new List<Column>();
				Attribute[] attrs = Attribute.GetCustomAttributes(t);

				foreach (Attribute atr in attrs)
				{
					if (atr is NPoco.PrimaryKeyAttribute)
					{
						var pka = (NPoco.PrimaryKeyAttribute)atr;
						tbl.PrimaryKeyNamesFromAttribute =pka.Value;
						tbl.IsAutoIncrement = tbl.IsAutoIncrement || pka.AutoIncrement;
					}

					if (atr is NPoco.TableNameAttribute)
					{
						var a = (NPoco.TableNameAttribute)atr;
						tbl.ClassName = t.Name;
						tbl.CleanName = a.Value;

						var props = t.GetProperties();

						foreach (var p in props)
						{
							var col = new Column();

							Attribute[] propAttributes = Attribute.GetCustomAttributes(p);
							foreach (Attribute patr in propAttributes)
							{
								if (patr is StringLengthAttribute)
								{
									col.StringLength = ((StringLengthAttribute)patr).MaximumLength;
								}

								if (patr is NPoco.ColumnAttribute)
								{
									var ca = patr as NPoco.ColumnAttribute;
									col.Name = String.IsNullOrEmpty(ca.Name) ? p.Name : ca.Name;
									col.PropertyName = p.Name;
									col.PropertyType = TypeMap.CommonSystemTypeFromFrameworkType(p.PropertyType.ToString());
									col.IsNullable = col.PropertyType.EndsWith("?");
									col.IsAutoIncrement = col.IsPK && tbl.IsAutoIncrement;
								}

								if (patr is EquivalentTypeForTestingAttribute)
								{
									var ca = patr as EquivalentTypeForTestingAttribute;
									col.PropertyType = ca.Name;
									col.IsNullable = col.PropertyType.EndsWith("?");
								}
							}

							if (!String.IsNullOrEmpty(col.PropertyName))
							{
								tbl.Columns.Add(col);
							}
						}
					}
				}

				if (!String.IsNullOrEmpty(tbl.ClassName))
				{
					// Assign Primary Key(s) if found
					if (!String.IsNullOrEmpty(tbl.PrimaryKeyNamesFromAttribute))
					{
						string[] pkNames = tbl.PrimaryKeyNamesFromAttribute.Split(',').Select(a => a.Trim()).ToArray();
						var cols = tbl.Columns.Where(a => pkNames.Contains(a.PropertyName));
						foreach (var col in cols)
						{
							col.IsPK = true;
							col.IsAutoIncrement = tbl.IsAutoIncrement;
						}
					}

					tbls.Add(tbl);
				}

			}

			currentPocos = tbls;
		}