Example #1
0
        internal object GetScalar(string sqlQuery, CSParameterCollection parameters)
        {
            long logId = Log(sqlQuery, parameters);

            try
            {
                using (ICSDbCommand dbCommand = CreateCommandInternal(sqlQuery, parameters))
                {
                    using (ICSDbReader reader = dbCommand.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            object r = reader[0];

                            return((r is DBNull) ? null : r);
                        }
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw new CSSQLException("GetScalar exception", ex, sqlQuery, parameters);
            }
            finally
            {
                LogEnd(logId);

                CSNameGenerator.Reset();
            }
        }
Example #2
0
        internal void ReadRelationToOne(CSSchemaField schemaField, ICSDbReader dataReader, Dictionary <string, string> aliasMap)
        {
            try
            {
                CSFieldValue fieldLocal = _fieldData["#" + schemaField.Relation.LocalKey];

                if (fieldLocal.Value != null)
                {
                    CSObject relationObject = CSFactory.New(schemaField.FieldType);

                    if (dataReader != null)
                    {
                        relationObject.FromDataReader(dataReader, aliasMap);
                    }
                    else if (!relationObject.ReadUsingUniqueKey(schemaField.Relation.ForeignKey, fieldLocal.Value))
                    {
                        throw new CSException("Relation " + schemaField.Name + " could not be read");
                    }

                    _fieldData[schemaField.Name].ValueDirect = relationObject;
                    _fieldData[schemaField.Name].ValueState  = CSFieldValueState.Read;
                }
                else
                {
                    _fieldData[schemaField.Name].ValueDirect = null;
                    _fieldData[schemaField.Name].ValueState  = CSFieldValueState.Read;
                }
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Example #3
0
        private T[] RunQuery <T>(string sql, CSParameterCollection parameters, int maxRows) where T : new()
        {
            Type objectType = typeof(T);

            List <T> list = new List <T>();

            if (maxRows == 0)
            {
                maxRows = int.MaxValue;
            }

            using (new CSTransaction(DB))
            {
                using (ICSDbReader reader = DB.CreateReader(sql ?? CSHelper.GetQueryExpression <T>(), parameters))
                {
                    int rowNum = 0;

                    while (rowNum < maxRows && reader.Read())
                    {
                        rowNum++;

                        T obj = new T();

                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            string columnName = reader.GetName(i);

                            PropertyInfo propertyInfo = objectType.GetProperty(columnName);

                            object columnValue = reader[i];

                            if (columnValue is DBNull)
                            {
                                columnValue = null;
                            }

                            if (propertyInfo != null)
                            {
                                propertyInfo.SetValue(obj, columnValue.Convert(propertyInfo.PropertyType), null);
                            }
                            else
                            {
                                FieldInfo fieldInfo = objectType.GetField(columnName);

                                if (fieldInfo != null)
                                {
                                    fieldInfo.SetValue(obj, columnValue.Convert(fieldInfo.FieldType));
                                }
                            }
                        }

                        list.Add(obj);
                    }
                }
            }

            return(list.ToArray());
        }
Example #4
0
        private List <TObjectType> GetObjects(string sqlQuery, CSParameterCollection parameters, Dictionary <string, string> aliasMap, IEnumerable <PrefetchField> prefetchFields)
        {
            using (CSTransaction csTransaction = new CSTransaction(Schema))
            {
                List <TObjectType> objectList = new List <TObjectType>();

                using (ICSDbReader reader = DB.CreateReader(sqlQuery, parameters))
                {
                    while (reader.Read())
                    {
                        TObjectType csObject = CSObject <TObjectType> .New();

                        csObject.Fire_ObjectReading();

                        csObject.FromDataReader(reader, aliasMap);

                        foreach (PrefetchField prefetchField in prefetchFields)
                        {
                            csObject.ReadRelationToOne(prefetchField.SchemaField, reader, prefetchField.AliasMap);
                        }

                        if (FilterPredicate != null)
                        {
                            bool shouldAdd = true;

                            foreach (Predicate <TObjectType> predicate in FilterPredicate.GetInvocationList())
                            {
                                if (!predicate(csObject))
                                {
                                    shouldAdd = false;
                                    break;
                                }
                            }

                            if (!shouldAdd)
                            {
                                continue;
                            }
                        }

                        csObject.ObjectDeleted += OnObjectDeleted;

                        objectList.Add(csObject);

                        csObject.Fire_ObjectRead();
                    }
                }

                csTransaction.Commit();

                return(objectList);
            }
        }
Example #5
0
        public T[] GetScalarList <T>(string sql, CSParameterCollection parameters)
        {
            List <T> list = new List <T>();

            using (new CSTransaction(DB))
            {
                using (ICSDbReader reader = DB.CreateReader(sql, parameters))
                {
                    while (reader.Read())
                    {
                        list.Add(reader[0].Convert <T>());
                    }
                }
            }

            return(list.ToArray());
        }
Example #6
0
        protected internal override string BuildGetKeys(string tableName, string[] columnList, string[] valueList, string[] primaryKeys, string identityField)
        {
            int id;

            using (ICSDbReader reader = CreateReader("SELECT @@IDENTITY", null))
            {
                reader.Read();

                id = (int)reader[0];
            }

            if (primaryKeys != null && primaryKeys.Length > 0 && identityField != null)
            {
                return(String.Format("SELECT {0} from {1} where {2} = " + id, String.Join(",", QuoteFieldList(primaryKeys)), QuoteTable(tableName), identityField));
            }

            return("");
        }
Example #7
0
        internal void FromDataReader(ICSDbReader dataReader, Dictionary <string, string> aliasMap)
        {
            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                string aliasName = dataReader.GetName(i);
                string fieldName;

                if (aliasMap == null)
                {
                    fieldName = aliasName;
                }
                else if (!aliasMap.TryGetValue(aliasName, out fieldName))
                {
                    continue;
                }

                CSSchemaColumn schemaColumn = _schema.Columns[fieldName];

                if (schemaColumn == null || schemaColumn.Hidden)
                {
                    continue;
                }

                object columnValue = dataReader[i];


                if (columnValue == _schema.DB.NullFieldValue())
                {
                    columnValue = null;
                }

                if (schemaColumn.MappedField.Trim && columnValue is string)
                {
                    columnValue = ((string)columnValue).Trim();
                }

                CSFieldValue fieldValue = _fieldData["#" + schemaColumn.Name];

                fieldValue.ValueDirect = columnValue;
                fieldValue.ValueState  = CSFieldValueState.Read;
            }

            _dataState = CSObjectDataState.Loaded;
        }
Example #8
0
        public CSGenericRecord RunSingleQuery(string sql, CSParameterCollection parameters)
        {
            CSGenericRecord rec = new CSGenericRecord();

            using (new CSTransaction(DB))
            {
                using (ICSDbReader reader = DB.CreateReader(sql, parameters))
                {
                    if (reader.Read())
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            rec[reader.GetName(i)] = (reader[i] is DBNull) ? null : reader[i];
                        }

                        return(rec);
                    }
                }
            }

            return(null);
        }
