Example #1
0
        public long Count <TCondition>(ISpecification <TCondition> specification)
        {
            Revenj.Utility.ChunkedMemoryStream cms = null;
            System.IO.TextWriter sw = null;
            var selectType          = "SELECT count(*)";

            if (specification == null)
            {
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(@"SELECT count(*) FROM ""FormABC"".""Input_entity"" r");
            }

            else
            {
                return(Query(specification).LongCount());
            }

            sw.Flush();
            cms.Position = 0;
            var  com   = PostgresCommandFactory.NewCommand(cms);          //TODO: sql template
            long total = 0;

            DatabaseQuery.Execute(com, dr => { total = dr.GetInt64(0); });
            return(total);
        }
Example #2
0
        public bool Exists <TCondition>(ISpecification <TCondition> specification)
        {
            Revenj.Utility.ChunkedMemoryStream cms = null;
            System.IO.TextWriter sw = null;
            var selectType          = "SELECT exists(SELECT *";

            if (specification == null)
            {
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(@"SELECT exists(SELECT * FROM ""FormABC"".""Input_entity"" r");
            }

            else
            {
                return(Query(specification).Any());
            }

            sw.Write(')');
            sw.Flush();
            cms.Position = 0;
            var  com   = PostgresCommandFactory.NewCommand(cms);          //TODO: sql template
            bool found = false;

            DatabaseQuery.Execute(com, dr => { found = dr.GetBoolean(0); });
            return(found);
        }
Example #3
0
        private void _InternalDoPersistLarge(Revenj.Utility.ChunkedMemoryStream cms, global::UseCase1.Customer[] insertedData, List <KeyValuePair <global::UseCase1.Customer, global::UseCase1.Customer> > updatedData, global::UseCase1.Customer[] deletedData)
        {
            var dbq = DatabaseQuery as IPostgresDatabaseQuery;



            if (insertedData.Length > 0)
            {
                dbq.BulkInsert(
                    "\"UseCase1\".\">tmp-Customer-insert<\"",
                    insertedData.Select((item, ind) => new IPostgresTuple[] { IntConverter.ToTuple(ind), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateRecordTupleFrom(item, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.TableTuples) }));
            }
            if (updatedData.Count > 0)
            {
                dbq.BulkInsert(
                    "\"UseCase1\".\">tmp-Customer-update<\"",
                    updatedData.Select((kv, ind) => new IPostgresTuple[] { IntConverter.ToTuple(ind), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateRecordTupleFrom(kv.Key, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.PrimaryKeyUpdateTuples), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateRecordTupleFrom(kv.Value, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.TableTuples) }));
            }
            if (deletedData.Length > 0)
            {
                dbq.BulkInsert(
                    "\"UseCase1\".\">tmp-Customer-delete<\"",
                    deletedData.Select((item, ind) => new IPostgresTuple[] { IntConverter.ToTuple(ind), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateRecordTupleFrom(item, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.PrimaryKeyDeleteTuples) }));
            }

            var sw = cms.GetWriter();

            sw.Write("/*NO LOAD BALANCE*/SELECT \"UseCase1\".\"persist_Customer_internal\"(");
            sw.Write(updatedData.Count);
            sw.Write(", ");
            sw.Write(deletedData.Length);



            sw.Write(")");



            sw.Flush();
            cms.Position = 0;
            var    com       = PostgresCommandFactory.NewCommand(cms, "SELECT \"UseCase1\".\"persist_Customer_internal\"(:updated_count,:deleted_count)");
            string _sqlError = null;

            DatabaseQuery.Execute(com, dr => _sqlError = dr.GetString(0));
            if (_sqlError != null)
            {
                throw new PostgresException(_sqlError);
            }


            foreach (var item in insertedData)
            {
                item.__ResetChangeTracking();
            }
            foreach (var item in updatedData)
            {
                item.Value.__ResetChangeTracking();
            }
        }
