2015年07月

Left Outer Joins, WHERE clause and COALESCE

I have three tables:



Users:                  Depts                   Default
| fid | uid | rights | | fid | did | rights | | fid | rights |
|-----+-----+--------| |-----+-----+--------| |-----+--------|
| 1 | 1 | 4 | | 1 | 10 | 2 | | 1 | 2 |
| 3 | 1 | 4 | | 2 | 10 | 2 | | 2 | 4 |
+--------------------+ +--------------------+ +--------------+


These define the access rights a user has to a file in the system based on their user ID and the department of which they are a member. When accessing a file the effective rights are determined in order by the default rights (if any), which are overridden by the departmental rights (if any), which are in turn overridden by the user permissions, if any.



Thus, in this example, user 1, member of department 10, has no user rights for file 2, but gains rights level 2 from his department. (Default rights of 4 don't apply because they're overridden by departmental rights)



I need to query this table structure to determine the effective rights of a user for one or more files. Initially, my only requirement was to obtain the effective rights for one file at a time which I did by assigning a priority to each of three subqueries, combining them with a UNION in order of priority, and then taking only the first result, like this:



select a.rights from 
(select 100 as priority, rights from `Users` where `uid`=1 and `fid` = 2 union
select 200 as priority, rights from `Depts` where `did`=10 and `fid` = 2 union
select 300 as priority, rights from `Default` where `fid` = 2
) as a
order by priority asc limit 1;


Now, this may not be particularly efficient, but it has served so far.





My problem now is to extend this to retrieve a user's effective rights for more than one files. Because of the priority structure this simply can't be done with the existing query.



I should be able to use a FULL OUTER JOIN across the three tables to produce a set of rights values I can COALESCE, but MySQL doesn't support FULL OUTER JOIN



After some digging around on Stack Overflow and elsewhere I arrived at this (UID, Dept ID and File Id are illustrative only):



select u.fid as fid, u.rights as urights, d.rights as drights, def.rights as defRights 
from
(users as u
left outer join Depts as d using (fid)
left outer join Default as def using (fid))
where fid in (21, 1823, 1830) and dept_id=10 and uid=1
union
select u.fid as fid, u.rights as urights, d.rights as drights, def.rights as defRights
from
(Depts as d
left outer join Users as u using (fid)
left outer join Default as def using (fid))
where fid in (21, 1823, 1830) and dept_id=10 and uid=1
union
select u.fid as fid, u.rights as urights, d.rights as drights, def.rights as defRights
from
(Default as def
left outer join Users as u using (fid)
left outer join Depts as d using (fid))
where fid in (21, 1823, 1830) and dept_id=10 and uid=1


Superficially, this appeared to work, but the use of the WHERE clause is filtering the results incorrectly. From my reading it seems that using an ON clause might be the solution, but getting the syntax right has defeated me.



For our example of file #2 above I should expect to get



| fid | Users | Depts | Default |
+-----+-------+-------+---------+
| 2 | null | 2 | 4 |
+-----+-------+-------+---------+


This should give effective rights of 2 when the three rights values are coalesced.



This query may well work for this simple example. The problem that emerged in testing is that some files that should be included weren't.





So, my question is this:



How can I write the query I have with ON clauses instead of WHERE?



As a bonus, is there a better way of achieving the same result?



As a footnote: this entire structure needs to be reviewed in time, but that time is not now. This is what I have to work with until some later development prompts us to implement something better.



Answers

You could create an inline view of all the files you are interested in by unioning them together, instead of using where in. This will maintain the outernish of the query chain



eg:



select files.fid, u.rights, d.rights, def.rights
from (
select 1 as fid
union all
select 2 as fid
union all
select 3 as fid
-- all the files you are interested in
) files
left join users u
on files.fid = u.fid
and u.uid = 2 -- put the user you are searching for here
left join depts d
on files.fid = d.fid
and d.did = 10 -- put the dept you are searching for here
left join `default` def
on files.fid = def.fid


quick demo here





How to debug missing dlls on Windows 10

I've got a Windows 10 ( Windows Universal ) project upgraded from Windows 8.1.



I try to launch this on Windows 10 phone and get




System.IO.FileNotFoundException




when it first time tries to refer to my Windows Runtime Component.




System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at FooRuntimeComponent.STAContextCapture.CaptureContext()
at FooApp.App..ctor()
at FooApp.Program.<>c.b__0_0(ApplicationInitializationCallbackParams p)}




Exception itself doesn't tell me which module is not found.
Here's a list of first level dependencies:



