NHibernate Mapping By Code Cascade All-Delete-Orphans - nhibernate

How to set cascade to all-delete-orphans with mapping by code in NHibernate?
[Flags]
public enum Cascade
{
None = 0,
Persist = 2,
Refresh = 4,
Merge = 8,
Remove = 16,
Detach = 32,
ReAttach = 64,
DeleteOrphans = 128,
All = 256,
}
How can I combine All & DeleteOrphans?

Try to use:
r.Cascade(Cascade.All | Cascade.DeleteOrphans);
Because the Cascade is a [Flag] ... multi could be used:
What does the [Flags] Enum Attribute mean in C#?

Related

How to write a cupy user-defined kernel function to calculate the segmented sum

I use the following function now, but I don't think it works, but I can't understand the description of the cupy kernel definition. This function is very memory intensive and time-consuming when it comes to huge data.
def cupy_sum(self, bins):
bidx = cupy.cumsum(bins) -1,
return cupy.diff(cupy.r_[0, cupy.cumsum(self)[bidx]])
Refer to other examples and write the following code, do not know if there is a problem.
sum_section_kernel = cp.ElementwiseKernel(
'raw T bins, raw T dats',
'float32 out',
'''
T bin_f = bins[i ];
T bin_l = bins[i+1];
T biv = 0;
for(size_t j=bin_f; j<bin_l; j++){
biv += dats[j];
}
out = biv;
''',
'summe')
a = cp.array([4, 3, 5], dtype=cp.float32)
b = cp.array([1, 1, 1.1, 1, 2, 2, 2, 3, 3, 3, 3, 3], dtype=cp.float32)
y = cp.empty(3, dtype=cp.float32)
a = cp.r_[0,a.cumsum()]
out = sum_section_kernel(a, b, y)
print(out)
> [ 4.100 6.000 15.000]
The example has been put in the above, and the speed has not been improved, but I think there is still the advantage of saving memory.

Sqlalchemy relationship issue working with datatables

