示例#1
0
 public JArray Retrieve(string soupName, params long[] soupEntryIds)
 {
     lock (smartlock)
     {
         DBHelper db            = Database;
         string   soupTableName = db.GetSoupTableName(soupName);
         var      result        = new JArray();
         if (String.IsNullOrWhiteSpace(soupTableName))
         {
             throw new SmartStoreException("Soup: " + soupName + " does not exist");
         }
         using (SQLiteStatement statement = db.Query(soupTableName, new[] { SoupCol }, String.Empty, String.Empty,
                                                     GetSoupEntryIdsPredicate(soupEntryIds)))
         {
             if (statement.DataCount > 0)
             {
                 do
                 {
                     string raw = statement.GetText(statement.ColumnIndex(SoupCol));
                     result.Add(JObject.Parse(raw));
                 } while (statement.Step() == SQLiteResult.ROW);
             }
         }
         return(result);
     }
 }
 /// <summary>
 ///     Helper to retrieve data.
 /// </summary>
 /// <param name="statement"></param>
 /// <param name="position"></param>
 /// <returns></returns>
 private object GetObject(SQLiteStatement statement, int position)
 {
     try
     {
         return(statement.GetText(position));
     }
     catch (Exception e)
     {
     }
     try
     {
         return(statement.GetInteger(position));
     }
     catch (Exception e)
     {
     }
     try
     {
         return(statement.GetFloat(position));
     }
     catch (Exception e)
     {
     }
     return(null);
 }
示例#3
0
 private object GetObject(SQLiteStatement statement, int position)
 {
     if (statement[position] == null)
     {
         return(null);
     }
     if (statement[position].GetType().Name == "String")
     {
         return(statement.GetText(position));
     }
     if (intTypes.Contains(statement[position].GetType().Name))
     {
         return(statement.GetInteger(position));
     }
     //
     //fallback try to detect type using exceptions
     //
     try
     {
         return(statement.GetText(position));
     }
     catch (Exception e)
     {
         PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error);
     }
     try
     {
         return(statement.GetInteger(position));
     }
     catch (Exception e)
     {
         PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error);
     }
     try
     {
         return(statement.GetFloat(position));
     }
     catch (Exception e)
     {
         PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error);
     }
     return(null);
 }
示例#4
0
        /// <summary>
        ///     Return JSONArray for one row of data from the statement
        /// </summary>
        /// <param name="statement"></param>
        /// <returns></returns>
        private JArray GetDataFromRow(SQLiteStatement statement)
        {
            var row         = new JArray();
            int columnCount = statement.ColumnCount;

            for (int i = 0; i < columnCount; i++)
            {
                if (statement[i] != null && (statement.ColumnName(i).EndsWith(SoupCol) || statement.ColumnName(i).StartsWith(SoupCol)))
                {
                    string raw = statement.GetText(i);
                    row.Add(JObject.Parse(raw));
                }
                else
                {
                    object raw = GetObject(statement, i);
                    if (raw != null)
                    {
                        long value;
                        if (long.TryParse(raw.ToString(), out value))
                        {
                            row.Add(new JValue(value));
                        }
                        else
                        {
                            double dvalue;
                            if (double.TryParse(raw.ToString(), out dvalue))
                            {
                                row.Add(new JValue(dvalue));
                            }
                            else
                            {
                                string rawString = raw.ToString();
                                if (rawString.Contains('{'))
                                {
                                    try {
                                        row.Add(JObject.Parse(rawString));
                                    }
                                    catch (Exception)
                                    {
                                        row.Add(raw.ToString());
                                    }
                                }
                                else
                                {
                                    row.Add(raw.ToString());
                                }
                            }
                        }
                    }
                }
            }
            return(row);
        }
        protected IndexSpec[] GetIndexSpecsFromDb(String soupName)
        {
            SQLiteStatement statement = Query(SmartStore.SoupIndexMapTable,
                                              new[] { SmartStore.PathCol, SmartStore.ColumnNameCol, SmartStore.ColumnTypeCol }, null,
                                              null, SmartStore.SoupNamePredicate, soupName);

            if (statement.DataCount < 1)
            {
                throw new SmartStoreException(String.Format("{0} does not have any indices", soupName));
            }
            var indexSpecs = new List <IndexSpec>();

            do
            {
                String path       = statement.GetText(SmartStore.PathCol);
                String columnName = statement.GetText(SmartStore.ColumnNameCol);
                var    columnType = new SmartStoreType(statement.GetText(SmartStore.ColumnTypeCol));
                indexSpecs.Add(new IndexSpec(path, columnType, columnName));
            } while (statement.Step() == SQLiteResult.ROW);
            statement.ResetAndClearBindings();
            return(indexSpecs.ToArray());
        }
