Halaman

Jumat, 05 September 2014

[MySQL] Using temporary variables on select statements

When your query need a long formula, to make it "maintainable" and "readable" you sometime need  the help of a temporary variable, simply put: you assign them on the select term by using the syntax:

@tempName := price * discount_percent as disc, @tempName2 := @tempName + price as net_price

And that should be it folks! ;)

Senin, 18 Agustus 2014

Solving Git always shows modified files even if they are not modified

Sometime when working on multiple environment(Windows, *nix), the new line character at the end is not visible to the human eye, but even if that is not visible, versioning system such as git will treat them as modifications.

When installing Git on windows machine, we usually press the "next" button rapidly that we are not aware that we are telling the Git to add automated "crlf" or "lf" to each line of the checked-out source code. The symptom such as :
  1. Always shows modified files even if we are not changing a particular file
  2. Even after revert or a hard --reset the modified files is showing
To remedy the situation use the following command in sequence:
  1. git config --global core.autocrlf false
  2. git reset --hard
  3. git status
The list of modified files which we are not modify should be gone now. 

Cheers! ;)

Jumat, 25 Juli 2014

[Haxe] Operator overloading!

yep, Haxe is powerful, but every language has their own weaknesses and strengths. To do a simple class overloading, we need to declare compilers meta and using 'abstracts' which are not quite comfy.. but for the sake of knowledge.. here is a snippet to implement a simple operator overloading in haxe..

@:allow(Mimit)
class _MyBase {
 var v: Int;
 public var y: Int;
 
 public function new(v: Int)
 {
  this.v = v;
  y = v * v;
 }
}

class _Mimit extends _MyBase{
 public function test() {
  trace(v);
 }
}

@:forward
abstract Mimit(_Mimit) from _Mimit to _Mimit {
 public function new(v:Int) this = new _Mimit(v);
 
 @:op(A + B) public function add(rhs:Mimit):Mimit {
  var t: Mimit = new Mimit(this.v + rhs.v);
  return t;
 }
}
let's analyze here for a momment...

  1. The class _MyBase needs a syntatix sugar of operator overloading, so that user could simply call _MyBase * _MyBase and use them accordingly
  2. Since the operator need to access 'private' member of the class we add the compiler's meta @:allow(Mimit) since the abstract Mimit will provided the overloads..
  3. And since the programmer will directly use the abstract class "Mimit" we declare another meta @:forward to the declaration, and telling the abstract to "implicitly" support _Mimit class on usages(fields and methods) by writing from _Mimit to _Mimit
  4. The from _Mimit to _Mimit basicly tells the compiler to please implicitly assign the this variable to _mimit and assign the this variable from _Mimit
There we have it, an operator overloading which works on haxe..
For the Usage: 
var m1: Mimit = new Mimit(10);
var m2: Mimit = new Mimit(20);
var m3: Mimit = m1 + m2;
  
m3.test();
trace(m3.y);
Frankly i'm not quite satisfied with things done this way.. a simple operator overloading made a bloated code base, i think i will stick to the static function..Cheers! :)

Rabu, 23 Juli 2014

[OpenFL/Haxe/HXCPP] Debugging

Playing with OpenFL is fun! mainly because of it's power for cross platform development.
Debugging program which target the flash platform is very easy, you just need the FlashDevelop software and download the flash player with the debug support

a step by step procedure to debug the "FLASH" target
  1. Open the project
  2. "Menu" »"Debug" »"Start Remote Session"
  3. Make sure you are using the "debug" configuration, and then press the "Play / Run" button
  4. Set's your breakpoints, inspect variables, etc is a snap..
If we are debugging windows target (hxcpp) this will introduce different approach, FlashDevelop currently working to support it 

But for those who like to use the command prompt, here is a step by step procedure..
  1. run "haxelib install hxcpp-debugger"
  2. on your project's application.xml add
    <haxedef name="HXCPP_DEBUGGER" />
    <haxelib name="hxcpp-debugger" />
  3. on your MAIN class file, add the import: "import debugger.Local;"
  4. on main haxe file, you need to add the function call
    new Local(true);
  5. run the program on your command prompt:
    lime test window
  6. It will run and then hit a default breakpoint, from there you should type "help" and read the documentation to proceed
  7. nice........
Haxe is quite mature, but it's cross platform nature made the debugging process which should be simple, becomes quite a challenging process, OpenFL supports html5 target, next i will try to learn on mapping js to haxe's source code using the technology source-code mappings on chrome and firefox browser..

