Exemple #1
0
        private static void DisplayDebug(IDbCommand command)
        {
#if DEBUG
            OrmDebug.Trace(command.CommandText);
            foreach (IDbDataParameter commandParameter in command.Parameters)
            {
                string value;
                if (commandParameter.Value == DBNull.Value)
                {
                    value = "NULL";
                }
                else if (commandParameter.Value is DateTime)
                {
                    value = ((DateTime)commandParameter.Value).ToString("MM/dd/yyyy HH:mm:ss.fff");
                }
                else if (commandParameter.Value is byte[] byteArrayValue)
                {
                    value = Encoding.UTF8.GetString(byteArrayValue, 0, byteArrayValue.Length);
                }
                else
                {
                    value = commandParameter.Value.ToString();
                }

                OrmDebug.Trace($"\t{commandParameter.ParameterName}:{value}");
            }
#endif
        }
Exemple #2
0
        private void CreateIndex(Index index)
        {
            var connection = SqlDataStore.GetWriteConnection();

            using var command = connection.CreateCommand();
            var sql = index.GetCreateSqlQuery();

            OrmDebug.Trace(sql);

            command.CommandText = sql;
            command.ExecuteNonQuery();
        }
Exemple #3
0
        public string ToStatement(List <IDataParameter> @params)
        {
            var result = new StringBuilder();

            result.Append($"UPDATE [{_entityName}]");
            result.Append(_setFieldList.ToStatement(@params));
            result.Append(_where.ToStatement(@params));
            var sql = result.Append(";").ToString();

            OrmDebug.Trace(sql);
            return(sql);
        }
        protected override void CreateForeignKey(ForeignKey foreignKey)
        {
            var connection = SqlDataStore.GetConnection();

            using (var command = connection.CreateCommand())
            {
                var sql = foreignKey.GetCreateSqlQuery();
                OrmDebug.Trace(sql);

                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
        }
Exemple #5
0
        public string ToStatement(List <IDataParameter> @params)
        {
            var result = new StringBuilder();

            result.Append(FromStatement());
            result.Append(JoinStatement(@params));
            result.Append(_where.ToStatement(@params));
            result.Append(_groupBy.ToStatement());
            result.Append(_orderBy.ToStatement());
            var sql = result.Append(";").ToString();

            OrmDebug.Trace(sql);
            return(sql);
        }
Exemple #6
0
        public void ApplyDelete(IEntityConflict entityConflict)
        {
            var entity = GetSyncableEntity();

            foreach (var row in Delete)
            {
                var existing = FindExisting(row);
                if (existing == null)
                {
                    OrmDebug.Trace(string.Format("Entity:{0}. deleted on remote but does not exist locally.", EntityName));
                    continue;
                }

                ApplyDeletetWhenExistInLocal(entityConflict, existing, entity, row);
            }
        }
Exemple #7
0
        private IDbConnection GetPoolConnection()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("SqlStoreBase");
            }

            lock (_connectionPool)
            {
                IDbConnection connection;

                do
                {
                    connection = GetFreeConnectionInPool();

                    if (connection != null)
                    {
                        if (Open(connection))
                        {
                            return(connection);
                        }

                        // Broken connection, maybe disposed
                        _connectionPool.Remove(connection);
                    }

                    if (_connectionPool.Count < ConnectionPoolSize)
                    {
                        connection = _dbEngine.GetNewConnection();
                        connection.Open();
                        _connectionPool.Add(connection);
                        Interlocked.Increment(ref _connectionCount);
                        OrmDebug.Trace("Creating pooled connection");
                        return(connection);
                    }

                    // pool is full, we have to wait
                    Thread.Sleep(1000);

                    // TODO: add a timeout?
                } while (connection == null);

                // this should never happen
                throw new TimeoutException("Unable to get a pooled connection.");
            }
        }
Exemple #8
0
        public void Populate()
        {
            var entity = GetSyncableEntity();

            OrmDebug.Trace("");
            PopulateInsert(entity);
            OrmDebug.Trace(GetChangeDisplay("Inserted", Insert));
            PopulateUpdate(entity);
            OrmDebug.Trace(GetChangeDisplay("Updated", Update));
            if (entity.IsDeleteTrackEnable)
            {
                PopulateDelete(entity);
                OrmDebug.Trace(GetChangeDisplay("Deleted", Delete));
            }
            PopulateLastSync(entity);
            OrmDebug.Trace(GetChangeDisplay("LastSync", Update));
        }
Exemple #9
0
        public IDbConnection GetConnection()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("ConnectionPool");
            }

            lock (_pool)
            {
                IDbConnection connection;

                do
                {
                    connection = GetFreeConnectionInPool();

                    if (connection != null)
                    {
                        if (Open(connection))
                        {
                            return(connection);
                        }

                        // Broken connection, maybe disposed
                        connection.Dispose();
                        _pool.Remove(connection);
                    }

                    if (_pool.Count < ConnectionPoolSize)
                    {
                        connection = _dbEngine.GetNewConnection();
                        connection.Open();
                        _pool.Add(connection);
                        OrmDebug.Trace("Creating pooled connection");
                        return(connection);
                    }

                    OrmDebug.Trace("Pool full waiting for free connection");
                    Thread.Sleep(1000);

                    // TODO: add a timeout?
                } while (connection == null);

                throw new TimeoutException("Unable to get a pooled connection.");
            }
        }
Exemple #10
0
        /// <summary>
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        /// <remarks>You <b>MUST</b> call CloseReader after calling this method to prevent a leak</remarks>
        public virtual IDataReader ExecuteReader(string sql)
        {
            try
            {
                var connection = GetReadConnection();
                var command    = SqlFactory.CreateCommand();
                command.CommandText = sql;
                command.Connection  = connection;
                command.Transaction = CurrentTransaction;

                var reader = command.ExecuteReader(CommandBehavior.Default);
                return(reader);
            }
            catch (Exception ex)
            {
                OrmDebug.Trace("SQLStoreBase::ExecuteReader threw: " + ex.Message);
                throw;
            }
        }
Exemple #11
0
        private static void DisplayDebug(IDbCommand command)
        {
#if DEBUG
            OrmDebug.Trace(command.CommandText);
            foreach (IDbDataParameter commandParameter in command.Parameters)
            {
                string value;
                if (commandParameter.Value == DBNull.Value)
                {
                    value = "NULL";
                }
                else if (commandParameter.Value is DateTime)
                {
                    value = ((DateTime)commandParameter.Value).ToString("MM/dd/yyyy HH:mm:ss.fff");
                }
                else
                {
                    value = commandParameter.Value.ToString();
                }

                OrmDebug.Trace(string.Format("\t{0}:{1}", commandParameter.ParameterName, value));
            }
#endif
        }