Example #4
0
            internal _FindResult_()
            {
                cms = Common.Utility.UseThreadLocalStream();
                NoTemplateCommand = PostgresCommandFactory.NewCommand(cms);

                this.RichFind    = PostgresCommandFactory.PreparedCommand(cms, "FS-FormABC-Input", @"SELECT _r FROM ""FormABC"".""Input_entity"" _r WHERE _r.""ID"" = $1", "uuid");
                this.RichFindAll = PostgresCommandFactory.PreparedCommand(cms, "FM-FormABC-Input", @"SELECT _r FROM ""FormABC"".""Input_entity"" _r WHERE _r.""ID"" = ANY($1)", "uuid[]");
            }
Example #5
0
            internal _FindResult_()
            {
                cms = Common.Utility.UseThreadLocalStream();
                NoTemplateCommand = PostgresCommandFactory.NewCommand(cms);

                this.RichFind    = PostgresCommandFactory.PreparedCommand(cms, "FS-Test-XYZ", @"SELECT _r FROM ""Test"".""XYZ_entity"" _r WHERE _r.""ID"" = $1", "int4");
                this.RichFindAll = PostgresCommandFactory.PreparedCommand(cms, "FM-Test-XYZ", @"SELECT _r FROM ""Test"".""XYZ_entity"" _r WHERE _r.""ID"" = ANY($1)", "int4[]");
            }
            internal _FindResult_()
            {
                cms = Common.Utility.UseThreadLocalStream();
                NoTemplateCommand = PostgresCommandFactory.NewCommand(cms);

                this.OnePkCommand = PostgresCommandFactory.NewCommand(cms, @"SELECT _r FROM ""UseCase1"".""SubmissionList"" _r WHERE _r.""URI"" = :pk");
                this.InPkCommand  = PostgresCommandFactory.NewCommand(cms, @"SELECT _r FROM ""UseCase1"".""SubmissionList"" _r WHERE _r.""URI"" IN (:pks)");
            }
            internal _FindResult_()
            {
                cms = Common.Utility.UseThreadLocalStream();
                NoTemplateCommand = PostgresCommandFactory.NewCommand(cms);

                this.RichFind    = PostgresCommandFactory.PreparedCommand(cms, "FS-UseCase1-Submission", @"SELECT _r FROM ""UseCase1"".""Submission_entity"" _r WHERE _r.""ID"" = $1", "uuid");
                this.RichFindAll = PostgresCommandFactory.PreparedCommand(cms, "FM-UseCase1-Submission", @"SELECT _r FROM ""UseCase1"".""Submission_entity"" _r WHERE _r.""ID"" = ANY($1)", "uuid[]");
            }
Example #8
0
        public IDbCommand CreateQuery(Revenj.Utility.ChunkedMemoryStream cms)
        {
            var writer = cms.GetWriter();

            writer.Write(Statement);
            writer.Flush();
            cms.Position = 0;
            return(PostgresCommandFactory.NewCommand(cms, Statement, Query.Selects.Count == 1));
        }
        internal void PopulateDatabase(IServiceProvider locator, SelectedSubmission.Result result, HashSet <string> processed)
        {
            var query = locator.Resolve <IDatabaseQuery>();
            var cms   = Common.Utility.UseThreadLocalStream();
            var sw    = cms.GetWriter();

            sw.Write("SELECT \"UseCase1\".\"SelectedSubmission\"(");


            sw.Write("'");
            _DatabaseCommon.Utility.StringToTuple(this.uri).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
            sw.Write("'");
            sw.Write(")");
            sw.Flush();
            cms.Position = 0;
            var com = PostgresCommandFactory.NewCommand(cms, "SELECT \"UseCase1\".\"SelectedSubmission\"(:args)");

            query.Execute(com, dr => PopulateFromReader(dr.GetValue(0), locator, result, processed, cms));
        }
