IndexInfo

1 view
Skip to first unread message

Tony Zakula

unread,
Nov 13, 2010, 9:39:32 PM11/13/10
to mynajs-general
I am trying to check whether an index exists or not by using
Myna.Table.indexInfo

It says it returns an array and each entry contains three pieces of
information. Does each item contain an object with those three
properties?

Can you explain how to do that?

The column names were easy. :-)

Thanks!

Tony Zakula

Mark Porter

unread,
Nov 14, 2010, 3:37:35 PM11/14/10
to mynajs-...@googlegroups.com
It is an array of objects. Here is a dump from myna_log_general:
var t = new Myna.Database("myna_log").getTable("myna_log_general")
Myna.print("<pre>" + Myna.dumpText(t.indexInfo) + "</pre>")

[ Array ]
+-[0] [ Object ]
| +-[columns] [ Array ]
| | \-[0] log_id
| +-[name] PRIMARY_KEY_B
| \-[unique] true
\-[1] [ Object ]
+-[columns] [ Array ]
| \-[0] event_ts
+-[name] MYNA_LOG__EVENT_TS_IDX_45175
\-[unique] false

Here is an example of how to determine if a unique index on "log_id" exists:

var hasLogIdIndex = t.indexInfo.reduce(function(found,index){
if (found) return found;
if (index.unique){
return index.columns.reduce(function(found,colname){
return colname == "log_id"
},false)
}
return false;
},false)

It seems that "hasIndex(colnames,isUnique)" is an obvious enhancement.
I have added that for the next release. I have also added
Array.prototype.contains(search) which dramatically simplifies the
above code:

hasLogIdIndex = t.indexInfo.contains(function(index){
return index.unique && index.columns.contains("log_id")
})

----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

> --
> You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
> To post to this group, send email to mynajs-...@googlegroups.com.
> To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.
>

Mark Porter

unread,
Nov 14, 2010, 3:58:29 PM11/14/10
to mynajs-...@googlegroups.com
After I sent that, I realized that the JS 1.7 array extra "some" is
essentially the same as "contains" here is the example using "some"

hasLogIdIndex = t.indexInfo.some(function(index){
return index.unique && index.columns.some(function(name){
return name == "log_id"
})
})

You can use this with "every" to search for indexes with more than one column:

//find a unique index of log_id, and type
hasIndex = t.indexInfo.some(function(index){
return index.unique
&& ["log_id","type"].every(function(targetName){
index.columns.some(function(name){
return name == targetName
})
})
})

I'm keeping the "contains" function because it has a more descriptive
name, and because it allows for a simple form for simple values.

----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

Tony Zakula

unread,
Nov 14, 2010, 4:31:13 PM11/14/10
to mynajs-...@googlegroups.com
So is that in SVN? or something you are going to to work on? I only
ask because I am working on data creation scripts. I was able to use
one line with code like this for the columns. I could roll my own,
but if your going to add it anyway.

if (columnNames.indexOf("childpagecollection") == -1){
table.addColumn({
name:"childpagecollection",
type:"TEXT"
});

Mark Porter

unread,
Nov 14, 2010, 5:12:39 PM11/14/10
to mynajs-...@googlegroups.com
That will work fine most of the time. The problem is that some DB's
like to rename or auto-generate their Primary key indexes. There is
also the possibility that someone has already created the index you
are interested in. If you try to create the same index with a
different name, the database will probably reject it. Much safer to
check for the columns and unique property.

I just committed the changes to Table and Array I talked about.
----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

Tony Zakula

unread,
Nov 14, 2010, 5:16:45 PM11/14/10
to mynajs-...@googlegroups.com
Good. Thanks. I will try it out. I am mostly just familiar with
Postgres so that helps.

Tony Zakula

Reply all
Reply to author
Forward
0 new messages