public void MirrorInsert(IList <Document> docs) { StringBuilder insertString = new StringBuilder(); using (SQLiteDataProvider sqlData = new SQLiteDataProvider()) { sqlData.Connect(Table.ConnectionString); System.Data.Common.DbTransaction tran; System.Data.Common.DbCommand cmd = sqlData.GetCommand(out tran); try { foreach (Hubble.Core.Data.Document doc in docs) { if (Table.DocIdReplaceField == null) { insertString.AppendFormat("Insert into {0} ([DocId]", _Table.DBTableName); } else { insertString.AppendFormat("Insert into {0} (", _Table.DBTableName); } int i = 0; foreach (Data.FieldValue fv in doc.FieldValues) { if (fv.Value == null) { continue; } if (i == 0 && Table.DocIdReplaceField != null) { insertString.AppendFormat("[{0}]", fv.FieldName); } else { insertString.AppendFormat(", [{0}]", fv.FieldName); } i++; } if (Table.DocIdReplaceField == null) { insertString.AppendFormat(") Values({0}", doc.DocId); } else { insertString.Append(") Values("); } i = 0; foreach (Data.FieldValue fv in doc.FieldValues) { if (fv.Value == null) { continue; } switch (fv.Type) { case Hubble.Core.Data.DataType.Varchar: case Hubble.Core.Data.DataType.NVarchar: case Hubble.Core.Data.DataType.Char: case Hubble.Core.Data.DataType.NChar: case Hubble.Core.Data.DataType.DateTime: case Hubble.Core.Data.DataType.Date: case Hubble.Core.Data.DataType.SmallDateTime: case Hubble.Core.Data.DataType.Data: if (i == 0 && Table.DocIdReplaceField != null) { insertString.AppendFormat("'{0}'", fv.Value.Replace("'", "''")); } else { insertString.AppendFormat(",'{0}'", fv.Value.Replace("'", "''")); } break; default: if (i == 0 && Table.DocIdReplaceField != null) { insertString.AppendFormat("{0}", fv.Value); } else { insertString.AppendFormat(",{0}", fv.Value); } break; } i++; } insertString.Append(")\r\n"); sqlData.ExecuteNonQuery(insertString.ToString()); insertString.Length = 0; } tran.Commit(); } catch (Exception e) { tran.Rollback(); throw e; } } }
public void MirrorUpdate(IList <FieldValue> fieldValues, IList <List <FieldValue> > docValues, IList <Hubble.Core.Query.DocumentResultForSort> docs) { StringBuilder sql = new StringBuilder(); using (SQLiteDataProvider sqlData = new SQLiteDataProvider()) { sqlData.Connect(Table.ConnectionString); sqlData.Connect(Table.ConnectionString); System.Data.Common.DbTransaction tran; System.Data.Common.DbCommand cmd = sqlData.GetCommand(out tran); try { for (int index = 0; index < docs.Count; index++) { sql.AppendFormat("update {0} set ", Table.DBTableName); for (int i = 0; i < fieldValues.Count; i++) { Data.FieldValue fvFieldName = fieldValues[i]; Data.FieldValue fv = docValues[index][i]; string value; if (fv.Value == null) { value = "NULL"; } else { switch (fv.Type) { case Hubble.Core.Data.DataType.Varchar: case Hubble.Core.Data.DataType.NVarchar: case Hubble.Core.Data.DataType.Char: case Hubble.Core.Data.DataType.NChar: 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 dTime = DateTime.Parse(fv.Value); value = string.Format("'{0}'", dTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); break; case DataType.Float: value = fv.Value; //insertString.AppendFormat(",{0:E}", double.Parse(fv.Value)); //We can change it like this in furture. break; default: value = fv.Value; break; } } if (i == 0) { sql.AppendFormat("[{0}]={1}", fvFieldName.FieldName, value); } else { sql.AppendFormat(",[{0}]={1}", fvFieldName.FieldName, value); } } int docid = docs[index].DocId; if (DocIdReplaceField == null) { sql.AppendFormat(" where docId = {0}; \r\n", docid); } else { long replaceFieldValue = this.DBProvider.GetDocIdReplaceFieldValue(docid); sql.AppendFormat(" where {0} = {1}; \r\n", DocIdReplaceField, replaceFieldValue); } sqlData.ExecuteNonQuery(sql.ToString()); sql.Length = 0; } tran.Commit(); } catch (Exception e) { tran.Rollback(); throw e; } } }
public void Insert(IList <Hubble.Core.Data.Document> docs) { StringBuilder insertString = new StringBuilder(); using (SQLiteDataProvider sqlData = new SQLiteDataProvider()) { sqlData.Connect(Table.ConnectionString); System.Data.Common.DbTransaction tran; System.Data.Common.DbCommand cmd = sqlData.GetCommand(out tran); try { 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}]", 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.Varchar: case Hubble.Core.Data.DataType.NVarchar: case Hubble.Core.Data.DataType.Char: case Hubble.Core.Data.DataType.NChar: 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 dTime = DateTime.Parse(fv.Value); insertString.AppendFormat(",'{0}'", dTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); break; case DataType.Float: insertString.AppendFormat(",{0}", fv.Value); //insertString.AppendFormat(",{0:E}", double.Parse(fv.Value)); //We can change it like this in furture. break; default: insertString.AppendFormat(",{0}", fv.Value); break; } } insertString.Append(");\r\n"); sqlData.ExecuteNonQuery(insertString.ToString()); insertString.Length = 0; } tran.Commit(); } catch (Exception e) { tran.Rollback(); throw e; } } }