API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
API-MS-WIN-CORE-SYNCH-L1-2-0.DLL
API-MS-WIN-CORE-PROCESSTHREADS-L1-1-2.DLL
API-MS-WIN-CORE-UTIL-L1-1-0.DLL
API-MS-WIN-CORE-ERRORHANDLING-L1-1-1.DLL
API-MS-WIN-CORE-STRING-L1-1-0.DLL
API-MS-WIN-CORE-COM-L1-1-1.DLL
MMDEVAPI.DLL
VCCORLIB140D_APP.DLL
MSVCP140D_APP.DLL
KERNEL32.DLL
OLE32.DLL
CONCRT140D_APP.DLL
VCRUNTIME140D_APP.DLL
UCRTBASED.DLL
WS2_32.DLL
API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ERROR-L1-1-1.DLL
API-MS-WIN-CORE-WINRT-L1-1-0.DLL


If I try to reference and use empty Windows Runtime Component it works.



I tried to recreate project from scratch still not working.



Seems that some of this dlls is missing on the phone. How to understand which one? Is there a FusLogW for VS2015 on Windows 10 ?





how to delete file in java?

I want to delete more than one mp4 format files



 Path p1 = Paths.get("0.mp4");
Files.delete(p1);


works perfectly fine
But when i want to delete more than one video ().mp4. 1.mp4, 2.mp4...). It didn't work.



 Path p1 = Paths.get(videopath);
System.out.println(videopath);
System.out.println(p1);
Files.delete(p1);


videopath is of type String



Output:



0.mp4



0.mp4



1.mp4



1.mp4



Logical error is in Files.delete(p1);



Answers

You might be looking for a variation of the following:



for(File file: fileList) file.delete();


You can replace fileList with something like dir.listFiles() for a given directory, or build a list of files.





Alternatives to synrchonous AJAX calls for absolute needed files?

I'm currently building a backbone app where I absolutely need some files before proceeding with certain tasks.



For Example:




the template files for rendering a view
a language.json file that holds the copy for the whole app


Now what I want to avoid is the callback hell that brings me to. Like I could never use this.template(data) because I always have to check a deferred object / promise if that template is already loaded.



getTemplate: function(path) {

return $.get('/templates/'+path).then(function(src) {

// is the copy already available?
return App.Deferreds.copy.then(function(copy) {

// insert copy into this html string
src = App.Models.Copy.insertCopy(src, copy);

// return a handlebars tempate with copy pre-filled
return Handlebars.compile(src);

});


});

}


So I thought I'd make the AJAX requests for those files synchronous and return the files content instead of a promise.



getTemplate: function(path) {

var template;

$.ajax({
dataType: "json",
url: '/templates/'+path,
async: false,
success: function(src) {

// insert copy into this html string
src = App.Models.Copy.insertCopy(src, copy);

template = Handlebars.compile(src);
}
});

return template;

}


Now chrome is telling me synchronous xmlhttprequest on the main thread is deprecated. And after researching it it is indeed removed from the specs (or will be).



Now what's my alternative here?
How do I load absolutely needed files via JavaScript into a web-app?



It seems odd to use AJAX in the first place when I ALWAYS have to wait for those files.



Answers

Seems like you are trying to implement a poor man's dependancy management system.
You should consider using an existing one such as RequireJS or Browserify





idangero swiper swipe to custom active slide

currently during working on one of my project i come to and issue with idangero swiper below is the issue.



I have 20 slider in wrapper below is html markup and on page load depend on the page i add active class to that particular slider and in markup i have added on Australia .



Html code



   <div class="si-teamList">
<ul class="si-teamList-wrp">


<li class="si-teams active"><span class="si-teamName"><a href="javascript:void(0)">Australia</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Bangladesh</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">England</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">India</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">New Zealand</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Pakistan</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">South Africa</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Sri Lanka</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">West Indies</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Zimbabwe</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Canada</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Ireland</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Kenya</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Netherlands</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Scotland</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Hong Kong</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">United Arab Emirates</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Nepal</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"><a href="javascript:void(0)">Papua New Guinea</a></span><div class="arrow-down"></div></li>
<li class="si-teams"><span class="si-teamName"> <a href="javascript:void(0)">Afghanistan</a></span><div class="arrow-down"></div></li>

</ul>
</div>
var swiperBox = $('.si-teamList').swiper({
slideClass: "si-teams",
wrapperClass: "si-teamList-wrp",
noSwiping: true,
slidesPerView: 11,
spaceBetween: 0,
slidesPerGroup: 1,
noSwipingClass: "si-teams",
setWrapperSize: true

});




↑このページのトップヘ