Example #9
0
        public CSGenericRecordList RunQuery(string sql, CSParameterCollection parameters)
        {
            CSGenericRecordList list = new CSGenericRecordList();

            using (new CSTransaction(DB))
            {
                using (ICSDbReader reader = DB.CreateReader(sql, parameters))
                {
                    while (reader.Read())
                    {
                        CSGenericRecord record = new CSGenericRecord();

                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            record[reader.GetName(i)] = (reader[i] is DBNull) ? null : reader[i];
                        }

                        list.Add(record);
                    }
                }
            }

            return(list);
        }
Example #10
0
        private bool Create()
        {
            CSParameterCollection parameters = new CSParameterCollection();

            List <string> fieldNames    = new List <string>();
            List <string> fieldValues   = new List <string>();
            List <string> sequenceNames = new List <string>();

            foreach (CSSchemaColumn schemaColumn in _schema.Columns)
            {
                CSFieldValue fieldValue = _fieldData["#" + schemaColumn.Name];

                if (fieldValue == null)
                {
                    continue;
                }

                CSSchemaField schemaField = fieldValue.SchemaField;

                if (schemaField.ClientGenerated && schemaColumn.IsKey)
                {
                    if (schemaField.FieldType == typeof(Guid))
                    {
                        fieldValue.Value = Guid.NewGuid();
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(schemaField.SequenceName) && (!fieldValue.IsDirty || schemaField.ReadOnly || schemaField.NoCreate || schemaColumn.Identity))
                    {
                        continue;
                    }
                }

                if (schemaField.ServerGenerated && schemaColumn.IsKey)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(schemaField.SequenceName))
                {
                    sequenceNames.Add(schemaField.SequenceName);
                    fieldValues.Add(null);
                }
                else
                {
                    CSParameter parameter = parameters.Add();

                    parameter.Value = fieldValue.ValueDirect;

                    fieldValues.Add("@" + parameter.Name.Substring(1));
                    sequenceNames.Add(null);
                }

                fieldNames.Add(schemaColumn.Name);
            }

            string[] primaryKeys = new string[_schema.KeyColumns.Count];

            for (int i = 0; i < _schema.KeyColumns.Count; i++)
            {
                primaryKeys[i] = _schema.KeyColumns[i].Name;
            }


            using (ICSDbReader reader = _schema.DB.ExecuteInsert(_schema.TableName, fieldNames.ToArray(), fieldValues.ToArray(), primaryKeys, sequenceNames.ToArray(), (_schema.IdentityColumn != null && _schema.IdentityColumn.MappedField != null) ? _schema.IdentityColumn.Name : null, parameters))
            {
                if (reader != null && !reader.IsClosed && reader.Read())
                {
                    FromDataReader(reader, null);
                }
            }

            _dataState = CSObjectDataState.Loaded;

            return(true);
        }
