// 获得数据定义方面的信息 // parameters: // strStyle 获得那些输出参数? all表示全部 分别指定则是logicnames/type/sqldbname/keystext/browsetext // return: // -1 一般性错误 // -5 未找到数据库对象 // -6 没有足够的权限 // 0 成功 public int GetDbInfo(User user, string strDbName, string strStyle, out LogicNameItem[] logicNames, out string strType, out string strSqlDbName, out string strKeysText, out string strBrowseText, out string strError) { strError = ""; logicNames = null; strType = ""; strSqlDbName = ""; strKeysText = ""; strBrowseText = ""; Debug.Assert(user != null, "GetDbInfo()调用错误,user参数不能为null。"); if (String.IsNullOrEmpty(strDbName) == true) { strError = "GetDbInfo()调用不合法,strDbName参数值不能为null或空字符串。"; return -1; } // 检查当前帐户是否有显示权限 string strExistRights = ""; bool bHasRight = user.HasRights(strDbName, ResType.Database, "read", out strExistRights); //******************对库集合加读锁****** this.m_lock.AcquireReaderLock(m_nTimeOut); #if DEBUG_LOCK this.WriteDebugInfo("GetDbInfo(),对库集合加读锁。"); #endif try { Database db = this.GetDatabase(strDbName); if (db == null) { strError = "未找到名为'" + strDbName + "'的数据库。"; return -5; } if (bHasRight == false) { strError = "您的帐户名为'" + user.Name + "',对'" + strDbName + "'数据库没有'读(read)'权限,目前的权限值为'" + strExistRights + "'。"; return -6; } // return: // -1 出错 // 0 正常 return db.GetInfo( strStyle, out logicNames, out strType, out strSqlDbName, out strKeysText, out strBrowseText, out strError); } finally { this.m_lock.ReleaseReaderLock(); //*****************对库集合解读锁************* #if DEBUG_LOCK this.WriteDebugInfo("GetDbInfo(),对库集合解读锁。"); #endif } }
// 新建数据库 // parameter: // user 帐户对象 // logicNames LogicNameItem数组 // strType 数据库类型,以逗号分隔,可以是file,accout // strSqlDbName 指定的Sql数据库名称,可以为null,系统自动生成一个,,如果数据库为文为文件型数据库,则认作数据源目录的名称 // strKeysDefault keys配置信息 // strBrowseDefault browse配置信息 // return: // -3 在新建库中,发现已经存在同名数据库, 本次不能创建 // -2 没有足够的权限 // -1 一般性错误,例如输入参数不合法等 // 0 操作成功 public int CreateDb(User user, LogicNameItem[] logicNames, string strType, string strSqlDbName, string strKeysDefault, string strBrowseDefault, out string strError) { strError = ""; if (strKeysDefault == null) strKeysDefault = ""; if (strBrowseDefault == null) strBrowseDefault = ""; if (strKeysDefault != "") { XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strKeysDefault); } catch (Exception ex) { strError = "加载keys配置文件内容到dom出错,原因:" + ex.Message; return -1; } } if (strBrowseDefault != "") { XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strBrowseDefault); } catch (Exception ex) { strError = "加载browse配置文件内容到dom出错,原因:" + ex.Message; return -1; } } string strEnLoginName = ""; // 可以一个逻辑库名也没有,不出错 string strLogicNames = ""; for (int i = 0; i < logicNames.Length; i++) { string strLang = logicNames[i].Lang; string strLogicName = logicNames[i].Value; if (strLang.Length != 2 && strLang.Length != 5) { strError = "语言版本字符串长度只能是2位或者5位,'" + strLang + "'语言版本不合法"; return -1; } if (this.IsExistLogicName(strLogicName, null) == true) { strError = "数据库中已存在'" + strLogicName + "'逻辑库名"; return -3; // 已存在相同数据库名 } strLogicNames += "<caption lang='" + strLang + "'>" + strLogicName + "</caption>"; if (String.Compare(logicNames[i].Lang.Substring(0, 2), "en", true) == 0) strEnLoginName = strLogicName; } strLogicNames = "<logicname>" + strLogicNames + "</logicname>"; // 检查当前帐户是否有创建数据库的权限 string strTempDbName = "test"; if (logicNames.Length > 0) strTempDbName = logicNames[0].Value; string strExistRights = ""; bool bHasRight = user.HasRights(strTempDbName, ResType.Database, "create", out strExistRights); if (bHasRight == false) { strError = "您的帐户名为'" + user.Name + "',对数据库没有'创建(create)'权限,目前的权限值为'" + strExistRights + "'。"; return -2; // 权限不够 } //**********对库集合加写锁**************** m_lock.AcquireWriterLock(m_nTimeOut); #if DEBUG_LOCK this.WriteDebugInfo("CreateDb(),对库集合加写锁。"); #endif try { if (strType == null) strType = ""; // 得到库的ID string strDbID = Convert.ToString(this.GetNewDbID()); string strPureCfgsDir = ""; string strTempSqlDbName = ""; if (strEnLoginName != "") { strTempSqlDbName = strEnLoginName + "_db"; strPureCfgsDir = strEnLoginName + "_cfgs"; } else { strTempSqlDbName = "dprms_" + strDbID + "_db"; strPureCfgsDir = "dprms_" + strDbID + "_cfgs"; } if (strSqlDbName == null || strSqlDbName == "") strSqlDbName = strTempSqlDbName; if (StringUtil.IsInList("file", strType, true) == false) { strSqlDbName = this.GetFinalSqlDbName(strSqlDbName); if (this.IsExistSqlName(strSqlDbName) == true) { strError = "不可能的情况,数据库中已存在'" + strSqlDbName + "'Sql库名"; return -1; } if (this.InstanceName != "") strSqlDbName = this.InstanceName + "_" + strSqlDbName; } string strDataSource = ""; if (StringUtil.IsInList("file", strType, true) == true) { strDataSource = strSqlDbName; strDataSource = this.GetFinalDataSource(strDataSource); if (this.IsExistFileDbSource(strDataSource) == true) { strError = "不可能的情况,数据库中已存在''文件数据目录"; return -1; } string strDataDir = this.DataDir + "\\" + strDataSource; if (Directory.Exists(strDataDir) == true) { strError = "不可能的情况,本地不会有重名的目录。"; return -1; } Directory.CreateDirectory(strDataDir); } strPureCfgsDir = this.GetFinalCfgsDir(strPureCfgsDir); // 把配置文件目录自动创建好 string strCfgsDir = this.DataDir + "\\" + strPureCfgsDir + "\\cfgs"; if (Directory.Exists(strCfgsDir) == true) { strError = "服务器已存在'" + strPureCfgsDir + "'配置文件目录,请指定其它的英文逻辑库名。"; return -1; } Directory.CreateDirectory(strCfgsDir); string strPureKeysLocalName = "keys.xml"; string strPureBrowseLocalName = "browse.xml"; int nRet = 0; // 写keys配置文件 nRet = DatabaseUtil.CreateXmlFile(strCfgsDir + "\\" + strPureKeysLocalName, strKeysDefault, out strError); if (nRet == -1) return -1; // 写browse配置文件 nRet = DatabaseUtil.CreateXmlFile(strCfgsDir + "\\" + strPureBrowseLocalName, strBrowseDefault, out strError); if (nRet == -1) return -1; if (StringUtil.IsInList("file", strType) == true) strSqlDbName = ""; // 这里发生xml片断可能会有小问题,应当用XmlTextWriter来发生? string strDbXml = "<database type='" + strType + "' id='" + strDbID + "' localdir='" + strPureCfgsDir + "' dbo='"+user.Name+"'>" // dbo参数为2006/7/4增加 + "<property>" + strLogicNames + "<datasource>" + strDataSource + "</datasource>" + "<seed>0</seed>" + "<sqlserverdb name='" + strSqlDbName + "'/>" + "</property>" + "<dir name='cfgs' localdir='cfgs'>" + "<file name='keys' localname='" + strPureKeysLocalName + "'/>" + "<file name='browse' localname='" + strPureBrowseLocalName + "'/>" + "</dir>" + "</database>"; this.NodeDbs.InnerXml = this.NodeDbs.InnerXml + strDbXml; XmlNodeList nodeListDb = this.NodeDbs.SelectNodes("database"); if (nodeListDb.Count == 0) { strError = "刚新建数据库,不可能一个数据库都不存在。"; return -1; } // 最后一个库为新建的数据库,加到集合里 XmlNode nodeDb = nodeListDb[nodeListDb.Count - 1]; // return: // -1 出错 // 0 成功 nRet = this.AddDatabase(nodeDb, out strError); if (nRet == -1) return -1; // 及时加入dbo特性 user.AddOwnerDbName(strTempDbName); // 及时保存到database.xml this.Changed = true; this.SaveXml(); } finally { m_lock.ReleaseWriterLock(); //***********对库集合解写锁**************** #if DEBUG_LOCK this.WriteDebugInfo("CreateDb(),对库集合解写锁。"); #endif } return 0; }
// 设置数据库基本信息 // parameter: // strDbName 数据库名称 // strLang 对应的语言版本,如果语言版本为null或者为空字符串,则从所有的语言版本中找 // logicNames LogicNameItem数组 // strType 数据库类型,以逗号分隔,可以是file,accout,目前无效,因为涉及到是文件库,还是sql库的问题 // strSqlDbName 指定的新Sql数据库名称,,目前无效 // strKeysDefault keys配置信息 // strBrowseDefault browse配置信息 // return: // -1 一般性错误 // -2 已存在同名的数据库 // -5 未找到数据库对象 // -6 没有足够的权限 // 0 成功 public int SetDbInfo(User user, string strDbName, LogicNameItem[] logicNames, string strType, string strSqlDbName, string strKeysText, string strBrowseText, out string strError) { strError = ""; Debug.Assert(user != null, "SetDbInfo()调用错误,user参数不能为null。"); if (String.IsNullOrEmpty(strDbName) == true) { strError = "SetDbInfo()调用错误,strDbName参数值不能为null或空字符串。"; return -1; } // 为避免死锁的问题,将查看权限的函数放在外面了 // 检查当前帐户是否有覆盖数据库结构的权限 string strExistRights = ""; bool bHasRight = user.HasRights(strDbName, ResType.Database, "overwrite", out strExistRights); //******************对库集合加读锁****** this.m_lock.AcquireReaderLock(m_nTimeOut); #if DEBUG_LOCK this.WriteDebugInfo("SetDbInfo(),对库集合加读锁。"); #endif try { Database db = this.GetDatabase(strDbName); if (db == null) { strError = "未找到名为'" + strDbName + "'的数据库。"; return -5; } if (bHasRight == false) { strError = "您的帐户名为'" + user.Name + "',对'" + strDbName + "'数据库没有'覆盖(overwrite)'权限,目前的权限值为'" + strExistRights + "'。"; return -6; } // return: // -1 出错 // -2 已存在同名的数据库 // 0 成功 int nRet = db.SetInfo(logicNames, strType, strSqlDbName, strKeysText, strBrowseText, out strError); if (nRet <= -1) return nRet; // 及时保存databases.xml this.Changed = true; this.SaveXml(); return 0; } finally { this.m_lock.ReleaseReaderLock(); //*****************对库集合解读锁************* #if DEBUG_LOCK this.WriteDebugInfo("SetDbInfo(),对库集合解读锁。"); #endif } }
// 得到数据库的所有逻辑名,放到一个字符串数组里 public LogicNameItem[] GetLogicNames() { ArrayList aLogicName = new ArrayList(); XmlNodeList captionList = this.PropertyNode.SelectNodes("logicname/caption"); for (int i = 0; i < captionList.Count; i++) { XmlNode captionNode = captionList[i]; string strLang = DomUtil.GetAttr(captionNode, "lang"); string strValue = captionNode.InnerText.Trim(); // 2012/2/16 // 有可以未定义语言,或未定义值,该怎么处理??? LogicNameItem item = new LogicNameItem(); item.Lang = strLang; item.Value = strValue; aLogicName.Add(item); } LogicNameItem[] logicNames = new LogicNameItem[aLogicName.Count]; for (int i = 0; i < aLogicName.Count; i++) { LogicNameItem item = (LogicNameItem)aLogicName[i]; logicNames[i] = item; } return logicNames; }
// 设置数据库的基本信息 // parameters: // logicNames LogicNameItem数组,用新的逻辑库名数组替换原来的逻辑库名数组 // strType 数据库类型,以逗号分隔,可以是file,accout,目前无效,因为涉及到是文件库,还是sql库的问题 // strSqlDbName 指定的新Sql数据库名称,目前无效,,如果数据库为文为文件型数据库,则返回数据源目录的名称 // strkeysDefault keys配置信息。如果为null,表示此项无效。(注:如果为""则表示要把文件内容清空) // strBrowseDefault browse配置信息。如果为null,表示此项无效。(注:如果为""则表示要把文件内容清空) // return: // -1 出错 // -2 已存在同名的数据库 // 0 成功 public int SetInfo(LogicNameItem[] logicNames, string strType, string strSqlDbName, string strKeysText, string strBrowseText, out string strError) { strError = ""; //****************对数据库加写锁*********** m_TailNolock.AcquireWriterLock(m_nTimeOut); #if DEBUG_LOCK this.container.WriteDebugInfo("SetInfo(),对'" + this.GetCaption("zh-CN") + "'数据库加写锁。"); #endif try { // 2008/4/30 changed // "" 和 null含义不同。后者表示不使用这个参数 /* if (strKeysText == null) strKeysText = ""; if (strBrowseText == null) strBrowseText = ""; * */ if (String.IsNullOrEmpty(strKeysText) == false) { XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strKeysText); } catch (Exception ex) { strError = "加载keys配置文件内容到dom出错(1),原因:" + ex.Message; return -1; } } if (String.IsNullOrEmpty(strBrowseText) == false) { XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strBrowseText); } catch (Exception ex) { strError = "加载browse配置文件内容到dom出错,原因:" + ex.Message; return -1; } } // 可以一个逻辑库名也没有,不出错 string strLogicNames = ""; for (int i = 0; i < logicNames.Length; i++) { string strLang = logicNames[i].Lang; string strLogicName = logicNames[i].Value; if (strLang.Length != 2 && strLang.Length != 5) { strError = "语言版本字符串长度只能是2位或者5位,'" + strLang + "'语言版本不合法"; return -1; } if (this.container.IsExistLogicName(strLogicName, this) == true) { strError = "数据库中已存在'" + strLogicName + "'逻辑库名"; return -2; } strLogicNames += "<caption lang='" + strLang + "'>" + strLogicName + "</caption>"; } // 修改LogicName,使用全部替换的方式 XmlNode nodeLogicName = this.PropertyNode.SelectSingleNode("logicname"); nodeLogicName.InnerXml = strLogicNames; int nRet = 0; // 2008/5/7 nRet = RefreshLognames(this.PureID, strLogicNames, out strError); if (nRet == -1) return -1; // 目前不支持修改strType,strSqlDbName if (strKeysText != null) // 2008/4/30 { string strKeysFileName = "";//this.GetFixedCfgFileName("keys"); string strDbName = this.GetCaption("zh"); // string strDbName = this.GetCaption("zh"); // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(strDbName + "/cfgs/keys", out strKeysFileName, out strError); if (nRet != 0) { if (nRet != -2 && nRet != -4) return -1; else if (nRet == -2) { // return: // -1 出错 // 0 成功 nRet = this.container.SetFileCfgItem( false, this.GetCaption("zh") + "/cfgs", null, "keys", out strError); if (nRet == -1) return -1; // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(this.GetCaption("zh") + "/cfgs/keys", out strKeysFileName, out strError); if (nRet != 0) { if (nRet != -4) return -1; } } } if (File.Exists(strKeysFileName) == false) { Stream s = File.Create(strKeysFileName); s.Close(); } nRet = DatabaseUtil.CreateXmlFile(strKeysFileName, strKeysText, out strError); if (nRet == -1) return -1; // 把缓冲清空 this.m_keysCfg = null; } if (strBrowseText != null) // 2008/4/30 { string strDbName = this.GetCaption("zh"); string strBrowseFileName = ""; // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(strDbName + "/cfgs/browse", out strBrowseFileName, out strError); if (nRet != 0) { if (nRet != -2 && nRet != -4) return -1; else if (nRet == -2) { // return: // -1 出错 // 0 成功 nRet = this.container.SetFileCfgItem( false, this.GetCaption("zh") + "/cfgs", null, "browse", out strError); if (nRet == -1) return -1; // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(this.GetCaption("zh") + "/cfgs/browse", out strBrowseFileName, out strError); if (nRet != 0) { if (nRet != -4) return -1; } } } if (File.Exists(strBrowseFileName) == false) { Stream s = File.Create(strBrowseFileName); s.Close(); } nRet = DatabaseUtil.CreateXmlFile(strBrowseFileName, strBrowseText, out strError); if (nRet == -1) return -1; // 把缓冲清空 // this.m_browseCfg = null; // this.m_bHasBrowse = true; // 缺省值 this.browse_table.Clear(); } return 0; } finally { //***************对数据库解写锁************ m_TailNolock.ReleaseWriterLock(); #if DEBUG_LOCK this.container.WriteDebugInfo("SetInfo(),对'" + this.GetCaption("zh-CN") + "'数据库解写锁。"); #endif } }
// 得到一个库的信息 // parameters: // strStyle 获得那些输出参数? all表示全部 分别指定则是logicnames/type/sqldbname/keystext/browsetext // logicNames 逻辑库名数组 // strType 数据库类型 以逗号分隔的字符串 // strSqlDbName 数据库物理名称,如果数据库为文为文件型数据库,则返回数据源目录的名称 // strKeyText 检索点文件内容 // strBrowseText 非用字文件内容 // strError 出错信息 // return: // -1 出错 // 0 正常 public int GetInfo( string strStyle, out LogicNameItem[] logicNames, out string strType, out string strSqlDbName, out string strKeysText, out string strBrowseText, out string strError) { logicNames = null; strType = ""; strSqlDbName = ""; strKeysText = ""; strBrowseText = ""; strError = ""; // 正规化strStyle的内容,便于后面处理 if (String.IsNullOrEmpty(strStyle) == true || StringUtil.IsInList("all", strStyle) == true) { strStyle = "logicnames,type,sqldbname,keystext,browsetext"; } //**********对数据库加读锁************** this.m_db_lock.AcquireReaderLock(m_nTimeOut); #if DEBUG_LOCK this.container.WriteDebugInfo("GetInfo(),对'" + this.GetCaption("zh-CN") + "'数据库加读锁。"); #endif try { logicNames = this.GetLogicNames(); // 获得type if (StringUtil.IsInList("type", strStyle) == true) strType = this.GetDbType(); // 获得sqldbname if (StringUtil.IsInList("sqldbname", strStyle) == true) { // 调入具体的函数,得到数据源信息,不包括实例名称 strSqlDbName = this.GetSourceName(); if (this.container.InstanceName != "" && strSqlDbName.Length > this.container.InstanceName.Length) { string strPart = strSqlDbName.Substring(0, this.container.InstanceName.Length); if (strPart == this.container.InstanceName) { strSqlDbName = strSqlDbName.Substring(this.container.InstanceName.Length + 1); //rmsService_Guestbook } } } string strDbName = ""; int nRet = 0; // 获得keystext if (StringUtil.IsInList("keystext", strStyle) == true) { string strKeysFileName = ""; strDbName = this.GetCaption("zh"); // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(strDbName + "/cfgs/keys", out strKeysFileName, out strError); if (nRet != 0) { if (nRet != -4) return -1; } if (File.Exists(strKeysFileName) == true) { StreamReader sr = new StreamReader(strKeysFileName, Encoding.UTF8); strKeysText = sr.ReadToEnd(); sr.Close(); } /* // keys文件 KeysCfg keysCfg = null; int nRet = this.GetKeysCfg(out keysCfg, out strError); if (nRet == -1) return -1; if (keysCfg != null) { if (keysCfg.dom != null) { TextWriter tw = new StringWriter(); keysCfg.dom.Save(tw); tw.Close(); strKeysText = tw.ToString(); } } */ } // 获得browsetext if (StringUtil.IsInList("browsetext", strStyle) == true) { string strBrowseFileName = ""; strDbName = this.GetCaption("zh"); // return: // -1 一般性错误,比如调用错误,参数不合法等 // -2 没找到节点 // -3 localname属性未定义或为值空 // -4 localname在本地不存在 // -5 存在多个节点 // 0 成功 nRet = this.container.GetFileCfgItemLocalPath(strDbName + "/cfgs/browse", out strBrowseFileName, out strError); if (nRet != 0) { if (nRet != -4) return -1; } if (File.Exists(strBrowseFileName) == true) { StreamReader sr = new StreamReader(strBrowseFileName, Encoding.UTF8); strBrowseText = sr.ReadToEnd(); sr.Close(); } /* // browse文件 BrowseCfg browseCfg = null; nRet = this.GetBrowseCfg(out browseCfg, out strError); if (nRet == -1) return -1; if (browseCfg != null) { if (browseCfg.dom != null) { TextWriter tw = new StringWriter(); browseCfg.dom.Save(tw); tw.Close(); strBrowseText = tw.ToString(); } } */ } return 0; } finally { //****************对数据库解读锁************** this.m_db_lock.ReleaseReaderLock(); #if DEBUG_LOCK this.container.WriteDebugInfo("GetInfo(),对'" + this.GetCaption("zh-CN") + "'数据库解读锁。"); #endif } }