Exemplo n.º 1
0
        public bool TryFindBrother(IDataRecord rowIdentfier, out IDataRecord result)
        {
            result = null;
            string        sql          = string.Format("Select * from {0} ", this.TableName);
            List <string> restrictions = new List <string>();

            foreach (string pkf in this.PrimaryKey)
            {
                int i = rowIdentfier.GetOrdinal(pkf);
                Guard.Assert(i >= 0);
                System.Type t = rowIdentfier.GetFieldType(i);
                restrictions.Add(string.Format("{0}={1}", pkf, SqliteExpression.GetExpressionString(rowIdentfier.GetValue(i), t)));
            }
            Guard.Assert(restrictions.Count > 0);
            string where = string.Join(" AND ", restrictions);
            string      resolved       = sql + "WHERE " + where;
            bool        enforceDispose = true;
            IDataReader r = this.Connection.ExecuteReader(resolved);

            try
            {
                bool b = r.Read();
                if (b)
                {
#if DEBUG
                    long recordcount = this.Connection.ExecuteScalarAsLong(string.Format("SELECT COUNT(*) FROM {0} WHERE {1}", this.TableName, where));
                    Guard.Assert(recordcount == 1, "extly one expected");
#endif
                    enforceDispose = false;
                    result         = r;
                    return(true);
                }
                else
                {
                    result = null;
                    Guard.Assert(enforceDispose);
                    return(false);
                }
            }
            finally
            {
                if (enforceDispose)
                {
                    r.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        public IEnumerable <string> GetInsertStatements(IEnumerable <Tuple <JObject, string> > nodes)
        {
            Guard.ArgumentNotNull(nodes, nameof(nodes));
            //Guard.ArgumentNotNull(rows, "rows");
            string insertTemplate = "INSERT INTO {0} ({1}) values ({2});";
            string columnNames    = string.Join(", ", this.Columns.Keys);

            foreach (Tuple <JObject, string> node in nodes)
            {
                Guard.AssertNotNull(node.Item1);
                Guard.AssertNotNull(node.Item2);
                List <string> rowValues = new List <string>();
                foreach (string prop in this.Columns.Keys)
                {
                    if (prop == "owner")
                    {
                        rowValues.Add(SqliteExpression.GetExpressionString(node.Item2, typeof(string)));
                    }
                    else
                    {
                        string      cv = node.Item1[prop].ToString();
                        System.Type t  = typeof(string);

                        if ((prop == "isPrivate") || (prop.StartsWith("is_"))) //FB is using "is_" pattern!
                        {
                            int isp = 0;
                            if (cv.ToLowerInvariant().Contains("tr"))
                            {
                                isp = 1;
                            }
                            rowValues.Add(SqliteExpression.GetExpressionString(isp, typeof(int)));
                        }
                        else
                        {
                            rowValues.Add(SqliteExpression.GetExpressionString(cv, typeof(string)));
                        }
                    }
                }
                string serializedRowValues = string.Join(", ", rowValues);

                yield return(string.Format(insertTemplate, this.TableName, columnNames, serializedRowValues));
            } //nodes
        }