Example #10
0
        public bool Exists <TCondition>(ISpecification <TCondition> specification)
        {
            Revenj.Utility.ChunkedMemoryStream cms = null;
            System.IO.TextWriter sw = null;
            var selectType          = "SELECT exists(SELECT *";

            if (specification == null)
            {
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(@"SELECT exists(SELECT * FROM ""Test"".""XYZ_entity"" r");
            }


            else if (specification is global::Test.XYZ.FindBy)
            {
                var spec = specification as global::Test.XYZ.FindBy;
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(selectType);
                sw.Write(@" FROM ""Test"".""XYZ.FindBy""(");

                sw.Write('\'');
                _DatabaseCommon.Utility.StringToTuple(spec.from).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write('\'');
                sw.Write(") _it");
            }
            else
            {
                return(Query(specification).Any());
            }

            sw.Write(')');
            sw.Flush();
            cms.Position = 0;
            var  com   = PostgresCommandFactory.NewCommand(cms);          //TODO: sql template
            bool found = false;

            DatabaseQuery.Execute(com, dr => { found = dr.GetBoolean(0); });
            return(found);
        }
Example #11
0
        private void _InternalDoPersistStandard(Revenj.Utility.ChunkedMemoryStream cms, global::UseCase1.Customer[] insertedData, List <KeyValuePair <global::UseCase1.Customer, global::UseCase1.Customer> > updatedData, global::UseCase1.Customer[] deletedData)
        {
            var sw = cms.GetWriter();

            sw.Write("/*NO LOAD BALANCE*/SELECT \"UseCase1\".\"persist_Customer\"(");
            PostgresTypedArray.ToArray(sw, cms.SmallBuffer, insertedData, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateTupleFrom);
            sw.Write(@"::""UseCase1"".""Customer_entity""[],");
            PostgresTypedArray.ToArray(sw, cms.SmallBuffer, updatedData.Select(it => it.Key), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateTupleFrom);
            sw.Write(@"::""UseCase1"".""Customer_entity""[],");
            PostgresTypedArray.ToArray(sw, cms.SmallBuffer, updatedData.Select(it => it.Value), _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateTupleFrom);
            sw.Write(@"::""UseCase1"".""Customer_entity""[],");
            PostgresTypedArray.ToArray(sw, cms.SmallBuffer, deletedData, _DatabaseCommon.FactoryUseCase1_Customer.CustomerConverter.CreateTupleFrom);
            sw.Write(@"::""UseCase1"".""Customer_entity""[]");



            sw.Write(")");



            sw.Flush();
            cms.Position = 0;
            var    com       = PostgresCommandFactory.NewCommand(cms, "SELECT \"UseCase1\".\"persist_Customer\"(:insert,:update_old,:update_new,:delete)");
            string _sqlError = null;

            DatabaseQuery.Execute(com, dr => _sqlError = dr.GetString(0));
            if (_sqlError != null)
            {
                throw new PostgresException(_sqlError);
            }


            foreach (var item in insertedData)
            {
                item.__ResetChangeTracking();
            }
            foreach (var item in updatedData)
            {
                item.Value.__ResetChangeTracking();
            }
        }
Example #12
0
        public long Count <TCondition>(ISpecification <TCondition> specification)
        {
            Revenj.Utility.ChunkedMemoryStream cms = null;
            System.IO.TextWriter sw = null;
            var selectType          = "SELECT count(*)";

            if (specification == null)
            {
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(@"SELECT count(*) FROM ""UseCase1"".""SelectedSubmission"" r");
            }


            else if (specification is global::UseCase1.SelectedSubmission.Where)
            {
                var spec = specification as global::UseCase1.SelectedSubmission.Where;
                cms = Common.Utility.UseThreadLocalStream();
                sw  = cms.GetWriter();
                sw.Write(selectType);
                sw.Write(@" FROM ""UseCase1"".""SelectedSubmission.Where""(");

                sw.Write('\'');
                _DatabaseCommon.Utility.StringToTuple(spec.uri).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write('\'');
                sw.Write(") _it");
            }
            else
            {
                return(Query(specification).LongCount());
            }

            sw.Flush();
            cms.Position = 0;
            var  com   = PostgresCommandFactory.NewCommand(cms);          //TODO: sql template
            long total = 0;

            DatabaseQuery.Execute(com, dr => { total = dr.GetInt64(0); });
            return(total);
        }
