Skip to content

Commit fea3d19

Browse files
committed
WIP
1 parent 154132f commit fea3d19

File tree

5 files changed

+201
-11
lines changed

5 files changed

+201
-11
lines changed

db/1_pgdb.tcl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,53 @@ namespace eval oodz {
240240
return $result
241241
}
242242
}
243+
244+
:public method get_columns_names {table} {
245+
set result ""
246+
set :db_handles [ns_db gethandle]
247+
# set query "SELECT column_name FROM information_schema.columns WHERE table_name = '$table' AND table_schema='public' ORDER BY ordinal_position ASC"
248+
set query "SELECT
249+
c.column_name,
250+
c.data_type,
251+
CASE
252+
WHEN tc.constraint_type = 'FOREIGN KEY' THEN 'YES'
253+
ELSE 'NO'
254+
END AS is_foreign_key,
255+
CASE
256+
WHEN tc.constraint_type = 'PRIMARY KEY' THEN 'YES'
257+
ELSE 'NO'
258+
END AS is_primary_key,
259+
CASE
260+
WHEN c.column_default LIKE 'nextval%' THEN 'YES'
261+
ELSE 'NO'
262+
END AS is_auto_increment
263+
FROM
264+
information_schema.columns c
265+
LEFT JOIN information_schema.key_column_usage kcu
266+
ON c.table_name = kcu.table_name
267+
AND c.column_name = kcu.column_name
268+
AND c.table_schema = kcu.table_schema
269+
LEFT JOIN information_schema.table_constraints tc
270+
ON kcu.constraint_name = tc.constraint_name
271+
AND kcu.table_schema = tc.table_schema
272+
AND (tc.constraint_type = 'FOREIGN KEY' OR tc.constraint_type = 'PRIMARY KEY')
273+
WHERE
274+
c.table_name = '$table'
275+
AND c.table_schema = 'public';"
276+
try {
277+
set rows [ns_db select ${:db_handles} $query]
278+
set rows [ns_db select ${:db_handles} $query]
279+
# oodzLog notice "QUERY: $query"
280+
while {[ns_db getrow ${:db_handles} $rows]} {
281+
lappend result [ns_set array $rows]
282+
}
283+
} trap {} {arr} {
284+
oodzLog error "DB ERROR: $arr"
285+
} finally {
286+
: release
287+
return $result
288+
}
289+
}
243290

