示例#1
0
        /// <summary>
        /// Executes the query defined on this instance and returns a matrix.
        /// </summary>
        public object[][] ExecuteMatrix(ConnectionStringSettings connection)
        {
            if (Top > 0 && eagerLoading.Count > 0)
            {
                throw new NotSupportedException("Cannot use eager loading with Top/Limit.");
            }

            Tenor.Data.DataTable rs = SearchWithDataTable(connection);

            List <object[]> result = new List <object[]>();

            foreach (DataRow row in rs.Rows)
            {
                List <object> colums = new List <object>();
                for (int i = 0; i < rs.Columns.Count; i++)
                {
                    if (row[i] is DBNull)
                    {
                        colums.Add(null);
                    }
                    else
                    {
                        colums.Add(row[i]);
                    }
                }
                result.Add(colums.ToArray());
            }

            return(result.ToArray());
        }
示例#2
0
 /// <summary>
 /// Executes the query defined on this instance and returns the number of returned rows.
 /// </summary>
 public int ExecuteCount(ConnectionStringSettings connection)
 {
     if (this.eagerLoading.Count > 0)
     {
         throw new NotSupportedException("Cannot use eager loading with aggregation.");
     }
     Tenor.Data.DataTable rs = SearchWithDataTable(connection, true);
     return(System.Convert.ToInt32(rs.Rows[0][0]));
 }
示例#3
0
        /// <summary>
        /// Executes the query defined on this instance.
        /// </summary>
        public EntityBase[] Execute(ConnectionStringSettings connection)
        {
            if (Top > 0 && eagerLoading.Count > 0)
            {
                throw new NotSupportedException("Cannot use eager loading with Top/Limit.");
            }
            if (Projections.Count > 0)
            {
                throw new NotSupportedException("Cannot use projections without a Linq Query.");
            }


            Tenor.Data.DataTable rs = SearchWithDataTable(connection);
            return(EntityBase.BindRows(rs, this));
        }
示例#4
0
        private Tenor.Data.DataTable SearchWithDataTable(ConnectionStringSettings connection, bool justCount, int?skip, int?take)
        {
            TenorParameter[] parameters = null;
            if (connection == null)
            {
                TableInfo table = TableInfo.CreateTableInfo(this.baseType);
                if (table == null)
                {
                    throw new Tenor.Data.MissingTableMetaDataException(this.baseType);
                }
                connection = table.GetConnection();
            }

            string sql = EntityBase.GetSearchSql(this, justCount, skip, take, connection, out parameters);

            Tenor.Data.DataTable rs = new Tenor.Data.DataTable(sql, parameters, connection);
            DataSet ds = new DataSet();

            ds.Tables.Add(rs);
            ds.EnforceConstraints = false;
            rs.Bind();

            return(rs);
        }
示例#5
0
        /// <summary>
        /// Gets the value of a lazy tagged property.
        /// This call can do a database access.
        /// </summary>
        internal virtual object GetPropertyValue(string propertyName, bool forceGetBinary)
        {
            //only loads if not loaded yet.
            //TODO: provide a way to reload from database.
            lock (propertyData)
            {
                if (forceGetBinary || !propertyData.ContainsKey(propertyName))
                {
                    //Getting class metadata.

                    TableInfo table = TableInfo.CreateTableInfo(this.GetType());

                    System.Reflection.PropertyInfo fieldP = this.GetType().GetProperty(propertyName);
                    if (fieldP == null)
                    {
                        throw new Tenor.Data.MissingFieldException(this.GetType(), propertyName);
                    }

                    ForeignKeyInfo fkInfo = null;

                    fkInfo = ForeignKeyInfo.Create(fieldP);

                    if (fkInfo != null)
                    {
                        // this is an FK, so, route to fk loading
                        return(LoadForeign(fieldP, null));
                    }
                    else
                    {
                        //Continue to the lazy property (lazy fields like byte[]) loading
                        FieldInfo field = FieldInfo.Create(fieldP);
                        if (field == null)
                        {
                            throw new Tenor.Data.MissingFieldException(this.GetType(), fieldP.Name);
                        }

                        if (!forceGetBinary && (field.FieldType == typeof(BinaryStream) || field.FieldType == typeof(BinaryStream).BaseType))
                        {
                            propertyData[propertyName] = new BinaryStream(this, propertyName);
                        }
                        else
                        {
                            GeneralDialect dialect = DialectFactory.CreateDialect(table.GetConnection());


                            ConditionCollection conditions = new ConditionCollection();

                            foreach (FieldInfo f in GetFields(this.GetType(), true))
                            {
                                conditions.Add(f.RelatedProperty.Name, f.PropertyValue(this));
                            }
                            if (conditions.Count == 0)
                            {
                                throw (new Tenor.Data.MissingPrimaryKeyException(this.GetType()));
                            }

                            TenorParameter[] parameters = null;
                            string           fieldsPart = dialect.CreateSelectSql(table.RelatedTable, null, new FieldInfo[] { field }, null, null);
                            string           wherePart  = dialect.CreateWhereSql(conditions, table.RelatedTable, null, out parameters);
                            string           sql        = dialect.CreateFullSql(table.RelatedTable,
                                                                                false, false,
                                                                                0, fieldsPart, null, null, wherePart);

                            Tenor.Diagnostics.Debug.DebugSQL("GetPropertyValue()", sql, parameters, table.GetConnection());
#if DEBUG
                            LastSearches.Push(sql);
#endif
                            Tenor.Data.DataTable rs = new Tenor.Data.DataTable(sql, parameters, table.GetConnection());
                            rs.Bind();

                            if (rs.Rows.Count == 0)
                            {
                                throw (new RecordNotFoundException());
                            }
                            else if (rs.Rows.Count > 1)
                            {
                                throw new ManyRecordsFoundException();
                            }
                            else
                            {
                                var obj = rs.Rows[0][field.DataFieldName];
                                if (obj == DBNull.Value)
                                {
                                    obj = null;
                                }

                                if (forceGetBinary)
                                {
                                    if (obj == null)
                                    {
                                        return new byte[] { }
                                    }
                                    ;
                                    else
                                    {
                                        return(obj);
                                    }
                                }
                                else
                                {
                                    propertyData[propertyName] = obj;
                                }
                            }
                        }
                    }
                }
                return(propertyData[propertyName]);
            }
        }
示例#6
0
 /// <summary>
 /// Executes the query defined on this instance respecting skip and take arguments.
 /// </summary>
 /// <param name="skip">Number of rows to skip</param>
 /// <param name="take">Number of rows to take</param>
 public EntityBase[] ExecuteSkipTake(int skip, int take, ConnectionStringSettings connection)
 {
     Tenor.Data.DataTable rs = SearchWithDataTable(connection, false, skip, take);
     return(EntityBase.BindRows(rs, this));
 }