Thursday, December 29, 2016

Quick Tip - Count lines

Here's a command to get the count of js lines in a node project:

find . -name '*.js' ! -path '*node_module*' | xargs wc -l

The -path part is to exclude the node_modules folders so you don't count 3rd part libs.

Wednesday, June 8, 2016

Quick Tip - import recognized in IntelliJ Idea

If you know you have the correct gradle configuration for an library, but the import isn't recognized in IntelliJ Idea, do the following:

File menu -> Invalidate Caches/Restart...

This can take a while.  IDEA will rebuild indexes.  But sometimes, this is required!

At command-line, you may need:

./gradlew clean build --refresh-dependencies


Friday, May 20, 2016

Kickstarting a Java program

Here are steps to create a new Java command-line program.  Use a different quick start if you are making a web server as there are guides for those.

0. Prerequisite: Install Gradle.

1. Create an empty folder, we will refer to as project1.

2. Copy this build.gradle code into a new build.gradle file in your project1 folder.

(Modified from https://docs.gradle.org/current/userguide/tutorial_java_projects.html )

apply plugin: 'java'apply plugin: 'idea'
sourceCompatibility = 1.8version = '0.1'jar {
    manifest {
        attributes 'Implementation-Title': 'War Game',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections',
    name: 'commons-collections',
    version: '3.2.2'    
    testCompile group: 'junit'
    name: 'junit', version: '4.+'}

test {
    systemProperties 'property': 'value'}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'       }
    }
}


3. At terminal, run
gradle wrapper
This will create a gradlew file for you.


4. At terminal, run
gradlew idea
Assuming using idea.

5. Open in idea

6. In IDEA, create this folder structure: src->main->java->project1

7. Right-click on the "java", mark as sources root.

8. Same thing for tests: src->test->java, mark java as test sources root.






Monday, July 2, 2012

JavaScript variable definition scope

function myFunction()
{
  var a =2;
  if (a>0)
  {
    var a=4;
    a++;
  }
  alert("A="+a);
}

The above code would display a message box saying "A=5"!  This is because variables scope is either global, or at function-level.  So, having {} blocks itself does not change the variable scope, and the "a" above is always the same variable.  Since nested functions and anonymous functions are allowed, this can be one way to achieve more granularly defined variables.

Tuesday, July 27, 2010

MySQL Innodb table database backups!

It is useful to be able to do database backups and restoration at the command line. Here's my commands for doing so, no 3rd party tools needed for this, just mysql. You can restore the database to a different server entirely.

mysqldump -h 127.0.0.1 -P [my_port] -u [me] -p[password] -B my_database >the_bak

where:
[password] is the database password (no space).
[me] is the mysql admin account username
my_database is the name of your database.
[my_port] database access port, default =3306

Restoration:
create database:
mysql -u[me] -p[password] information_schema -e "create database my_database"

restore database:
mysql -u[me] -p[password] my_database < ./the_bak.sql

New MySQL 5 stored procedures

Seems the old syntax of my stored procedure creation doesn't work in MySQL 5. Don't use $$ as a delimiter anymore!

Here's my NEW sample for you. I included a cursor.

DELIMITER |
CREATE PROCEDURE sp_my_stored_proc()
BEGIN

declare done int default 0;
declare n_value bigint default 0;
declare str_value varchar(200) default '';
declare cur1 cursor for select the_id, the_val from my_table;
declare continue handler for not found set done=1;

open cur1;
repeat
fetch cur1 into n_value, str_value;

if not done then
[multiple statements can go here, each terminated by ;]
end if;

UNTIL done END REPEAT;
CLOSE cur1;
END |

Thursday, May 14, 2009

Quasitime.com

Quaisitime.com is now released! I have been working on this for a couple years now. Thanks to all my blog readers. Now you can see what I have been using my web programming for. Go to quasitime.com and get your free account (pro and business solutions available as well).

I know I haven't posted for a while as it has been crunch time at my company, but I will resume shortly. I have a number of tips I need to post. I am also considering posting an RFC that, if adopted by operating systems and browsers, will make the world wide web work better.

-Dan

Sunday, February 24, 2008

Javascript Reference Pointers

