public void Delete(IList <int> docIds) { StringBuilder sql = new StringBuilder(); sql.Append("delete "); sql.AppendFormat(" {0} where docId in (", Table.DBTableName); int i = 0; foreach (int docId in docIds) { if (i++ == 0) { sql.AppendFormat("{0}", docId); } else { sql.AppendFormat(",{0}", docId); } } sql.Append(")"); using (OLEDataProvider sqlData = new OLEDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.ExcuteSql(sql.ToString()); } }
public void Truncate(string tableName) { Debug.Assert(Table != null); string sql = string.Format("truncate table {0}", tableName); using (OLEDataProvider sqlData = new OLEDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.ExcuteSql(sql); } }
public int ExcuteSql(string sql) { using (OLEDataProvider sqlData = new OLEDataProvider()) { string connectionString; if (Table == null) { connectionString = this.ConnectionString; } else { connectionString = Table.ConnectionString; } sqlData.Connect(connectionString); return(sqlData.ExcuteSql(sql)); } }
public void Drop() { Debug.Assert(Table != null); string sql = string.Format("drop table {0}", Table.DBTableName); using (OLEDataProvider sqlData = new OLEDataProvider()) { string testExistSql = string.Format("select * from {0} where rownum=1", Table.DBTableName); sqlData.Connect(Table.ConnectionString); try { sqlData.QuerySql(testExistSql); sqlData.ExcuteSql(sql); } catch { } } }
public void Update(Document doc, IList <Hubble.Core.Query.DocumentResultForSort> docs) { StringBuilder sql = new StringBuilder(); sql.AppendFormat("update {0} set ", Table.DBTableName); int i = 0; foreach (Data.FieldValue fv in doc.FieldValues) { string value; if (fv.Value == null) { value = "NULL"; } else { switch (fv.Type) { case Hubble.Core.Data.DataType.NVarchar: case Hubble.Core.Data.DataType.NChar: value = string.Format("N'{0}'", fv.Value.Replace("'", "''")); break; case Hubble.Core.Data.DataType.Varchar: case Hubble.Core.Data.DataType.Char: case Hubble.Core.Data.DataType.Data: value = string.Format("'{0}'", fv.Value.Replace("'", "''")); break; case Hubble.Core.Data.DataType.DateTime: case Hubble.Core.Data.DataType.Date: case Hubble.Core.Data.DataType.SmallDateTime: DateTime dateTime; if (!DateTime.TryParseExact(fv.Value, "yyyy-MM-dd HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out dateTime)) { dateTime = DateTime.Parse(fv.Value); } value = string.Format("to_date('{0}','yyyy-mm-dd HH24:MI:SS')", dateTime.ToString("yyyy-MM-dd HH:mm:ss")); break; default: value = string.Format("{0}", fv.Value); break; } } if (i++ == 0) { sql.AppendFormat("{0}={1}", GetFieldName(fv.FieldName), value); } else { sql.AppendFormat(",{0}={1}", GetFieldName(fv.FieldName), value); } } if (DocIdReplaceField == null) { sql.Append(" where docId in ("); } else { sql.AppendFormat(" where {0} in (", DocIdReplaceField); } i = 0; foreach (Query.DocumentResultForSort docResult in docs) { int docId = docResult.DocId; if (DocIdReplaceField == null) { if (i++ == 0) { sql.AppendFormat("{0}", docId); } else { sql.AppendFormat(",{0}", docId); } } else { long replaceFieldValue = this.DBProvider.GetDocIdReplaceFieldValue(docId); if (i++ == 0) { sql.AppendFormat("{0}", replaceFieldValue); } else { sql.AppendFormat(",{0}", replaceFieldValue); } } } sql.Append(")"); using (OLEDataProvider sqlData = new OLEDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.ExcuteSql(sql.ToString()); } }
public void Insert(IList <Document> docs) { StringBuilder insertString = new StringBuilder(); insertString.Append("BEGIN "); foreach (Hubble.Core.Data.Document doc in docs) { insertString.AppendFormat("Insert into {0} (DocId", _Table.DBTableName); foreach (Data.FieldValue fv in doc.FieldValues) { if (fv.Value == null) { continue; } insertString.AppendFormat(", {0}", GetFieldName(fv.FieldName)); } insertString.AppendFormat(") Values({0}", doc.DocId); foreach (Data.FieldValue fv in doc.FieldValues) { if (fv.Value == null) { continue; } switch (fv.Type) { case Hubble.Core.Data.DataType.NVarchar: case Hubble.Core.Data.DataType.NChar: insertString.AppendFormat(",N'{0}'", fv.Value.Replace("'", "''")); break; case Hubble.Core.Data.DataType.Varchar: case Hubble.Core.Data.DataType.Char: case Hubble.Core.Data.DataType.Data: insertString.AppendFormat(",'{0}'", fv.Value.Replace("'", "''")); break; case Hubble.Core.Data.DataType.DateTime: case Hubble.Core.Data.DataType.Date: case Hubble.Core.Data.DataType.SmallDateTime: DateTime dateTime; if (!DateTime.TryParseExact(fv.Value, "yyyy-MM-dd HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out dateTime)) { dateTime = DateTime.Parse(fv.Value); } insertString.AppendFormat(",to_date('{0}','yyyy-mm-dd HH24:MI:SS')", dateTime.ToString("yyyy-MM-dd HH:mm:ss")); break; default: insertString.AppendFormat(",{0}", fv.Value); break; } } insertString.Append("); "); } insertString.Append(" END;"); using (OLEDataProvider sqlData = new OLEDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.ExcuteSql(insertString.ToString()); } }
public void Create() { Debug.Assert(Table != null); //List<string> primaryKeys = new List<string>(); StringBuilder sql = new StringBuilder(); sql.AppendFormat("create table {0} (", Table.DBTableName); sql.Append("DocId Int NOT NULL Primary Key,"); int i = 0; for (i = 0; i < Table.Fields.Count; i++) { Data.Field field = Table.Fields[i]; string fieldSql = GetFieldLine(field); if (fieldSql != null) { sql.Append(fieldSql); if (i < Table.Fields.Count - 1) { sql.Append(","); } } //if (field.PrimaryKey) //{ // primaryKeys.Add(field.Name); //} } //if (primaryKeys.Count > 0) //{ // i = 0; // sql.Append(" primary key NONCLUSTERED("); // foreach (string pkName in primaryKeys) // { // if (i == 0) // { // sql.Append(pkName); // } // else // { // sql.Append("," + pkName); // } // i++; // } // sql.Append(")"); //} sql.Append(")"); using (OLEDataProvider sqlData = new OLEDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.ExcuteSql(sql.ToString()); //sqlData.ExcuteSql(string.Format("create UNIQUE CLUSTERED Index I_{0}_DocId on {0}(DocId)", // Table.DBTableName)); if (!string.IsNullOrEmpty(Table.SQLForCreate)) { sqlData.ExcuteSql(Table.SQLForCreate); } } }