示例#6
0
 private static List <string> GetAllSoupNames(string databasePath)
 {
     lock (smartlock)
     {
         var      soupNames = new List <string>();
         DBHelper db        = DBHelper.GetInstance(databasePath);
         using (SQLiteStatement stmt = db.Query(SoupNamesTable, new[] { SoupNameCol }, String.Empty, String.Empty,
                                                String.Empty))
         {
             if (stmt.DataCount > 0)
             {
                 do
                 {
                     soupNames.Add(stmt.GetText(0));
                 } while (stmt.Step() == SQLiteResult.ROW);
             }
         }
         return(soupNames);
     }
 }
示例#7
0
        /// <summary>
        ///     Re-index all soup elements for passed indexPaths
        /// </summary>
        /// <param name="soupName"></param>
        /// <param name="indexPaths"></param>
        /// <param name="handleTx"></param>
        public void ReIndexSoup(string soupName, string[] indexPaths, bool handleTx)
        {
            lock (smartlock)
            {
                DBHelper db            = DBHelper.GetInstance(DatabasePath);
                string   soupTableName = db.GetSoupTableName(soupName);
                if (String.IsNullOrWhiteSpace(soupTableName))
                {
                    throw new SmartStoreException("Soup: " + soupName + " does not exist");
                }
                Dictionary <string, IndexSpec> mapAllSpecs = IndexSpec.MapForIndexSpecs(GetSoupIndexSpecs(soupName));
                IndexSpec[] indexSpecs =
                    (from indexPath in indexPaths where mapAllSpecs.ContainsKey(indexPath) select mapAllSpecs[indexPath])
                    .ToArray();
                if (indexSpecs.Length == 0)
                {
                    return; // nothing to do
                }
                if (handleTx)
                {
                    db.BeginTransaction();
                }

                using (
                    SQLiteStatement stmt = db.Query(soupTableName, new[] { IdCol, SoupCol }, String.Empty, String.Empty,
                                                    String.Empty))
                {
                    if (stmt.DataCount > 0)
                    {
                        try
                        {
                            do
                            {
                                string soupEntryId = stmt.GetText(0);
                                string soupRaw     = stmt.GetText(1);

                                try
                                {
                                    JObject soupElt       = JObject.Parse(soupRaw);
                                    var     contentValues = new Dictionary <string, object>();
                                    foreach (IndexSpec indexSpec in indexSpecs)
                                    {
                                        ProjectIndexedPaths(soupElt, contentValues, indexSpec);
                                    }
                                    db.Update(soupTableName, contentValues, IdPredicate, soupEntryId + String.Empty);
                                }
                                catch (JsonException e)
                                {
                                    PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error);
                                    Debug.WriteLine("SmartStore.ReIndexSoup: Could not parse soup element " + soupEntryId);
                                }
                            } while (stmt.Step() == SQLiteResult.ROW);
                        }
                        finally
                        {
                            if (handleTx)
                            {
                                db.CommitTransaction();
                            }
                        }
                    }
                }
            }
        }