Пример #1
0
        private TableConfiguration getTableConfiguration(Type objectType)
        {
            TableConfiguration result = _tableConfigurations.Where(x => x.TableMap.ObjectType == objectType).FirstOrDefault();

            if (result == null)
            {
                throw new Exception(string.Format("Type {0} has not been configured in the DatabaseFactory", objectType.FullName));
            }

            return(result);
        }
Пример #2
0
        private PropertyMap getKeyPropertyMap(TableConfiguration tableConfiguration)
        {
            PropertyMap result = tableConfiguration.PropertyMaps.Where(x => x.KeyType != KeyType.None).FirstOrDefault();

            if (result == null)
            {
                throw new Exception(string.Format("Key property has not been configured for Type {0}", tableConfiguration.TableMap.ObjectType.FullName));
            }

            return(result);
        }
Пример #3
0
        public void Insert(object obj)
        {
            //TODO: How to make Insert use NonQueryCommand and ScalarCommand?

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type objectType = obj.GetType();
            TableConfiguration  tableConfiguration = getTableConfiguration(objectType);
            PropertyMap         keyPropertyMap     = getKeyPropertyMap(tableConfiguration);
            IList <PropertyMap> propertyMaps       = tableConfiguration.PropertyMaps.Where(x => x.KeyType != KeyType.Identity).ToList();
            bool keyIsIdentity = keyPropertyMap.KeyType == KeyType.Identity;

            string[] columnNames    = propertyMaps.Select(x => x.ColumnName).ToArray();
            string[] parameterNames = propertyMaps.Select(x => x.ParameterName).ToArray();

            //TODO: How to handle different database dialects?
            string commandText = string.Format("INSERT INTO {0}({1}) VALUES({2});", tableConfiguration.TableMap.TableName, string.Join(", ", columnNames), string.Join(", ", parameterNames));

            if (keyIsIdentity)
            {
                commandText = string.Format("{0} {1}", commandText, Constants.IDENTITY_DIALECT[_providerName]);
            }

            using (IDbCommand command = _connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.CommandType = CommandType.Text;

                if (_transaction != null)
                {
                    command.Transaction = _transaction;
                }

                addParameters(command, propertyMaps, obj);

                if (keyIsIdentity)
                {
                    object scalarValue = command.ExecuteScalar();
                    setPropertyValue(obj, keyPropertyMap.PropertyName, scalarValue);
                }
                else
                {
                    command.ExecuteNonQuery();
                }
            }
        }
Пример #4
0
        public IList <TObject> Select <TObject>()
        {
            TableConfiguration tableConfiguration = getTableConfiguration(typeof(TObject));

            string[] columnNames = tableConfiguration.PropertyMaps.Select(x => x.ColumnName).ToArray();

            //TODO: How to handle different database dialects?
            string commandText = string.Format("SELECT {0} FROM {1};", string.Join(", ", columnNames), tableConfiguration.TableMap.TableName);

            QueryCommand <TObject> queryCommand = new QueryCommand <TObject>(_connection, _transaction, commandText, _tableConfigurations);

            foreach (PropertyMap propertyMap in tableConfiguration.PropertyMaps)
            {
                queryCommand.addMap(propertyMap.PropertyName, propertyMap.ColumnName);
            }

            return(queryCommand.Execute());
        }
Пример #5
0
        public void Delete(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type objectType = obj.GetType();
            TableConfiguration tableConfiguration = getTableConfiguration(objectType);
            PropertyMap        keyPropertyMap     = getKeyPropertyMap(tableConfiguration);

            string commandText = string.Format("DELETE FROM {0} WHERE {1} = {2}", tableConfiguration.TableMap.TableName, keyPropertyMap.ColumnName, keyPropertyMap.ParameterName);

            NonQueryCommand nonQueryCommand = new NonQueryCommand(_connection, _transaction, commandText);

            object parameterValue = getPropertyValue(obj, keyPropertyMap.PropertyName);

            nonQueryCommand.addParameter(keyPropertyMap.ParameterName, parameterValue, keyPropertyMap.ParameterType);

            nonQueryCommand.Execute();
        }