Kamis, 05 Juni 2014

[Javascript] Uploading File Field using AJAX

This simple snippet will show you how to upload file through the AJAX `protocol` using pure Javascript
<form id="form" method="post" action="" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" name="submit" value="submit"/>
</form>

<script>
 var form = document.getElementById('form');
 
 form.onsubmit = function(a){
  //get the file we would likt to submit
  var theFile = form.elements.namedItem("file").files[0];
  
  if(theFile != undefined){
   var xhr = new XMLHttpRequest(); 
   
   //monitor the on upload data progress (so we could implement the simple progress bar perhaps..?)
   xhr.upload.addEventListener("progress", function(e){ 
    console.log(e);
   }, false);
   
   //POST method with the target url of 'test.php'
   xhr.open( "POST" , "test.php");  
   
   var ff= new FormData();
   //append any post data here
   ff.append( "submit" , 1 );
   //append the file here
   ff.append( "file" , theFile );
   //finally send the ajax request!
   xhr.send(ff);
  }
  
  return false;
 }
</script>

Sabtu, 01 Februari 2014

[MySQL] Note about, listing table rows which is not mentioned on some other table row

Straight from StackOverflow:

SELECT table1.*
FROM table1
LEFT JOIN table2 ON table2.name=table1.name
LEFT JOIN table3 ON table3.name=table1.name
WHERE table2.name IS NULL AND table3.name IS NULL

MySQL, as well as all other systems except SQL Server, is able to optimize LEFT JOIN / IS NULL to return FALSE as soon the matching value is found, and it is the only system that cared to document this behavior. […] Since MySQL is not capable of using HASH and MERGE join algorithms, the only ANTI JOIN it is capable of is the NESTED LOOPS ANTI JOIN

Essentially, [NOT IN] is exactly the same plan that LEFT JOIN / IS NULL uses, despite the fact these plans are executed by the different branches of code and they look different in the results of EXPLAIN. The algorithms are in fact the same in fact and the queries complete in same time.

It’s hard to tell exact reason for [performance drop when using NOT EXISTS], since this drop is linear and does not seem to depend on data distribution, number of values in both tables etc., as long as both fields are indexed. Since there are three pieces of code in MySQL that essentialy do one job, it is possible that the code responsible for EXISTS makes some kind of an extra check which takes extra time.

Jumat, 31 Januari 2014

Lua, Function List on Notepad++

New feature for the notepad++ version 6.5.2! it could shows you the function list for the currently viewed document, function list for php, c++, c, perl is supported out-of the box

For those of you Lua programmer using notepad++, here is a simple how to add Lua function list to the notepad++
  1. Locate your %APP_DATA% folder, if you are using the zip distribution, go to your extraction directory
  2. Open functionList.xml
  3. On NotepadPlus >> functionList >> associationMap , see the commented LangID for LUA, on my installation it is 23
  4. Add the following
    <association langID="23" id="lua_function"/> on that section
  5. Creating the parser:
    on NotepadPlus >> functionList >> parsers add the following section
    <parser id="lua_function" displayName="Lua" commentExpr="((--\[\[.*--\]\])|(--.*?$))">
     <function
      mainExpr="function\s+(\w+[:\.])?(\w+\s*\(\s*[\w,\s]*\s*\))"
      displayMode="$className->$functionName">
      <functionName>
       <nameExpr expr="\w+\s*\(\s*[\w,\s]*\s*\)"/>
      </functionName>
      <className>
       <nameExpr expr="[\w]+(?=[\s]*[:.])"/>
      </className>
     </function>
    </parser>
    
  6. Warning: if you copy paste xml content on line 5, please make sure that the xml properties are in one line, if they appear to be 2 line, it is because html page wordwrapping ;)
  7. Restart notepad++
  8. Open your lua file, open function list panel (Menu >> View >> FunctionList)
If the functions is not showing, chances are :
  1. You are using Gary's mod lua lexer.. uninstalling it should overcome the problem
  2. Wrong language ID noted from step 3
That's all! :)
Have fun!

Minggu, 26 Januari 2014

How to find out built in defines from a specific GCC compiler?

A little trick i read from LuaJit's makefile and the gcc manual page. To list all built-in defines use the following (assuming that you don't have yohan.h file on the current directory..)

# touch yohan.h
# gcc -E -dM yohan.h

