コード例 #1
0
        public override int Process(string name, List <dynamic> datas)
        {
            if (datas == null || datas.Count == 0)
            {
                return(0);
            }

            if (EntityAdapters.TryGetValue(name, out var metadata) && EntitySessions.TryGetValue(name, out var session))
            {
                switch (metadata.PipelineMode)
                {
                default:
                case PipelineMode.Update:
                case PipelineMode.Insert:
                case PipelineMode.InsertAndIgnoreDuplicate:
                {
                    var insertStmt = session.Prepare(metadata.InsertSql);
                    var batch      = new BatchStatement();
                    foreach (var data in datas)
                    {
                        List <object> values = new List <object>();
                        foreach (var column in metadata.Columns)
                        {
                            if (column.DataType.FullName != DataTypeNames.TimeUuid)
                            {
                                values.Add(column.Property.GetValue(data));
                            }
                            else
                            {
                                var value = column.Property.GetValue(data);
                                values.Add(value == DefaultTimeUuid ? TimeUuid.NewId() : value);
                            }
                        }

                        batch.Add(insertStmt.Bind(values.ToArray()));
                    }
                    ;
                    // Execute the batch
                    session.Execute(batch);
                    break;
                }

                case PipelineMode.InsertNewAndUpdateOld:
                {
                    throw new NotImplementedException("Sql Server not suport InsertNewAndUpdateOld yet.");
                }
                }
            }
            return(datas.Count);
        }
コード例 #2
0
        private void InitDatabaseAndTable()
        {
            foreach (var adapter in EntityAdapters)
            {
                var cluster = Cluster.Builder()
                              .AddContactPoints(ConnectString)
                              .Build();

                var session = cluster.Connect();
                EntitySessions.AddOrUpdate(adapter.Key, session);
                session.CreateKeyspaceIfNotExists(adapter.Value.Table.Database);
                session.ChangeKeyspace(adapter.Value.Table.Database);
                //session.Execute($"DROP table {adapter.Value.Table.Database}.{adapter.Value.Table.Name};");
                session.Execute(GenerateCreateTableSql(adapter.Value));
                //session.Execute(GenerateCreateIndexes(adapter.Value));
            }
        }
コード例 #3
0
        private void InitDatabaseAndTable()
        {
            foreach (var adapter in EntityAdapters)
            {
                var cluster = Cluster.Builder()
                              .AddContactPoints(ConnectString)
                              .Build();

                var session = cluster.Connect();
                session.CreateKeyspaceIfNotExists(adapter.Value.Table.Database);
                session.ChangeKeyspace(adapter.Value.Table.Database);
                session.Execute(GenerateCreateTableSql(adapter.Value));
                var createIndexCql = GenerateCreateIndexes(adapter.Value);
                if (!string.IsNullOrEmpty(createIndexCql))
                {
                    session.Execute(createIndexCql);
                }
                EntitySessions.AddOrUpdate(adapter.Key, session);
            }
        }
コード例 #4
0
        public override int Process(string entityName, List <DataObject> datas)
        {
            if (datas == null || datas.Count == 0)
            {
                return(0);
            }
            int count = 0;

            if (EntityAdapters.TryGetValue(entityName, out var metadata) && EntitySessions.TryGetValue(entityName, out var session))
            {
                switch (metadata.PipelineMode)
                {
                case PipelineMode.Insert:
                {
                    var insertStmt = session.Prepare(metadata.InsertSql);
                    var batch      = new BatchStatement();
                    foreach (var data in datas)
                    {
                        data.Remove(Env.IdColumn);
                        var now = DateTime.Now;
                        data.Add("CDate", DateTime.Now);
                        data["run_id"] = DateTime.Now;
                        batch.Add(insertStmt.Bind(data.Values.ToArray()));
                    }
                    ;
                    // Execute the batch
                    session.Execute(batch);

                    // ...you should reuse the prepared statement
                    // Bind the parameters and add the statement to the batch batch
                    break;
                }

                case PipelineMode.InsertAndIgnoreDuplicate:
                {
                    IMapper mapper = new Mapper(session);
                    foreach (var data in datas)
                    {
                        mapper.InsertIfNotExists <DataObject>(data);
                    }
                    break;
                }

                case PipelineMode.InsertNewAndUpdateOld:
                {
                    throw new NotImplementedException("Sql Server not suport InsertNewAndUpdateOld yet.");
                }

                case PipelineMode.Update:
                {
                    break;
                }

                default:
                {
                    var     insertStmt = session.Prepare(metadata.InsertSql);
                    IMapper mapper     = new Mapper(session);
                    // ...you should reuse the prepared statement
                    // Bind the parameters and add the statement to the batch batch
                    foreach (var data in datas)
                    {
                        mapper.Insert <DataObject>(data);
                    }
                    break;
                }
                }
            }
            return(count);
        }