Example #1
0
        private int GetRemainCount(long from, DBAdapter.IDBAdapter dbAdapter)
        {
            string dbAdapterName = (dbAdapter as INamedExternalReference).Name;

            _HasInsertCount = true;

            if (dbAdapterName.IndexOf("sqlserver", 0, StringComparison.CurrentCultureIgnoreCase) == 0)
            {
                //For sql server

                string sql = string.Format("select rows from sysindexes where id = object_id('{0}') and indid in (0,1)",
                                           _DBProvider.Table.DBTableName.Replace("'", "''"));

                System.Data.DataSet ds = null;

                try
                {
                    ds = dbAdapter.QuerySql(sql);
                }
                catch
                {
                    ds = null;
                }

                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        return(int.Parse(ds.Tables[0].Rows[0][0].ToString()) -
                               _DBProvider.DocumentCount);
                    }
                }
            }

            //For others
            {
                string sql = string.Format("select count(*) from {0} where {2} > {1}",
                                           _DBProvider.Table.DBTableName, from, _DBProvider.Table.DocIdReplaceField);

                System.Data.DataSet ds = null;
                int times = 0;

                if (!_FastestMode)
                {
                    while (times < 3)
                    {
                        try
                        {
                            ds = dbAdapter.QuerySql(sql);
                            break;
                        }
                        catch
                        {
                            ds = null;
                            times++;
                        }
                    }
                }

                int count;

                if (ds == null)
                {
                    count           = 50000000;
                    _HasInsertCount = false;
                }
                else
                {
                    count = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                }

                return(count);
            }
        }
Example #2
0
        private long GetLastId(int lastDocId, DBAdapter.IDBAdapter dbAdapter)
        {
            if ((_Flags & SyncFlags.Rebuild) != 0)
            {
                //Rebuild
                return(long.MaxValue);
            }

            string sql = string.Format("select id from {0} where Opr = 'Insert'",
                                       _DBProvider.Table.TriggerTableName);

            System.Data.DataSet ds = dbAdapter.QuerySql(sql);

            if (ds.Tables[0].Rows.Count <= 0)
            {
                long from = -1;

                while (lastDocId > 0)
                {
                    from = _DBProvider.GetDocIdReplaceFieldValue(lastDocId);

                    if (from != long.MaxValue)
                    {
                        break;
                    }

                    lastDocId--;
                }

                if (from < 0)
                {
                    from = _DBProvider.GetDocIdReplaceFieldValue(lastDocId);
                }

                if (from == long.MaxValue)
                {
                    sql = string.Format("insert into {0} (id, Opr, Fields) values(-1, 'Insert', '')",
                                        _DBProvider.Table.TriggerTableName);
                }
                else
                {
                    sql = string.Format("insert into {0} (id, Opr, Fields) values({1}, 'Insert', '')",
                                        _DBProvider.Table.TriggerTableName, from);
                }
                dbAdapter.ExcuteSql(sql);
                return(from);
            }
            else
            {
                long lastId = long.Parse(ds.Tables[0].Rows[0][0].ToString());

                if (lastId < 0)
                {
                    return(long.MaxValue);
                }
                else
                {
                    return(lastId);
                }
            }
        }