and if the "yohan.h" contains your own code of defines, it will also be resolved.. giving you the exact picture of what's happening with your sources defines

Selasa, 07 Januari 2014

[c++, c#] Journey of cross compiling mono 3.2.5, utilizing ScratchBox2

This one is a tough one, usually i get away compiling just by trial-and-error after successfully compiling the library for the windows target. But compiling this one is very much a challenge.. and then out of curiosity.. here i am writing this blog entry so that someday i could revisit and think .. what the **** am i doing compiling libraries and not doing those projects!

Well if my future me see this post, "I do it because of cross environment, playbook, win 7.5, vala, mono, xna, cocos2dxna, and Pou.. i bet you will understand and remember.. hehe, and yes, i know, it is very silly.. but Y.O.L.O!"

So let's go with it shall we..

After trying to compile using BB NDK SDK version 2.1 i encountered problem with the assembler compilation. If i can recall the error correctly, the message was "invalid operation $03" or something similar like that..

After 2 nights of blackened eyes, i give up and then tried to compile on a virtual machine, for that machine i choose LINUX Mint 15 for the OS, chosen simply because the OS iso file lying around on my hard-disk. For the virtualization, i'm using vmware

I was thinking about using linux because i could simply use ./configure and then make, but i think to myself it would be better if i could directly compile this for the arm architecture, after diving to google, there is a very exciting project callled Scratchbox2, and then here i am using the command

$ sudo apt-get install scratchbox2
$ sudo apt-get install qemu

Then i tried to find linux arm compiler toolchain, for this purpose i search for the file arm2009q1-203-none-linux-gnueabi simply because that is the compiler i used in Windows when venturing with WebOS.. (love that platform.. hixs..)

here is the name of the file i downloaded from codesourcery: arm-2009q1-203-arm-none-linux-gnueabi.bin

What?? you need the web address you say? here it is: http://sourcery.mentor.com/public/gnu_toolchain/arm-none-linux-gnueabi/

Let's go to the terminal and then type the following..

$ chmod +x arm2009*
$ ./arm2009*

Follow those installation screen with care, i choose to install on default directory.. and that is at $HOME/CodeSourcery

Here is the trick! if i use sb2-init with the -c and without -C -static it will fail to run sb2-build-libtool! don't know about that -c, but if i did not specify -C -static, the compiled will try to find the library linux.so.3, about the -lpthread, that's because when compiling mono, there are undefined references to pthread, so i decided to put that to default.. maybe we should find a cleaner solution to this

Therefore here is the command i use to initialize sb2

$ sb2-init -d -C "-static -lpthread" arm $HOME/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi-gcc

$ sb2-init -d arm $HOME/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi-gcc

when not using this arm-2009q1-203-arm-none-linux-gnueabi.bin release, for example using built in arm-linux-gnuebi-gcc (from the apt), i failed to run sb2-build-libtool, a workaround would be adding parameter -C "-static" to sb2-init but i guess this would break some build system.. note to self: Need to find-out building custom rootfs! (finger crossed, perhaps copying the libc content? hehe..)

important! you need to init sb2 in $HOME/CodeSourcery/Sourcery_G++_Lite/arm-none-linux-gnueabi/libc because that is the rootfs for that toolchain

let's try with a simple hello world program shall we..

$ sb2
$ gcc hello.cpp -o hello
$ file hello --> this should show that we have compiled for the arm-architecture! ;)
$ ./hello --> will run!
$ exit ->> this will exit us from the "jail" arm-environment we had setup earlier

Ok, now is the fun part, let's go to the extracted tarball mono source folder, cross your fingers :p and then type

$ sb2
$ ./configure --with-monotouch=no --without-mcs-docs --disable-mono-debugger CFLAGS="-DBROKEN_64BIT_ATOMICS_INTRINSIC -DARM_FPU_NONE" LIBS=-lpthread --disable-mcs-build

The configuration is a success!

But, the potential horror might now came along.. anyway.. let's proceed!

before make! i add the patch to the mono/mini/mini.arm.h
#define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
change that to
//#define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1

on the same terminal, lets go make some!

$ make
$ make install DESTDIR=$HOME/mymono

there you have it! a compiled mono for arm architecture!

Some hints:

for the mono's ./configure script, to show the options accepted by the configure run
$ ./configure --help

Ps. If you like and learn something from other people, and you can't or don't want to donate to them directly, show your support by sharing the article, google+ing the article, or perhaps checking out the advertisements which may appear on the website ;) many thanks before!