Example #11
0
        internal bool ReadFields(CSStringCollection columnList, CSStringCollection keyList, List <object> valueList)
        {
            List <string> fieldList = new List <string>();
            List <string> aliasList = new List <string>();
            Dictionary <string, string> fieldAliasMap = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            CSFilter whereClause = new CSFilter();
            CSTable  table       = new CSTable(_schema);

            CSParameterCollection parameters = new CSParameterCollection();

            foreach (CSSchemaColumn schemaColumn in _schema.Columns)
            {
                if (keyList.Contains(schemaColumn.Name))
                {
                    CSParameter parameter = parameters.Add();

                    parameter.Value = valueList[keyList.IndexOf(schemaColumn.Name)];

                    whereClause = whereClause.And(table.TableAlias + "." + _schema.DB.QuoteField(schemaColumn.Name) + "=" + parameter.Name);

                    _fieldData["#" + schemaColumn.Name].ValueDirect = parameter.Value;
                    _fieldData["#" + schemaColumn.Name].ValueState  = CSFieldValueState.Read;
                }
                else if (columnList.Contains(schemaColumn.Name))
                {
                    string alias = CSNameGenerator.NextFieldAlias;

                    fieldList.Add(table.TableAlias + "." + schemaColumn.Name);
                    aliasList.Add(alias);

                    fieldAliasMap[alias] = schemaColumn.Name;
                }
            }


            /** Build query for prefetch of relations **/

            CSJoinList joinList = new CSJoinList();

            List <PrefetchField> prefetchFields = GetPrefetchFieldsOne(table, fieldList, aliasList, joinList, null);

            if (whereClause.Expression.Length == 0)
            {
                return(false);
            }

            if (fieldList.Count == 0)
            {
                return(true);
            }

            string sqlQuery = _schema.DB.BuildSelectSQL(table.TableName, table.TableAlias, fieldList.ToArray(), aliasList.ToArray(), joinList.BuildJoinExpressions(), whereClause.Expression, null, 1, 1, true, false);

            using (CSTransaction csTransaction = new CSTransaction(_schema))
            {
                using (ICSDbReader dataReader = _schema.DB.CreateReader(sqlQuery, parameters))
                {
                    if (!dataReader.Read())
                    {
                        return(false);
                    }

                    FromDataReader(dataReader, fieldAliasMap);

                    foreach (PrefetchField prefetchField in prefetchFields)
                    {
                        ReadRelationToOne(prefetchField.SchemaField, dataReader, prefetchField.AliasMap);
                    }
                }

                csTransaction.Commit();
            }

            return(true);
        }