244291
:method my_columns {table columns} {
245292
set my_columns [list]

init.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ source [file join [ns_library shared] oodz/packages.tcl]
1717

1818
# Load OODZ Framework source files, sources from specific folder in alphabetical order. Do not change Modules order!!!
1919

20-
set oodzFrameworkModules [list base db conf ui rest dateTime helpers crypto session fileStorage]
20+
set oodzFrameworkModules [list base db conf ui rest dateTime helpers crypto session fileStorage mop]
2121
# set oodzFrameworkModules [list base db conf rest dateTime helpers crypto session fileStorage]
2222
foreach oodzModule $oodzFrameworkModules {
2323

mop/baseClass.tcl

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,128 @@
1-
namespace eval oodz {
2-
nx::Class create baseClass -superclass superClass {
3-
:method init {} {
4-
1+
namespace eval mop {
2+
nx::Class create baseClass {
3+
:method init {args} {
54
}
65
################################################################
76
# Object data manipulation methods
8-
9-
# Public interface to add or modify values in an object, need dicttool package
10-
# Accepts only one argument, type dict, key value pairs to add or if key exists replace value
117
:public method add {args} {
128
set a [lindex $args 0]
139
if {$a ne "" && [dict is_dict $a] == 1} {
14-
# set :obj_data [dict merge ${:obj_data} $a]
1510
dict for {key value} $a {
16-
:object property {${key} ${value}}
11+
if {[: prop_exists ${key}]} {
12+
: ${key} set ${value}
13+
} else {
14+
:object property -accessor public [list ${key} ${value}]
15+
}
1716
}
17+
} else {
18+
return -code error "Invalid argument: expected a dictionary"
1819
}
1920
}
21+
22+
:public method prop_exists {key} {
23+
try {
24+
set result [: ${key} exists]
25+
} trap {} {} {
26+
set result 0
27+
} finally {
28+
return $result
29+
}
30+
}
31+
32+
:public method get {{what ""} {result_type D}} {
33+
# Initialize result as a list or dictionary based on the result_type
34+
if {$result_type eq "L"} {
35+
set result [list]
36+
} else {
37+
set result [dict create]
38+
}
39+
# If no properties are specified, get all variable names
40+
if {[llength $what] == 0} {
41+
set what [: info vars]
42+
}
43+
# Iterate over the properties and gather their values
44+
foreach prop $what {
45+
if {$result_type eq "L"} {
46+
if {[:datatype [: cget -${prop}]] == 1} {
47+
set a [[: cget -${prop}] get]
48+
lappend result $a
49+
} else {
50+
lappend result [: cget -${prop}]
51+
}
52+
} else {
53+
if {[:datatype [: cget -${prop}]] == 1} {
54+
set a [[: cget -${prop}] get]
55+
dict set result $prop $a
56+
} else {
57+
dict set result $prop [: cget -${prop}]
58+
}
59+
}
60+
}
61+
return $result
62+
}
63+
64+
:method datatype {varName} {
65+
# Check if the variable is an object by attempting to get its class info
66+
if { [catch {${varName} info class}] } {
67+
return 0
68+
} else {
69+
return 1
70+
}
71+
}
72+
73+
# Public interface to remove specific keys from object data, accepts list as of keys as parameter
74+
:public method remove {args} {
75+
set to_remove [lindex $args 0]
76+
foreach key $to_remove {
77+
: ${key} unset
78+
}
79+
}
80+
81+
# Public interface to replace specific keys from object data, accepts dict as a parameter. old_key -> new_key. Values will stay the same.
82+
:public method replace {args} {
83+
set a [lindex $args 0]
84+
if {$a ne "" && [dict is_dict $a] == 1} {
85+
dict for {old_key new_key} $a {
86+
if {$old_key ne $new_key} {
87+
:add [dict create $new_key [: $old_key get]]
88+
:remove $old_key
89+
}
90+
}
91+
}
92+
}
93+
94+
# Return 1 if object has no properties or all properties are empty, 0 otherwise
95+
:public method is_empty {} {
96+
set objprops [: info vars]
97+
if {[llength $objprops] == 0} {
98+
return 1
99+
} else {
100+
foreach prop $objprops {
101+
if {[:$prop get] ne ""} {
102+
return 0
103+
}
104+
}
105+
return 1
106+
}
107+
}
108+
109+
# Return 1 if object has properties and at least one of them is not empty, 0 otherwise
110+
:public method is_not_empty {} {
111+
set objprops [: info vars]
112+
if {[llength $objprops] == 0} {
113+
return 0
114+
} else {
115+
foreach prop $objprops {
116+
if {[:$prop get] ne ""} {
117+
return 1
118+
}
119+
}
120+
return 0
121+
}
122+
}
123+
124+
:public method asJSON {} {
125+
return [tcl2json [: get]]
126+
}
20127
}
21128
}

mop/baseObj.tcl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace eval mop {
2+
nx::Class create baseObj -superclass baseClass {
3+
:property -accessor public {identifier ""}
4+
:property -accessor public {obj:required}
5+
# :property -accessor public {db:required ${::db}}
6+
7+
:method init {args} {
8+
next
9+
if {[::db table_exists ${:obj}] eq 1} {
10+
if {${:identifier} ne "" && [::oodz::DataType is_uuid ${:identifier}] == 1} {
11+
:add [: read uuid]
12+
} elseif {${:identifier} ne "" && [string is entier -strict ${:identifier}] == 1} {
13+
:add [: read id]
14+
} else {
15+
set a [::db get_columns_names ${:obj}]
16+
foreach line $a {
17+
set propname [dict get $line column_name]
18+
:add [dict create $propname ""]
19+
}
20+
}
21+
} else {
22+
oodzLog error "Cant init object TABLE doesnt exist"
23+
return -code error "Cant init object TABLE/VIEW doesnt exist"
24+
}
25+
}
26+
27+
:method read {args} {
28+
set idType [lindex $args 0]
29+
if {$idType eq "uuid"} {
30+
return [lindex [::db select_all ${:obj} * uuid_${:obj}=\'${:identifier}\'] 0]
31+
} elseif {$idType eq "id"} {
32+
return [lindex [::db select_all ${:obj} * ${:obj}.id=\'${:identifier}\'] 0]
33+
}
34+
}
35+
}
36+
}

session/base_classes.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace eval ::oodz {
130130

131131

132132

133-
133+
# To implement session from DataBase
134134
SessionClass create dbs -superclasses ISession
135135

136136
nx::Class create SessionFactory {

0 commit comments

Comments
 (0)