Cache数据库的父子表(学生,课程,成绩(子表)),基于SQL storage

1.学生表结构:

/// 学生表 父表
Class User.DHCBStudent Extends (%Persistent, %XML.Adaptor) [ SqlTableName = DHC_BStudent, StorageStrategy = NewStorage1 ]
{
/// 定义Relationship关系(在父表中只需要写这一句话即可)
Relationship ChildCourse As User.DHCBStudentCourse [ Cardinality = children, Inverse = DHCParRef ];
/// 学号
Property DHCCode As %String [ Required, SqlColumnNumber = 2, SqlFieldName = DHC_Code ];

Index DHCCodeIndex On DHCCode [ Unique ];

Index DHCCodeIndex2 On (DHCCode, DHCName) [ Unique ];

/// 姓名
Property DHCName As %String [ Required, SqlColumnNumber = 3, SqlFieldName = DHC_Name ];

/// 出生日期
Property DHCDobData As %Date [ SqlColumnNumber = 4, SqlFieldName = DHC_DobDate ];

/// 性别
Property DHCSexDR As CTSex [ SqlColumnNumber = 5, SqlFieldName = DHC_Sex_DR ];

/// 是否有效
Property DHCActive As %String(DISPLAYLIST = ",Yes,No", VALUELIST = ",Y,N") [ SqlColumnNumber = 6, SqlFieldName = DHC_Active ];

Cache数据库的父子表(学生,课程,成绩(子表)),基于SQL storage_第1张图片

2.课程表结构。

/// 科目表
Class User.DHCCourse Extends (%Persistent, %XML.Adaptor) [ SqlTableName = DHC_Course, StorageStrategy = NewStorage1 ]
{
/// 课程代码
Property CrsCode As %String [ SqlColumnNumber = 2, SqlFieldName = Crs_Code ];

/// 课程名称
Property CrsDesc As %String [ SqlColumnNumber = 3, SqlFieldName = Crs_Desc ];

/// 是否激活
Property CrsActiveFlag As %String(DISPLAYLIST = ",Yes,No", VALUELIST = ",Y,N") [ SqlColumnNumber = 4, SqlFieldName = Crs_ActiveFlag ];

Cache数据库的父子表(学生,课程,成绩(子表)),基于SQL storage_第2张图片

3.成绩表(子表)结构:

/// Function:学生选课表 子表
/// Sc_Crs_DR(DHC_Course), Sc_Score(%Numeric), Sc_ScoreType(%String)
Class User.DHCBStudentCourse Extends %Persistent [ SqlTableName = DHC_BStudentCourse, StorageStrategy = NewStorage1 ]
{

/// 在子表中定义关联
Relationship DHCParRef As User.DHCBStudent [ Cardinality = parent, Inverse = ChildCourse, Required, SqlFieldName = DHCSC_ParRef ];

/// 定义childsub Init...作用是把父表RoeID传过来
Property ScCrsChildsub As %Numeric(SCALE = 0) [ InitialExpression = {$i(^DHCBSTUDENT($p($s($d(initvalue):initvalue,1:%d(0)),$c(1),1),"SC",0))}, Required, SqlColumnNumber = 2, SqlFieldName = ScCrs_Childsub ];

/// 做ScCrsChildsub的Index[unique]
Index RowIDBasedIDKeyIndex On ScCrsChildsub [ IdKey, PrimaryKey, Unique ];

Property ScCrsDR As DHCCourse [ SqlColumnNumber = 3, SqlFieldName = Sc_Crs_DR ];

Property ScScore As %Float [ SqlColumnNumber = 4, SqlFieldName = Sc_Score ];

Property ScScoreType As %String [ SqlColumnNumber = 5, SqlFieldName = Sc_ScoreType ];

Cache数据库的父子表(学生,课程,成绩(子表)),基于SQL storage_第3张图片

4.增删改查操作:

/// 父子表的增删改查操作
Class User.Student.DHCStudentCourse Extends (%RegisteredObject, %XML.Adaptor)
{

/// function :子表的数据新增
/// Debug:  w ##class(User.Student.DHCStudentCourse).InsertChildData("3","测试2","99","你好")
ClassMethod InsertChildData(DHCRowID As %String, CourseName As %String, Score As %String, Type As %String) As %String
{
	q:DHCRowID="" "无法新增数据"
	s:CourseName'="" CourseID=$o(^DHCCOURSE(0,"Name",CourseName,0))  //根据name找id
	//比如传入父节点的id
	s obj = ##class(User.DHCBStudentCourse).%New(DHCRowID)
	d obj.DHCParRefSetObjectId(DHCRowID)
	d obj.ScCrsDRSetObjectId(CourseID)
	s obj.ScScore = Score
	s obj.ScScoreType = Type
	TSTART
	s sc = obj.%Save()
	if (sc = 1)
	{
		TCOMMIT
		s DHCRowID = obj.%Id()   //查询新增的主键
		s result = "数据新增成功,主键为:"_DHCRowID
	}
	else
	{
		TROLLBACK
		s result = "数据新增失败,失败原因为:"_$system.OBJ.DisplayError(sc)
	}
	d obj.%Close()
	q result
}

/// Function:子表修改
/// Input: ScCrsRowId  子表的RowId
/// Debug: w ##class(User.Student.DHCStudentCourse).UpdateChildData("5||1","测试1","100","还好么")
ClassMethod UpdateChildData(ScCrsRowId As %String, CourseName As %String, Score As %String, Type As %String) As %String
{
	q:ScCrsRowId="" "无法修改数据"
	s:CourseName'="" CourseID=$o(^DHCCOURSE(0,"Name",CourseName,0))  //根据name找id
	s result = ""
	s obj = ##class(User.DHCBStudentCourse).%OpenId(ScCrsRowId)
	if (obj)
	{
		d obj.ScCrsDRSetObjectId(CourseID)
		s obj.ScScore = Score
		s obj.ScScoreType = Type
		TSTART
		s sc = obj.%Save()
		if (sc = 1)
		{
			TCOMMIT
			s ScCrsRowId = obj.%Id()   //查询新增的主键
			s result = "数据修改成功,主键为:"_ScCrsRowId
		}
		else
		{
			TROLLBACK
			s result = "数据修改失败,失败原因为:"_$system.OBJ.DisplayError(sc)
		}
	}
	d obj.%Close()
	q result
}

/// Function: 对象的方式来删除数据
/// Debug: w ##class(User.Student.DHCStudentCourse).DeleteData("9||1")
ClassMethod DeleteData(ScCrsRowId As %String) As %String
{
	q:($get(ScCrsRowId) = "") "输入的ScCrsRowId为空"
	Set result = ""
	TSTART
	s obj = ##class(User.DHCBStudentCourse).%OpenId(ScCrsRowId)
	if (obj)
	{
		s sc = obj.%DeleteId(ScCrsRowId)
		if ($$$ISOK(sc))
		{
			TCOMMIT
			s result = "成功删除数据"
		}
		else
		{
			TROLLBACK
			s ErrInfo = $System.OBJ.DisplayError(sc)
			s result = "删除失败的原因为:"_ErrInfo	
		}
		d obj.%Close()
	}
	else
	{
		TROLLBACK
		s result = "数据不存在,因此删除失败"
	}
	q result
}

/// Function:查询学生的数据
/// Debug: d ##class(User.Student.DHCStudentCourse).GetStudentInfo("学生1")
ClassMethod GetStudentInfo(DHCName As %String) As %String
{
	if (DHCName '= "")
	{
		//根据人名查询
		s DHCRowId = $o(^DHCBSTUDENT(0,"Name",DHCName,0))
		if (DHCRowId '= "")
		{
			s ParRef = DHCRowId
			s ChildSub = 0
			for
			{
				s ChildSub = $o(^DHCBSTUDENTCO(ParRef,"SC",ChildSub)) q:ChildSub=""
				s ScCrsRowId = ParRef_"||"_ChildSub
				s ScCrsDR = ""
				s DataStr = $g(^DHCBSTUDENTCO(ParRef,"SC",ChildSub))
				s ScCrsDR = $p(DataStr,"^",1)    //课程id ^DHCCOURSE
				s:ScCrsDR'="" ScCrsDR=$p(^DHCCOURSE(ScCrsDR),"^",2)
				s scScore = $p(DataStr,"^",2)    //分数
				s scCrsType = $p(DataStr,"^",3)  //考试类型
				w DHCName_" "_ScCrsRowId_" "_ScCrsDR_" "_scScore_" "_scCrsType ,!
			}
		}
	}
	else
	{//查询所有的数据
		s ParRef = 0   //循环学生id
		for
		{
			s ParRef = $o(^DHCBSTUDENT(ParRef)) q:ParRef=""
			s DHCName2 = $p(^DHCBSTUDENT(ParRef),"^",2)
			s ChildSub = 0
			for
			{
				s ChildSub = $o(^DHCBSTUDENTCO(ParRef,"SC",ChildSub)) q:ChildSub=""
				s ScCrsRowId = ParRef_"||"_ChildSub
				s ScCrsDR = ""
				s DataStr = $g(^DHCBSTUDENTCO(ParRef,"SC",ChildSub))
				s ScCrsDR = $p(DataStr,"^",1)    //课程id ^DHCCOURSE
				s:ScCrsDR'="" ScCrsDR=$p(^DHCCOURSE(ScCrsDR),"^",2)
				s scScore = $p(DataStr,"^",2)    //分数
				s scCrsType = $p(DataStr,"^",3)  //考试类型
				w DHCName2_" "_ScCrsRowId_" "_ScCrsDR_" "_scScore_" "_scCrsType ,!
			}
		}
	}
}

/// Function:根据学生姓名,科目,查询成绩
/// Debug: d ##class(User.Student.DHCStudentCourse).GetStudentScore("学生2","测试1")
ClassMethod GetStudentScore(DHCName As %String, DHCDesc As %String) As %String
{

	q:DHCName="" "输入的姓名为空"
	q:DHCDesc="" "输入的科目为空"
	//根据人名查询学生DHCSRowId,根据科目名称查询科目DHCCRowId
	s DHCSRowId = $o(^DHCBSTUDENT(0,"Name",DHCName,0))
	s DHCCRowId = $o(^DHCCOURSE(0,"Name",DHCDesc,0))
	if (DHCSRowId '= "" && DHCCRowId '= "")
	{
		&sql(
			SELECT Sc_Crs_RowID,Sc_Score,Sc_ScoreType INTO :RowID,:Score,:Type FROM
			sqluser.DHC_BStudentCourse WHERE DHCSC_ParRef = :DHCSRowId AND Sc_Crs_DR = :DHCCRowId
		)
		if ($g(RowID) '= "")
		{
			w "姓名:"_DHCName ,!
			w "科目:"_DHCDesc ,!
			w "成绩:"_Score ,!
			w "描述:"_Type
		}
		else
		{
			w "查询结果为空,数据不存在"
		}
		
	}
}

}

 

你可能感兴趣的:(Cache数据库)