Example #13
0
        private void _InternalDoPersist(Revenj.Utility.ChunkedMemoryStream cms, global::FormABC.Input[] insertedData, List <KeyValuePair <global::FormABC.Input, global::FormABC.Input> > updatedData, global::FormABC.Input[] deletedData)
        {
            var sw = cms.GetWriter();

            if (insertedData.Length == 1 && updatedData.Count == 0 && deletedData.Length == 0)
            {
                sw.Write("/*NO LOAD BALANCE*/SELECT \"FormABC\".\"insert_Input\"(ARRAY['");
                _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(insertedData[0], _DatabaseCommon.FactoryFormABC_Input.InputConverter.TableTuples).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""])");



                sw.Flush();
                cms.Position = 0;
                var com = PostgresCommandFactory.NewCommand(cms, "SELECT \"FormABC\".\"insert_Input\"(:insert)");
                DatabaseQuery.Execute(com);
            }
            else if (insertedData.Length == 0 && updatedData.Count == 1 && deletedData.Length == 0)
            {
                sw.Write("/*NO LOAD BALANCE*/SELECT \"FormABC\".\"update_Input\"(ARRAY['");
                _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(updatedData[0].Key, _DatabaseCommon.FactoryFormABC_Input.InputConverter.PrimaryKeyUpdateTuples).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""],ARRAY['");
                _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(updatedData[0].Value, _DatabaseCommon.FactoryFormABC_Input.InputConverter.TableTuples).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""]");



                sw.Write(')');



                sw.Flush();
                cms.Position = 0;
                var    com       = PostgresCommandFactory.NewCommand(cms, "SELECT \"FormABC\".\"update_Input\"(:old_record,:new_record)");
                string _sqlError = null;
                DatabaseQuery.Execute(com, dr => _sqlError = dr.GetString(0));
                if (_sqlError != null)
                {
                    throw new PostgresException(_sqlError);
                }
            }
            else
            {
                sw.Write("/*NO LOAD BALANCE*/SELECT \"FormABC\".\"persist_Input\"('");
                var arr = new IPostgresTuple[insertedData.Length];
                for (int i = 0; i < insertedData.Length; i++)
                {
                    arr[i] = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(insertedData[i], _DatabaseCommon.FactoryFormABC_Input.InputConverter.TableTuples);
                }
                ArrayTuple.From(arr).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""[],'");
                arr = new IPostgresTuple[updatedData.Count];
                for (int i = 0; i < updatedData.Count; i++)
                {
                    arr[i] = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(updatedData[i].Key, _DatabaseCommon.FactoryFormABC_Input.InputConverter.PrimaryKeyUpdateTuples);
                }
                ArrayTuple.From(arr).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""[],'");
                for (int i = 0; i < updatedData.Count; i++)
                {
                    arr[i] = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(updatedData[i].Value, _DatabaseCommon.FactoryFormABC_Input.InputConverter.TableTuples);
                }
                ArrayTuple.From(arr).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""[],'");
                arr = new IPostgresTuple[deletedData.Length];
                for (int i = 0; i < deletedData.Length; i++)
                {
                    arr[i] = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateRecordTupleFrom(deletedData[i], _DatabaseCommon.FactoryFormABC_Input.InputConverter.PrimaryKeyDeleteTuples);
                }
                ArrayTuple.From(arr).InsertRecord(sw, cms.SmallBuffer, string.Empty, PostgresTuple.EscapeQuote);
                sw.Write(@"'::""FormABC"".""Input_entity""[]");



                sw.Write(")");



                sw.Flush();
                cms.Position = 0;
                var    com       = PostgresCommandFactory.NewCommand(cms, "SELECT \"FormABC\".\"persist_Input\"(:insert,:update_pairs,:delete)");
                string _sqlError = null;
                DatabaseQuery.Execute(com, dr => _sqlError = dr.GetString(0));
                if (_sqlError != null)
                {
                    throw new PostgresException(_sqlError);
                }
            }

            foreach (var item in insertedData)
            {
                item.__ResetChangeTracking();
            }
            foreach (var item in updatedData)
            {
                item.Value.__ResetChangeTracking();
            }
        }