Does Javascript assign values by "pointer" (to use the C term)?
Yes, indeed. Let's take a look how this impacts code.

When dealing with Strings you generally don't need to worry about this because the String modification functions don't actually modify the original String, they just return a new one.
Try this code after guessing what the output will be:

// Test 1: Append
var str_a = "aloha";
var str_b = str_a;
alert("str_a==str_b: "+(str_a==str_b));
str_b = "aloh";
str_b += "a";
alert("str_a="+str_a);
alert("str_b="+str_b);
alert("str_a==str_b: "+(str_a==str_b));

// Test 2: toUppercase
str_b = str_a;
str_b.toUpperCase();
alert("str_a="+str_a);
alert("str_b="+str_b);


In the first test appending to str_b does not affect str_a, even though they originally pointed to the same string. The appending actually sets str_b to a new string that contains the appended string. The comparison is overridden though, so your == for string comparisons still works even though the pointer values are different.

In the second test, the toUpperCase() function does not modify either string. It only returns a new string that is uppercase, which we haven't used. So, in place functions will not affect str_a, even if they are used on str_b. Obviously, if we did copy the result to str_b, then str_b would point to something else, and not affect str_a. I haven't tested with the valueOf() function, so if you are using that, you may want to test it to check the pointer behavior.

Next, let's look at the Date Object. What would you expect as a result from this code?

var jsdate_a = new Date();
var jsdate_b = jsdate_a;
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
jsdate_b.setHours(0);
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);


In this case jsdate_a does get its hours reset to zero by modifying jsdate_b! That is because the Date Class has functions which modify the data in the object itself, without just creating a new instance. Since jsdate_a and jsdate_b point to the same Date Object, a modification to the data pointed to by one of them, affects the data pointed to by the other.

Thursday, October 4, 2007

Firefox object member size limit

I encountered an issue in Firefox with my data coming back from AJAX. The object members of the XML Doc were being truncated to 4096 bytes. If this happens, you can use the function normalize(); on the object resulting from the parser.parseFromString call to have Firefox correct this. Make sure to do a browser check, so you don't get a script error in other browsers that don't have teh normalize function. IE 7 does not have normalize(), but it does not need it in this case because it does not break up the nodes into 4K chunks when you create the document from teh XML stream.

To see what's going on, we can look at the W3C spec at http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html.

"When a document is first made available via the DOM, there is only one Text node for each block of text. Users may create adjacent Text nodes that represent the contents of a given element without any intervening markup, but should be aware that there is no way to represent the separations between these nodes in XML or HTML, so they will not (in general) persist between DOM editing sessions. The normalize() method on Element merges any such adjacent Text objects into a single node for each block of text; this is recommended before employing operations that depend on a particular document structure, such as navigation with XPointers."

According to the first line in the quote above, IE 7 has the correct behavior in this regard and not Firefox. When the document is first created, the browser should not be splitting the nodes into chunks. However, the data is still accessible, even when split. It is placed in child nodes. You should be able to access it via the DOM child node functions/properties. This requires extra coding, so it much easier to just use the "normalize()" function.

MySql stored proc delimiters

When creating a stored proc in MySQL (MySQL 5.0+ supports this), unfortunatly you have to workaround delimiter issues. If you just use semicolons to terminate each line as you would in MS SQL Server, it won't work. So, I'm posting a template here to help developers. I'll use $$ as the delimitor, but you can use a different symbol if you like:

DELIMITER $$;
DROP PROCEDURE IF EXISTS `my_db`.`my_proc_name`$$
CREATE PROCEDURE `my_db`.`my_proc_name` ()
BEGIN
declare my_var_A INT;
[some more statements, each terminated by ; ... ];
END$$
DELIMITER ;$$

The first line configures your new symbol ($$) to be statement terminator. This allows us to put semicolons, the ordinary statement termintor inside our stored procedure, without MySQL trying to go ahead and start executing them as statements when we run the above script to create the stored procedure. See the line "END$$". We terminate that with our new "real" delimiter to allow MySQL to execute that block, thereby creating the whole stored procedure. Then we restore the delimiter to semicolon. Now when we execute our stored procedure, we should be using the default semicolon delimiter, and so the stored procedure will run fine, having ordinary statement terminators.