I'm currently struggling creating web tables using bootstrap datatables, sqlalchemy and sqlalchemy-datatables.
Sqlalchemy seems generating correct sql query, datatables is populated with correct information.
However when I'm trying to search for record in datatable search field I'm getting an error:
DataTables warning: table id=main_table - Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with VrfMain.scope has an attribute 'cast'
I tried to remove any relationships from query, and it works.
So problem is with relationship somewhere. Can anyone help me please ?
Here is my sql models:
class VrfMain(db.Model):
__tablename__ = 'vrf_main'
id = Column(Integer, primary_key=True, autoincrement=True)
vrf_name = Column(String, unique=True)
rd = Column(String, unique=True)
primary_rt = Column(String, unique=True)
additional_rt = Column(String, unique=True)
description = Column(String)
scope_id = Column(Integer, ForeignKey('subnet_scopes.id'))
scope = relationship('SubnetScopes')
def __init__(self, vrf_name, rd, primary_rt, description, scope_id):
self.vrf_name = vrf_name
self.rd = rd
self.primary_rt = primary_rt
self.description = description
self.scope_id = scope_id
class SubnetScopes(db.Model):
__tablename__ = 'subnet_scopes'
id = Column(Integer, primary_key=True, autoincrement=True)
scope_name = Column(String, unique=True)
def __init__(self, scope_name):
self.scope_name = scope_name
def __repr__(self):
return str(self.scope_name)
Here is part of flask code:
# defining datatable columns
columns = [
ColumnDT(VrfMain.id),
ColumnDT(VrfMain.vrf_name),
ColumnDT(VrfMain.rd),
ColumnDT(VrfMain.primary_rt),
ColumnDT(VrfMain.additional_rt),
ColumnDT(VrfMain.description),
ColumnDT(VrfMain.scope)
]
query = VrfMain.query.\
join(SubnetScopes).\
filter(VrfMain.scope_id == SubnetScopes.id).\
with_entities(VrfMain.id, VrfMain.vrf_name, VrfMain.rd, VrfMain.primary_rt, VrfMain.additional_rt, VrfMain.description, SubnetScopes.scope_name)
print(query)
params = request.args.to_dict()
rowTable = DataTables(params, query, columns)
return jsonify(rowTable.output_result())
Here is sql query that is generated
SELECT vrf_main.id AS vrf_main_id, vrf_main.vrf_name AS vrf_main_vrf_name, vrf_main.rd AS vrf_main_rd, vrf_main.primary_rt AS vrf_main_primary_rt, vrf_main.additional_rt AS vrf_main_additional_rt, vrf_main.description AS vrf_main_description, subnet_scopes.scope_name AS subnet_scopes_scope_name
FROM vrf_main INNER JOIN subnet_scopes ON subnet_scopes.id = vrf_main.scope_id
WHERE vrf_main.scope_id = subnet_scopes.id
Here is javascript code:
$(document).ready(function() {
var table = $('#main_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "{{ url_for('home_blueprint.get_vrf_data') }}"
},
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
I decide not to use sqlalchemy-datatables. instead I'm will use code:
all_vrf_data = []
for row in VrfMain.query.all():
row_proccessed = dict(row.__dict__); row_proccessed.pop('_sa_instance_state', None)
all_vrf_data.append(row_proccessed)
return_data = {"data": all_vrf_data}
return json.dumps(return_data, indent=4, sort_keys=True, default=str)

Syntax error in spatial query?

I wrote a function for found all pois around a track
controller :
def index
#track = Track.friendly.find(params[:track_id])
#tracks = Track.where(way_id: #track.id)
#way = Way.find(1)
#poi_start = Poi.find(#way.point_start)
#pois = #track.pois.sleepsAndtowns
#pois = #way.poi_around_track_from(#poi_start, 50000, #pois)
end
way.rb
def poi_around_track_from(poi, dist, pois)
around_sql = <<-SQL
SELECT
ST_DWithin(
ST_LineSubstring(
way.path,
ST_LineLocatePoint(way.path, pta.lonlat::geometry) + #{dist} / ST_Length(way.path::geography),
ST_LineLocatePoint(way.path, pta.lonlat::geometry) + 100000 / ST_Length(way.path::geography)
),
ptb.lonlat,
2000) is true as pois
FROM ways way, pois pta, pois ptb
WHERE way.id = #{self.id}
and pta.id = #{poi.id}
and ptb.id = #{pois.ids}
SQL
Poi.find_by_sql(around_sql).pois
end
This function return :
syntax error at or near "["
LINE 13: and ptb.id = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
What's wrong, how can I fix it ?
Since you are using standard sql to build the query, (not the ActiveRecord), you will have to use the standard IN clues with where
It looks like pois.ids is returning an array, so, you will have to turn it to a string in the format as below
[1,2] #=> (1,2)
Change,
WHERE way.id = #{self.id}
and pta.id = #{poi.id}
and ptb.id = #{pois.ids}
to
WHERE way.id = #{self.id}
and pta.id = #{poi.id}
and ptb.id IN (#{pois.ids.join(',')})
You can change pois.ids as #semeera207 wrote to string or go another way and compare ptb.id to pois.ids as an array.
WHERE way.id = #{self.id}
and pta.id = #{poi.id}
and array[ptb.id] && #{pois.ids}
To make it faster create gin index
Create index on pois using gin((array[id]));

Insert list register with onchange (Odoo)

class base(models.Model):
_name = 'base'
name = fields.Char("Name")
c_id = fields.Many2one('base.ch')
class base_ch(models.Model):
_name = 'base.ch'
name = fields.Char("Name")
q_ids = fields.One2many("base.q","c_id")
class base_q(models.Model):
_name = "base.q"
name = fields.Char("Name")
c_id = fields.Many2one('base.ch',"Basec")
class base_h(models.Model):
_name = "base.h"
name = fields.Char("Name")
select = fields.Selection([('a', 'A'), ('b', 'B')], "select")
desc = fields.Char("Desc")
I have these classes and I want to add in the base class a field of the base_h class in tree view format.
I need to do an onchange function in the base class that when choosing c_id modify the name field of the added field of the base_h class with the records of c_id.q_ids
I tried:
#api.onchange('ch_id')
def onchange_ch(self):
if self.ch_id.q_ids:
self.one2manyfield.name = [(6, 0, self.ch_id.q_ids)]
#also with-> self.one2manyfield = [(6, 0, self.ch_id.q_ids)]
But it does not work
(6, 0, [IDs]) will take list of ids.
Try with following code.
#api.onchange('ch_id')
def onchange_ch(self):
if self.ch_id and self.ch_id.q_ids:
one2manyfield = [(6, 0, self.cht_id.q_ids.ids)]
This is the function that works
#api.onchange('ch_id')
def onch_ch(self):
valors = []
for r in self.ch_id.q_ids:
register = {'name': r.name}
valors.append(register)
self.one2manyfield = valors

Enum bitwise masking limitations

I was trying to enumerate filetypes with bitmasking for fast and easy distinguishing on bitwise OR:
typedef enum {
FileTypeDirectory = 1,
FileTypePIX = 2,
FileTypeJPG = 4,
FileTypePNG = 8,
FileTypeGIF = 16,
FileTypeHTML = 32,
FileTypeXML = 64,
FileTypeTXT = 128,
FileTypePDF = 256,
FileTypePPTX = 512,
FileTypeAll = 1023
} FileType;
My OR operations did work until 128, afterwards it failed. Are enums on a 64 Bit Mac OSX limited to Byte Datatypes? (2^7=128)
All enum constants in C are of type int and not of the type of the enumeration itself. So the restriction is not in the storage size for enum variables, but only in the number of bits for an int.
I don't know much of objective-c (as this is tagged also) but it shouldn't deviate much from C.
I'm not quite sure how you used the OR operator but it works for me well with your typedef.
FileType _fileType = FileTypeGIF | FileTypePDF | FileTypePPTX;
NSLog(#"filetype is : %d", _fileType);
the result is:
filetype is : 784
which is correct values because 16 + 256 + 512 is precisely 784.
(it has been tested on real device only.)